Original Post
Hear it goes... this is starting off as a double buffering class...
i found some code, and changed it to an abstract class since i want to always remeber what function i must override... although it would be cooler if the compiler just said hey you need this function, instead of class is abstract doesn't define this function... Any how here is the code
Now i want to make an applet that extends my Double Buffer Class... when i try it lets me but says applet not initialized can someone show me an example of makeing an applet that extends this panel? Eventually i want to be able to use this for a application or applet.
The quick fix i did was DoubleBuffer extend Applet, but that defeats what i am trying to do in the first place.
[edited by - darkWang on May 14, 2003 7:14:27 PM]
import java.awt.*;
import java.applet.*;
public abstract class DoubleBuffer extends Panel{
// class variables
private int bufferWidth;
private int bufferHeight;
private Image bufferImage;
private Graphics bufferGraphics;
public DoubleBuffer(){
super();
}
public void update(Graphics g){
paint(g);
}
public void paint(Graphics g){
if(bufferWidth!=getSize().width || bufferHeight!=getSize().height || bufferImage==null || bufferGraphics==null)
resetBuffer();
if(bufferGraphics!=null){
// this clears the offscreen image, not the onscreen one
bufferGraphics.clearRect(0,0,bufferWidth,bufferHeight);
// calls the paintbuffer method with the offscreen graphics as a param
paintBuffer(bufferGraphics);
// we finaly paint the offscreen image onto the onscreen image
g.drawImage(bufferImage,0,0,this);
}
}
public abstract void paintBuffer(Graphics g); //{
// in classes extended from this one, add something to paint here!
// always remember, g is the offscreen graphics
//}
private void resetBuffer(){
// always keep track of the image size
bufferWidth=getSize().width;
bufferHeight=getSize().height;
// clean up the previous image
if(bufferGraphics!=null){
bufferGraphics.dispose();
bufferGraphics=null;
}
if(bufferImage!=null){
bufferImage.flush();
bufferImage=null;
}
System.gc();
// create the new image with the size of the panel
bufferImage=createImage(bufferWidth,bufferHeight);
bufferGraphics=bufferImage.getGraphics();
}
}