Solved

Drag and drop images in java

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 49
mysql jsp example issue 32 48
JAVA API design with micro service cloud in mind 1 47
learn programming 8 41
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand columnThat will then direct you to their download page.From that page s…
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 …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

861 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