Tugas #PBOA-tugas2 (Menggambar Rumah)
Class and Object
Berikut ini adalah program untuk membuat suatu gambar 2D menggunakan BlueJ. Berikut adalah class beserta source code yang digunakan.
- Picture
- Circle
- Square
- Triangle
- Canvas
 public class Picture  
 {  
   private Square wall;  
   private Square wall12;  
   private Square wall13;  
   private Square wall21;  
   private Square wall22;  
   private Square wall31;  
   private Square wall32;  
   private Square wall33;  
   private Square wall34;  
   private Square wall41;  
   private Square wall42;  
   private Square wall43;  
   private Square wall51;  
   private Square wall52;  
   private Square wall53;  
   private Square wall54;  
   private Square door1;  
   private Square door2;  
   private Square door3;  
   private Square door4;  
   private Square door5;  
   private Square window;  
   private Square window12;  
   private Square window13;  
   private Square window14;  
   private Square window15;  
   private Square window21;  
   private Square window22;  
   private Square window23;  
   private Square window31;  
   private Square window32;  
   private Square window33;  
   private Square window34;  
   private Square window35;  
   private Square window36;  
   private Square window37;  
   private Square window41;  
   private Square window42;  
   private Square window43;  
   private Square window44;  
   private Square window45;  
   private Square window51;  
   private Square window52;  
   private Square window53;  
   private Square window54;  
   private Square window55;  
   private Square window56;  
   private Square window57;  
   private Triangle roof;  
   private Triangle roof2;  
   private Triangle roof3;  
   private Triangle roof4;  
   private Triangle roof5;  
   private Circle sun;  
   private Square trunk1;  
   private Square trunk2;  
   private Triangle tree1;  
   private Triangle tree2;  
   private Triangle tree3;  
   /**  
    * Constructor for objects of class Picture  
    */  
   public Picture()  
   {  
     // nothing to do... instance variables are automatically set to null  
   }  
   /**  
    * Draw this picture.  
    */  
   public void draw()  
   {  
     wall = new Square();  
     wall.moveVertical(280);  
     wall.moveHorizontal(-60);  
     wall.changeSize(70);  
     wall.makeVisible();  
     wall12 = new Square();  
     wall12.moveVertical(210);  
     wall12.moveHorizontal(-60);  
     wall12.changeSize(70);  
     wall12.makeVisible();  
     wall13 = new Square();  
     wall13.moveVertical(140);  
     wall13.moveHorizontal(-60);  
     wall13.changeSize(70);  
     wall13.makeVisible();  
     wall21 = new Square();  
     wall21.changeColor("blue");  
     wall21.moveVertical(280);  
     wall21.moveHorizontal(10);  
     wall21.changeSize(70);  
     wall21.makeVisible();  
     wall22 = new Square();  
     wall22.changeColor("blue");  
     wall22.moveVertical(210);  
     wall22.moveHorizontal(10);  
     wall22.changeSize(70);  
     wall22.makeVisible();  
     wall31 = new Square();  
     wall31.moveVertical(280);  
     wall31.moveHorizontal(80);  
     wall31.changeSize(70);  
     wall31.makeVisible();  
     wall32 = new Square();  
     wall32.moveVertical(210);  
     wall32.moveHorizontal(80);  
     wall32.changeSize(70);  
     wall32.makeVisible();  
     wall33 = new Square();  
     wall33.moveVertical(140);  
     wall33.moveHorizontal(80);  
     wall33.changeSize(70);  
     wall33.makeVisible();  
     wall34 = new Square();  
     wall34.moveVertical(70);  
     wall34.moveHorizontal(80);  
     wall34.changeSize(70);  
     wall34.makeVisible();  
     wall41 = new Square();  
     wall41.changeColor("blue");  
     wall41.moveVertical(280);  
     wall41.moveHorizontal(150);  
     wall41.changeSize(70);  
     wall41.makeVisible();  
     wall42 = new Square();  
     wall42.changeColor("blue");  
     wall42.moveVertical(210);  
     wall42.moveHorizontal(150);  
     wall42.changeSize(70);  
     wall42.makeVisible();  
     wall43 = new Square();  
     wall43.changeColor("blue");  
     wall43.moveVertical(140);  
     wall43.moveHorizontal(150);  
     wall43.changeSize(70);  
     wall43.makeVisible();  
     wall51 = new Square();  
     wall51.moveVertical(280);  
     wall51.moveHorizontal(220);  
     wall51.changeSize(70);  
     wall51.makeVisible();  
     wall52 = new Square();  
     wall52.moveVertical(210);  
     wall52.moveHorizontal(220);  
     wall52.changeSize(70);  
     wall52.makeVisible();  
     wall53 = new Square();  
     wall53.moveVertical(140);  
     wall53.moveHorizontal(220);  
     wall53.changeSize(70);  
     wall53.makeVisible();  
     wall54 = new Square();  
     wall54.moveVertical(70);  
     wall54.moveHorizontal(220);  
     wall54.changeSize(70);  
     wall54.makeVisible();  
     door1 = new Square();  
     door1.changeColor("yellow");  
     door1.moveVertical(320);  
     door1.moveHorizontal(-40);  
     door1.changeSize(30);  
     door1.makeVisible();  
     door2 = new Square();  
     door2.changeColor("green");  
     door2.moveVertical(320);  
     door2.moveHorizontal(30);  
     door2.changeSize(30);  
     door2.makeVisible();  
     door3 = new Square();  
     door3.changeColor("yellow");  
     door3.moveVertical(320);  
     door3.moveHorizontal(100);  
     door3.changeSize(30);  
     door3.makeVisible();  
     door4 = new Square();  
     door4.changeColor("green");  
     door4.moveVertical(320);  
     door4.moveHorizontal(170);  
     door4.changeSize(30);  
     door4.makeVisible();  
     door5 = new Square();  
     door5.changeColor("yellow");  
     door5.moveVertical(320);  
     door5.moveHorizontal(240);  
     door5.changeSize(30);  
     door5.makeVisible();  
     window = new Square();  
     window.changeColor("yellow");  
     window.moveVertical(290);  
     window.moveHorizontal(-50);  
     window.changeSize(10);  
     window.makeVisible();  
     window12 = new Square();  
     window12.changeColor("yellow");  
     window12.moveVertical(250);  
     window12.moveHorizontal(-5);  
     window12.changeSize(10);  
     window12.makeVisible();  
     window13 = new Square();  
     window13.changeColor("yellow");  
     window13.moveVertical(210);  
     window13.moveHorizontal(-35);  
     window13.changeSize(10);  
     window13.makeVisible();  
     window14 = new Square();  
     window14.changeColor("yellow");  
     window14.moveVertical(160);  
     window14.moveHorizontal(-5);  
     window14.changeSize(10);  
     window14.makeVisible();  
     window15 = new Square();  
     window15.changeColor("yellow");  
     window15.moveVertical(150);  
     window15.moveHorizontal(-55);  
     window15.changeSize(10);  
     window15.makeVisible();  
     window21 = new Square();  
     window21.changeColor("yellow");  
     window21.moveVertical(290);  
     window21.moveHorizontal(50);  
     window21.changeSize(10);  
     window21.makeVisible();  
     window22 = new Square();  
     window22.changeColor("yellow");  
     window22.moveVertical(260);  
     window22.moveHorizontal(20);  
     window22.changeSize(10);  
     window22.makeVisible();  
     window23 = new Square();  
     window23.changeColor("yellow");  
     window23.moveVertical(230);  
     window23.moveHorizontal(50);  
     window23.changeSize(10);  
     window23.makeVisible();  
     window31 = new Square();  
     window31.changeColor("yellow");  
     window31.moveVertical(290);  
     window31.moveHorizontal(90);  
     window31.changeSize(10);  
     window31.makeVisible();  
     window32 = new Square();  
     window32.changeColor("yellow");  
     window32.moveVertical(290);  
     window32.moveHorizontal(130);  
     window32.changeSize(10);  
     window32.makeVisible();  
     window33 = new Square();  
     window33.changeColor("yellow");  
     window33.moveVertical(250);  
     window33.moveHorizontal(120);  
     window33.changeSize(10);  
     window33.makeVisible();  
     window34 = new Square();  
     window34.changeColor("yellow");  
     window34.moveVertical(210);  
     window34.moveHorizontal(90);  
     window34.changeSize(10);  
     window34.makeVisible();  
     window35 = new Square();  
     window35.changeColor("yellow");  
     window35.moveVertical(160);  
     window35.moveHorizontal(120);  
     window35.changeSize(10);  
     window35.makeVisible();  
     window36 = new Square();  
     window36.changeColor("yellow");  
     window36.moveVertical(115);  
     window36.moveHorizontal(90);  
     window36.changeSize(10);  
     window36.makeVisible();  
     window37 = new Square();  
     window37.changeColor("yellow");  
     window37.moveVertical(90);  
     window37.moveHorizontal(130);  
     window37.changeSize(10);  
     window37.makeVisible();  
     window41 = new Square();  
     window41.changeColor("yellow");  
     window41.moveVertical(290);  
     window41.moveHorizontal(180);  
     window41.changeSize(10);  
     window41.makeVisible();  
     window42 = new Square();  
     window42.changeColor("yellow");  
     window42.moveVertical(260);  
     window42.moveHorizontal(160);  
     window42.changeSize(10);  
     window42.makeVisible();  
     window43 = new Square();  
     window43.changeColor("yellow");  
     window43.moveVertical(260);  
     window43.moveHorizontal(205);  
     window43.changeSize(10);  
     window43.makeVisible();  
     window44 = new Square();  
     window44.changeColor("yellow");  
     window44.moveVertical(210);  
     window44.moveHorizontal(180);  
     window44.changeSize(10);  
     window44.makeVisible();  
     window45 = new Square();  
     window45.changeColor("yellow");  
     window45.moveVertical(160);  
     window45.moveHorizontal(200);  
     window45.changeSize(10);  
     window45.makeVisible();  
     window51 = new Square();  
     window51.changeColor("yellow");  
     window51.moveVertical(290);  
     window51.moveHorizontal(230);  
     window51.changeSize(10);  
     window51.makeVisible();  
     window52 = new Square();  
     window52.changeColor("yellow");  
     window52.moveVertical(250);  
     window52.moveHorizontal(270);  
     window52.changeSize(10);  
     window52.makeVisible();  
     window53 = new Square();  
     window53.changeColor("yellow");  
     window53.moveVertical(210);  
     window53.moveHorizontal(250);  
     window53.changeSize(10);  
     window53.makeVisible();  
     window54 = new Square();  
     window54.changeColor("yellow");  
     window54.moveVertical(160);  
     window54.moveHorizontal(230);  
     window54.changeSize(10);  
     window54.makeVisible();  
     window55 = new Square();  
     window55.changeColor("yellow");  
     window55.moveVertical(160);  
     window55.moveHorizontal(270);  
     window55.changeSize(10);  
     window55.makeVisible();  
     window56 = new Square();  
     window56.changeColor("yellow");  
     window56.moveVertical(115);  
     window56.moveHorizontal(275);  
     window56.changeSize(10);  
     window56.makeVisible();  
     window57 = new Square();  
     window57.changeColor("yellow");  
     window57.moveVertical(90);  
     window57.moveHorizontal(250);  
     window57.changeSize(10);  
     window57.makeVisible();  
     roof = new Triangle();  
     roof.moveVertical(125);  
     roof.moveHorizontal(-15);  
     roof.changeSize(50, 70);  
     roof.makeVisible();  
     roof2 = new Triangle();  
     roof2.moveVertical(195);  
     roof2.moveHorizontal(55);  
     roof2.changeSize(50, 70);  
     roof2.makeVisible();  
     roof3 = new Triangle();  
     roof3.moveVertical(55);  
     roof3.moveHorizontal(125);  
     roof3.changeSize(50, 70);  
     roof3.makeVisible();  
     roof4 = new Triangle();  
     roof4.moveVertical(125);  
     roof4.moveHorizontal(195);  
     roof4.changeSize(50, 70);  
     roof4.makeVisible();  
     roof5 = new Triangle();  
     roof5.moveVertical(55);  
     roof5.moveHorizontal(265);  
     roof5.changeSize(50, 70);  
     roof5.makeVisible();  
     sun = new Circle();  
     sun.changeColor("yellow");  
     sun.moveVertical(-40);  
     sun.moveHorizontal(440);  
     sun.changeSize(50);  
     sun.makeVisible();  
     trunk1 = new Square();  
     trunk1.changeColor ("blue");  
     trunk1.moveVertical(330);  
     trunk1.moveHorizontal(380);  
     trunk1.changeSize(20);  
     trunk1.makeVisible();  
     trunk2 = new Square();  
     trunk2.changeColor ("blue");  
     trunk2.moveVertical(310);  
     trunk2.moveHorizontal(380);  
     trunk2.changeSize(20);  
     trunk2.makeVisible();  
     tree1 = new Triangle();  
     tree1.changeColor ("magenta");  
     tree1.moveVertical(310);  
     tree1.moveHorizontal(400);  
     tree1.changeSize(40, 80);  
     tree1.makeVisible();  
     tree2 = new Triangle();  
     tree2.changeColor ("magenta");  
     tree2.moveVertical(295);  
     tree2.moveHorizontal(400);  
     tree2.changeSize(30, 60);  
     tree2.makeVisible();  
     tree3 = new Triangle();  
     tree3.changeColor ("magenta");  
     tree3.moveVertical(285);  
     tree3.moveHorizontal(400);  
     tree3.changeSize(20, 40);  
     tree3.makeVisible();  
   }  
   /**  
    * Change this picture to black/white display  
    */  
   public void setBlackAndWhite()  
   {  
     if(wall != null)  // only if it's painted already...  
     {  
       wall.changeColor("black");  
       window.changeColor("white");  
       roof.changeColor("black");  
       sun.changeColor("black");  
     }  
   }  
   /**  
    * Change this picture to use color display  
    */  
   public void setColor()  
   {  
     if(wall != null)  // only if it's painted already...  
     {  
       wall.changeColor("red");  
       window.changeColor("black");  
       roof.changeColor("green");  
       sun.changeColor("yellow");  
     }  
   }  
 }  
 import java.awt.*;  
 import java.awt.geom.*;  
 public class Circle  
 {  
   private int diameter;  
   private int xPosition;  
   private int yPosition;  
   private String color;  
   private boolean isVisible;  
   /**  
    * Create a new circle at default position with default color.  
    */  
   public Circle()  
   {  
     diameter = 30;  
     xPosition = 20;  
     yPosition = 60;  
     color = "blue";  
     isVisible = false;  
   }  
   /**  
    * Make this circle visible. If it was already visible, do nothing.  
    */  
   public void makeVisible()  
   {  
     isVisible = true;  
     draw();  
   }  
   /**  
    * Make this circle invisible. If it was already invisible, do nothing.  
    */  
   public void makeInvisible()  
   {  
     erase();  
     isVisible = false;  
   }  
   /**  
    * Move the circle a few pixels to the right.  
    */  
   public void moveRight()  
   {  
     moveHorizontal(20);  
   }  
   /**  
    * Move the circle a few pixels to the left.  
    */  
   public void moveLeft()  
   {  
     moveHorizontal(-20);  
   }  
   /**  
    * Move the circle a few pixels up.  
    */  
   public void moveUp()  
   {  
     moveVertical(-20);  
   }  
   /**  
    * Move the circle a few pixels down.  
    */  
   public void moveDown()  
   {  
     moveVertical(20);  
   }  
   /**  
    * Move the circle horizontally by 'distance' pixels.  
    */  
   public void moveHorizontal(int distance)  
   {  
     erase();  
     xPosition += distance;  
     draw();  
   }  
   /**  
    * Move the circle vertically by 'distance' pixels.  
    */  
   public void moveVertical(int distance)  
   {  
     erase();  
     yPosition += distance;  
     draw();  
   }  
   /**  
    * Slowly move the circle horizontally by 'distance' pixels.  
    */  
   public void slowMoveHorizontal(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       xPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Slowly move the circle vertically by 'distance' pixels.  
    */  
   public void slowMoveVertical(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       yPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Change the size to the new size (in pixels). Size must be >= 0.  
    */  
   public void changeSize(int newDiameter)  
   {  
     erase();  
     diameter = newDiameter;  
     draw();  
   }  
   /**  
    * Change the color. Valid colors are "red", "yellow", "blue", "green",  
    * "magenta" and "black".  
    */  
   public void changeColor(String newColor)  
   {  
     color = newColor;  
     draw();  
   }  
   /*  
    * Draw the circle with current specifications on screen.  
    */  
   private void draw()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition,   
           diameter, diameter));  
       canvas.wait(10);  
     }  
   }  
   /*  
    * Erase the circle on screen.  
    */  
   private void erase()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.erase(this);  
     }  
   }  
 }  
 import java.awt.*;  
 public class Square  
 {  
   private int size;  
   private int xPosition;  
   private int yPosition;  
   private String color;  
   private boolean isVisible;  
   /**  
    * Create a new square at default position with default color.  
    */  
   public Square()  
   {  
     size = 15;  
     xPosition = 60;  
     yPosition = 50;  
     color = "black";  
     isVisible = false;  
   }  
   /**  
    * Make this square visible. If it was already visible, do nothing.  
    */  
   public void makeVisible()  
   {  
     isVisible = true;  
     draw();  
   }  
   /**  
    * Make this square invisible. If it was already invisible, do nothing.  
    */  
   public void makeInvisible()  
   {  
     erase();  
     isVisible = false;  
   }  
   /**  
    * Move the square a few pixels to the right.  
    */  
   public void moveRight()  
   {  
     moveHorizontal(20);  
   }  
   /**  
    * Move the square a few pixels to the left.  
    */  
   public void moveLeft()  
   {  
     moveHorizontal(-20);  
   }  
   /**  
    * Move the square a few pixels up.  
    */  
   public void moveUp()  
   {  
     moveVertical(-20);  
   }  
   /**  
    * Move the square a few pixels down.  
    */  
   public void moveDown()  
   {  
     moveVertical(20);  
   }  
   /**  
    * Move the square horizontally by 'distance' pixels.  
    */  
   public void moveHorizontal(int distance)  
   {  
     erase();  
     xPosition += distance;  
     draw();  
   }  
   /**  
    * Move the square vertically by 'distance' pixels.  
    */  
   public void moveVertical(int distance)  
   {  
     erase();  
     yPosition += distance;  
     draw();  
   }  
   /**  
    * Slowly move the square horizontally by 'distance' pixels.  
    */  
   public void slowMoveHorizontal(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       xPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Slowly move the square vertically by 'distance' pixels.  
    */  
   public void slowMoveVertical(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       yPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Change the size to the new size (in pixels). Size must be >= 0.  
    */  
   public void changeSize(int newSize)  
   {  
     erase();  
     size = newSize;  
     draw();  
   }  
   /**  
    * Change the color. Valid colors are "red", "yellow", "blue", "green",  
    * "magenta" and "black".  
    */  
   public void changeColor(String newColor)  
   {  
     color = newColor;  
     draw();  
   }  
   /*  
    * Draw the square with current specifications on screen.  
    */  
   private void draw()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.draw(this, color,  
           new Rectangle(xPosition, yPosition, size, size));  
       canvas.wait(10);  
     }  
   }  
   /*  
    * Erase the square on screen.  
    */  
   private void erase()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.erase(this);  
     }  
   }  
 }  
 import java.awt.*;  
 public class Triangle  
 {  
   private int height;  
   private int width;  
   private int xPosition;  
   private int yPosition;  
   private String color;  
   private boolean isVisible;  
   /**  
    * Create a new triangle at default position with default color.  
    */  
   public Triangle()  
   {  
     height = 30;  
     width = 40;  
     xPosition = 50;  
     yPosition = 15;  
     color = "red";  
     isVisible = false;  
   }  
   /**  
    * Make this triangle visible. If it was already visible, do nothing.  
    */  
   public void makeVisible()  
   {  
     isVisible = true;  
     draw();  
   }  
   /**  
    * Make this triangle invisible. If it was already invisible, do nothing.  
    */  
   public void makeInvisible()  
   {  
     erase();  
     isVisible = false;  
   }  
   /**  
    * Move the triangle a few pixels to the right.  
    */  
   public void moveRight()  
   {  
     moveHorizontal(20);  
   }  
   /**  
    * Move the triangle a few pixels to the left.  
    */  
   public void moveLeft()  
   {  
     moveHorizontal(-20);  
   }  
   /**  
    * Move the triangle a few pixels up.  
    */  
   public void moveUp()  
   {  
     moveVertical(-20);  
   }  
   /**  
    * Move the triangle a few pixels down.  
    */  
   public void moveDown()  
   {  
     moveVertical(20);  
   }  
   /**  
    * Move the triangle horizontally by 'distance' pixels.  
    */  
   public void moveHorizontal(int distance)  
   {  
     erase();  
     xPosition += distance;  
     draw();  
   }  
   /**  
    * Move the triangle vertically by 'distance' pixels.  
    */  
   public void moveVertical(int distance)  
   {  
     erase();  
     yPosition += distance;  
     draw();  
   }  
   /**  
    * Slowly move the triangle horizontally by 'distance' pixels.  
    */  
   public void slowMoveHorizontal(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       xPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Slowly move the triangle vertically by 'distance' pixels.  
    */  
   public void slowMoveVertical(int distance)  
   {  
     int delta;  
     if(distance < 0)   
     {  
       delta = -1;  
       distance = -distance;  
     }  
     else   
     {  
       delta = 1;  
     }  
     for(int i = 0; i < distance; i++)  
     {  
       yPosition += delta;  
       draw();  
     }  
   }  
   /**  
    * Change the size to the new size (in pixels). Size must be >= 0.  
    */  
   public void changeSize(int newHeight, int newWidth)  
   {  
     erase();  
     height = newHeight;  
     width = newWidth;  
     draw();  
   }  
   /**  
    * Change the color. Valid colors are "red", "yellow", "blue", "green",  
    * "magenta" and "black".  
    */  
   public void changeColor(String newColor)  
   {  
     color = newColor;  
     draw();  
   }  
   /*  
    * Draw the triangle with current specifications on screen.  
    */  
   private void draw()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       int[] xpoints = { xPosition, xPosition + (width/2), xPosition - (width/2) };  
       int[] ypoints = { yPosition, yPosition + height, yPosition + height };  
       canvas.draw(this, color, new Polygon(xpoints, ypoints, 3));  
       canvas.wait(10);  
     }  
   }  
   /*  
    * Erase the triangle on screen.  
    */  
   private void erase()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.erase(this);  
     }  
   }  
 }  
 import javax.swing.*;  
 import java.awt.*;  
 import java.util.List;  
 import java.util.*;  
 public class Canvas  
 {  
   // Note: The implementation of this class (specifically the handling of  
   // shape identity and colors) is slightly more complex than necessary. This  
   // is done on purpose to keep the interface and instance fields of the  
   // shape objects in this project clean and simple for educational purposes.  
   private static Canvas canvasSingleton;  
   /**  
    * Factory method to get the canvas singleton object.  
    */  
   public static Canvas getCanvas()  
   {  
     if(canvasSingleton == null) {  
       canvasSingleton = new Canvas("BlueJ Shapes Demo", 550, 400,   
           Color.white);  
     }  
     canvasSingleton.setVisible(true);  
     return canvasSingleton;  
   }  
   // ----- instance part -----  
   private JFrame frame;  
   private CanvasPane canvas;  
   private Graphics2D graphic;  
   private Color backgroundColour;  
   private Image canvasImage;  
   private List<Object> objects;  
   private HashMap<Object, ShapeDescription> shapes;  
   /**  
    * Create a Canvas.  
    * @param title title to appear in Canvas Frame  
    * @param width the desired width for the canvas  
    * @param height the desired height for the canvas  
    * @param bgClour the desired background colour of the canvas  
    */  
   private Canvas(String title, int width, int height, Color bgColour)  
   {  
     frame = new JFrame();  
     canvas = new CanvasPane();  
     frame.setContentPane(canvas);  
     frame.setTitle(title);  
     canvas.setPreferredSize(new Dimension(width, height));  
     backgroundColour = bgColour;  
     frame.pack();  
     objects = new ArrayList<Object>();  
     shapes = new HashMap<Object, ShapeDescription>();  
   }  
   /**  
    * Set the canvas visibility and brings canvas to the front of screen  
    * when made visible. This method can also be used to bring an already  
    * visible canvas to the front of other windows.  
    * @param visible boolean value representing the desired visibility of  
    * the canvas (true or false)   
    */  
   public void setVisible(boolean visible)  
   {  
     if(graphic == null) {  
       // first time: instantiate the offscreen image and fill it with  
       // the background colour  
       Dimension size = canvas.getSize();  
       canvasImage = canvas.createImage(size.width, size.height);  
       graphic = (Graphics2D)canvasImage.getGraphics();  
       graphic.setColor(backgroundColour);  
       graphic.fillRect(0, 0, size.width, size.height);  
       graphic.setColor(Color.black);  
     }  
     frame.setVisible(visible);  
   }  
   /**  
    * Draw a given shape onto the canvas.  
    * @param referenceObject an object to define identity for this shape  
    * @param color      the color of the shape  
    * @param shape      the shape object to be drawn on the canvas  
    */  
    // Note: this is a slightly backwards way of maintaining the shape  
    // objects. It is carefully designed to keep the visible shape interfaces  
    // in this project clean and simple for educational purposes.  
   public void draw(Object referenceObject, String color, Shape shape)  
   {  
     objects.remove(referenceObject);  // just in case it was already there  
     objects.add(referenceObject);   // add at the end  
     shapes.put(referenceObject, new ShapeDescription(shape, color));  
     redraw();  
   }  
   /**  
    * Erase a given shape's from the screen.  
    * @param referenceObject the shape object to be erased   
    */  
   public void erase(Object referenceObject)  
   {  
     objects.remove(referenceObject);  // just in case it was already there  
     shapes.remove(referenceObject);  
     redraw();  
   }  
   /**  
    * Set the foreground colour of the Canvas.  
    * @param newColour  the new colour for the foreground of the Canvas   
    */  
   public void setForegroundColor(String colorString)  
   {  
     if(colorString.equals("red"))  
       graphic.setColor(Color.red);  
     else if(colorString.equals("black"))  
       graphic.setColor(Color.black);  
     else if(colorString.equals("blue"))  
       graphic.setColor(Color.blue);  
     else if(colorString.equals("yellow"))  
       graphic.setColor(Color.yellow);  
     else if(colorString.equals("green"))  
       graphic.setColor(Color.green);  
     else if(colorString.equals("magenta"))  
       graphic.setColor(Color.magenta);  
     else if(colorString.equals("white"))  
       graphic.setColor(Color.white);  
     else  
       graphic.setColor(Color.black);  
   }  
   /**  
    * Wait for a specified number of milliseconds before finishing.  
    * This provides an easy way to specify a small delay which can be  
    * used when producing animations.  
    * @param milliseconds the number   
    */  
   public void wait(int milliseconds)  
   {  
     try  
     {  
       Thread.sleep(milliseconds);  
     }   
     catch (Exception e)  
     {  
       // ignoring exception at the moment  
     }  
   }  
   /**  
    * Redraw ell shapes currently on the Canvas.  
    */  
   private void redraw()  
   {  
     erase();  
     for(Iterator i=objects.iterator(); i.hasNext(); ) {  
       ((ShapeDescription)shapes.get(i.next())).draw(graphic);  
     }  
     canvas.repaint();  
   }  
   /**  
    * Erase the whole canvas. (Does not repaint.)  
    */  
   private void erase()  
   {  
     Color original = graphic.getColor();  
     graphic.setColor(backgroundColour);  
     Dimension size = canvas.getSize();  
     graphic.fill(new Rectangle(0, 0, size.width, size.height));  
     graphic.setColor(original);  
   }  
   /************************************************************************  
    * Inner class CanvasPane - the actual canvas component contained in the  
    * Canvas frame. This is essentially a JPanel with added capability to  
    * refresh the image drawn on it.  
    */  
   private class CanvasPane extends JPanel  
   {  
     public void paint(Graphics g)  
     {  
       g.drawImage(canvasImage, 0, 0, null);  
     }  
   }  
   /************************************************************************  
    * Inner class CanvasPane - the actual canvas component contained in the  
    * Canvas frame. This is essentially a JPanel with added capability to  
    * refresh the image drawn on it.  
    */  
   private class ShapeDescription  
   {  
     private Shape shape;  
     private String colorString;  
     public ShapeDescription(Shape shape, String color)  
     {  
       this.shape = shape;  
       colorString = color;  
     }  
     public void draw(Graphics2D graphic)  
     {  
       setForegroundColor(colorString);  
       graphic.fill(shape);  
     }  
   }  
 }  

 
 
 
Komentar
Posting Komentar