Reputation: 41
As you may have noticed a question I already asked.
But the problems as soon as I apply random. as you have advised me.That's what I get as a result:
https://www.youtube.com/watch?v=EELo_-eh3fA
That is when I use random images begin to jump around the screen.
Here is a code I use:
int x = (int) (Math.random() * 290);
int y = (int) (Math.random() * 290);
g.drawImage(image, x,y,32,32, null);
How can I make sure that images are not jumping. I need pictures to be displayed randomly on the screen and remained on the spot!
Upvotes: 0
Views: 138
Reputation: 347234
Painting may occur for any number of reasons, not just when you want it.
Each time paint is called, it is responsible for rebuilding the entire graphical content. Basically what is happening, you are assigning a new random position to the image each time it is painted.
Instead, create a reference to something like a Point
and use it each time paint is called again.
Based on you previous example, you should not be overriding paint
, but instead should be using paintComponent
and you MUST call super.paintComponent
to ensure that the Graphics
context is updated correctly, this especially important when dealing with JComponent
as it is transparent
Upvotes: 1