import java.awt.*; // Target (main) class: Create the CUCSDrawing drawing window public class CUCSGraphicsApplication { public static void main(String args[]) { CUCSDrawing d = new CUCSDrawing(new Face[] { new Face(), new Frown(), new ColoredFrown(Color.red) } ); } } // Class Drawing: a simple graphics window that draws a list faces of Faces public class CUCSDrawing extends Frame { Face[] faces; CUCSDrawing(Face[] f) { super(); // <-- if not present, would be automatically called anyway: // MUST initialize fields of superclass // Reminder: if a superclass' constructor is called, it must be // called at the very START of the subclass' constructor. faces = f; resize(350,150); move(0,75); setTitle("Drawing"); show(); toFront(); } public void paint(Graphics g) { g.setColor(Color.black); g.translate(0, getInsets().top); for (int i = 0; i < faces.length; i++) { faces[i].paint(g); g.translate(110,0); } } } // class Face: a face with eyes and a mouth public class Face { public void drawEyes(Graphics g) { g.fillOval(20,20,10,10); g.fillOval(70,20,10,10); } public void drawMouth(Graphics g) { g.drawLine(30,70,70,70); } public void paint(Graphics g) { g.drawOval(0,0,100,100); drawEyes(g); drawMouth(g); } } // a Face with a frowning mouth public class Frown extends Face { // no constructor: new Frown () automatically uses Face() public void drawMouth(Graphics g) { g.drawLine(20,80,40,70); g.drawLine(40,70,60,70); g.drawLine(60,70,80,80); } } // a Frown whose eyes can be colored public class ColoredFrown extends Frown { Color eyeColor; ColoredFrown(Color eye) { // note: Frown() is automatically called! eyeColor = eye; } public void drawEyes(Graphics g) { Color savedColor = g.getColor(); g.setColor(eyeColor); super.drawEyes(g); g.setColor(savedColor); } } // illustrative example public class Oops extends ColoredFrown { // MUST define a constructor that takes 1 or more arguments: // otherwise the default constructor Oops() will // try to automatically call ColoredFrown(), which DOES NOT EXIST! }