• Post Reply Bookmark Topic Watch Topic
  • New Topic

Making a graphic vanish when reaching the top of a frame/panel  RSS feed

 
Colm Dickson
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,


If I have a simple animation where a ball/shape moves up and down the screen, then, rather than stop it at the top of the screen or let it move off the screen, how would I simply make it vanish when the top of the shape hits the top of the frame. As it is moving, I am already calling repaint() method to show the change in (x,y) coordinates plus I am also re-setting the background colour so I don't get the smear effect. What then must I do to retain what I have yet blank out the shape when it hits the top because as it stands, any change to the paint() method is called each time I repaint() and thus occurs with every movement whereas I just want this thing to happen once, at the top.



Thanks in advance for any suggestions,. greatly appreciated.

Colm
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Colm Dickson wrote:If I have a simple animation where a ball/shape moves up and down the screen, then, rather than stop it at the top of the screen or let it move off the screen, how would I simply make it vanish when the top of the shape hits the top of the frame. As it is moving, I am already calling repaint() method to show the change in (x,y) coordinates plus I am also re-setting the background colour so I don't get the smear effect.

This suggests that you have a problem in your code. Is this Swing or AWT? Are you overriding the paintComponent or paint methods respectively? In this override are you calling super.paintComponent(g) or super.paint(g) respectively?

What then must I do to retain what I have yet blank out the shape when it hits the top because as it stands, any change to the paint() method is called each time I repaint() and thus occurs with every movement whereas I just want this thing to happen once, at the top.

To figure out what changes your code needs, we'll likely need to see your code.
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The general technique is to use a boolean member variable in your graphic component and toggle it when the ball touches the boundry. This is done in/from the event code.
 
Colm Dickson
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies.

So I could have the code inside my paintComponent method in such a way that if the x,y position of the ball was within the bondary then I could call drawOval with my updated coordinates (Using an if statement) and if it was not then I could call a method(Using an else statement) e.g. vanish(). My idea for this would be to set the coordinates to a point that is off the screen. How does this sound?

Instead of vanishing, I could do something like drawString("Pop!",x,y), using the ball coordinates(as in bursting) but I currently do not know how to mimic a pop/bang in graphics.

Would the idea above sound ok or is there a better way?

Thanks again,

Colm
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Colm Dickson wrote:So I could have the code inside my paintComponent method in such a way that if the x,y position of the ball was within the bondary then I could call drawOval with my updated coordinates (Using an if statement) and if it was not then I could call a method(Using an else statement) e.g. vanish(). My idea for this would be to set the coordinates to a point that is off the screen. How does this sound?

A key point to always remember is to strive to keep program logic out of your paintComponent method. In otherwords, in a program like this one, I'd have a timer (Swing Timer perhaps) update the position of the ball and call repaint stimulating the JVM to call paint/paintComponent. If the ball reaches a boundary, then the timer would change a switch, a boolean variable, so that the paint component no longer draws the ball.

The best thing you can do is to experiment with code and see what happens -- that's the fun part about Graphics programming. Play with the code, try different things, and then if you get stuck, come on back with your code and let's see if we can help you out.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!