Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 516
  • Last Modified:

Drag and drop images in java

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
scjp
Asked:
scjp
1 Solution
 
heyhey_Commented:
you can listen for mouse move events, adjust the current image coordinates and call repaint.
0
 
terajivCommented:
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
 
Sasha_MapaCommented:
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now