Solved

Drag and drop images in java

Posted on 2000-03-07
3
498 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
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array220 challenge 8 46
endX challenge 2 50
strCopies  challenge 17 73
Image decoding from Camera 3 45
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

706 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now