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

[java] Double Buffering Images?

Started by Thanhda Apr 5, 2005 at 12:26 AM 2 replies 1.1k views
Original Post
Thanhda
Thanhda
hi, i am trying to program a java applet game, and it keeps flickering. i was told that it was double buffering, i cant seem to get it to work. if anyone can help me out that would be awsome. code

import java.applet.*;
import java.awt.*;
import java.net.*;

public class Threads1 extends Applet implements Runnable {

   Image pic;
   URL base;
   MediaTracker mt;
   int width, height;
   int i = 200;
   Thread t = null;
   boolean threadSuspended;

   public void init() { 
      mt = new MediaTracker(this);
      try {
           base = getDocumentBase();
       }
       catch (Exception e) {}
       pic = getImage(base,"FFRJ/start.gif");
       try {
              mt.waitForAll();
       }
       catch (InterruptedException  e) {}
      width = getSize().width;
      height = getSize().height;
      setBackground( Color.black );
   }
   public void destroy() {
      System.out.println("destroy()");
   }

   public void start() {
      if ( t == null ) {
         t = new Thread( this );
         threadSuspended = false;
         t.start();
      }
      else {
         if ( threadSuspended ) {
            threadSuspended = false;
            synchronized( this ) {
               notify();
            }
         }
      }
   }
   public void stop() {
      threadSuspended = true;
   }
   public void run() {
      try {
         while (true) {
            if(i<=-100)
                i=300;
            i-=5;
            if ( i == 10 ) {
               i = 0;
            }
            showStatus( "i is " + i );
            if ( threadSuspended ) {
               synchronized( this ) {
                  while ( threadSuspended ) {
                     wait();
                  }
               }
            }
            repaint();
            t.sleep( 10 );  
         }
      }
      catch (InterruptedException e) { }
   }
   public void paint( Graphics g ) {

      g.setColor( Color.green );
      g.drawImage(pic,20,i,this);
      g.drawRect( 10, i, 100, 15 );

   }
}



Thanhda TieDigital Shock, Technical Director & Business Relations15-75 Bayly Street West, Suite #173Ajax, Ontario, L1S 7K7Tel: 416.875.1634http://www.digitalshock.net
Thanhda
Thanhda
Quote:
Original post by capn_midnight
seriously, check the faq


Thanks Man, sorry for not checking FAQ, totally forgot about that.
Thanhda TieDigital Shock, Technical Director & Business Relations15-75 Bayly Street West, Suite #173Ajax, Ontario, L1S 7K7Tel: 416.875.1634http://www.digitalshock.net
Kevinator
Kevinator
It's pretty simple (for the future).. extend JApplet and use Swing, and it will be done automatically for you.

Topic Locked

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

Sign in to reply to this topic.