Skip to main content

PBO-A Tugas Membuat Rumah

Berikut saya sertakan codingan saya dalam membuat rumah dalam BlueJ.

Saya menggunakan 5 kelas dalam mengerjakannya.
Class yang pertama adalah canvas. Di dalam class canvas ini berisi fungsi fungsi untuk membuat tempat agar bisa ditempati objek-objek. Berikut codingannya:
 import javax.swing.*;   
  import java.awt.*;   
  import java.util.List;   
  import java.util.*;   
  public class Canvas   
  {    
   private static Canvas canvasSingleton;   
    public static final Color hijau_pekat = new Color(0,153,0);  
    public static final Color coklat = new Color(102,51,0);  
    public static final Color abuabu = new Color(153,153,153);  
    public static final Color abuabu_gelap = new Color(51,51,51);  
   public static Canvas getCanvas()   
   {   
    if(canvasSingleton == null) {   
     canvasSingleton = new Canvas("BlueJ Shapes Demo", 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("pink"))   
     graphic.setColor(Color.pink);   
    else if(colorString.equals("white"))   
     graphic.setColor(Color.white);   
    else if(colorString.equals("orange"))  
     graphic.setColor(Color.orange);   
    else if(colorString.equals("hijautua"))  
     graphic.setColor(hijau_pekat);  
    else if(colorString.equals("coklat"))  
     graphic.setColor(coklat);  
    else if(colorString.equals("abuabu"))  
     graphic.setColor(abuabu);  
    else if(colorString.equals("abuabugelap"))  
     graphic.setColor(abuabu_gelap);  
    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);   
    }   
   }   
  }   
Class kedua adalah triangle, class ini berfungsi untuk membuat objek segitiga, berikut codingannya:
 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);   
    }   
   }   
  }   
Class 3 adalah square, class ini berfungsi untuk membuat objek persegi, berikut codingannya:
 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);   
    }   
   }   
  }   
Class 4 adalah circle, class ini berfungsi untuk membuat objek lingkaran. Berikut codingannya:
 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);   
    }   
   }   
  }   
Class 5 adalah recta, class ini berfungsi untuk membuat bangun persegi panjang. Berikut codingannya
 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);   
    }   
   }   
  }   

Dari class-class tersebut saya membentuk beberapa objek, berikut adalah object-object yang saya buat.

Dari object-object tersebut, jadilah rumah dan pemandangannya.

Nama: Bobbi Aditya
NRP : 05111740000099
Kelas : PBO A

Comments

Post a Comment

Popular posts from this blog

MPPL - Pengalaman proyek

Nama : Bobbi Aditya Kelas : MPPL-C NRP : 05111740000099 Berikut ini adalah proyek yang saya ketahui Nama Proyek :   Pembangunan Aplikasi MSO (Maintenance System Online) Berbasis Web Deskripsi : Aplikasi ini berguna untuk pemantauan alat-alat yang ada pada PT. Semen Indonesia - Gresik apabila terjadi kerusakan yang harus segera dilaporkan, atau terdapat jadwal pemeliharaan rutin. Lokasi : PT. Semen Indonesia (Persero) Tbk. - Gresik Jl. Veteran, Gresik Jawa Timur, 61122 Waktu Proyek: 02 - 31 Januari 2019 Tim yang terlibat: Almas Aqmarina 05111640000171 Daniel Kurniawan 0511164000081 Produk yang dihasilkan: Sebuah aplikasi yang berada di dalam server Semen Indonesia yang berguna untuk melakukan pemantuan terhadap alat-alat yang ada pada PT. Semen Indonesia. Untuk addressnya tidak bisa diakses dari luar jaringan PT. Semen Indonesia. Fitur: • Tabel Laporan Harian • Pengelolaan Laporan Harian (tambah, ubah, hapus) • Unduh ...

PWEB-FRAMEWORK

Nama : Bobbi Aditya Kelas : PWEB C NRP : 05111740000099 Dalam kesempatan kali ini, saya melanjutkan tutorial dari petanikode dari tutorial 4 sampai tutorial 6 Dalam tutorial 4, saya belajar untuk melakukan partisi-partisi dalam tampilan index website. Saya melakukan partisi halaman menjadi 7 partisi. Berikut adalah source code dari masing-masing partisi: -head.php <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no"> <title><?php echo SITE_NAME .": ". ucfirst($this->uri->segment(1)) ." - ". ucfirst($this->uri->segment(2)) ?></title> <!-- Bootstrap core CSS--> <link href="<?php echo base_url('assets/bootstrap/css/bootstrap.min.css') ?>" rel="stylesheet"> <!-- Custom fonts for this te...

PBO-FOX and RABBIT

Nama : Bobbi Aditya NRP : 05111740000099 Kelas : PBO A Pada tugas kali ini , saya membuat simulasi fox and rabbit. Berikut saya sertakan design class yang telah saya buat: Berikut saya sertakan kodingan tiap kelas yang saya buat: - Kelas Fox import java.util.List; import java.util.Iterator; import java.util.Random; /** * A simple model of a fox. * Foxes age, move, eat rabbits, and die. * * @author Bobbi Aditya * @version 1.00 */ public class Fox { // Characteristics shared by all foxes (static fields). // The age at which a fox can start to breed. private static final int BREEDING_AGE = 10; // The age to which a fox can live. private static final int MAX_AGE = 150; // The likelihood of a fox breeding. private static final double BREEDING_PROBABILITY = 0.35; // The maximum number of births. private static final int MAX_LITTER_SIZE = 5; // The food value of a single rabbit. In effect, this...