// Shape.java
// Definition of interface Shape
import java.awt.Graphics;
public interface Shape {
public abstract void draw(Graphics g);
public abstract String getName();
}
// Test.java
// Driver for point, line, triangle hierarchy
import java.awt.Graphics;
import javax.swing.*;
public class Test extends JApplet{
Shape arrayOfShapes[];
public void init()
{
//Circle circle = new Circle( 0.4, 0.4, 0.2 );
arrayOfShapes = new Shape[ 3 ];
// aim arrayOfShapes[0] at subclass Point object
arrayOfShapes[ 0 ] = new Point( 0.1, 0.1 );
// aim arrayOfShapes[1] at subclass Line object
arrayOfShapes[ 1 ] = new Line(0.1, 0.4, 0.4, 0.1);
// aim arrayOfShapes[2] at subclass Triangle object
arrayOfShapes[ 2 ] = new Triangle(new Point(0.1,0.8),new Point(0.8,0.6), new Point(0.8,0.2));
}
public void paint(Graphics g)
{
for ( int i = 0; i < arrayOfShapes.length; i++ )
arrayOfShapes[i].draw(g);
}
}
// FrameTest.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FrameTest extends JFrame{
Shape arrayOfShapes[];
FrameTest(String title)
{
super(title);
}
public void init()
{
JPanel panel = new JPanel()
{
public void paint(Graphics g)
{
super.paint(g);
for ( int i = 0; i < arrayOfShapes.length; i++ )
arrayOfShapes[i].draw(g);
}
};
panel.setBackground(Color.white);
getContentPane().add(panel);
arrayOfShapes = new Shape[ 3 ];
// aim arrayOfShapes[0] at subclass Point object
arrayOfShapes[ 0 ] = new Point( 0.1, 0.1 );
// aim arrayOfShapes[1] at subclass Line object
arrayOfShapes[ 1 ] = new Line(0.1, 0.4, 0.4, 0.1);
// aim arrayOfShapes[2] at subclass Triangle object
arrayOfShapes[ 2 ] = new Triangle(new Point(0.1,0.8),new Point(0.8,0.6), new Point(0.8,0.1));
}
public static void main( String args[])
{
FrameTest window = new FrameTest("Shapes");
window.init();
WindowAdapter wl = new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
System.exit(0);
}
};
window.addWindowListener(wl);
window.setSize(120,120);
window.show();
}
}
// Triangle.java
// Definition of class Triangle (version 1)
import java.awt.Graphics;
/**
* Triangle describes a two-dimensional Triangle.
*
*/
public class Triangle implements Shape
{
public Point pa = new Point();
public Point pb = new Point();
public Point pc = new Point();
public Triangle() { }
public Triangle(Point a, Point b, Point c) { setTriangle(a,b,c); }
public void setTriangle(Point a, Point b, Point c)
{
pa.setPoint(a.x,a.y);
pb.setPoint(b.x,b.y);
pc.setPoint(c.x,c.y);
}
public String getName() { return "Triangle"; }
public void draw(Graphics g)
{
g.drawLine(pa.nX(),pa.nY(),pb.nX(),pb.nY());
g.drawLine(pb.nX(),pb.nY(),pc.nX(),pc.nY());
g.drawLine(pc.nX(),pc.nY(),pa.nX(),pa.nY());
}
}
// Line.java
// Definition of class Line (version 1)
import java.awt.Graphics;
/**
* Line describes a two-dimensional line.
*
* The line is defined by parametric equations
*
* x(u) = pa.x * (1-u) + pb.x * u;
* y(u) = pa.y * (1-u) + pb.y * u;
*/
public class Line implements Shape
{
public Point pa = new Point();
public Point pb = new Point();
public Line() { setLine(0,0,0,0); }
public Line(Point a, Point b) { setLine(a.x,a.y,b.x,b.y); }
public Line(double x1, double y1, double x2, double y2)
{
setLine(x1,y1,x2,y2);
}
public void setLine(double x1, double y1, double x2, double y2)
{
pa.setPoint(x1,y1);
pb.setPoint(x2,y2);
}
public Point getPoint(double u)
{
double up = 1.0-u;
double x = pa.x * up + pb.x * u;
double y = pa.y * up + pb.y * u;
return new Point(x,y);
}
public String getName() { return "Line"; }
public void draw(Graphics g)
{
g.drawLine(pa.nX(),pa.nY(),pb.nX(),pb.nY());
}
}
// Point.java
// Definition of class Point (version 2)
import java.awt.Graphics;
/**
* Point describes a two-dimensional point.
*/
public class Point implements Shape
{
static final double SCALE = 100;
public double x, y; // coordinates of the Point
// No-argument constructor
public Point() { setPoint( 0, 0 ); }
// Constructor
public Point( double a, double b ) { setPoint( a, b ); }
// Set x and y coordinates of Point
public void setPoint( double a, double b )
{
x = a;
y = b;
}
// get x pixel value
public int nX() { return (int)(x*SCALE); }
// get y pixel value
public int nY() { return (int)(y*SCALE); }
// offset (translate) point by the amount (tx, ty)
public void offset(double tx, double ty)
{
x += tx;
y += ty;
}
public String getName() { return "Point"; }
public void draw(Graphics g)
{
int nx = nX();
int ny = nY();
g.fillOval(nx-5,ny-5,11,11);
}
// convert the point into a String representation
public String toString()
{ return " (" + x + ", " + y + ")"; }
}
Maintained by John Loomis, last updated 7 June 2000