JavaRanch Home    
 
This page:         last edited 28 September 2007         What's Changed?         Edit
READ ONLY

Code Barn Shapes   

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 ;
          
          Circleint 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 ;
          
          Boxint 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 ;
          
          Polyint[] 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 =
          {
              // J
              new Box50 , 70 , 100 , 20 , Color.red ) ,
              new Box90 , 70 , 20 , 110 , Color.blue ) ,
              new Circle50 , 150 , 60 , 60 , Color.green ) ,
              new Circle70 , 170 , 20 , 20 , Color.white ) ,
              new Box50 , 90 , 40 , 90 , Color.white ) ,
              
              // a
              new Circle130 , 150 , 60 , 60 , Color.green ) ,
              new Box170 , 180 , 20 , 30 , Color.blue ) ,
              new Circle150 , 170 , 20 , 20 , Color.white ) ,
              
              // v
              new Poly( vx , vy , Color.black ) ,
              
              // a
              new Circle290 , 150 , 60 , 60 , Color.green ) ,
              new Box330 , 180 , 20 , 30 , Color.blue ) ,
              new Circle310 , 170 , 20 , 20 , Color.white ) ,
          };
          
          ShowShapes()
          {
              setBounds( 200 ,150 , 400 , 250 );
              setVisible( true );
          }
          
          public void paint( Graphics g )
          {
              forint 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

JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton