import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
/* This applet draws circles wherever the mouse has been clicked. When the maximum number of circles have been drawn, all the circles are joined by lines. */
public class Figures extends Applet
{
private Graphics g;
private int [] xValues;
private int [] yValues;
private int numberClicks;
final int maxClicks = 10;
public void init ()
{
addMouseListener
(new DrawPictureListener ());
g = getGraphics
();
xValues =
new int [maxClicks];
yValues =
new int [maxClicks];
numberClicks
= 0;
} // method init
/* Inner class that draws a circle
when the user clicks the mouse. It then connects all the circles with lines
after the last one is drawn. */
class DrawPictureListener implements
MouseListener
{
public void
mousePressed (MouseEvent e)
{
int x = e.getX ();
int y = e.getY ();
if (numberClicks < maxClicks)
{
xValues [numberClicks] = x;
yValues [numberClicks] = y;
numberClicks ++;
Circle circle = new Circle (Color.red, x, y, 10);
circle.drawCircle (g);
}
else
{
Graph graph = new Graph ();
graph.drawGraph (g, xValues, yValues, maxClicks);
}
} // method
mouseClicked
public void
mouseEntered (MouseEvent e) {}
public void
mouseExited (MouseEvent e) {}
public void
mouseClicked (MouseEvent e) {}
public void
mouseReleased (MouseEvent e) {}
} // class DrawPictureListener
} // class Figures
class Graph
{
public void drawGraph (Graphics g,
int [] xValues, int [] yValues, int maxClicks)
{
g.setColor
(Color.blue);
for (int count
= 0; count < maxClicks-1; count ++)
g.drawLine (xValues [count], yValues [count], xValues [count+1], yValues
[count+1]);
g.drawLine
(xValues [0], yValues [0], xValues [maxClicks-1], yValues [maxClicks-1]);
} // method drawGraph
} // class Graph
class Circle
{
private Color color;
private int x, y, diameter;
Circle (Color c, int xPosition, int
yPosition, int d)
{
color = c;
x = xPosition;
y = yPosition;
diameter =
d;
} // constructor
public void drawCircle (Graphics g)
{
g.setColor
(color);
g.drawOval
(x, y, diameter, diameter);
} // method drawCircle
} // class Circle