• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Linking 2 images with a button in a GUI

 
Tom Little
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Little
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
edited
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50182
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surely the two images would be two instances of the same class?

Moving to our GUIs forum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic