Senin, 10 September 2018

Tugas Rumah PBO A (10 September 2018)

Haiii!!!
Pada post kali ini, saya akan mempublish tugas rumah PBO A tentang implementasi Picture Project (menggambar rumah menggunakan aplikasi BlueJ).

Untuk pengerjaan project kali ini, dibutuhkan 5 class, yaitu:
1. Main (atau Picture)
    berisi perintah untuk menampilkan gambar secara keseluruhan, dimana bangun-bangun 2D tersebut diposisikan sesuai dengan keinginan kita
2. Canvas
3. Square
4. Circle
5. Triangle

1. Main

 /**  
   * Write a description of class main here.  
  *  
  * @author Yemima Sutanto  
  * @version 1.7 15 Sept 2018  
  */  

 public class main{  
   private Square hall1,hall2;   
   private Square wd1,wd2,wd3,wd4,pintuhall,tiang1,tiang2,kotak1,kotak2;  
   private Triangle ataphall1,ataphall2,ataptambahan;  
   private Circle matahari;  
   
   /**   
   * Draw this picture.   
   */ 
  
   public void draw(){   
    hall1 = new Square();   
    hall1.moveVertical(160);   
    hall1.moveHorizontal(0);   
    hall1.changeSize(90);   
    hall1.makeVisible(); 
  
    hall2 = new Square();  
    hall2.moveVertical(160);   
    hall2.moveHorizontal(90);  
    hall2.changeSize(90);  
    hall2.makeVisible();
  
    ataphall1 = new Triangle();  
    ataphall1.changeColor("red");  
    ataphall1.changeSize(50, 90);   
    ataphall1.moveHorizontal(55);   
    ataphall1.moveVertical(145);   
    ataphall1.makeVisible();
   
    ataphall2 = new Triangle();   
    ataphall2.changeColor("red");  
    ataphall2.changeSize(50, 90);   
    ataphall2.moveHorizontal(145);   
    ataphall2.moveVertical(145);   
    ataphall2.makeVisible();
  
    ataptambahan = new Triangle();  
    ataptambahan.changeColor("red");  
    ataptambahan.changeSize(-50, 90);   
    ataptambahan.moveHorizontal(100);   
    ataptambahan.moveVertical(195);   
    ataptambahan.makeVisible();
  
    wd1 = new Square();  
    wd1.changeColor("black");  
    wd1.changeSize(20);  
    wd1.moveHorizontal(20);   
    wd1.moveVertical(180);   
    wd1.makeVisible();
   
    wd2 = new Square();  
    wd2.changeColor("black");  
    wd2.changeSize(20);  
    wd2.moveHorizontal(40);   
    wd2.moveVertical(180);   
    wd2.makeVisible();
  
    wd3 = new Square();  
    wd3.changeColor("black");  
    wd3.changeSize(20);  
    wd3.moveHorizontal(125);   
    wd3.moveVertical(180);   
    wd3.makeVisible(); 
 
    wd4 = new Square();  
    wd4.changeColor("black");  
    wd4.changeSize(20);  
    wd4.moveHorizontal(145);   
    wd4.moveVertical(180);    
    wd4.makeVisible(); 
 
    pintuhall = new Square();   
    pintuhall.changeColor("lightblue");  
    pintuhall.moveHorizontal(70);   
    pintuhall.moveVertical(210);   
    pintuhall.changeSize(45);   
    pintuhall.makeVisible();
   
    matahari = new Circle();   
    matahari.moveHorizontal(80);   
    matahari.moveVertical(-100);   
    matahari.changeSize(100);   
    matahari.makeVisible(); 
  
    tiang1 = new Square();  
    tiang1.changeColor("black");  
    tiang1.moveHorizontal(-190);   
    tiang1.moveVertical(140);   
    tiang1.changeSize(140);   
    tiang1.makeVisible(); 
  
    tiang2 = new Square();  
    tiang2.changeColor("black");  
    tiang2.moveHorizontal(230);   
    tiang2.moveVertical(140);   
    tiang2.changeSize(140);   
    tiang2.makeVisible();
   
    kotak1 = new Square();  
    kotak1.changeColor("green");  
    kotak1.moveHorizontal(-50);   
    kotak1.moveVertical(210);   
    kotak1.changeSize(50);   
    kotak1.makeVisible(); 
 
    kotak2 = new Square();  
    kotak2.changeColor("green");  
    kotak2.moveHorizontal(180);   
    kotak2.moveVertical(210);   
    kotak2.changeSize(50);   
    kotak2.makeVisible();  
   }   
   
   /**   
   * Change this picture to black/white display   
   */   
   
    public void setBlackAndWhite(){   
    if(hall1 != null){ // only if it's painted already...   
     hall1.changeColor("black");   
     wd1.changeColor("white");   
     ataphall1.changeColor("black");   
     matahari.changeColor("black");   
    }   
   } 
  
   /**   
   * Change this picture to use color display   
   */  
 
   public void setColor(){   
    if(hall1 != null){ // only if it's painted already...   
     hall1.changeColor("blue");   
     wd1.changeColor("black");   
     ataphall1.changeColor("red");   
     matahari.changeColor("yellow");   
    }   
   } 
 }  

2. Canvas

 import javax.swing.*;  
 import java.awt.*;  
 import java.util.List;  
 import java.util.*;  

 /**  
  * Canvas is a class to allow for simple graphical drawing on a canvas.  
  * This is a modification of the general purpose Canvas, specially made for  
  * the BlueJ "shapes" example.   
  *  
  * @author Yemima Sutanto  
  * @version 1.7 15 Sept 2018  
  */  

 public class Canvas{   
   private static Canvas canvasSingleton;  
   public static final Color brown = new Color(102,51,0);  
   public static final Color lightblue = new Color(51,204,255);
   /**  
    * Factory method to get the canvas singleton object.  
    */  

   public static Canvas getCanvas(){  
     if(canvasSingleton == null) {  
       canvasSingleton = new Canvas("Ini Canvas", 300, 300,   
           Color.gray);  
     }  
     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("brown"))   
     graphic.setColor(brown);   
     else if(colorString.equals("white"))   
     graphic.setColor(Color.white);   
     else if(colorString.equals("orange"))  
     graphic.setColor(Color.orange);  
     else if(colorString.equals("lightblue"))  
     graphic.setColor(lightblue);  
     else if(colorString.equals("gray"))  
     graphic.setColor(Color.gray);  
     else if(colorString.equals("darkgreen"))  
     graphic.setColor(new Color(0,102, 0));
     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. Square

 import java.awt.*;  
 
  /**  
  * A square that can be manipulated and that draws itself on a canvas.  
  *   
  * @author Yemima Sutanto  
  * @version 1.7 15 Sept 2018  
  */ 
 
 public class Square{  
   private int size;  
   private int sbX;  
   private int sbY;  
   private String color;  
   private boolean isVisible;  

   /**  
    * Create a new square at default position with default color.  
    */  

   public Square(){  
     size = 30;  
     sbX = 60;  
     sbY = 50;  
     color = "blue";  
     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();  
     sbX += distance;  
     draw();  
   }
  
   /**  
    * Move the square vertically by 'distance' pixels.  
    */
  
   public void moveVertical(int distance){  
     erase();  
     sbY += 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++){  
       sbX += 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++){  
       sbY += 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(sbX, sbY, size, size));  
       canvas.wait(10);  
     }  
   }  

   /*  
    * Erase the square on screen.  
    */  

   private void erase(){  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.erase(this);  
     }  
   }  
 }  

4. Circle

 import java.awt.*;  
 import java.awt.geom.*;  
 /**  
  * A circle that can be manipulated and that draws itself on a canvas.  
  *   
  * @author Yemima Sutanto  
  * @version 1.7 15 Sept 2018  
  */  
 public class Circle {  
   private int diameter;  
   private int sbX;  
   private int sbY;  
   private String color;  
   private boolean isVisible;
  
   /**  
    * Create a new circle at default position with default color.  
    */
  
   public Circle(){  
     diameter = 30;  
     sbX = 20;  
     sbY = 60;  
     color = "yellow";  
     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();  
     sbX += distance;  
     draw();  
   }  

   /**  
    * Move the circle vertically by 'distance' pixels.  
    */  

   public void moveVertical(int distance){  
     erase();  
     sbY += 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++){  
       sbX += 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++){  
       sbY += 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(sbX, sbY,   
           diameter, diameter));  
       canvas.wait(10);  
     }  
   }
  
   /*  
    * Erase the circle on screen.  
    */  

   private void erase(){  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.erase(this);  
     }  
   }  
 }  

5. Triangle

 import java.awt.*;  
 /**  
  * A triangle that can be manipulated and that draws itself on a canvas.  
  *   
  * @author Yemima Sutanto  
  * @version 1.7 15 Sept 2018  
  */  
 public class Triangle{  
   private int height;  
   private int width;  
   private int sbX;  
   private int sbY;  
   private String color;  
   private boolean isVisible;  
   /**  
    * Create a new triangle at default position with default color.  
    */  
   public Triangle(){  
     height = 30;  
     width = 40;  
     sbX = 50;  
     sbY = 15;  
     color = "green";  
     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();  
     sbX += distance;  
     draw();  
   }  
   /**  
    * Move the triangle vertically by 'distance' pixels.  
    */  
   public void moveVertical(int distance){  
     erase();  
     sbY += 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++){  
       sbX += 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++){  
       sbY += 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 = { sbX, sbX + (width/2), sbX - (width/2) };  
       int[] ypoints = { sbY, sbY + height, sbY + 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);  
     }  
   }  
 }  

Untuk hasil compile dari code-code diatas, berikut saya tampilkan.. :)

Sekian dan terima kasih :D

Tidak ada komentar:

Posting Komentar