Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello!

I would like to draw an image on the screen, and then remove it, but there's a little problem.

I can easily draw it, but then when it comes to remove, I just don't know how.
No, not simply filling the image's location with the background color, it isn't that easy.

The program I am trying to develop is a game, actually a snake. An endless one, so my snake doesn't have an endpoint, just a head which is going.

So when I draw the image I want the program to draw it UNDER the snake, so which I have already draw should not be delete.

When a snake takes the bonus (which is the image), it should be removed from the screen by redrawing which was on the top of it.

A picture of it:
http://imageshack.us/m/840/2449/nvtelenx.png[^]
Posted
Updated 12-May-11 22:10pm
v2
Comments
Dalek Dave 13-May-11 4:10am    
Edited for Grammar and Readability

Override the paint method to display the valid content.
Write the code so that it it recalculates the postions [on a timer probaly] and the call repaint().
 
Share this answer
 
Comments
velvet7 12-May-11 11:06am    
And then how can I remove the image from the screen without erasing the snake?
Nagy Vilmos 13-May-11 7:08am    
on each repaint the snake is drwan in it's new position.
Dalek Dave 13-May-11 4:10am    
Good answer.
You can't only remove the background image. You will have to invalidate and repaint the entire section that the background image is rendered on.

How you need to do this depends a bit on how you structured your application. But like Nagy Vilmos said you can redraw part of your canvas. You can do this by calling the repaint method with the coordinates and size. This will trigger all child canvas objects within the clipbounds to also repaint.

Keep in mind your actual solution strongly depends on how you are painting on the canvas in the first place. Are you using one canvas and painting all shapes on it manually. Or does each shape have its own canvas?
 
Share this answer
 
Comments
Dalek Dave 13-May-11 4:10am    
Nice.
You need to think of a stack of layers (JPanels/Composites). There must be

- a background layer (colored)
- a border layer (transparent with the borders of the game field)
- at least one bonus layer (for the fruits, also transparent)
- a action layer (for the snake, also transparent)
- a layer for animations(?)

... and so on. That makes it easier for you to draw things on it. You can also add and delete layers while runtime (thinking of game levels...).

Also check this:
How to Use Layered Panes @ oracle.com[^]

regards
Torsten
 
Share this answer
 
Comments
Dalek Dave 13-May-11 4:11am    
You thought about that one haven't you :)
TorstenH. 13-May-11 5:05am    
One of my project uses sea maps and displays the ships on that map as small darts(plots) from different radars. I use such a stack of layers for that, each sort of plots has its own layer. Quite similar - even if it's not a game :-)
Nagy Vilmos 13-May-11 7:09am    
TVG! Slightly more detailed then I went into.
velvet7 13-May-11 9:39am    
Nice answer, thank you, that is what I needed! 5 points!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900