To learn more about polymorphism, mosey over to the campfire to hear about How my Dog learned Polymorphism
This is an application, not an applet. Here is a UML class diagram to show what classes we will be working with:
We have an interface called "Shape". The classes Circle, Box and Poly (short for polygon) all implement the Shape interface. That means that they have agreed to implement all of the methods in Shape.
Here is the complete source code for Shape.java:
import java.awt.* ;
public interface Shape
{
public void draw( Graphics g );
}
Here is the source for our three classes:
import java.awt.* ;
public class Circle implements Shape
{
private int x ;
private int y ;
private int wide ;
private int high ;
private Color color ;
Circle( int x , int y , int wide , int high , Color color )
{
this.x = x ;
this.y = y ;
this.wide = wide ;
this.high = high ;
this.color = color ;
}
public void draw( Graphics g )
{
g.setColor( color );
g.fillOval( x , y , wide , high );
}
}
import java.awt.* ;
public class Box implements Shape
{
private int x ;
private int y ;
private int wide ;
private int high ;
private Color color ;
Box( int x , int y , int wide , int high , Color color )
{
this.x = x ;
this.y = y ;
this.wide = wide ;
this.high = high ;
this.color = color ;
}
public void draw( Graphics g )
{
g.setColor( color );
g.fillRect( x , y , wide , high );
}
}
import java.awt.* ;
public class Poly implements Shape
{
int[] x ;
int[] y ;
private Color color ;
Poly( int[] x , int[] y , Color color )
{
this.x = x ;
this.y = y ;
this.color = color ;
}
public void draw( Graphics g )
{
g.setColor( color );
g.fillPolygon( x , y , x.length );
}
}
And to show off polymorphism in action, here is a program that creates a bunch of Circle, Box and Poly objects and stores them in a Shape array. Then it goes through the array and draws all of the shapes.
Go ahead and copy these programs to your disk as Shape.java, Cirle.java, Box.java, Poly.java and ShowShapes.java. Compile 'em up and run ShowShapes. You should see the beginnings of how most modern graphical operating systems work.
Here's ShowShapes.java:
import java.awt.* ;
public class ShowShapes extends Frame
{
static int[] vx = { 200 , 220 , 240 , 260 , 280 , 250 , 230 };
static int[] vy = { 150 , 150 , 190 , 150 , 150 , 210 , 210 };
static Shape[] shapes =
{
new Box( 50 , 70 , 100 , 20 , Color.red ) ,
new Box( 90 , 70 , 20 , 110 , Color.blue ) ,
new Circle( 50 , 150 , 60 , 60 , Color.green ) ,
new Circle( 70 , 170 , 20 , 20 , Color.white ) ,
new Box( 50 , 90 , 40 , 90 , Color.white ) ,
new Circle( 130 , 150 , 60 , 60 , Color.green ) ,
new Box( 170 , 180 , 20 , 30 , Color.blue ) ,
new Circle( 150 , 170 , 20 , 20 , Color.white ) ,
new Poly( vx , vy , Color.black ) ,
new Circle( 290 , 150 , 60 , 60 , Color.green ) ,
new Box( 330 , 180 , 20 , 30 , Color.blue ) ,
new Circle( 310 , 170 , 20 , 20 , Color.white ) ,
};
ShowShapes()
{
setBounds( 200 ,150 , 400 , 250 );
setVisible( true );
}
public void paint( Graphics g )
{
for( int i = 0 ; i < shapes.length ; i++ )
{
shapes[ i ].draw( g );
}
}
public static void main( String[] args )
{
new ShowShapes();
}
}
Note that to stop the program, you'll need to pull up the console you started it in and press control-C.
CodeBarnBeginnerJava