Rumah Sederhana dengan BlueJ!

Pada minggu ini, saya mendapatkan tugas untuk membuat rumahmenggunakan BlueJ. Setelah
berpikir cukup lama, saya akhirnya membuat rumah sederhana ini dengan beberapa pohon dan awan untuk melengkapi project minggu ini.

Dalam pembuatannya, saya membaginya menjadi 6 Class, antara lain:
  1. Picture
  2. Canvas
  3. Square
  4. Triangle
  5. Circle
  6. Rectangle
Dengan class-class tersebut, saya dapat menghasilkan gambar rumah berbentuk dua dimensi. Class Canvas berisi fungsi-fungsi yang berguna untuk merangkai bentuk gambar rumah tersebut, sedangkan Class Picture berguna untuk merangai bentuk gambarnya.

Source Code

Picture

 public class Picture  
  {  
    private Triangle roof;  
    private Recta wall,door,tree1,tree2;  
    private Square window1,window2,window3,window4;  
    private Circle knob,sun;  
    private Circle leaf1,leaf2,leaf3,leaf4,leaf5,leaf6;  
    private Circle cl1,cl2,cl3,cl4,cl5,cl6;  
   public Picture()   
   {   
     drawRoof();  
     drawWall();  
     drawWindow();  
     drawDoor();  
     drawKnob();  
     drawTree();  
     drawSky();  
   }  
   public void drawRoof()  
   {  
     roof = new Triangle();  
     roof.changeColor("black");  
     roof.moveHorizontal(400);  
     roof.moveVertical(45);  
     roof.makeVisible();  
     roof.changeSize(140, 300);  
   }   
   public void drawWall()  
   {  
     wall = new Recta();  
     wall.moveHorizontal(40);  
     wall.makeVisible();  
     wall.changeSize(300, 150);  
   }  
   public void drawWindow()  
   {  
     window1 = new Square();  
     window1.moveHorizontal(60);  
     window1.moveVertical(55);  
     window1.makeVisible();  
     window1.changeSize(40);  
     window1.changeColor("grey");  
     window2 = new Square();  
     window2.moveHorizontal(270);  
     window2.moveVertical(55);  
     window2.makeVisible();  
     window2.changeSize(40);  
     window2.changeColor("grey");  
     window3 = new Square();  
     window3.moveHorizontal(63);  
     window3.moveVertical(58);  
     window3.makeVisible();  
     window3.changeSize(34);  
     window3.changeColor("white");  
     window4 = new Square();  
     window4.moveHorizontal(273);  
     window4.moveVertical(58);  
     window4.makeVisible();  
     window4.changeSize(34);  
     window4.changeColor("white");  
   }    
   public void drawDoor()  
   {  
   door = new Recta();  
   door.moveHorizontal(125);  
   door.moveVertical(50);  
   door.makeVisible();  
   door.changeSize(55, 100);  
   door.changeColor("grey");  
   }  
   public void drawKnob()  
   {  
   knob = new Circle();  
   knob.makeVisible();  
   knob.changeSize(7);  
   knob.moveVertical(230);  
   knob.moveHorizontal(405);  
   knob.changeColor("black");  
   }  
   public void drawTree()  
   {  
   tree1 = new Recta();  
   tree1.moveHorizontal(-20);  
   tree1.moveVertical(-70);  
   tree1.makeVisible();  
   tree1.changeSize(20, 220);  
   tree1.changeColor("brown");  
   tree2 = new Recta();  
   tree2.moveHorizontal(-70);  
   tree2.moveVertical(0);  
   tree2.makeVisible();  
   tree2.changeSize(12, 150);  
   tree2.changeColor("brown");  
   leaf1 = new Circle();  
   leaf1.makeVisible();  
   leaf1.changeSize(80);  
   leaf1.changeColor("green");  
   leaf1.moveHorizontal(160);  
   leaf1.moveVertical(20);  
   leaf2 = new Circle();  
   leaf2.makeVisible();  
   leaf2.changeSize(80);  
   leaf2.changeColor("green");  
   leaf2.moveHorizontal(220);  
   leaf2.moveVertical(20);  
   leaf3 = new Circle();  
   leaf3.makeVisible();  
   leaf3.changeSize(100);  
   leaf3.changeColor("green");  
   leaf3.moveHorizontal(180);  
   leaf3.moveVertical(-35);  
   leaf4 = new Circle();  
   leaf4.makeVisible();  
   leaf4.changeSize(35);  
   leaf4.changeColor("springgreen");  
   leaf4.moveHorizontal(120);  
   leaf4.moveVertical(135);  
   leaf5 = new Circle();  
   leaf5.makeVisible();  
   leaf5.changeSize(35);  
   leaf5.changeColor("springgreen");  
   leaf5.moveHorizontal(190);  
   leaf5.moveVertical(135);  
   leaf6 = new Circle();  
   leaf6.makeVisible();  
   leaf6.changeSize(70);  
   leaf6.changeColor("springgreen");  
   leaf6.moveHorizontal(140);  
   leaf6.moveVertical(100);  
   }  
   public void drawSky()  
   {  
   sun = new Circle();  
   sun.changeColor("orange");  
   sun.makeVisible();  
   sun.changeSize(160);  
   sun.moveHorizontal(600);  
   sun.moveVertical(-100);  
   cl1 = new Circle();  
   cl1.changeColor("lightblue");  
   cl1.makeVisible();  
   cl1.moveHorizontal(575);  
   cl1.changeSize(50);  
   cl2 = new Circle();  
   cl2.changeColor("lightblue");  
   cl2.makeVisible();  
   cl2.moveHorizontal(650);  
   cl2.changeSize(50);  
   cl3 = new Circle();  
   cl3.changeColor("lightblue");  
   cl3.makeVisible();  
   cl3.moveHorizontal(600);  
   cl3.moveVertical(-20);  
   cl3.changeSize(75);  
   cl4 = new Circle();  
   cl4.changeColor("lightblue");  
   cl4.makeVisible();  
   cl4.moveHorizontal(40);  
   cl4.moveVertical(-20);  
   cl4.changeSize(50);  
   cl5 = new Circle();  
   cl5.changeColor("lightblue");  
   cl5.makeVisible();  
   cl5.moveHorizontal(25);  
   cl6 = new Circle();  
   cl6.changeColor("lightblue");  
   cl6.makeVisible();  
   cl6.moveHorizontal(75);  
   }  
  }   

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 Hazimi   
  *   
  * @version 1.0   
  */   
  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 Shapes Demo", 1000, 800, Color.white);   
    }   
    canvasSingleton.setVisible(true);   
    return canvasSingleton;   
   }   
   // ----- instance part -----   
   private JFrame frame;   
   private CanvasPane canvas;   
   private Graphics2D graphic;   
   private Color backgroundColour;   
   private Image canvasImage;   
   private List objects;   
   private HashMap 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();   
    shapes = new HashMap();   
   }   
   /**   
   * 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("magenta"))   
     graphic.setColor(Color.magenta);   
    else if(colorString.equals("white"))   
     graphic.setColor(Color.white);   
    else if(colorString.equals("orange"))  
     graphic.setColor(Color.orange);  
    else if(colorString.equals("springgreen"))  
     graphic.setColor(new Color(0,255,127));  
    else if(colorString.equals("brown"))   
     graphic.setColor(new Color(102,51,0));   
    else if(colorString.equals("grey"))   
     graphic.setColor(new Color(190,190,190));   
    else if(colorString.equals("lightblue"))   
     graphic.setColor(new Color(0,191,255));   
    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);   
    }   
   }   
  }  

Square

 import java.awt.*;    
  public class Square    
  {    
   private int size;    
   private int xPosition;    
   private int yPosition;    
   private String color;    
   private boolean isVisible;    
   // Default Square  
   public Square()    
   {    
   size = 100;    
   xPosition = 260;    
   yPosition = 200;    
   color = "red";    
   isVisible = false;    
   }    
   // To make shape visible  
   public void makeVisible()    
   {    
   isVisible = true;    
   draw();    
   }    
   // To make shape invisible   
   public void makeInvisible()    
   {    
   erase();    
   isVisible = false;    
   }    
   // To move shape to right  
   public void moveRight()    
   {    
   moveHorizontal(20);    
   }    
   // To move shape to left  
   public void moveLeft()    
   {    
   moveHorizontal(-20);    
   }    
   // To move shape to up   
   public void moveUp()    
   {    
   moveVertical(-20);    
   }    
   // To move shape to down  
   public void moveDown()    
   {    
   moveVertical(20);    
   }    
   // To move shape horizontally by pixel   
   public void moveHorizontal(int distance)    
   {    
   erase();    
   xPosition += distance;    
   draw();    
   }    
   // To move shape vertically by pixel  
   public void moveVertical(int distance)    
   {    
   erase();    
   yPosition += distance;    
   draw();    
   }    
   // To change the size of the shape  
   public void changeSize(int newSize)    
   {    
   erase();    
   size = newSize;    
   draw();    
   }    
   // To change the color of the shape   
   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);    
   }    
   }    
  }   

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;    
   // Default Triangle  
   public Triangle()    
   {    
   height = 30;    
   width = 40;    
   xPosition = 50;    
   yPosition = 15;    
   color = "blue";    
   isVisible = false;    
   }    
   // To make shape visible  
   public void makeVisible()    
   {    
   isVisible = true;    
   draw();    
   }    
   // MTo make shape invisible   
   public void makeInvisible()    
   {    
   erase();    
   isVisible = false;    
   }    
   // To move shape to right  
   public void moveRight()    
   {    
   moveHorizontal(20);    
   }    
   // To move shape to left  
   public void moveLeft()    
   {    
   moveHorizontal(-20);    
   }    
   // To move shape to up  
   public void moveUp()    
   {    
   moveVertical(-20);    
   }    
   // To move shape to down  
   public void moveDown()    
   {    
   moveVertical(20);    
   }    
   // To move shape horizontally by pixel   
   public void moveHorizontal(int distance)    
   {    
   erase();    
   xPosition += distance;    
   draw();    
   }    
   // To move shape vertically by pixel   
   public void moveVertical(int distance)    
   {    
   erase();    
   yPosition += distance;    
   draw();    
   }    
   // To change the size of the shape  
   public void changeSize(int newHeight, int newWidth)    
   {    
   erase();    
   height = newHeight;    
   width = newWidth;    
   draw();    
   }    
   // To change the color of the shape  
   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);    
   }    
   }    
  }    

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;    
   // Default Circle  
   public Circle()   
   {    
   diameter = 30;    
   xPosition = 20;    
   yPosition = 60;    
   color = "blue";    
   isVisible = false;    
   }    
   // To make shape visible  
   public void makeVisible()    
   {    
   isVisible = true;    
   draw();    
   }    
   // To make shape invisible   
   public void makeInvisible()    
   {    
   erase();    
   isVisible = false;    
   }    
   // To move shape to right   
   public void moveRight()    
   {    
   moveHorizontal(20);    
   }    
   // To move shape to left  
   public void moveLeft()    
   {    
   moveHorizontal(-20);    
   }    
   // To move shape to up  
   public void moveUp()    
   {    
   moveVertical(-20);    
   }    
   // To move shape to down  
   public void moveDown()    
   {    
   moveVertical(20);    
   }    
   // To move shape horizontally by pixel   
   public void moveHorizontal(int distance)    
   {    
   erase();    
   xPosition += distance;    
   draw();    
   }    
   // To move shape vertically by pixel    
   public void moveVertical(int distance)    
   {    
   erase();    
   yPosition += distance;    
   draw();    
   }    
   // To change the size of the shape  
   public void changeSize(int newDiameter)    
   {    
   erase();    
   diameter = newDiameter;    
   draw();    
   }    
   // To change the color of the shape    
   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);    
   }    
   }    
  }    

Rectangle

 import java.awt.*;    
  public class Recta    
  {    
   private int width;  
   private int length;  
   private int xPosition;    
   private int yPosition;    
   private String color;    
   private boolean isVisible;    
   // Default Square  
   public Recta()    
   {    
   width = 100;  
   length = 50;  
   xPosition = 260;    
   yPosition = 200;    
   color = "red";    
   isVisible = false;    
   }    
   // To make shape visible  
   public void makeVisible()    
   {    
   isVisible = true;    
   draw();    
   }    
   // To make shape invisible   
   public void makeInvisible()    
   {    
   erase();    
   isVisible = false;    
   }    
   // To move shape to right  
   public void moveRight()    
   {    
   moveHorizontal(20);    
   }    
   // To move shape to left  
   public void moveLeft()    
   {    
   moveHorizontal(-20);    
   }    
   // To move shape to up   
   public void moveUp()    
   {    
   moveVertical(-20);    
   }    
   // To move shape to down  
   public void moveDown()    
   {    
   moveVertical(20);    
   }    
   // To move shape horizontally by pixel   
   public void moveHorizontal(int distance)    
   {    
   erase();    
   xPosition += distance;    
   draw();    
   }    
   // To move shape vertically by pixel  
   public void moveVertical(int distance)    
   {    
   erase();    
   yPosition += distance;    
   draw();    
   }    
   // To change the size of the shape  
   public void changeSize(int newWidth, int newLength)    
   {    
   erase();    
   width = newWidth;  
   length = newLength;  
   draw();    
   }    
   // To change the color of the shape   
   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, width, length));    
    canvas.wait(10);    
   }    
   }    
   private void erase()    
   {    
   if(isVisible) {    
    Canvas canvas = Canvas.getCanvas();    
    canvas.erase(this);    
   }    
   }    
  }    

Tampilan/Hasil Akhir   

Komentar

Postingan populer dari blog ini

Halaman Berita? HTML & CSS!

Menggunakan CodeIgniter!!

Sistem Informasi Cinema 21 - APSI