Minggu, 16 September 2018

PBO A Menggambar Rumah

Nama: Ayu Mutiara Sari
Kelas: PBO A
NRP: 05111740000149

1. Kode Canvas

 import javax.swing.*;  
 import java.awt.*;  
 import java.util.List;  
 import java.util.*;  
 /**  
  * Write a description of class Picture here.  
  * Menggambar rumah  
  * @author (Ayu Mutiara Sari)  
  * @version (09.18)  
  */  
 public class Canvas  
 {  
   private static Canvas canvasSingleton;  
   public static Canvas getCanvas()  
   {  
     if(canvasSingleton == null) {  
       canvasSingleton = new Canvas("BlueJ Shapes Demo", 700, 500,   
                      Color.white);  
     }  
     canvasSingleton.setVisible(true);  
     return canvasSingleton;  
   }  
   private JFrame frame;  
   private CanvasPane canvas;  
   private Graphics2D graphic;  
   private Color backgroundColour;  
   private Image canvasImage;  
   private List objects;  
   private HashMap shapes;  
   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();  
     shapes = new HashMap();  
   }  
 public void setVisible(boolean visible)  
   {  
     if(graphic == null) {  
       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);  
   }  
   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();  
   }  
   public void erase(Object referenceObject)  
   {  
     objects.remove(referenceObject);  // just in case it was already there  
     shapes.remove(referenceObject);  
     redraw();  
   }  
   public void setForegroundColor(String colorString)  
   {  
       if(colorString.equals("red"))  
       graphic.setColor(Color.red);  
     else if(colorString.equals("yellow"))  
       graphic.setColor(Color.yellow);  
     else if(colorString.equals("green"))  
       graphic.setColor(Color.green);  
     else if(colorString.equals("black"))  
       graphic.setColor(Color.black);  
     else if(colorString.equals("blue"))  
       graphic.setColor(Color.blue);  
     else if(colorString.equals("white"))  
       graphic.setColor(Color.white);  
     else  
       graphic.setColor(Color.black);  
   }  
   public void wait(int milliseconds)  
   {  
     try  
     {  
       Thread.sleep(milliseconds);  
     }   
     catch (Exception e)  
     {  
     }  
   }  
   private void redraw()  
   {  
     erase();  
     for(Iterator i=objects.iterator(); i.hasNext(); ) {  
       ((ShapeDescription)shapes.get(i.next())).draw(graphic);  
     }  
     canvas.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);  
   }  
   private class CanvasPane extends JPanel  
   {  
     public void paint(Graphics g)  
     {  
       g.drawImage(canvasImage, 0, 0, null);  
     }  
   }  
   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);  
     }  
   }  
 }  
2. Kode Circle

 /**  
  * Write a description of class Circle here.  
  * @author (Ayu Mutiara Sari)  
  * @version (09.18)  
  */  
  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;  
   public Circle()  
   {  
     diameter = 50;  
     xPosition = 20;  
     yPosition = 60;  
     color = "yellow";  
     isVisible = false;  
   }  
   public Circle(int x, int y){  
     diameter = 50;  
     xPosition = x;  
     yPosition = y;  
     color = "yellow";  
   }  
    public Circle(int x, int y, String col){  
     this(x,y,col,20);  
   }  
    public Circle(int x, int y, String col, int d){  
      diameter = d;  
     xPosition = x;  
     yPosition = y;  
     color = "blue";  
     makeVisible();  
   }  
   public void makeVisible()  
   {  
     isVisible = true;  
     draw();  
   }  
   public void makeInvisible()  
   {  
     erase();  
     isVisible = false;  
   }  
   public void moveRight()  
   {  
     moveHorizontal(20);  
   }  
   public void moveLeft()  
   {  
     moveHorizontal(-20);  
   }  
   public void moveUp()  
   {  
     moveVertical(-20);  
   }  
   public void moveDown()  
   {  
     moveVertical(20);  
   }  
   public void moveHorizontal(int distance)  
   {  
     erase();  
     xPosition += distance;  
     draw();  
   }  
   public void moveVertical(int distance)  
   {  
     erase();  
     yPosition += distance;  
     draw();  
   }  
   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();  
     }  
   }  
   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();  
     }  
   }  
   public void changeSize(int newDiameter)  
   {  
     erase();  
     diameter = newDiameter;  
     draw();  
   }  
   public void changeColor(String newColor)  
   {  
     color = newColor;  
     draw();  
   }  
   private void draw()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition,   
                              diameter, diameter));  
       canvas.wait(10);  
     }  
   }  
   private void erase()  
   {  
     if(isVisible) {  
       Canvas canvas = Canvas.getCanvas();  
       canvas.erase(this);  
     }  
   }  
 }  
3. Kode Square

 /**  
  * Write a description of class Square here.  
  *  
  * @author (ayu mutiara sari)  
  * @version (09.18)  
  */  
 import java.awt.*;  
 public class Square  
 {  
   private int size;  
      private int xPosition;  
      private int yPosition;  
      private String color;  
      private boolean isVisible;  
   public Square()  
   {  
           size = 30;  
           xPosition = 60;  
           yPosition = 50;  
           color = "red";  
           isVisible = false;  
   }  
      public void makeVisible()  
      {  
           isVisible = true;  
           draw();  
      }  
      public void makeInvisible()  
      {  
           erase();  
           isVisible = false;  
      }  
   public void moveRight()  
   {  
           moveHorizontal(20);  
   }  
   public void moveLeft()  
   {  
           moveHorizontal(-20);  
   }  
   public void moveUp()  
   {  
           moveVertical(-20);  
   }  
   public void moveDown()  
   {  
           moveVertical(20);  
   }  
   public void moveHorizontal(int distance)  
   {  
           erase();  
           xPosition += distance;  
           draw();  
   }  
   public void moveVertical(int distance)  
   {  
           erase();  
           yPosition += distance;  
           draw();  
   }  
   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();  
           }  
   }  
   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();  
           }  
   }  
   public void changeSize(int newSize)  
   {  
           erase();  
           size = newSize;  
           draw();  
   }  
   public void changeColor(String newColor)  
   {  
           color = newColor;  
           draw();  
   }  
      private void draw()  
      {  
           if(isVisible) {  
                Canvas canvas = Canvas.getCanvas();  
                canvas.draw(this, color,  
                               new Rectangle(xPosition, yPosition, size, size));  
                canvas.wait(10);  
           }  
      }  
      private void erase()  
      {  
           if(isVisible) {  
                Canvas canvas = Canvas.getCanvas();  
                canvas.erase(this);  
           }  
      }  
 }  
4. Kode Triangle

 /**  
  * Write a description of class Triangle here.  
  *  
  * @author (ayu mutiara sari)  
  * @version (09.18)  
  */  
 import java.awt.*;  
 public class Triangle  
 {  
   private int height;  
   private int width;  
      private int xPosition;  
      private int yPosition;  
      private String color;  
      private boolean isVisible;  
   public Triangle()  
   {  
           height = 30;  
           width = 40;  
           xPosition = 50;  
           yPosition = 15;  
           color = "green";  
           isVisible = false;  
   }  
      public void makeVisible()  
      {  
           isVisible = true;  
           draw();  
      }  
      public void makeInvisible()  
      {  
           erase();  
           isVisible = false;  
      }  
   public void moveRight()  
   {  
           moveHorizontal(20);  
   }  
   public void moveLeft()  
   {  
           moveHorizontal(-20);  
   }  
   public void moveUp()  
   {  
           moveVertical(-20);  
   }  
   public void moveDown()  
   {  
           moveVertical(20);  
   }  
   public void moveHorizontal(int distance)  
   {  
           erase();  
           xPosition += distance;  
           draw();  
   }  
   public void moveVertical(int distance)  
   {  
           erase();  
           yPosition += distance;  
           draw();  
   }  
   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();  
           }  
   }  
   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();  
           }  
   }  
   public void changeSize(int newHeight, int newWidth)  
   {  
           erase();  
           height = newHeight;  
           width = newWidth;  
           draw();  
   }  
   public void changeColor(String newColor)  
   {  
           color = newColor;  
           draw();  
   }  
      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);  
           }  
      }  
      private void erase()  
      {  
           if(isVisible) {  
                Canvas canvas = Canvas.getCanvas();  
                canvas.erase(this);  
           }  
      }  
 }  

0 komentar:

Posting Komentar