Tugas melukis pemandangan

Nama : Vinsensius Yuda Pratama
NRP : 05111740000156
kelas : PBO B

Membuat pemandangan dengan Blue J dengan class :

  • picture
  • canvas
  • segitiga
  • kotak
  • lingkaran

Source code canvas

 import javax.swing.*;    
  import java.awt.*;    
  import java.util.List;    
  import java.util.*;    
  /**    
  * Membuat canvas   
  *    
  * @author (Vinsensius Yuda Pratama)    
  *    
  */    
  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("orange"))    
    graphic.setColor(Color.orange);   
   else if(colorString.equals("white"))    
    graphic.setColor(Color.white);    
   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("light blue"))    
    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);    
   }    
   }    
  }    

Source code picture
 /**    
  * Membuat Pemandangan   
  * @author (Vinsensius Yuda Pratama)   
  */    
  public class Picture    
  {    
   private Kotak rumah;    
   private Kotak pintu;    
   private Kotak kaca;   
   private Kotak jalan;    
   private lingkaran awan;    
   private segitiga gunung;   
   private segitiga atap;    
   private lingkaran Matahari;    
   private Kotak alas;    
   /**    
   * Constructor for objects of class Picture    
   */    
   public Picture()    
   {    
   // nothing to do... instance variables are automatically set to null    
   }    
   /**    
   * Draw this picture.    
   */    
   public void draw()    
   {   
   alas = new Kotak();    
   alas.changeColor("brown");    
   alas.moveHorizontal(-200);    
   alas.moveVertical(420);    
   alas.changeWidth(1600);    
   alas.changeHeight(1200);    
   alas.makeVisible();    
   Matahari = new lingkaran();    
   Matahari.changeColor("orange");    
   Matahari.moveHorizontal(800);    
   Matahari.moveVertical(80);    
   Matahari.changeSize(150);    
   Matahari.makeVisible();    
   gunung = new segitiga();   
   gunung.changeColor("grey");   
   gunung.moveHorizontal(200);    
   gunung.moveVertical(70);    
   gunung.changeSize(400, 700);    
   gunung.makeVisible();    
   rumah = new Kotak();    
   rumah.changeColor("green");    
   rumah.moveVertical(400);    
   rumah.moveHorizontal(400);    
   rumah.changeWidth(350);    
   rumah.changeHeight(300);    
   rumah.makeVisible();    
   kaca = new Kotak();    
   kaca.changeColor("blue");    
   kaca.moveHorizontal(400);    
   kaca.moveVertical(500);    
   kaca.changeWidth(60);    
   kaca.changeHeight(60);    
   kaca.makeVisible();   
   kaca = new Kotak();    
   kaca.changeColor("blue");    
   kaca.moveHorizontal(450);    
   kaca.moveVertical(500);    
   kaca.changeWidth(60);    
   kaca.changeHeight(60);    
   kaca.makeVisible();    
   pintu = new Kotak();    
   pintu.changeColor("black");    
   pintu.moveHorizontal(400);    
   pintu.moveVertical(500);    
   pintu.changeWidth(50);    
   pintu.changeHeight(100);    
   pintu.makeVisible();    
   jalan = new Kotak();    
   jalan.changeColor("black");    
   jalan.moveHorizontal(100);    
   jalan.moveVertical(435);    
   jalan.changeWidth(100);    
   jalan.changeHeight(300);    
   jalan.makeVisible();    
   atap = new segitiga();    
   atap.changeColor("red");    
   atap.changeSize(100, 500);    
   atap.moveHorizontal(575);    
   atap.moveVertical(350);    
   atap.makeVisible();     
   awan = new lingkaran();    
   awan.changeColor("light blue");    
   awan.moveHorizontal(720);    
   awan.moveVertical(0);    
   awan.changeSize(45);    
   awan.makeVisible();   
   awan = new lingkaran();    
   awan.changeColor("light blue");    
   awan.moveHorizontal(805);    
   awan.moveVertical(0);    
   awan.changeSize(85);    
   awan.makeVisible();   
   awan = new lingkaran();    
   awan.changeColor("light blue");    
   awan.moveHorizontal(750);    
   awan.moveVertical(0);    
   awan.changeSize(60);    
   awan.makeVisible();    
   awan = new lingkaran();    
   awan.changeColor("light blue");    
   awan.moveHorizontal(120);    
   awan.moveVertical(0);    
   awan.changeSize(45);    
   awan.makeVisible();   
   awan = new lingkaran();    
   awan.changeColor("light blue");    
   awan.moveHorizontal(205);    
   awan.moveVertical(0);    
   awan.changeSize(85);    
   awan.makeVisible();   
   awan = new lingkaran();    
   awan.changeColor("light blue");    
   awan.moveHorizontal(150);    
   awan.moveVertical(0);    
   awan.changeSize(60);    
   awan.makeVisible();    
   }    
  }    

Source code lingkaran
 import java.awt.*;    
  import java.awt.geom.*;    
  /**    
  * @author (Vinsenius Yuda Pratama)    
  */    
  public class lingkaran    
  {    
   private int diameter;    
   private int xPosition;    
   private int yPosition;    
   private String color;    
   private boolean isVisible;    
   /**    
   * Create a new lingkaran at default position with default color.    
   */    
   public lingkaran()    
   {    
     diameter = 30;    
     xPosition = 20;    
     yPosition = 60;    
     color = "blue";    
     isVisible = false;    
   }    
   /**    
    * Make this lingkaran visible. If it was already visible, do nothing.    
    */    
   public void makeVisible()    
   {    
     isVisible = true;    
     draw();    
   }    
   /**    
    * Make this lingkaran invisible. If it was already invisible, do nothing.    
    */    
   public void makeInvisible()    
   {    
     erase();    
     isVisible = false;    
   }    
   /**    
   * Move the lingkaran a few pixels to the right.    
   */    
   public void moveRight()    
   {    
     moveHorizontal(20);    
   }    
   /**    
   * Move the lingkaran a few pixels to the left.    
   */    
   public void moveLeft()    
   {    
     moveHorizontal(-20);    
   }    
   /**    
   * Move the lingkaran a few pixels up.    
   */    
   public void moveUp()    
   {    
     moveVertical(-20);    
   }    
   /**    
   * Move the lingkaran a few pixels down.    
   */    
   public void moveDown()    
   {    
     moveVertical(20);    
   }    
   /**    
   * Move the lingkaran horizontally by 'distance' pixels.    
   */    
   public void moveHorizontal(int distance)    
   {    
     erase();    
     xPosition += distance;    
     draw();    
   }    
   /**    
   * Move the lingkaran vertically by 'distance' pixels.    
   */    
   public void moveVertical(int distance)    
   {    
     erase();    
     yPosition += distance;    
     draw();    
   }    
   /**    
   * Slowly move the lingkaran 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 lingkaran 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 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 lingkaran 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);    
     }    
   }    
   /*    
    * Erase the lingkaran on screen.    
    */    
   private void erase()    
   {    
     if(isVisible) {    
      Canvas canvas = Canvas.getCanvas();    
      canvas.erase(this);    
     }    
   }    
  }    

Source code segitiga
 import java.awt.*;    
  /**    
  * @author (Vinsensius Yuda Pratama)    
  */    
  public class segitiga    
  {    
   private int height;    
   private int width;    
   private int xPosition;    
   private int yPosition;    
   private String color;    
   private boolean isVisible;    
   /**    
   * Create a new segitiga at default position with default color.    
   */    
   public segitiga()    
   {    
   height = 30;    
   width = 40;    
   xPosition = 50;    
   yPosition = 15;    
   color = "green";    
   isVisible = false;    
   }    
   /**    
   * Make this segitiga visible. If it was already visible, do nothing.    
   */    
   public void makeVisible()    
   {    
   isVisible = true;    
   draw();    
   }    
   /**    
   * Make this segitiga invisible. If it was already invisible, do nothing.    
   */    
   public void makeInvisible()    
   {    
   erase();    
   isVisible = false;    
   }    
   /**    
   * Move the segitiga a few pixels to the right.    
   */    
   public void moveRight()    
   {    
   moveHorizontal(20);    
   }    
   /**    
   * Move the segitiga a few pixels to the left.    
   */    
   public void moveLeft()    
   {    
   moveHorizontal(-20);    
   }    
   /**    
   * Move the segitiga a few pixels up.    
   */    
   public void moveUp()    
   {    
   moveVertical(-20);    
   }    
   /**    
   * Move the segitiga a few pixels down.    
   */    
   public void moveDown()    
   {    
   moveVertical(20);    
   }    
   /**    
   * Move the segitiga horizontally by 'distance' pixels.    
   */    
   public void moveHorizontal(int distance)    
   {    
   erase();    
   xPosition += distance;    
   draw();    
   }    
   /**    
   * Move the segitiga vertically by 'distance' pixels.    
   */    
   public void moveVertical(int distance)    
   {    
   erase();    
   yPosition += distance;    
   draw();    
   }    
   /**    
   * Slowly move the segitiga 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 segitiga 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 segitiga 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 segitiga on screen.    
   */    
   private void erase()    
   {    
   if(isVisible) {    
    Canvas canvas = Canvas.getCanvas();    
    canvas.erase(this);    
   }    
   }    
  }    

Source code Kotak
 import java.awt.*;    
  /**    
  * @author (Vinsensius Yuda Pratama)    
  * @version (6.3/20180916)    
  */    
  public class Kotak    
  {    
   private int width;    
   private int height;    
   private int xPosition;    
   private int yPosition;    
   private String color;    
   private boolean isVisible;    
   /**    
   * Create a new Kotak at default position with default color.    
   */    
   public Kotak()    
   {    
   width = 30;    
   height = 30;    
   xPosition = 60;    
   yPosition = 50;    
   color = "red";    
   isVisible = false;    
   }    
   /**    
   * Make this Kotak visible. If it was already visible, do nothing.    
   */    
   public void makeVisible()    
   {    
   isVisible = true;    
   draw();    
   }    
   /**    
   * Make this Kotak invisible. If it was already invisible, do nothing.    
   */    
   public void makeInvisible()    
   {    
   erase();    
   isVisible = false;    
   }    
   /**    
   * Move the Kotak a few pixels to the right.    
   */    
   public void moveRight()    
   {    
   moveHorizontal(20);    
   }    
   /**    
   * Move the Kotak a few pixels to the left.    
   */    
   public void moveLeft()    
   {    
   moveHorizontal(-20);    
   }    
   /**    
   * Move the Kotak a few pixels up.    
   */    
   public void moveUp()    
   {    
   moveVertical(-20);    
   }    
   /**    
   * Move the Kotak a few pixels down.    
   */    
   public void moveDown()    
   {    
   moveVertical(20);    
   }    
   /**    
   * Move the Kotak horizontally by 'distance' pixels.    
   */    
   public void moveHorizontal(int distance)    
   {    
   erase();    
   xPosition += distance;    
   draw();    
   }    
   /**    
   * Move the Kotak vertically by 'distance' pixels.    
   */    
   public void moveVertical(int distance)    
   {    
   erase();    
   yPosition += distance;    
   draw();    
   }    
   /**    
   * Slowly move the Kotak 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 Kotak 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 width to the new width (in pixels). Width must be >= 0.    
   */    
   public void changeWidth(int newWidth)    
   {    
   erase();    
   width = newWidth;    
   draw();    
   }    
   /**    
   * Change the height to the new height (in pixels). Height must be >= 0.    
   */    
   public void changeHeight(int newHeight)    
   {    
   erase();    
   height = newHeight;    
   draw();    
   }    
   /**    
   * Change the color. Valid colors are "red", "yellow", "blue", "green",    
   * "magenta" and "black".    
   */    
   public void changeColor(String newColor)    
   {    
   color = newColor;    
   draw();    
   }    
   /*    
   * Draw the Kotak with current specifications on screen.    
   */    
   private void draw()    
   {    
   if(isVisible) {    
    Canvas canvas = Canvas.getCanvas();    
    canvas.draw(this, color, new Rectangle(xPosition, yPosition, width, height));    
    canvas.wait(10);    
   }    
   }    
   /*    
   * Erase the Kotak on screen.    
   */    
   private void erase()    
   {    
   if(isVisible) {    
      Canvas canvas = Canvas.getCanvas();    
      canvas.erase(this);    
     }    
   }    
  }    

Screenshot hasil

Komentar

Postingan populer dari blog ini

Tugas PBO B Membuat DataBase Mahasiswa Sederhana

PBO B : Game PONG