Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  What is the best way to drag an image??  (Read 1692 times)
0 Members and 1 Guest are viewing this topic.
Offline ferneto

Junior Newbie




Think fast!!.....Talk slow!!Chinese thinking


« Posted 2003-08-26 22:00:51 »

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
Offline poisonousparrot

Senior Newbie




Current Project - NightStalkerMUD


« Reply #1 - Posted 2003-08-28 20:09:09 »

I have the same question. Grin

Signatures are stupid
Offline zparticle

Senior Member




Thick As A Brick


« Reply #2 - Posted 2003-08-28 20:53:25 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
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();
     }
}

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #3 - Posted 2003-08-29 03:37:46 »

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.

Offline ferneto

Junior Newbie




Think fast!!.....Talk slow!!Chinese thinking


« Reply #4 - Posted 2003-08-29 17:43:01 »

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 Smiley
Offline GergisKhan

Junior Member




"C8 H10 N4 O2"


« Reply #5 - Posted 2003-08-30 01:09:12 »

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?

gK

"Go.  Teach them not to mess with us."
          -- Cao Cao, Dynasty Warriors 3
Offline ferneto

Junior Newbie




Think fast!!.....Talk slow!!Chinese thinking


« Reply #6 - Posted 2003-08-30 03:35:06 »

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
Offline kevglass

JGO Kernel


Medals: 120
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #7 - Posted 2003-08-30 08:08:39 »

Quote

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?


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 Smiley).


Good Luck!

Kev

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #8 - Posted 2003-08-30 10:29:11 »

Quote

[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?


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.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline Abuse

JGO Coder


Medals: 11


falling into the abyss of reality


« Reply #9 - Posted 2003-08-30 10:51:23 »

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.

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #10 - Posted 2003-09-07 00:20:24 »

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.Huh?

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

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (6 views)
2014-07-29 10:59:34

E.R. Fleming (20 views)
2014-07-29 03:07:13

E.R. Fleming (8 views)
2014-07-29 03:06:25

pw (39 views)
2014-07-24 01:59:36

Riven (39 views)
2014-07-23 21:16:32

Riven (26 views)
2014-07-23 21:07:15

Riven (28 views)
2014-07-23 20:56:16

ctomni231 (59 views)
2014-07-18 06:55:21

Zero Volt (51 views)
2014-07-17 23:47:54

danieldean (42 views)
2014-07-17 23:41:23
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!