Solved

Drag and drop images in java

Posted on 2000-03-07
3
506 Views
Last Modified: 2013-12-14
I am developing an paintbrush program in java where the user is able to draw
lines circles etc on to the canvas
I"d also like to provide him with some image icons so that user could drag and drop them on to the canvas
Can you clarify the following questions
1> Is it necessary to have DnD for the image part
2> Can you give me some info abt where i could get source code for dragging and dropping an image
 
0
Comment
Question by:scjp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 2594534
you can listen for mouse move events, adjust the current image coordinates and call repaint.
0
 
LVL 3

Expert Comment

by:terajiv
ID: 2594813
Hi,
Go to following site u will fine some stuff there..
http://java.sun.com/products/jfc/index.html#dnd
or
I will give another one pl. wait
0
 
LVL 7

Accepted Solution

by:
Sasha_Mapa earned 20 total points
ID: 2696003
1. No, it is not needed.

2. Here's a small applet that allows dragging an image around. It uses double buffering technique to avoid flickering.


import java.awt.*;
import java.applet.*;

public class MyApplet extends Applet{

  public void init(){
     setLayout(null);
     MyComponent comp = new MyComponent(getCodeBase(),"image.gif");
     comp.reshape(20,20,300,300);
     add(comp);
  }

}
                       

class MyComponent extends Canvas{

  private Image dbImage; // Image for double buffering, to avoid flickering.
  private Image myImage;
  private int imageX=0, imageY=0; // The location of our image;

  private int lastDragX, lastDragY;

  public MyComponent(Image image){
    myImage = image;
  }


  public void update(Graphics g){
     Graphics dbGraphics = getDBGraphics();
     dbGraphics.setColor(getBackground());
     dbGraphics.fillRect(0,0,bounds().width,bounds().height);
     paint(dbGraphics);
     g.drawImage(dbImage,0,0,null);
  }

 
  public void paint(Graphics g){
     g.drawImage(myImage,imageX,imageY,null);
  }


   public boolean mouseDown(Event e, int x, int y){
      lastDragX = x;
      lastDragY = y;
      return false;
   }


  public boolean mouseDrag(Event e, int x, int y){
     imageX += x-lastDragX;
     imageY += y-lastDragY;
     lastDragX = x;
     lastDragY = y;
     repaint();
     return false;
  }



 

  private final Graphics getDBGraphics(){
 if ((dbImage == null) || ((dbImage.getWidth(null)!=bounds().width)||
(dbImage.getHeight(null)!=bounds().height))){
   dbImage = createImage((int)Math.max(bounds().width,1),
                      (int)Math.max(bounds().height,1));
        // Using Math.max is needed because createImage only takes positive
        // values, which seems strange to me, since components can even
        // have negative size.
    }
    return dbImage.getGraphics();
  }

}
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question