File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Linking 2 images with a button in a GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Linking 2 images with a button in a GUI" Watch "Linking 2 images with a button in a GUI" New topic
Author

Linking 2 images with a button in a GUI

Tom Little
Greenhorn

Joined: Oct 24, 2009
Posts: 20
Hi everyone,

For my class my project is very simple and straight forward. I have to create a volleyball player in a GUI window. I have already done this but I wanted to take it a step further.
I currently have the picture of the volleyball player getting ready to spike the volleyball over the net. Now what I want to do is create a button that you can click and once you
click the button, the image shows the volleyball player having already spiked the ball. I do not want to create an animation but basically two images linked by one button.

So the first picture would contain a volleyball player getting ready to spike the ball with a button in the window. Once you press the button the image changes to a picture of the same
volleyball player having already spiked the ball.

I do not know exactly where to start with this so I was wondering If I could get a little help just to get this idea rolling. Thanks a lot

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Add a JButton to your gui. Register a ActionListener to that button. In the ActionListener replace the image with the new one and call the invalidate method on the component.

That should work.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Tom Little
Greenhorn

Joined: Oct 24, 2009
Posts: 20
edited
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I would advise you to read this part of the Java tutorial. Or even better read the entire tutorial. It will cover all the basics (and some more) but then you'll feel much more familiar with Java.
Tom Little
Greenhorn

Joined: Oct 24, 2009
Posts: 20
Wouter Oet wrote:I would advise you to read this part of the Java tutorial. Or even better read the entire tutorial. It will cover all the basics (and some more) but then you'll feel much more familiar with Java.


Thanks I read through it and got the button to appear on my GUI!

My first code was



and later down I put





Now I am working with ActionListener to try to link the two images. . .

Reading through I am not sure if the two images I have need to be in separate classes although or what and I am a bit confused here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38334
    
  23
Surely the two images would be two instances of the same class?

Moving to our GUIs forum.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Linking 2 images with a button in a GUI