Class and Object 2

Class dan Object 2 (Menggambar Rumah)
TUGAS PBO-A 2 
Komponen kelas yang dibutuhkan :
  1. Picture (Untuk menampilkan keseluruhan gambar)
  2. Canvas (Sebagai media untuk menggambar)
  3. Circle (Bentuk gambar lingkaran)
  4. Square (Bentuk gambar persegi)
  5. Triangle (Bentuk gambar segitiga)

Source code untuk masing-masing kelas

1. Picture

 public class Picture  
 {  
   private Square wall;  
   private Square wall1;  
   private Square wall2;  
   private Square wall3;  
   private Square wall4;  
   private Square window;  
   private Square window1;  
   private Square window2;  
   private Square window3;  
   private Square window4;  
   private Square door;  
   private Square door1;  
   private Square door2;  
   private Square door3;  
   private Square door4;  
   private Triangle roof;  
   private Triangle roof1;  
   private Triangle roof2;  
   private Triangle roof3;  
   private Triangle roof4;  
   private Circle sun;  
   private Circle cloud;  
   private Circle cloud1;  
   private Circle cloud2;  
   private Circle cloud3;  
   private Circle cloud4;  
   private Circle cloud5;  
   private Circle cloud6;  
   private Circle cloud7;  
   private Circle tree;  
   private Square tree1;  
   private Square tree2;  
   private Square tree3;  
   private Square tree4;  
   private Square tree5;  
   private Circle tree6;  
   private Square tree7;  
   private Square tree8;  
   private Square tree9;  
   private Square tree10;  
   private Square tree11;  
   /**  
    * 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(40);  
     wall.moveHorizontal(-60);  
     wall.changeSize(200);  
     wall.makeVisible();  
     window = new Square();  
     window.changeColor("black");  
     window.moveHorizontal(29);  
     window.moveVertical(100);  
     window.makeVisible();  
     wall1 = new Square();  
     wall1.changeColor("green");  
     wall1.moveVertical(110);  
     wall1.moveHorizontal(140);  
     wall1.changeSize(100);  
     wall1.makeVisible();  
     roof = new Triangle();  
     roof.changeSize(100, 207);  
     roof.moveHorizontal(249);  
     roof.moveVertical(125);  
     roof.makeVisible();  
     roof1 = new Triangle();  
     roof1.changeSize(53, 102);  
     roof1.moveHorizontal(400);  
     roof1.moveVertical(242);  
     roof1.makeVisible();  
     window1 = new Square();  
     window1.changeColor("black");  
     window1.moveHorizontal(170);  
     window1.moveVertical(140);  
     window1.changeSize(60);  
     window1.makeVisible();  
     tree11 = new Square();  
     tree11.changeColor("black");  
     tree11.moveHorizontal(327);  
     tree11.moveVertical(99);  
     tree11.changeSize(20);  
     tree11.makeVisible();  
     wall2 = new Square();  
     wall2.changeColor("red");  
     wall2.moveVertical(110);  
     wall2.moveHorizontal(240);  
     wall2.changeSize(100);  
     wall2.makeVisible();  
     roof2 = new Triangle();  
     roof2.changeSize(53, 102);  
     roof2.moveHorizontal(500);  
     roof2.moveVertical(242);  
     roof2.makeVisible();  
     window2 = new Square();  
     window2.changeColor("black");  
     window2.moveHorizontal(270);  
     window2.moveVertical(140);  
     window2.changeSize(60);  
     window2.makeVisible();  
     wall3 = new Square();  
     wall3.changeColor("green");  
     wall3.moveVertical(110);  
     wall3.moveHorizontal(-160);  
     wall3.changeSize(100);  
     wall3.makeVisible();  
     roof3 = new Triangle();  
     roof3.changeSize(53, 102);  
     roof3.moveHorizontal(100);  
     roof3.moveVertical(242);  
     roof3.makeVisible();  
     window3 = new Square();  
     window3.changeColor("black");  
     window3.moveHorizontal(-130);  
     window3.moveVertical(140);  
     window3.changeSize(60);  
     window3.makeVisible();  
     tree4 = new Square();  
     tree4.changeColor("black");  
     tree4.moveHorizontal(-267);  
     tree4.moveVertical(99);  
     tree4.changeSize(20);  
     tree4.makeVisible();  
     wall4 = new Square();  
     wall4.changeColor("red");  
     wall4.moveVertical(110);  
     wall4.moveHorizontal(-260);  
     wall4.changeSize(100);  
     wall4.makeVisible();  
     roof4 = new Triangle();  
     roof4.changeSize(53, 102);  
     roof4.moveHorizontal(-1);  
     roof4.moveVertical(242);  
     roof4.makeVisible();  
     window4 = new Square();  
     window4.changeColor("black");  
     window4.moveHorizontal(-230);  
     window4.moveVertical(140);  
     window4.changeSize(60);  
     window4.makeVisible();  
     door = new Square();  
     door.changeColor("black");  
     door.moveHorizontal(-28);  
     door.moveVertical(100);  
     door.changeSize(45);  
     door.makeVisible();  
     door1 = new Square();  
     door1.changeColor("black");  
     door1.moveHorizontal(-28);  
     door1.moveVertical(150);  
     door1.changeSize(45);  
     door1.makeVisible();  
     door2= new Square();  
     door2.changeColor("black");  
     door2.moveHorizontal(29);  
     door2.moveVertical(70);  
     door2.changeSize(20);  
     door2.makeVisible();  
     door3= new Square();  
     door3.changeColor("black");  
     door3.moveHorizontal(70);  
     door3.moveVertical(70);  
     door3.changeSize(20);  
     door3.makeVisible();  
     door4= new Square();  
     door4.changeColor("black");  
     door4.moveHorizontal(111);  
     door4.moveVertical(70);  
     door4.changeSize(20);  
     door4.makeVisible();  
     sun = new Circle();  
     sun.changeColor("yellow");  
     sun.moveHorizontal(400);  
     sun.moveVertical(-5);  
     sun.changeSize(80);  
     sun.makeVisible();  
     cloud = new Circle();  
     cloud.changeColor("blue");  
     cloud.moveHorizontal(483);  
     cloud.moveVertical(-95);  
     cloud.changeSize(100);  
     cloud.makeVisible();  
     cloud1 = new Circle();  
     cloud1.changeColor("blue");  
     cloud1.moveHorizontal(430);  
     cloud1.moveVertical(-90);  
     cloud1.changeSize(65);  
     cloud1.makeVisible();  
     cloud2 = new Circle();  
     cloud2.changeColor("blue");  
     cloud2.moveHorizontal(368);  
     cloud2.moveVertical(-90);  
     cloud2.changeSize(65);  
     cloud2.makeVisible();  
     cloud3 = new Circle();  
     cloud3.changeColor("blue");  
     cloud3.moveHorizontal(273);  
     cloud3.moveVertical(-95);  
     cloud3.changeSize(100);  
     cloud3.makeVisible();  
     cloud4 = new Circle();  
     cloud4.changeColor("blue");  
     cloud4.moveHorizontal(190);  
     cloud4.moveVertical(-95);  
     cloud4.changeSize(100);  
     cloud4.makeVisible();  
     cloud5 = new Circle();  
     cloud5.changeColor("blue");  
     cloud5.moveHorizontal(130);  
     cloud5.moveVertical(-90);  
     cloud5.changeSize(65);  
     cloud5.makeVisible();  
     cloud6 = new Circle();  
     cloud6.changeColor("blue");  
     cloud6.moveHorizontal(35);  
     cloud6.moveVertical(-95);  
     cloud6.changeSize(100);  
     cloud6.makeVisible();  
     cloud7 = new Circle();  
     cloud7.changeColor("blue");  
     cloud7.moveHorizontal(-25);  
     cloud7.moveVertical(-95);  
     cloud7.changeSize(65);  
     cloud7.makeVisible();  
     tree = new Circle();  
     tree.changeColor("green");  
     tree.moveHorizontal(-60);  
     tree.moveVertical(80);  
     tree.changeSize(80);  
     tree.makeVisible();  
     tree1 = new Square();  
     tree1.changeColor("black");  
     tree1.moveHorizontal(-267);  
     tree1.moveVertical(19);  
     tree1.changeSize(20);  
     tree1.makeVisible();  
     tree2 = new Square();  
     tree2.changeColor("black");  
     tree2.moveHorizontal(-267);  
     tree2.moveVertical(39);  
     tree2.changeSize(20);  
     tree2.makeVisible();  
     tree3 = new Square();  
     tree3.changeColor("black");  
     tree3.moveHorizontal(-267);  
     tree3.moveVertical(59);  
     tree3.changeSize(20);  
     tree3.makeVisible();  
     tree4 = new Square();  
     tree4.changeColor("black");  
     tree4.moveHorizontal(-267);  
     tree4.moveVertical(79);  
     tree4.changeSize(20);  
     tree4.makeVisible();  
     tree6 = new Circle();  
     tree6.changeColor("green");  
     tree6.moveHorizontal(540);  
     tree6.moveVertical(80);  
     tree6.changeSize(80);  
     tree6.makeVisible();  
     tree7 = new Square();  
     tree7.changeColor("black");  
     tree7.moveHorizontal(327);  
     tree7.moveVertical(19);  
     tree7.changeSize(20);  
     tree7.makeVisible();  
     tree8 = new Square();  
     tree8.changeColor("black");  
     tree8.moveHorizontal(327);  
     tree8.moveVertical(39);  
     tree8.changeSize(20);  
     tree8.makeVisible();  
     tree9 = new Square();  
     tree9.changeColor("black");  
     tree9.moveHorizontal(327);  
     tree9.moveVertical(59);  
     tree9.changeSize(20);  
     tree9.makeVisible();  
     tree10 = new Square();  
     tree10.changeColor("black");  
     tree10.moveHorizontal(327);  
     tree10.moveVertical(79);  
     tree10.changeSize(20);  
     tree10.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("blue");  
       sun.changeColor("yellow");  
     }  
   }  
 }  

2. Canvas

 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", 600, 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);  
     }  
   }  
 }  

3. Circle

 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);  
     }  
   }  
 }  

4. Square

 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 = 100;  
     xPosition = 260;  
     yPosition = 200;  
     color = "red";  
     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);  
     }  
   }  
 }  

5. Triangle

 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 = "blue";  
     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);  
     }  
   }  
 }  

Beginilah hasil akhir gambar saya



Sampai bertemu di materi selanjutnya.

Komentar

Postingan populer dari blog ini

Tugas Membuat Jam Digital

Codeigniter "Toko Buah"

Personal Web