Langsung ke konten utama

Tugas Rumah PBO A

Hey guys! In this post I am going to show what I have been working on this week.

I am asked to draw a house from 2D Shapes compiled in BlueJ. In this project, I use 6 classes, which are:

1. Picture

    This is the main method for displaying the house picture I have designed.
 /*  
  * Make your Picture here.  
  *  
  * @author Elkana Hans Widersen  
  * @version (16/9/2018)  
  */  
 public class Picture{  
   private Rect BackWall, BorderWindow, Roof, LeftRoofBorder, FrontWall, DoorBorder, Land;  
   private Triangle SideRoof, UpperWall, RightRoofBorder;  
   private Square Window;  
   private Circle WindowBorder, RightWindow, Sun;  
     
   public void draw(){  
     Land = new Rect();  
     Land.moveHorizontal(-50);  
     Land.moveVertical(220);  
     Land.changeSize(300,30);  
     Land.makeVisible();  
     Land.changeColor("green");  
       
     Sun = new Circle();  
     Sun.moveHorizontal(190);  
     Sun.moveVertical(0);  
     Sun.changeSize(50);  
     Sun.makeVisible();  
     Sun.changeColor("yellow");  
       
     BackWall = new Rect();  
     BackWall.moveHorizontal(0);  
     BackWall.moveVertical(140);  
     BackWall.changeSize(120,80);  
     BackWall.makeVisible();  
     BackWall.changeColor("red");  
       
     BorderWindow = new Rect();  
     BorderWindow.moveHorizontal(20);  
     BorderWindow.moveVertical(160);  
     BorderWindow.changeSize(29,35);  
     BorderWindow.makeVisible();  
       
     Window = new Square();  
     Window.moveHorizontal(22);  
     Window.moveVertical(165);  
     Window.changeSize(25);  
     Window.makeVisible();  
     Window.changeColor("blue");  
       
     SideRoof = new Triangle();  
     SideRoof.moveHorizontal(10);  
     SideRoof.moveVertical(115);  
     SideRoof.changeSize(60,40);  
     SideRoof.makeVisible();  
     SideRoof.changeColor("black");  
       
     Roof = new Rect();  
     Roof.moveHorizontal(10);  
     Roof.moveVertical(80);  
     Roof.changeSize(140,60);  
     Roof.makeVisible();  
     Roof.changeColor("black");  
       
     LeftRoofBorder = new Rect();  
     LeftRoofBorder.moveHorizontal(-15);  
     LeftRoofBorder.moveVertical(140);  
     LeftRoofBorder.changeSize(155,8);  
     LeftRoofBorder.makeVisible();  
     LeftRoofBorder.changeColor("blue");  
       
     FrontWall = new Rect();  
     FrontWall.moveHorizontal(120);  
     FrontWall.moveVertical(130);  
     FrontWall.changeSize(60,90);  
     FrontWall.makeVisible();  
     FrontWall.changeColor("red");  
       
     RightRoofBorder = new Triangle();  
     RightRoofBorder.moveHorizontal(150);  
     RightRoofBorder.moveVertical(115);  
     RightRoofBorder.changeSize(50,80);  
     RightRoofBorder.makeVisible();  
     RightRoofBorder.changeColor("blue");  
       
     UpperWall = new Triangle();  
     UpperWall.moveHorizontal(150);  
     UpperWall.moveVertical(127);  
     UpperWall.changeSize(38,60);  
     UpperWall.makeVisible();  
     UpperWall.changeColor("red");  
       
     WindowBorder = new Circle();  
     WindowBorder.moveHorizontal(164);  
     WindowBorder.moveVertical(110);  
     WindowBorder.changeSize(30);  
     WindowBorder.makeVisible();  
     WindowBorder.changeColor("yellow");  
       
     RightWindow = new Circle();  
     RightWindow.moveHorizontal(166);  
     RightWindow.moveVertical(112);  
     RightWindow.changeSize(26);  
     RightWindow.makeVisible();  
       
     DoorBorder = new Rect();  
     DoorBorder.moveHorizontal(135);  
     DoorBorder.moveVertical(160);  
     DoorBorder.changeSize(30,60);  
     DoorBorder.makeVisible();  
     DoorBorder.changeColor("magenta");  
   }  
     
   public void setBlackAndWhite(){    
   if(BackWall != null){ // only if it's painted already...    
    Land.changeColor("black");  
    Sun.changeColor("black");  
    BackWall.changeColor("black");  
    LeftRoofBorder.changeColor("black");  
    BorderWindow.changeColor("black");  
    WindowBorder.changeColor("black");  
    FrontWall.changeColor("black");  
    RightRoofBorder.changeColor("black");  
    UpperWall.changeColor("black");  
    Window.changeColor("white");  
    RightWindow.changeColor("white");  
    DoorBorder.changeColor("white");  
    }    
   }  
     
   public void setColor(){    
   if(BackWall != null){ // only if it's painted already...    
    Land.changeColor("green");  
    Sun.changeColor("yellow");  
    BackWall.changeColor("red");  
    LeftRoofBorder.changeColor("blue");  
    FrontWall.changeColor("red");  
    RightRoofBorder.changeColor("blue");  
    UpperWall.changeColor("red");  
    BorderWindow.changeColor("yellow");  
    WindowBorder.changeColor("yellow");  
    Window.changeColor("blue");  
    RightWindow.changeColor("blue");  
    DoorBorder.changeColor("magenta");   
    }    
   }    
 }   

2. Canvas

   
 /*  
  * Declaration of methods here.  
  *  
  * @author Elkana Hans Widersen  
  * @version (16/9/2018)  
  */  
 import javax.swing.*;  
 import java.awt.*;  
 import java.util.List;  
 import java.util.*;  
   
 public class Canvas{  
   private static Canvas canvasSingleton;  
     
   public static Canvas getCanvas(){  
     if(canvasSingleton == null){  
       canvasSingleton = new Canvas("BlueJ Shapes Demo", 300, 300,   
           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) {  
       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);  
     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("magenta"))  
       graphic.setColor(Color.magenta);  
     else if(colorString.equals("white"))  
       graphic.setColor(Color.white);  
     else  
       graphic.setColor(Color.black);  
   }  
   
   public void wait(int millisecond){  
     try{  
       Thread.sleep(millisecond);  
     }   
     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);  
     }  
   }  
 }  

3. Circle

 import java.awt.*;  
 import java.awt.geom.*;  
   
 /*  
  * Declaration of the Circle here.  
  *  
  * @author Elkana Hans Widersen  
  * @version (16/9/2018)  
  */  
   
 public class Circle{  
   private int diameter;  
   private int xPosition;  
   private int 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(-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);  
     }  
   }  
 } 

4. Square

   
 /*  
  * Declaration of the Square here.  
  *  
  * @author Elkana Hans Widersen  
  * @version (16/9/2018)  
  */  
 import java.awt.*;  
 import java.awt.geom.*;  
 public class Square{  
  private int side;  
  private int xPosition;  
  private int yPosition;  
  private String color;  
  private boolean isVisible;  
    
  public Square(){  
    side = 10;  
    xPosition = 50;  
    yPosition = 50;  
    color = "yellow";  
    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 changeColor(String newColor){  
    color = newColor;  
    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();  
    side = newSize;  
    draw();  
  }  
   
  private void draw(){  
    if(isVisible) {  
      Canvas canvas = Canvas.getCanvas();  
      canvas.draw(this, color,  
          new Rectangle(xPosition, yPosition, side, side));  
      canvas.wait(10);  
    }  
  }  
   
  private void erase(){  
    if(isVisible) {  
      Canvas canvas = Canvas.getCanvas();  
      canvas.erase(this);  
    }  
  }  
 }  

5. Triangle

 import java.awt.*;  
   
 /*  
  * Declaration of the Triangle here.  
  *  
  * @author Elkana Hans Widersen  
  * @version (16/9/2018)  
  */  
   
 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);  
     }  
   }  
 }  

6. Rectangle

 /*  
  * Declaration of the Rectangle here.  
  *  
  * @author Elkana Hans Widersen  
  * @version (16/9/2018)  
  */  
 import java.awt.*;  
 import java.awt.geom.*;  
 public class Rect{  
  private int length;  
  private int width;  
  private int xPosition;  
  private int yPosition;  
  private String color;  
  private boolean isVisible;  
    
  public Rect(){  
    length = 20;  
    width = 10;  
    xPosition = 50;  
    yPosition = 50;  
    color = "yellow";  
    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 changeColor(String newColor){  
    color = newColor;  
    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 newLength, int newWidth){  
    erase();  
    length = newLength;  
    width = newWidth;  
    draw();  
  }  
   
  private void draw(){  
    if(isVisible) {  
      Canvas canvas = Canvas.getCanvas();  
      canvas.draw(this, color,  
          new Rectangle(xPosition, yPosition, length, width));  
      canvas.wait(10);  
    }  
  }  
   
  private void erase(){  
    if(isVisible) {  
      Canvas canvas = Canvas.getCanvas();  
      canvas.erase(this);  
    }  
  }  
 }  

When you click on method "new Picture()", it will display something like this:



Thank you and see you next time!



Komentar

Postingan populer dari blog ini

Fox and Rabbit Simulator

Hi guys! I'm going to show you a Fox and Rabbit Simulator made in BlueJ. We're going to use these classes Simulator SimulatorView Location Field FieldStats Counter Randomizer Rabbit Fox Here is the source code of those classes: Simulator import java.util.Random; import java.util.List; import java.util.ArrayList; import java.util.Iterator; import java.awt.Color; /** * A simple predator-prey simulator, based on a rectangular field * containing rabbits and foxes. * * @author Elkana Hans Widersen * @version 1.0 */ public class Simulator { /** * Constants representing configuration information for the simulation. */ private static final int def_width = 50; // The default width for the grid. private static final int def_depth = 50; // The default depth of the grid. private static final double foxProbability = 0.02; // The probability that a fo...

Tugas 1 PBO A

This is my biodata compiled in Java (first time usage). Check it out! /** * Program membuat biodata menggunakan Java - BlueJ * * Author : Elkana Hans W * Version 1 , Date : 3 September 2018 */ public class biodata{ public biodata(){ System.out.print("PBO-Tugas1\n"); System.out.print("Nama\t: Elkana Hans Widersen\n"); System.out.print("Kelas\t: PBO A\n"); System.out.print("Alamat Rumah: Merak III/P2-104 Rewwin\n"); System.out.print("Email\t: elknhns@gmail.com\n"); System.out.print("Blog\t: elkanahans.blogspot.com\n"); System.out.print("No HP/WA: 082143646716\n"); System.out.print("Twitter\t: @elknhns\n"); } }

Tugas PBO Psychological Support Chatbot

Hi! We're back again! This time I'm going to show you my recent project on making a chatbot for technical support, called Monokuro Boo. It's great for you if you have traumas, problems, distress, etc. I'm using 3 classes for this chatbot, which are: SupportSystem InputReader Responder These are the source code. Check it out! SupportSystem /** * The main class for the support system, consisting greetings template. * * @author (Elkana Hans Widersen) * @version (08.10.2018) */ public class SupportSystem { private InputReader reader; private Responder responder; /** * Creates a technical support system. */ public SupportSystem() { reader = new InputReader(); responder = new Responder(); } /** * Start the technical support system. This will print a * welcome message and enter into a dialog with the user, * until the user ends the dialo...