FAQ 4c
Updated: 1/10/00

>>>>>> My Screen keeps flashing, what do I do?
==============================================

Here is the Applet function you need to overwrite: 
public void update(Graphics g);

This function is the one that calls paint().  It performs some default 
drawing operations before paint() gets started.  One of these operations is 
to clear the screen using the background color.

Here is how you overwrite the update() function:

class MyApplet extends Applet {
	public void update(Graphics g){paint(g);}
}


That's it.  Now you may need to erase the screen yourself before drawing.
You can do that with these lines:


class MyApplet extends Applet {

	public void paint(Graphics g){
	Rectangle r = getBounds();

		g.setColor(getBackground());	// or pick another color
		g.fillRect(0,0,r.width,r.height);
	}
}


If you have components (like other panels or self-drawn buttons) you might need to 
include some other paint() type functions.  Here are some other functions that 
update() can call:
public void paintAll(Graphics g);		// paints this and child components
public void paintComponents(Graphics g);	// paints child components

	

:::::: My pictures flash while I draw them, what do I do?
=========================================================

If you are drawing a lot of pictures to the screen, over and over (like an arcade game),
your pictures may end up flashing or looking transparent.  This is because the paint 
function is erasing and drawing directly to the screen.

You solve this by drawing to an off-screen buffer, then copying the buffer to the screen.
This means that all the erasing and redrawing will be finished before you display your 
final image.  No more flickering, ghost-like figures.

Here is an example of using an offscreen buffer:

// 'BounceApplet' code file
import java.applet.*;
import java.awt.*;

class BounceApplet extends Applet {

	final static int WIDTH = 300;
	final static int HEIGHT = 200;
	final static int MILLIS_PER_PAINT = 50;

	Point center,delta;
	long lastDraw;
	Image offscreen;

	BounceApplet(){
		center.move(0,0);
		delta.move(10,10);
		lastDraw = System.currentTimeMillis() + MILLIS_PER_PAINT;
		offscreen = null;
		reshape(0,0,WIDTH,HEIGHT);	// resize the applet
	}

	public void update(Graphics g){
		if (offscreen==null) offscreen = createImage(WIDTH,HEIGHT);
		paint(offscreen.getGraphics()) ;
		g.drawImage(offscreen,0,0,this);
	}

	public void paint(Graphics g){
		g.setColor(Color.yellow);
		g.fillRect(0,0,WIDTH,HEIGHT);
		g.fillRect(center.x-5,center.y-5,center.x+5,center.y+5);

		moveBall();
		while (System.currentTimeMillis()<lastDraw) {}
		lastDraw = System.currentTimeMillis() + MILLIS_PER_PAINT;

		repaint();			// a crude looping method
	}

	void moveBall(){
		center.x += delta.x;
		if (center.x<0 || center.x>WIDTH) {
			center.x -= delta.x;
			delta.x = -delta.x;
		}
		center.y += delta.y;
		if (center.y<0 || center.y>HEIGHT) {
			center.y -= delta.y;
			delta.y = -delta.y;
		}
	}

	public void start(){repaint();}
}




The core of this program is in update().  The offscreen is an Image.  It has to be created.
Then you can recover it's Graphics object and draw into it.  Finally, you draw the Image 
into the screen Graphics object (the Graphics object passed into update()).

You can create the offscreen in the init() function if you like.  You will need to use the 
waitForImage() function from FAQ_3a to make sure it is created successfully.

This applet creates a bouncing ball that moves around the screen.  You can adjust its speed 
by changing the value of delta.  You can adjust the frame rate (number of times drawn 
each second) by changing the MILLIS_PER_PAINT.  

There are a thousand Milliseconds per second.  With a value of MILLIS_PER_PAINT=50, you won't 
get more than 20 paints each second (1000 / 50 = 20).


:::::: start and stop function
==============================

Applets have a start() and stop() function that you can overwrite.
The start() function is called whenever a browser displays your applet.
The stop() function is called when your applet is hidden.

Overwrite the start and stop functions to respond to these events.
If I was kinder, I would have halted the paint loop when stop() was called.
As things are, the applet is going to eat up 100% of the cpu time.


:::::: final static values
==========================

final static values are constants.  This value is the same every time you use 
it in your code.  It will cause an error if you try to change it.