Sunday, September 16, 2018

Tugas 2 - PBO A - Membuat Gambar Rumah


Dalam membuat gambar rumah ini saya menggunakan 6 Class yaitu :
1. Picture (sebagai main class)
2. Circle (sebagai class yang mengatur gambar lingkaran)
3. Square (sebagai class yang mengatur gambar persegi)
4. Triangle (sebagai class yang mengatur gambar segitiga)
5. Rectangle (sebagai class yang mengatur gambar persegi Panjang)
6. Canvas (sebagai library class)

Berikut Source Code yang saya gunakan untuk membuat gambar tersebut dalam Java :

Picture :

 /**  
  * Write a description of class Shapes here.  
  *  
  * @author Haikal Almaz Said  
  * 16092018  
  */  
 public class Picture  
 {  
   public Picture()  
   {  
     DrawAll();  
   }  
   public void DrawAll()  
   {  
     DrawBackground();  
     DrawHouse();  
     DrawTree();  
     DrawMoon();  
     DrawCloud();  
   }  
   public void DrawHouse()  
   {  
     Square House = new Square();  
     Square Window1 = new Square();  
     Square Window2 = new Square();  
     Rectangles Door = new Rectangles();  
     Circle Knob = new Circle();  
     Triangle Roof = new Triangle();  
     House.moveTo(410,355);  
     House.changeSize(250);  
     House.changeColor("red");  
     Window1.moveTo(450,405);  
     Window1.changeSize(50);  
     Window1.changeColor("black");  
     Window2.moveTo(570,405);  
     Window2.changeSize(50);  
     Window2.changeColor("black");  
     Door.moveTo(490,485);  
     Door.changeSize(90,120);  
     Door.changeColor("white");  
     Knob.moveTo(560,540);  
     Knob.changeSize(5);  
     Knob.changeColor("black");  
     Roof.moveTo(530,230);  
     Roof.changeSize(400,150);  
     Roof.changeColor("magenta");  
   }  
   public void DrawTree()  
   {  
     Rectangles Tree = new Rectangles();  
     Circle Leaf1 = new Circle();  
     Circle Leaf2 = new Circle();  
     Circle Leaf3 = new Circle();  
     Circle Leaf4 = new Circle();  
     Circle Leaf5 = new Circle();  
     Circle Leaf6 = new Circle();  
     Tree.moveTo(100,460);  
     Tree.changeSize(90,200);  
     Tree.changeColor("brown");  
     Leaf1.moveTo(60,380);  
     Leaf1.changeSize(50);  
     Leaf1.changeColor("green");  
     Leaf2.moveTo(135,380);  
     Leaf2.changeSize(50);  
     Leaf2.changeColor("green");  
     Leaf3.moveTo(40,320);  
     Leaf3.changeSize(50);  
     Leaf3.changeColor("green");  
     Leaf4.moveTo(155,320);  
     Leaf4.changeSize(50);  
     Leaf4.changeColor("green");  
     Leaf5.moveTo(100,320);  
     Leaf5.changeSize(50);  
     Leaf5.changeColor("green");  
     Leaf6.moveTo(100,280);  
     Leaf6.changeSize(50);  
     Leaf6.changeColor("green");  
   }  
   public void DrawMoon()  
   {  
     Circle Moon = new Circle();  
     Moon.moveTo(640,0);  
     Moon.changeSize(80);  
     Moon.changeColor("yellow");  
   }  
   public void DrawCloud()  
   {  
     Circle Cloud1 = new Circle();  
     Circle Cloud2 = new Circle();  
     Circle Cloud3 = new Circle();  
     Circle Cloud4 = new Circle();  
     Circle Cloud5 = new Circle();  
     Circle Cloud6 = new Circle();  
     Circle Cloud7 = new Circle();  
     Circle Cloud8 = new Circle();  
     Cloud1.moveTo(680,100);  
     Cloud1.changeSize(40);  
     Cloud1.changeColor("white");  
     Cloud2.moveTo(620,80);  
     Cloud2.changeSize(50);  
     Cloud2.changeColor("white");  
     Cloud3.moveTo(580,100);  
     Cloud3.changeSize(40);  
     Cloud3.changeColor("white");  
     Cloud4.moveTo(400,80);  
     Cloud4.changeSize(30);  
     Cloud4.changeColor("white");  
     Cloud5.moveTo(340,60);  
     Cloud5.changeSize(40);  
     Cloud5.changeColor("white");  
     Cloud6.moveTo(295,80);  
     Cloud6.changeSize(30);  
     Cloud6.changeColor("white");  
     Cloud7.moveTo(-40,80);  
     Cloud7.changeSize(60);  
     Cloud7.changeColor("white");  
     Cloud8.moveTo(60,110);  
     Cloud8.changeSize(40);  
     Cloud8.changeColor("white");  
   }  
   public void DrawBackground()  
   {  
     Rectangles Grass = new Rectangles();  
     Rectangles Path = new Rectangles();  
     Grass.moveTo(0,585);  
     Grass.changeSize(1000,150);  
     Grass.changeColor("green");  
     Path.moveTo(475,605);  
     Path.changeSize(120,150);  
     Path.changeColor("brown");  
   }  
 }  

Circle :

 import java.awt.*;  
 import java.awt.geom.*;  
 /**  
  * Write a description of class Circle here.  
  *  
  * @author Haikal Almaz Said  
  * 16092018  
  */  
 public class Circle  
 {  
   private int radius;  
   private int centerx;  
   private int centery;  
   private String color;  
   private boolean isVisible;  
   public Circle()  
   {  
     radius = 50;  
     centerx = 0;  
     centery = 0;  
     color = "yellow";  
     isVisible = true;  
     draw();  
   }  
   public void SetRadius(int r)  
   {  
     radius = r;  
   }  
   public void SetCenter(int x, int y)  
   {  
     centerx = x;  
     centery = y;  
   }  
   public double Circumference()  
   {  
     return 2*3.14*radius;  
   }  
   public double Area()  
   {  
     return 3.14*radius*radius;  
   }  
   public void moveTo(int x, int y)  
   {  
     erase();  
     centerx = x;  
     centery = y;  
     draw();  
   }  
   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();  
     centerx += distance;  
     draw();  
   }  
   public void moveVertical(int distance)  
   {  
     erase();  
     centery += 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++)  
     {  
       centerx += 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++)  
     {  
       centery += delta;  
       draw();  
     }  
   }  
   public void changeSize(int newRadius)  
   {  
     erase();  
     radius = newRadius;  
     draw();  
   }  
   public void changeColor(String newColor)  
   {  
     color = newColor;  
     draw();  
   }  
   private void draw()  
   {  
     if(isVisible)  
     {  
       Canvas canvasobj = Canvas.getCanvas();  
       canvasobj.draw(this, color, new Ellipse2D.Double(centerx, centery, 2*radius, 2*radius));  
       canvasobj.wait(10);  
     }  
   }  
   private void erase()  
   {  
     if(isVisible)  
     {  
       Canvas canvasobj = Canvas.getCanvas();  
       canvasobj.erase(this);  
     }  
   }  
 }  

Square :

 import java.awt.*;  
 /**  
  * Write a description of class Square here.  
  *  
  * @author Haikal Almaz Said  
  * 16092018  
  */  
 public class Square  
 {  
   private int side;  
   private int centerx;  
   private int centery;  
   private String color;  
   private boolean isVisible;  
   public Square()  
   {  
     side = 25;  
     centerx = 50;  
     centery = 0;  
     color = "blue";  
     isVisible = true;  
     draw();  
   }  
   public void SetSide(int s)  
   {  
     side = s;  
   }  
   public void SetCenter(int x, int y)  
   {  
     centerx = x;  
     centery = y;  
   }  
   public double Circumference()  
   {  
     return 4 * side;  
   }  
   public double Area()  
   {  
     return side * side;  
   }  
   public void moveTo(int x, int y)  
   {  
     erase();  
     centerx = x;  
     centery = y;  
     draw();  
   }  
   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();  
     centerx += distance;  
     draw();  
   }  
   public void moveVertical(int distance)  
   {  
     erase();  
     centery += 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++)  
     {  
       centerx += 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++)  
     {  
       centery += delta;  
       draw();  
     }  
   }  
   public void changeSize(int newSide)  
   {  
     erase();  
     side = newSide;  
     draw();  
   }  
   public void changeColor(String newColor)  
   {  
     color = newColor;  
     draw();  
   }  
   private void draw()  
   {  
     if(isVisible)  
     {  
       Canvas canvasobj = Canvas.getCanvas();  
       canvasobj.draw(this, color, new Rectangle(centerx, centery, side, side));  
       canvasobj.wait(10);  
     }  
   }  
   private void erase()  
   {  
     if(isVisible)  
     {  
       Canvas canvasobj = Canvas.getCanvas();  
       canvasobj.erase(this);  
     }  
   }  
 }  

Triangle :

 import static java.lang.Math.sqrt;  
 import java.awt.*;  
 /**  
  * Write a description of class Triangle here.  
  *  
  * @author Haikal Almaz Said  
  * 16092018  
  */  
 public class Triangle  
 {  
   private int bottom;  
   private int height;  
   private double hypo;  
   private int centerx;  
   private int centery;  
   private String color;  
   private boolean isVisible;  
   public Triangle()  
   {  
     bottom = 100;  
     height = 100;  
     centerx = 50;  
     centery = 50;  
     color = "red";  
     isVisible = true;  
     draw();  
   }  
   public void SetBottom(int s)  
   {  
     bottom = s;  
   }  
   public void SetHeight(int h)  
   {  
     height = h;  
   }  
   public void SetCenter(int x, int y)  
   {  
     centerx = x;  
     centery = y;  
   }  
   public double Circumference()  
   {  
     hypo = Math.sqrt(0.5*bottom*0.5*bottom + height*height);  
     return 2*hypo + bottom;  
   }  
   public double Area()  
   {  
     return 0.5 * bottom * height;  
   }  
   public void moveTo(int x, int y)  
   {  
     erase();  
     centerx = x;  
     centery = y;  
     draw();  
   }  
   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();  
     centerx += distance;  
     draw();  
   }  
   public void moveVertical(int distance)  
   {  
     erase();  
     centery += 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++)  
     {  
       centerx += 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++)  
     {  
       centery += delta;  
       draw();  
     }  
   }  
   public void changeSize(int newBottom, int newHeight)  
   {  
     erase();  
     bottom = newBottom;  
     height = newHeight;  
     draw();  
   }  
   public void changeColor(String newColor)  
   {  
     color = newColor;  
     draw();  
   }  
   private void draw()  
   {  
     if(isVisible)  
     {  
       Canvas canvasobj = Canvas.getCanvas();  
       int[] xpoints = {centerx, centerx + (bottom/2), centerx - (bottom/2)};  
       int[] ypoints = {centery, centery + height, centery + height};  
       canvasobj.draw(this, color, new Polygon(xpoints, ypoints,3));  
       canvasobj.wait(10);  
     }  
   }  
   private void erase()  
   {  
     if(isVisible)  
     {  
       Canvas canvasobj = Canvas.getCanvas();  
       canvasobj.erase(this);  
     }  
   }  
 }  

Rectangle :

 import java.awt.*;  
 /**  
  * Write a description of class Rectangle here.  
  *  
  * @author Haikal Almaz Said  
  * 1609201  
  */  
 public class Rectangles  
 {  
   private int aside;  
   private int bside;  
   private int centerx;  
   private int centery;  
   private boolean isVisible;  
   private String color;  
   public Rectangles()  
   {  
     aside = 100;  
     bside = 50;  
     centerx = 200;  
     centery = 200;  
     isVisible = true;  
     color = "magenta";  
     draw();  
   }  
   public void SetSide(int l, int s)  
   {  
     aside = l;  
     bside = s;  
   }  
   public void SetCenter(int x, int y)  
   {  
     centerx = x;  
     centery = y;  
   }  
   public double Circumference()  
   {  
     return 2*aside + 2*bside;  
   }  
   public double Area()  
   {  
     return aside * bside;  
   }  
   public void moveTo(int x, int y)  
   {  
     erase();  
     centerx = x;  
     centery = y;  
     draw();  
   }  
   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();  
     centerx += distance;  
     draw();  
   }  
   public void moveVertical(int distance)  
   {  
     erase();  
     centery += 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++)  
     {  
       centerx += 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++)  
     {  
       centery += delta;  
       draw();  
     }  
   }  
   public void changeSize(int newASide, int newBSide)  
   {  
     erase();  
     aside = newASide;  
     bside = newBSide;  
     draw();  
   }  
   public void changeColor(String newColor)  
   {  
     color = newColor;  
     draw();  
   }  
   private void draw()  
   {  
     if(isVisible)  
     {  
       Canvas canvasobj = Canvas.getCanvas();  
       canvasobj.draw(this, color, new Rectangle(centerx, centery, aside, bside));  
       canvasobj.wait(10);  
     }  
   }  
   private void erase()  
   {  
     if(isVisible)  
     {  
       Canvas canvasobj = Canvas.getCanvas();  
       canvasobj.erase(this);  
     }  
   }  
 }  

Canvas :
 import javax.swing.*;  
 import java.awt.*;  
 import java.util.List;  
 import java.util.*;  
 /**  
  * Write a description of class Canvas here.  
  *  
  * @author Haikal Almaz Said  
  * 16092018  
  */  
 public class Canvas  
 {  
   private static Canvas canvasSingleton;  
   public static Canvas getCanvas()  
   {  
     if(canvasSingleton == null)  
     {  
       canvasSingleton = new Canvas("House", 1000, 750, Color.blue);  
     }  
     canvasSingleton.setVisible(true);  
     return canvasSingleton;  
   }  
   private JFrame frame;  
   public CanvasPane canvas;  
   private Graphics2D graphic;  
   private Color backgroundColor;  
   private Image canvasImage;  
   private List<Object>objects;  
   private HashMap<Object, ShapeDescription> 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));  
     backgroundColor = bgColour;  
     frame.pack();  
     objects = new ArrayList<Object>();  
     shapes = new HashMap<Object, ShapeDescription>();  
   }  
   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(backgroundColor);  
       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);  
     objects.add(referenceObject);  
     shapes.put(referenceObject, new ShapeDescription(shape, color));  
     redraw();  
   }  
   public void erase(Object referenceObject)  
   {  
     objects.remove(referenceObject);  
     shapes.remove(referenceObject);  
     redraw();  
   }  
   public void setForegroundColor(String colorString)  
   {  
     if(colorString.equals("white"))  
     {  
       graphic.setColor(Color.white);  
     }  
     else if(colorString.equals("magenta"))  
     {  
       graphic.setColor(Color.magenta);  
     }  
     else if(colorString.equals("green"))  
     {  
       graphic.setColor(Color.green);  
     }  
     else if(colorString.equals("yellow"))  
     {  
       graphic.setColor(Color.yellow);  
     }  
     else if(colorString.equals("blue"))  
     {  
       graphic.setColor(Color.blue);  
     }  
     else if(colorString.equals("red"))  
     {  
       graphic.setColor(Color.red);  
     }  
     else if(colorString.equals("brown"))  
     {  
       graphic.setColor(new Color(139,69,19));  
     }  
     else if(colorString.equals("black"))  
     {  
       graphic.setColor(Color.black);  
     }  
   }  
   public void wait(int milliseconds)  
   {  
     try  
     {  
       Thread.sleep(milliseconds);  
     }  
     catch(Exception e)  
     {  
     }  
   }  
   private void redraw()  
   {  
     erase();  
     for(Object shape : objects)  
     {  
       shapes.get(shape).draw(graphic);  
     }  
     canvas.repaint();  
   }  
   private void erase()  
   {  
     Color original = graphic.getColor();  
     graphic.setColor(backgroundColor);  
     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);  
     }  
   }  
 }  

No comments:

Post a Comment