Skip to main content
GameDev.net gamedev.net
🔒 Locked

Java Applet - paint() not repaint()

Started by azoundria May 25, 2006 at 1:10 PM 2 replies 2.6k views
Original Post
azoundria
azoundria
 public void run() {
  while (Thread.currentThread() == animator) {
   // Display the next frame of animation.
   enemymove();
   you.move();
      
   // Delay for a while
   try {
    Thread.sleep(delay);
   } catch (InterruptedException e) {
    break;
   }

   // Advance the frame
   frame++;

   repaint();
  }
 }

 public void paint(Graphics g) {
  g.setColor(Color.WHITE);
  g.drawOval(you.x - 16, you.y - 8, 32, 16);
  g.setColor(Color.WHITE);
  g.drawString(notes, 20, 20);
  g.setColor(Color.WHITE);
  g.drawString("Health: " + you.health + "/" + you.maxhealth, 50, 50);
 }
I am fairly new to Java and trying to build an applet game. The game runs but the screen refresh is rather slow and there is a noticeable flickers as it clears and regenerates the screen. I just want it to redraw everything, not to clear first. I tried changing repaint() to paint() and it is giving me errors. I somehow need to include 'g' in the calling I think, but Im not sure how I do this? I have tried just about everything, including google, and have been stuck for a couple days so help is very appreciated.
Ravuya
Ravuya
You should implement double-buffering to reduce flicker. Here is a slightly overengineered example.
Bob Janova
Bob Janova
To not clear first, you can simply override update() to do nothing (or call paint directly, I can't remember).
Im_wasting_my_life
Im_wasting_my_life
I think you would benefit from a pdf doc located here. Page 5 explains how to implement double buffering. On page 7 he explains how use "active rendering", a method that allows you to control the drawing more directly. Although he is using the "JPanel" instead of "JApplet" everything there should work just about the same.

I would definitely recommend the book. His site, here, has a lot of good information including his source code and drafts (almost exactly as they were published) for the chapters of his book.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.