Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: SimpleApplet7.html |
/** USE EXISTING JAVA CLASSES **/
import java.awt.*;
import java.applet.Applet;
/** EXTEND THE JAVA APPLET CLASS **/
public class SimpleApplet7 extends Applet
{final int maxnumlines = 20;
// Define max number of lines,
final int maxsize = maxnumlines + 5;
// max array size,
Point startpoint[] = new Point[maxsize];
// the array of start points,
Point endpoint[] = new Point[maxsize];
// the array of end points,
Point anchorpoint, connectpoint;
// the current anchor point and
int entrynum = 0;
// connection point, and the
// current array entry number,
// starting with 0 (instead of 1).
public void init()
{setBackground(Color.white);}
// Change the gray background to white.
/** HANDLE THE MOUSEDOWN EVENT **/
public boolean mouseDown(Event e, int x, int
y)
{if (entrynum < maxsize)
{anchorpoint
= new Point(x,y);
// Initialize both points to
connectpoint = anchorpoint;
// the current mouse position.
return true;}
// Note that this will give us a
else
// way to prevent painting a single
{return false;}
// point (see the mouseUp method).
}
Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: SimpleApplet7.html |
Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: SimpleApplet7.html |
public void paint(Graphics g)
{g.setColor(Color.blue);
for (int i = 0; i
< entrynum; i++)
// Note that nothing happens here,
{if (i
< maxnumlines)
// until a mouseDown event occurs.
{g.drawLine(startpoint[i].x, startpoint[i].y,
// Repaint all lines from 0 to
endpoint[i].x, endpoint[i].y);} //
maxnumlines-1.
else
{g.setColor(Color.red);
// Also, if entrynum > maxnumlines,
g.drawString("Sorry, line quota exceeded !!", // then print
this instead of any
endpoint[i].x, endpoint[i].y);}
// extra line.
}
if (connectpoint !=
anchorpoint)
// Paint a temporary purple line in
{g.setColor(Color.magenta);
// response to a mouseDrag event.
g.drawLine(anchorpoint.x, anchorpoint.y,
connectpoint.x, connectpoint.y);}
}
}
Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: SimpleApplet7.html |