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();
bufferimage=createImage(d.width,d.height);
bufferg=bufferimage.getGraphics();
MediaTracker tracker=new MediaTracker(this);
backgroundimage=getImage(getDocumentBase(),"block.jpg");
tracker.addImage(backgroundimage,0);
//Note that "pen.gif" is transparent.
penimage=getImage(getDocumentBase(),"pen.gif");
tracker.addImage(penimage,1);
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
bufferg.drawImage(backgroundimage,0,0,this);
//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.
bufferg.drawImage(penimage,penx-6,peny-6,this);
//Finally we draw the buffer image on
//the screen
g.drawImage(bufferimage,0,0,this);
}
}
public void update(Graphics g)
{
paint(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.
repaint(70);
return true;
}
} |
 |  |
|