• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with repaint()  RSS feed

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, this problem carries on from my last problem, but is totally different.
I am making a pacman game as an applet.
I have a class that extends JApplet
and a class that extends JPanel
I'll get straight to the point, I am trying to animate an object
moving across the screen, I have just made a sample program
showing a RED BALL moving across the screen.
When the mouse is pressed , a for loop starts to draw the ball,
I have used an empty for loop to create a pause.
Each time the balls x-position increases by 1, it is drawn (repaint())
back to the screen. For some reason, it is not being drawn till the end
of the for loop.

The code is displayed below, please can someone try executing this, so
they can see what I mean, and if possible explain how to get the ball to move smoothly across the screen. Thanks very much




and here is the P2.java
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to separate the processing (for-loop) from the painting thread,
usually by creating a separate thread for the loop

something like this

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thanks that works.
I kind of see whats happening, but that didn't happen when I was using
a normal application, and extending JPanel.
I've never liked threads, and now there back to haunt me
Anyway, it works now, just need to play around with it thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!