Home E-Books Special Effects and Game Development in Java The Applet MouseMoveTest

Special Effects and Game Development in Java(TM) - The Applet MouseMoveTest

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

public class mousemovetest extends Applet {

//The variable "background" image points 
//to the background image (the image in the notepad)
Image backgroundimage;

//The variable "penimage" is the pen that moves
//when you move the mouse cursor.
Image penimage;

//Now we must use double buffering in order to avoid
//graphical problems
Image bufferimage;
Graphics bufferg;

//The position of the mouse cursor is stored
//in the variables 
//"penx" and "peny" so that the paint() method
//can draw "penimage" on the right position
int penx;
int peny;

public void init()
    //Initialize the tools for the double buffering
    Dimension d=size();

    MediaTracker tracker=new MediaTracker(this);

    //Note that "pen.gif" is transparent.

    try {tracker.waitForAll();}
    catch(InterruptedException e)
        System.out.println("Something interrupted the image loading...");

public void paint(Graphics g)
    if (bufferg!=null)
		//First we draw the background image

		//Then we draw the image of the pen
		//at the positions which are held
		//by "penx" and "peny". We must also adjust
		//the position of the image since the 
		//tip of the pen is not pointing exactly
		//to the edge of the image.

        //Finally we draw the buffer image on
		//the screen

public void update(Graphics g)

public boolean mouseMove(Event e, int x, int y)
	//By updating the position of the pen
	//with the variables "penx" and "peny" we can
	//draw the pen at this position later.
    penx=x; peny=y;

	//In order to not "overload" the webbrowser we use
	//the repaint() method.
    return true;