/* An application that reads data from a file, Figures.txt, that contains coordinates for circles and squares. It stores these in an array of Figures. Then it displays the figures on the window frame. It uses inheritance and polymorphism. */
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Figures
{
public static void main (String [] args)
{
FigureFrame
frame = new FigureFrame ();
frame.addWindowListener
(new WindowCloser ());
frame.show ();
}
} // class Figures
class WindowCloser extends WindowAdapter
{
public void windowClosing (WindowEvent e)
{System.exit (0);}
} // class WindowCloser
// Class that creates a frame and adds buttons and a canvas to it.
class FigureFrame extends Frame
{
private FigureList figureList;
private Graphics g;
private Button start, next;
private Canvas canvas;
private Panel panel;
FigureFrame ()
{
super ("Figures");
setSize (300,
300);
setBackground
(Color.cyan);
panel = new
Panel ();
start = new
Button ("Start");
start.addActionListener
(new StartListener ());
panel.add (start);
next = new Button
("Next");
next.addActionListener
(new NextListener ());
panel.add (next);
canvas = new
Canvas ();
canvas.setSize
(200, 200);
canvas.setBackground
(Color.white);
panel.add (canvas);
add (panel);
figureList =
new FigureList ();
} // constructor
/* Inner class that listens for the start
button. It gets the graphics for the canvas, reads in the list of
coordinates and then displays the first figure. */
class StartListener implements ActionListener
{
public void
actionPerformed (ActionEvent e)
{
g = canvas.getGraphics ();
figureList.readList ();
figureList.displayNextFigure (g);
} // method
actionPerformed
} // class StartListener
// Inner class that displays the next figure.
class NextListener implements ActionListener
{
public void
actionPerformed (ActionEvent e)
{
figureList.displayNextFigure (g);
} // method
actionPerformed
} // class NextListener
} // class Figures
// FigureList handles the array of Figures. It reads them in and
then displays the next one.
class FigureList
{
private Figure [] list;
private int listSize;
final int maxSize = 10;
private int nextLocation;
FigureList ()
{
list = new Figure
[maxSize];
listSize = 0;
nextLocation
= 0;
} // constructor
// Method that reads the data in from the
file and stores it in the array, list.
public void readList ()
{
Figure figure;
try
{
BufferedReader figureFile = new BufferedReader ( new InputStreamReader
(new FileInputStream ("Figures.txt")));
String type = figureFile.readLine ();
while ((type != null) && (listSize < maxSize))
{
if (type.equals ("Circle")) figure = new Circle (type);
else figure = new Square (type);
figure.readFigure (figureFile);
list [listSize] = figure;
listSize ++;
type = figureFile.readLine ();
}
figureFile.close ();
} catch (IOException
e) {System.out.println ("IO Error");}
catch (NumberFormatException
e) {System.out.println ("Number Error.");}
} // method readList
// Method that displays the next figure and
then updates the nextLocation variable.
public void displayNextFigure (Graphics g)
{
if (nextLocation
< listSize) list [nextLocation].displayFigure (g);
else System.out.println
("All figures displayed");
nextLocation
++;
} // method displayNextFigure
} // class FigureList
// Figure is the super class that reads in the coordinates.
class Figure
{
protected String type;
protected int x, y;
protected Color color;
Figure (String t) { type = t;} // constructor
public void readFigure (BufferedReader infile)
throws IOException
{
x = Integer.parseInt
(infile.readLine ());
y = Integer.parseInt
(infile.readLine ());
} // method readFigure
public void displayFigure (Graphics g)
{
g.setColor (color);
} // method displayFigure
}// class Figure
/* The Circle class inherits the x and y coordinates from the Figure
class. It then reads in the diameter of the circle. */
class Circle extends Figure
{
private int diameter;
Circle (String t) {super (t);} // constructor
public void readFigure (BufferedReader infile)
throws IOException
{
super.readFigure
(infile);
diameter = Integer.parseInt
(infile.readLine ());
color = Color.red;
} // method readFigure
public void displayFigure (Graphics g)
{
super.displayFigure
(g);
g.fillOval (x,
y, diameter, diameter);
} // method displayFigure
} // class Circle
/* The Square class inherits the x and y coordinates from the Figure
class and the reads in the length of the side. */
class Square extends Figure
{
private int side;
Square (String t) { super (t); } // constructor
public void readFigure (BufferedReader infile)
throws IOException
{
super.readFigure (infile);
color = Color.green;
side = Integer.parseInt
(infile.readLine ());
} // method readFigure
public void displayFigure (Graphics g)
{
super.displayFigure
(g);
g.fillRect (x,
y, side, side);
} // method displayFigure
} // class Square