Tugas Menggambar Rumah

     Class Diagram:


     Class 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 Picture Demo", 500, 300, Color.white);   
   }   
   canvasSingleton.setVisible(true);   
   return canvasSingleton;   
   }   
   // ----- instance part -----   
   private JFrame frame;   
   private CanvasPane canvas;   
   private Graphics2D graphic;   
   private Color backgroundColor;   
   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 bgColor the desired background color of the canvas   
   */   
   private Canvas(String title, int width, int height, Color bgColor)   
   {   
    frame = new JFrame();   
    canvas = new CanvasPane();   
    frame.setContentPane(canvas);   
    frame.setTitle(title);   
    frame.setLocation(30, 30);   
    canvas.setPreferredSize(new Dimension(width, height));   
    backgroundColor = bgColor;   
    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 color   
     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);   
   }   
   /**   
   * 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 color of the Canvas.   
   * @param newColor the new color for the foreground of the Canvas    
   */   
   public void setForegroundColor(String colorString)   
   {   
   if(colorString.equals("red")) {   
    graphic.setColor(new Color(250,0,0));   
   }   
   else if(colorString.equals("cyan")) {   
    graphic.setColor(new Color(0,255,255));   
   }   
   else if(colorString.equals("blue")) {   
    graphic.setColor(new Color(0,0,255));   
   }   
   else if(colorString.equals("very dark blue")) {   
    graphic.setColor(new Color(30,75,153));   
   }   
   else if(colorString.equals("yellow")) {   
    graphic.setColor(new Color(255, 204, 0));   
   }   
   else if(colorString.equals("light green")) {   
    graphic.setColor(new Color(0,255,51));   
   }   
   else if(colorString.equals("green")) {   
    graphic.setColor(new Color(24, 139, 34));   
   }   
   else if(colorString.equals("brown")) {   
    graphic.setColor(new Color(102,51,0));   
   }   
   else if(colorString.equals("white")) {   
    graphic.setColor(Color.white);   
   }   
   else if(colorString.equals("purple")){   
    graphic.setColor(new Color(102,0,153));   
   }   
   else if(colorString.equals("black")) {   
    graphic.setColor(Color.black);   
   }   
   else if(colorString.equals("orange")){  
    graphic.setColor(new Color(255,165,0));  
   }  
   else if(colorString.equals("magenta")){  
    graphic.setColor(new Color(255,0,255));  
   }  
   else if(colorString.equals("navajo")){  
    graphic.setColor(new Color(255,222,173));  
   }  
   else if(colorString.equals("azure")){  
    graphic.setColor(new Color(240,255,255));  
   }  
   else if(colorString.equals("sky")){  
    graphic.setColor(new Color(135,206,250));  
   }  
   else if(colorString.equals("light cyan")){  
    graphic.setColor(new Color(244,255,255));  
   }  
   else if(colorString.equals("gold")){  
    graphic.setColor(new Color(184,134,11));  
   }  
   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(Object shape : objects) {   
     shapes.get(shape).draw(graphic);   
    }   
    canvas.repaint();   
   }   
   /**   
   * Erase the whole canvas. (Does not 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);   
   }   
   /************************************************************************   
   * 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);   
    }   
   }   
  }  
  
   Class 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 = 125;    
    xPosition = 50;    
    yPosition = 50;    
    color = "green";    
    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);    
    }    
   }    
  }  
  
Class Circle :

 import java.awt.*;    
 import java.awt.geom.*;   
     
  public class Circle   
  {   
   private double diameter;   
   private double xPosition,yPosition;   
   private String color;   
   private boolean isVisible;   
   public Circle()   
   {   
   diameter=30;   
   xPosition=20;   
   yPosition=60;   
   color="blue";   
   isVisible=false;   
   }   
   public void makeVisible()   
   {   
   isVisible=true;   
   draw();   
   }   
   public void makeInVisible()   
   {   
   erase();   
   isVisible=false;   
   }   
   public void moveRight()   
   {   
   moveHorizontal(20);   
   }   
   public void moveLeft()   
   {   
   moveHorizontal(-40);   
   }   
   public void moveUp()   
   {   
   moveVertical(20);   
   }   
   public void moveDown()   
   {   
   moveVertical(-40);   
   }   
   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();    
   }    
   /**    
   * 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);    
    }    
   }    
   private void erase()    
   {    
    if(isVisible) {    
     Canvas canvas = Canvas.getCanvas();    
     canvas.erase(this);    
    }    
   }    
  }  
  
Class 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 = 60;   
    width = 80;   
    xPosition = 50;   
    yPosition = 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(40);   
   }   
   /**   
   * 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(40);   
   }   
   /**   
   * 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);   
    }   
   }   
  }  
   
  Class Picture : 
 public class Picture   
  {   
    private Square background;    
    private Circle sun;    
    private Square wall;    
    private Square wall1;    
    private Square wall2;    
    private Square wall3;    
    private Square wall4;    
    private Circle tree;    
    private Square house1;   
    private Square window11;   
    private Square window12;   
    private Square door1;   
    private Triangle roof1;   
    private Square grass;   
    private Circle cloud1;   
    private Circle cloud2;   
    private Circle cloud3;   
    private Circle cloud4;   
    private Circle cloud5;   
    private Circle cloud6;   
    private Circle knob;  
    private Circle tree6;    
    /**    
     * Draw this picture.    
     */    
    public void draw() {   
     background=new Square();   
     background.changeColor("sky");   
     background.changeSize(500);   
     background.moveHorizontal(-50);    
     background.moveVertical(-50);    
     background.makeVisible();    
     sun=new Circle();    
     sun.changeColor("orange");   
     sun.changeSize(80);   
     sun.moveHorizontal(360);    
     sun.moveVertical(-40);    
     sun.makeVisible();   
     grass=new Square();   
     grass.changeColor("green");   
     grass.changeSize(500);   
     grass.moveHorizontal(-50);    
     grass.moveVertical(230);    
     grass.makeVisible();   
     house1=new Square();   
     house1.changeColor("navajo");   
     house1.changeSize(120);   
     house1.moveHorizontal(125);    
     house1.moveVertical(120);    
     house1.makeVisible();   
     window11=new Square();   
     window11.changeColor("light cyan");   
     window11.changeSize(30);   
     window11.moveHorizontal(130);    
     window11.moveVertical(165);    
     window11.makeVisible();   
     window12=new Square();   
     window12.changeColor("light cyan");   
     window12.changeSize(30);   
     window12.moveHorizontal(210);    
     window12.moveVertical(165);    
     window12.makeVisible();   
     door1=new Square();   
     door1.changeColor("gold");   
     door1.changeSize(40);   
     door1.moveHorizontal(165);    
     door1.moveVertical(200);    
     door1.makeVisible();   
     knob = new Circle();  
     knob.changeColor("brown");  
     knob.changeSize(10);  
     knob.moveHorizontal(195);  
     knob.moveVertical(210);  
     knob.makeVisible();  
     roof1=new Triangle();   
     roof1.changeColor("red");   
     roof1.changeSize(100,120);   
     roof1.moveHorizontal(185);    
     roof1.moveVertical(55);    
     roof1.makeVisible();   
     cloud1=new Circle();    
     cloud1.changeColor("azure");   
     cloud1.changeSize(50);   
     cloud1.moveHorizontal(400);    
     cloud1.moveVertical(0);    
     cloud1.makeVisible();   
     cloud2=new Circle();    
     cloud2.changeColor("azure");   
     cloud2.changeSize(30);   
     cloud2.moveHorizontal(380);    
     cloud2.moveVertical(10);    
     cloud2.makeVisible();   
     cloud3=new Circle();    
     cloud3.changeColor("azure");   
     cloud3.changeSize(30);   
     cloud3.moveHorizontal(440);    
     cloud3.moveVertical(10);    
     cloud3.makeVisible();   
     cloud4=new Circle();    
     cloud4.changeColor("azure");   
     cloud4.changeSize(50);   
     cloud4.moveHorizontal(100);    
     cloud4.moveVertical(10);    
     cloud4.makeVisible();   
     cloud5=new Circle();    
     cloud5.changeColor("azure");   
     cloud5.changeSize(40);   
     cloud5.moveHorizontal(80);    
     cloud5.moveVertical(20);    
     cloud5.makeVisible();   
     cloud6=new Circle();    
     cloud6.changeColor("azure");   
     cloud6.changeSize(40);   
     cloud6.moveHorizontal(140);    
     cloud6.moveVertical(20);    
     cloud6.makeVisible();  
    }   
  }  



Komentar

Postingan Populer