home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Java by Example
/
jbecd.bin
/
JBE-CD
/
NTUsers
/
JBECODE.ZIP
/
JavaByExample
/
chap25
/
MouseApplet4.java
< prev
Wrap
Text File
|
1996-03-12
|
2KB
|
76 lines
import java.awt.*;
import java.applet.*;
public class MouseApplet4 extends Applet
{
Point startPoint;
Point points[];
int numPoints;
boolean drawing;
boolean painting;
public void init()
{
startPoint = new Point(0, 0);
points = new Point[1000];
numPoints = 0;
drawing = false;
painting = false;
resize(400, 300);
}
public void paint(Graphics g)
{
if (painting)
{
painting = false;
int oldX = startPoint.x;
int oldY = startPoint.y;
for (int x=0; x<numPoints; ++x)
{
g.drawLine(oldX, oldY, points[x].x, points[x].y);
oldX = points[x].x;
oldY = points[x].y;
}
}
}
public boolean mouseDown(Event evt, int x, int y)
{
drawing = true;
startPoint.x = x;
startPoint.y = y;
return true;
}
public boolean mouseMove(Event evt, int x, int y)
{
if ((drawing) && (numPoints < 1000))
{
points[numPoints] = new Point(x, y);
++numPoints;
}
return true;
}
public boolean keyDown(Event evt, int key)
{
if (key == Event.F2)
{
painting = true;
repaint();
}
else if (key == Event.F3)
{
painting = true;
drawing = false;
numPoints = 0;
repaint();
}
return true;
}
}