What is the best way to drag an image??

Hi srs.

I’m quite confuse to get a image loaded and have a listener to attach the moviments of the mouse to the image.
Actually I’m programming a table tenis game and having some diffuculties to draw a image over a background and do this feature.
What should i do??Add a new Panel and draw the image on it??

Tks in advance!!
Fernando

I have the same question. ;D

One of the manys way to handle this would be:

Sub-class Panel to a new type, say MyPanel that takes an Image in the constructor and assigns it to a member var. The panel listens for mouse motion events and keep track of the mouse x and y and then repaints itself and the image whgere the mouse was. Something like this.


public class MyPanel extends Panel implements MouseMotionListener
{
    ImageIcon i = null;
    int x=0, y=0;

    public MyPanel(ImageIcon image)
    {
        i=image;
    }

    public void paint(Graphics g)
    {
        super.paint(g);
        g.drawImage(i,x,y);
     }

    public void mouseMoved(MouseEvent evt)
    {
         x= evt.getX();
         y=evt.getY();
         repaint();
     }
}


Does nobody read the documentation anymore? Or search on java.sun.com, or google, or read the Java Tutorials?

I notice we are getting a lot of questions from people that are so vague it seems they haven’t even tried to find an answer for themselves.

If you mentioned what you tried and the problem you were seeing or the error message you got etc. it would make a lot more sense.

Search on java.sun.com first please. Check the Java Tutorials there, they cover almost everything.
Enough of these “how do you draw an image?” questions…

Try this site first http://javaalmanac.com/

Look what I get when I search for “image”

http://javaalmanac.com/egs/java.awt/LoadImage.html?l=find
http://javaalmanac.com/egs/java.awt/DrawImage.html?l=find

Or “mouse”

http://javaalmanac.com/egs/java.awt.event/MouseMotion.html?l=find

Now if you don’t understand these bits of code then by all means ask here and gives us some details.

First…thank u!

Load an draw an image…I’ve done that before. The problem was that i was not figuring out how to attach a listener to an image since images are not serializable…it’s not a component as u know.
I’ve done lots of google seaches asking for “dragging an image in Java”…no sucsses.
Now…i can see how to do it.

…don’t stress man. Be cool :slight_smile:

You know, I never considered this particular problem at all, but now that I do, I have a question myself!

zparticle,

In your code, doesn’t this mean that if the mouse moves at ALL in the components space, the image moves also? I wonder if it shouldn’t be checking for mouseClick to enable a value… or is there a mouseDrag event? No, don’t answer that, lest swpalmer hit me with the “read the docs” line which I’m going to do anyway! I was just thinking out loud.

[EDIT: yes, it’s called mouseDragged() and it checks for left-button held down, as i see it.]

However, this only works, as I see it, inside A component… what about intercomponent? Say I wanted to make a map editor (obvious applcation) with a terrain toolbar at the right, where I’d drag terrains onto a grid in the main panel… what would you suggest there?

Great…can someone post something about that???

Actually i need to load, and have an image…say a paddle for example, attached to the mouse.
Is not Canvas a better idea??

See ya.
Fernando

Well, you’ve got two options.

  1. You carry on with the slightly hacky solution. You catch mouse events for mouseEntered() and mouseExited() on all the components that are important to you. When the mouse leaves you stop drawing the image, when the mouse entered you start drawing the image (both under the mouse cursor). This is going to be a lot of fiddling about but you could probably build yourself a generic infrastructure to handle it.

  2. Go away and read the dnd API in java.awt.dnd. And read this tutorial:

http://java.sun.com/docs/books/tutorial/dnd/

Its a horrible API, made slightly better during the move to 1.4. Even so once you do get this API to work, you’ll be able to drag and drop with cursor changes easily. It will also drag/drop between components, and actually between applications (which is quite cool :)).

Good Luck!

Kev

mouseDragged is triggered by any mouse button drag event, not just the left mouse button. Its proped up at either end by mousePressed and mouseReleased, so you’ll want to check for a click within your paddle to start with, and set some sort of state var to id. what you’re dragging. Then mouseDragged can use the delta positions to readjust the object to its new position, and mouseReleased can reset your dragging state variables to their inactive form.

Dragging from one component to another is a pain, DnD is probably the best option, but then again - do you paint in PSP or Photoshop by dragging pixels from your pallete to your image? Thought not :wink:

And yes, if you’re doing your own drawing then Canvas is usually a better idea than JPanel.

i’ve forgotten the method name, but isn’t there a method to tell the awt event dispatch to pass events to all components in the awt tree, not just the leaf component?

If you use that, you could place your dragging code in the Frame root component, and it would still receive the events regardless of what child component was being dragged over.

Is it not the case that MouseDragged events are always sent to the component where the drag started, AND the mouseReleased event is also sent there.???

SwingUtilities is your friend in this case, as it provides methods for translating the co-ordinates from one component to another.