Minggu, 16 September 2018

PBO Rumah

RUMAH

Nama  : Timothyus Tanner
NRP    : 05111740000103
Kelas   : PBO A

Tugas membuat rumah.

1. Canvas

 /* Author : TIMOTHYUS TANNER  
  * Kelas : PBO A  
  */   
  import javax.swing.*;   
  import java.awt.*;   
  import java.util.List;   
  import java.util.*;   
  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);  
   public static Canvas getCanvas()   
   {   
    if(canvasSingleton == null) {   
     canvasSingleton = new Canvas("Ini Rumahku", 600, 400,    
       Color.white);   
    }   
    canvasSingleton.setVisible(true);   
    return canvasSingleton;   
   }   
   private JFrame frame;   
   private CanvasPane canvas;   
   private Graphics2D graphic;   
   private Color backgroundColour;   
   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));   
    backgroundColour = bgColour;   
    frame.pack();   
    objects = new ArrayList<Object>();   
    shapes = new HashMap<Object, ShapeDescription>();   
   }   
   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);   
   }    
   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); // 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("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);  
   }    
   public void wait(int milliseconds)   
   {   
    try   
    {   
     Thread.sleep(milliseconds);   
    }    
    catch (Exception e)   
    {   
     // ignoring exception at the moment   
    }   
   }   
   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. Triangle

 /* Author : TIMOTHYUS TANNER  
  * Kelas : PBO A  
  */  
 import java.awt.*;   
 public class Triangle   
  {   
   private int height;   
   private int width;   
   private int xPosition;   
   private int yPosition;   
   private String color;   
   private boolean isVisible;   
   // New Triangle  
   public Triangle()   
   {   
    height = 30;   
    width = 40;   
    xPosition = 50;   
    yPosition = 15;   
    color = "blue";   
    isVisible = false;   
   }   
   // Make Shape Visible  
   public void makeVisible()   
   {   
    isVisible = true;   
    draw();   
   }   
   // Make Shape Invisible  
   public void makeInvisible()   
   {   
    erase();   
    isVisible = false;   
   }   
   // Geser Kanan  
   public void moveRight()   
   {   
    moveHorizontal(20);   
   }   
   // Geser Kiri   
   public void moveLeft()   
   {   
    moveHorizontal(-20);   
   }   
   // Geser Atas  
   public void moveUp()   
   {   
    moveVertical(-20);   
   }   
   // Geser Bawah  
   public void moveDown()   
   {   
    moveVertical(20);   
   }   
   // Geser Horizontal dengan banyak pixel  
   public void moveHorizontal(int distance)   
   {   
    erase();   
    xPosition += distance;   
    draw();   
   }   
   // Geser Vertikal dengan banyak pixel  
   public void moveVertical(int distance)   
   {   
    erase();   
    yPosition += distance;   
    draw();   
   }   
   // Ganti Ukuran Shape >=0  
   public void changeSize(int newHeight, int newWidth)   
   {   
    erase();   
    height = newHeight;   
    width = newWidth;   
    draw();   
   }   
   // "red", "yellow", "blue", "green", "pink" and "black"   
   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);   
    }   
   }   
  }   

3. Rectangle

 import java.awt.*;   
  public class Recta   
  {   
   private int P;   
   private int L;  
   private int xPosition;   
   private int yPosition;   
   private String color;   
   private boolean isVisible;   
   // New Square  
   public Recta()   
   {   
    P = 100;   
    L = 50;  
    xPosition = 260;   
    yPosition = 200;   
    color = "red";   
    isVisible = false;   
   }   
   // Make Shape Visible  
   public void makeVisible()   
   {   
    isVisible = true;   
    draw();   
   }   
   // Make Shape Invisible  
   public void makeInvisible()   
   {   
    erase();   
    isVisible = false;   
   }   
   // Geser Kanan  
   public void moveRight()   
   {   
    moveHorizontal(20);   
   }   
   // Geser Kiri  
   public void moveLeft()   
   {   
    moveHorizontal(-20);   
   }   
   // Geser Atas  
   public void moveUp()   
   {   
    moveVertical(-20);   
   }   
   // Geser Bawah  
   public void moveDown()   
   {   
    moveVertical(20);   
   }   
   // Geser Horizontal dengan banyak pixel  
   public void moveHorizontal(int distance)   
   {   
    erase();   
    xPosition += distance;   
    draw();   
   }   
   // Geser Vertikal dengan banyak pixel  
   public void moveVertical(int distance)   
   {   
    erase();   
    yPosition += distance;   
    draw();   
   }   
   // Ganti Ukuran Shape >=0  
   public void changeSize(int newP,int newL)   
   {   
    erase();   
    P = newP;   
    L = newL;  
    draw();   
   }   
   // "red", "yellow", "blue", "green", "pink" and "black"   
   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, P, L));   
     canvas.wait(10);   
    }   
   }   
   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;   
   // New Square  
   public Square()   
   {   
    size = 100;   
    xPosition = 260;   
    yPosition = 200;   
    color = "red";   
    isVisible = false;   
   }   
   // Make Shape Visible  
   public void makeVisible()   
   {   
    isVisible = true;   
    draw();   
   }   
   // Make Shape Invisible  
   public void makeInvisible()   
   {   
    erase();   
    isVisible = false;   
   }   
   // Geser Kanan  
   public void moveRight()   
   {   
    moveHorizontal(20);   
   }   
   // Geser Kiri  
   public void moveLeft()   
   {   
    moveHorizontal(-20);   
   }   
   // Geser Atas  
   public void moveUp()   
   {   
    moveVertical(-20);   
   }   
   // Geser Bawah  
   public void moveDown()   
   {   
    moveVertical(20);   
   }   
   // Geser Horizontal dengan banyak pixel  
   public void moveHorizontal(int distance)   
   {   
    erase();   
    xPosition += distance;   
    draw();   
   }   
   // Geser Vertikal dengan banyak pixel  
   public void moveVertical(int distance)   
   {   
    erase();   
    yPosition += distance;   
    draw();   
   }   
   // Ganti Ukuran Shape >=0  
   public void changeSize(int newSize)   
   {   
    erase();   
    size = newSize;   
    draw();   
   }   
   // "red", "yellow", "blue", "green", "pink" and "black"   
   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);   
    }   
   }   
  }   

5. 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;   
   // New Circle  
   public Circle()  
   {   
    diameter = 30;   
    xPosition = 20;   
    yPosition = 60;   
    color = "blue";   
    isVisible = false;   
   }   
   // Make Shape Visible  
   public void makeVisible()   
   {   
    isVisible = true;   
    draw();   
   }   
   // Make Shape Invisible  
   public void makeInvisible()   
   {   
    erase();   
    isVisible = false;   
   }   
   // Geser Kanan  
   public void moveRight()   
   {   
    moveHorizontal(20);   
   }   
   // Geser Kiri  
   public void moveLeft()   
   {   
    moveHorizontal(-20);   
   }   
   // Geser Atas  
   public void moveUp()   
   {   
    moveVertical(-20);   
   }   
   // Geser Bawah  
   public void moveDown()   
   {   
    moveVertical(20);   
   }   
   // Geser Horizontal dengan banyak pixel  
   public void moveHorizontal(int distance)   
   {   
    erase();   
    xPosition += distance;   
    draw();   
   }   
   // Geser Vertikal dengan banyak pixel  
   public void moveVertical(int distance)   
   {   
    erase();   
    yPosition += distance;   
    draw();   
   }    
   // Ganti Ukuran Shape >=0  
   public void changeSize(int newDiameter)   
   {   
    erase();   
    diameter = newDiameter;   
    draw();   
   }   
   // "red", "yellow", "blue", "green", "pink" and "black"   
   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);   
    }   
   }   
  }   

6. Picture

 /* Author : TIMOTHYUS TANNER  
  * Kelas : PBO A  
  */  
 public class Picture   
  {   
   private Triangle roof, grass1, grass2, grass3, grass4, grass5;   
   private Recta wall,door1,door2,tree,window4,window5,window3,window6;  
   private Square window1,window2;  
   private Circle knob1,knob2,sun;  
   private Circle leaf1,leaf2,leaf3;  
   private Circle cl1,cl2,cl3,cl4,cl5,cl6;  
   public Picture()   
   {   
    drawRoof();  
    drawWall();  
    drawWindow();  
    drawDoor();  
    drawKnob();  
    drawTree();  
    drawSky();  
   }   
   public void drawRoof()  
   {  
   roof = new Triangle();  
   roof.moveHorizontal(160);  
   roof.moveVertical(95);  
   roof.makeVisible();  
   roof.changeSize(90, 200);  
   }   
   public void drawWall()  
   {  
   wall = new Recta();  
   wall.moveHorizontal(-142);  
   wall.makeVisible();  
   wall.changeSize(180, 190);  
   }  
   public void drawWindow()  
   {  
   window1= new Square();  
   window1.moveHorizontal(-125);  
   window1.moveVertical(10);  
   window1.makeVisible();  
   window1.changeSize(60);  
   window1.changeColor("yellow");  
   window4= new Recta();  
   window4.moveHorizontal(-125);  
   window4.moveVertical(37);  
   window4.makeVisible();  
   window4.changeSize(60,3);  
   window4.changeColor("black");  
   window5= new Recta();  
   window5.moveHorizontal(-97);  
   window5.moveVertical(10);  
   window5.makeVisible();  
   window5.changeSize(3,60);  
   window5.changeColor("black");  
   window2= new Square();  
   window2.moveHorizontal(-40);  
   window2.moveVertical(10);  
   window2.makeVisible();  
   window2.changeSize(60);  
   window2.changeColor("yellow");  
   window3= new Recta();  
   window3.moveHorizontal(-40);  
   window3.moveVertical(37);  
   window3.makeVisible();  
   window3.changeSize(60,3);  
   window3.changeColor("black");  
   window6= new Recta();  
   window6.moveHorizontal(-12);  
   window6.moveVertical(10);  
   window6.makeVisible();  
   window6.changeSize(3,60);  
   window6.changeColor("black");  
   }    
   public void drawDoor()  
   {  
   door1 = new Recta();  
   door1.moveHorizontal(-107);  
   door1.moveVertical(95);  
   door1.makeVisible();  
   door1.changeSize(120, 95);  
   door1.changeColor("yellow");  
   door2 = new Recta();  
   door2.moveHorizontal(-48);  
   door2.moveVertical(95);  
   door2.makeVisible();  
   door2.changeSize(3, 95);  
   door2.changeColor("black");  
   }  
   public void drawKnob()  
   {  
   knob1 = new Circle();  
   knob1.makeVisible();  
   knob1.changeSize(17);  
   knob1.moveVertical(270);  
   knob1.moveHorizontal(170);  
   knob2 = new Circle();  
   knob2.makeVisible();  
   knob2.changeSize(17);  
   knob2.moveVertical(270);  
   knob2.moveHorizontal(199);  
   }  
   public void drawTree()  
   {  
   tree = new Recta();  
   tree.moveHorizontal(220);  
   tree.moveVertical(70);  
   tree.makeVisible();  
   tree.changeSize(20, 120);  
   tree.changeColor("brown");  
   leaf1 = new Circle();  
   leaf1.makeVisible();  
   leaf1.changeSize(80);  
   leaf1.changeColor("green");  
   leaf1.moveHorizontal(400);  
   leaf1.moveVertical(160);  
   leaf2 = new Circle();  
   leaf2.makeVisible();  
   leaf2.changeSize(80);  
   leaf2.changeColor("green");  
   leaf2.moveHorizontal(459);  
   leaf2.moveVertical(160);  
   leaf3 = new Circle();  
   leaf3.makeVisible();  
   leaf3.changeSize(80);  
   leaf3.changeColor("green");  
   leaf3.moveHorizontal(430);  
   leaf3.moveVertical(105);  
   grass1 = new Triangle();  
   grass1.makeVisible();  
   grass1.changeColor("darkgreen");  
   grass1.moveHorizontal(430);  
   grass1.moveVertical(348);  
   grass1.changeSize(30,30);  
   grass2 = new Triangle();  
   grass2.makeVisible();  
   grass2.changeColor("darkgreen");  
   grass2.moveHorizontal(450);  
   grass2.moveVertical(348);  
   grass2.changeSize(30,30);  
   grass2 = new Triangle();  
   grass2.makeVisible();  
   grass2.changeColor("darkgreen");  
   grass2.moveHorizontal(470);  
   grass2.moveVertical(348);  
   grass2.changeSize(30,30);  
   grass2 = new Triangle();  
   grass2.makeVisible();  
   grass2.changeColor("darkgreen");  
   grass2.moveHorizontal(410);  
   grass2.moveVertical(348);  
   grass2.changeSize(30,30);  
   }  
   public void drawSky()  
   {  
   sun = new Circle();  
   sun.changeColor("yellow");  
   sun.makeVisible();  
   sun.changeSize(160);  
   sun.moveHorizontal(-50);  
   sun.moveVertical(-100);  
   cl3 = new Circle();  
   cl3.changeColor("gray");  
cl3.makeVisible(); cl3.moveHorizontal(410); cl3.moveVertical(-20); cl3.changeSize(50); cl1 = new Circle(); cl1.changeColor("gray"); cl1.makeVisible(); cl1.moveHorizontal(395); cl2 = new Circle(); cl2.changeColor("gray"); cl2.makeVisible(); cl2.moveHorizontal(445); } }

Hasil





Tidak ada komentar:

Posting Komentar