Implementasi Rumah dan Pemandangan dengan menggunakan Blue J


Surabaya, 16 September 2018 

Nama : Isnaini Nurul KurniaSari
Kelas  : PBO B
NRP   : 05111740000010
 

Hai semua. Ini adalah tugas mata kuliah Pemrograman Berbasis Objek implementasi membuat Pemandangan dan rumah dengan menggunakan Blue Java.

            BlueJ merupakan salah satu alat pengembangan bahasa Java yang secara khusus di-design untuk proses pembelajaran java di level perkenalan. Di dalam BlueJ terdapat suatu fitur yang sangat membantu dalam proses pembelajaran yaitu adanya otomatisasi dalam penggambaran class diagram, sehingga pengguna dapat mendapat esensi dari berpikir dalam OO menggunakan bahasa Java.Hai semua. Ini adalah tugas implementasi mata kuliah Pemrograman Berbasis objek.

Dalam pembuatan gambar Rumah dan pemandangan digunakan beberapa library Java sebagai penunjang pemrograman grafis seperti :

  1. import java.util.*
  2. import java.awt.*
  3. import java.util.List
  4. import javax.swing.*
  • Class - class yang digunakan dalam pembuatan gambar Rumah ini terbagi menjadi: 
      1. Library class, bernama Canvas, berfungsi sebagai utility yang memungkinkan untuk memunculkan gambar
      2. Shape class, digunakan untuk mengatur bentuk yang ingin ditampilkan. Terdiri atas:
        1. Circle
        2. Square
        3. Triangle
        4. Box    
  •  Berikut adalah Source Code untuk Canvas:
    import javax.swing.*;
    import java.awt.*;
    import java.util.List;
    import java.util.*;
    
    /**
     * /**
     * Nama : Isnaini Nurul KurniaSari
     * Kelas: PBO B
     * NRP  : 05111740000010
     * (Surabaya, 16 September 2018)
     */
    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, 600, 
                                             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("brown"))
                graphic.setColor(new Color (92,32,6));
            else if(colorString.equals("forest green"))
                graphic.setColor(new Color (34,139,34)); 
            else if(colorString.equals("dark goldenrod"))
                graphic.setColor(new Color (205,149,12));  
            else if(colorString.equals("biru awan"))
                graphic.setColor(new Color (0,191,255));
            else if(colorString.equals("light goldenrod"))
                graphic.setColor(new Color(255, 130, 71 ));
            else if(colorString.equals("warna sawah"))
                graphic.setColor(new Color (154,205,50));
            else if(colorString.equals("grey"))
                graphic.setColor(new Color (139, 134, 134 )); 
            else if(colorString.equals("warna pintu"))
                graphic.setColor(new Color (139,115, 85 )); 
            else if(colorString.equals("rumput tetangga"))
                graphic.setColor(new Color (107, 142, 35));    
            else if(colorString.equals("warna tanah"))
                graphic.setColor(new Color (189, 183, 107 ));
            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);
      }
        }
    
    }
    
    
  • Berikut adalah Source Code untuk Picture:
    
    /**
     * Nama : Isnaini Nurul KurniaSari
     * Kelas: PBO B
     * NRP  : 05111740000010
     * (Surabaya, 16 September 2018)
     */
    public class Picture
    {
        private Box wall;
        private Box window1;
        private Box window2;
        private Box door;
        private Box batang;
        private Box jalan;
        private Box sawah;
        private Box tanah;
        private Box putih;
        private Triangle roof;
        private Circle sun;
        private Circle rumput;
        private Circle awan;
        private Circle danau;
        private Circle pohon;
        private Triangle mountain1;
        private Triangle mountain2;
        
        /**
         * Constructor for objects of class Picture
         */
        public Picture()
        {
            // nothing to do... instance variables are automatically set to null
        }
    
        /**
         * Draw this picture.
         */
        public void draw()
        {
            sawah = new Box();
            sawah.changeColor("warna sawah");
            sawah.moveVertical(180);
            sawah.moveHorizontal(500);
            sawah.changeWidth(1000);
            sawah.changeHeight(250);
            sawah.makeVisible();
           
            tanah = new Box();
            tanah.changeColor("warna sawah");
            tanah.moveVertical(400);
            tanah.moveHorizontal(500);
            tanah.changeWidth(1500);
            tanah.changeHeight(500);
            tanah.makeVisible();
            
            tanah = new Box();
            tanah.changeColor("warna tanah");
            tanah.moveVertical(180);
            tanah.moveHorizontal(-60);
            tanah.changeWidth(500);
            tanah.changeHeight(1500);
            tanah.makeVisible();
            
            wall = new Box();
            wall.moveVertical(350);
            wall.changeColor("light goldenrod");
            wall.changeWidth(150);
            wall.changeHeight(150);
            wall.makeVisible();
            
            door = new Box();
            door.changeColor("warna pintu");
            door.moveVertical(430);
            door.moveHorizontal(50);
            door.changeWidth(40);
            door.changeHeight(70);
            door.makeVisible();
            
            batang = new Box();
            batang.changeColor("brown");
            batang.moveVertical(350);
            batang.moveHorizontal(600);
            batang.changeWidth(70);
            batang.changeHeight(400);
            batang.makeVisible();
      
            batang = new Box();
            batang.changeColor("brown");
            batang.moveVertical(350);
            batang.moveHorizontal(900);
            batang.changeWidth(50);
            batang.changeHeight(200);
            batang.makeVisible();
            
            batang = new Box();
            batang.changeColor("brown");
            batang.moveVertical(320);
            batang.moveHorizontal(265);
            batang.changeWidth(60);
            batang.changeHeight(470);
            batang.makeVisible();
            
            pohon = new Circle();
            pohon.changeColor("green");
            pohon.moveHorizontal(290);
            pohon.moveVertical(260);
            pohon.changeSize(90);
            pohon.makeVisible();
            
            pohon = new Circle();
            pohon.changeColor("green");
            pohon.moveHorizontal(250);
            pohon.moveVertical(260);
            pohon.changeSize(60);
            pohon.makeVisible();
            
            pohon = new Circle();
            pohon.changeColor("green");
            pohon.moveHorizontal(360);
            pohon.moveVertical(270);
            pohon.changeSize(60);
            pohon.makeVisible();
           
            pohon = new Circle();
            pohon.changeColor("green");
            pohon.moveHorizontal(360);
            pohon.moveVertical(270);
            pohon.changeSize(60);
            pohon.makeVisible();
            
            pohon = new Circle();
            pohon.changeColor("green");
            pohon.moveHorizontal(294);
            pohon.moveVertical(230);
            pohon.changeSize(60);
            pohon.makeVisible();
            
            pohon = new Circle();
            pohon.changeColor("green");
            pohon.moveHorizontal(270);
            pohon.moveVertical(230);
            pohon.changeSize(40);
            pohon.makeVisible();
            
            pohon = new Circle();
            pohon.changeColor("green");
            pohon.moveHorizontal(345);
            pohon.moveVertical(240);
            pohon.changeSize(50);
            pohon.makeVisible();
            
            pohon = new Circle();
            pohon.changeColor("green");
            pohon.moveHorizontal(345);
            pohon.moveVertical(300);
            pohon.changeSize(50);
            pohon.makeVisible();
            
            pohon = new Circle();
            pohon.changeColor("green");
            pohon.moveHorizontal(270);
            pohon.moveVertical(300);
            pohon.changeSize(50);
            pohon.makeVisible();
            
            sun = new Circle();
            sun.changeColor("forest green");
            sun.moveHorizontal(910);
            sun.moveVertical(260);
            sun.changeSize(110);
            sun.makeVisible();
           
            sun = new Circle();
            sun.changeColor("forest green");
            sun.moveHorizontal(610);
            sun.moveVertical(240);
            sun.changeSize(120);
            sun.makeVisible();
            
            window1 = new Box();
            window1.changeColor("black");
            window1.moveHorizontal(20);
            window1.moveVertical(375);
            window1.makeVisible();
            
            window2 = new Box();
            window2.changeColor("black");
            window2.moveHorizontal(100);
            window2.moveVertical(375);
            window2.makeVisible();
           
            roof = new Triangle();
            roof.changeColor("blue");
            roof.changeSize(85, 215);
            roof.moveHorizontal(85);
            roof.moveVertical(300);
            roof.makeVisible();
             
            rumput = new Circle();
            rumput.changeColor("green");
            rumput.moveHorizontal(780);
            rumput.moveVertical(500);
            rumput.changeSize(65);
            rumput.makeVisible();
            
            rumput = new Circle();
            rumput.changeColor("green");
            rumput.moveHorizontal(650);
            rumput.moveVertical(500);
            rumput.changeSize(65);
            rumput.makeVisible();
          
            rumput = new Circle();
            rumput.changeColor("green");
            rumput.moveHorizontal(700);
            rumput.moveVertical(470);
            rumput.changeSize(100);
            rumput.makeVisible();
           
            rumput = new Circle();
            rumput.changeColor("rumput tetangga");
            rumput.moveHorizontal(240);
            rumput.moveVertical(500);
            rumput.changeSize(65);
            rumput.makeVisible();
            
            rumput = new Circle();
            rumput.changeColor("rumput tetangga");
            rumput.moveHorizontal(110);
            rumput.moveVertical(500);
            rumput.changeSize(65);
            rumput.makeVisible();
          
            rumput = new Circle();
            rumput.changeColor("rumput tetangga");
            rumput.moveHorizontal(150);
            rumput.moveVertical(470);
            rumput.changeSize(100);
            rumput.makeVisible();
            
            rumput = new Circle();
            rumput.changeColor("rumput tetangga");
            rumput.moveHorizontal(300);
            rumput.moveVertical(470);
            rumput.changeSize(100);
            rumput.makeVisible();
            
            rumput = new Circle();
            rumput.changeColor("rumput tetangga");
            rumput.moveHorizontal(360);
            rumput.moveVertical(500);
            rumput.changeSize(65);
            rumput.makeVisible();
            
            rumput = new Circle();
            rumput.changeColor("rumput tetangga");
            rumput.moveHorizontal(50);
            rumput.moveVertical(500);
            rumput.changeSize(65);
            rumput.makeVisible();
            
            rumput = new Circle();
            rumput.changeColor("rumput tetangga");
            rumput.moveHorizontal(10);
            rumput.moveVertical(520);
            rumput.changeSize(65);
            rumput.makeVisible();
            
            rumput = new Circle();
            rumput.changeColor("rumput tetangga");
            rumput.moveHorizontal(-20);
            rumput.moveVertical(490);
            rumput.changeSize(65);
            rumput.makeVisible();
            
            tanah = new Box();
            tanah.changeColor("biru awan");
            tanah.moveVertical(-40);
            tanah.moveHorizontal(-60);
            tanah.changeWidth(1000);
            tanah.changeHeight(200);
            tanah.makeVisible();
                  
            sun = new Circle();
            sun.changeColor("white");
            sun.moveHorizontal(20);
            sun.moveVertical(-110);
            sun.changeSize(120);
            sun.makeVisible();
            
            sun = new Circle();
            sun.changeColor("white");
            sun.moveHorizontal(-20);
            sun.moveVertical(-80);
            sun.changeSize(60);
            sun.makeVisible();
            
            sun = new Circle();
            sun.changeColor("white");
            sun.moveHorizontal(120);
            sun.moveVertical(-80);
            sun.changeSize(60);
            sun.makeVisible();
           
            sun = new Circle();
            sun.changeColor("white");
            sun.moveHorizontal(170);
            sun.moveVertical(-80);
            sun.changeSize(60);
            sun.makeVisible();
            
            sun = new Circle();
            sun.changeColor("white");
            sun.moveHorizontal(280);
            sun.moveVertical(-90);
            sun.changeSize(100);
            sun.makeVisible();
           
            sun = new Circle();
            sun.changeColor("white");
            sun.moveHorizontal(230);
            sun.moveVertical(-90);
            sun.changeSize(60);
            sun.makeVisible();
            
            sun = new Circle();
            sun.changeColor("white");
            sun.moveHorizontal(370);
            sun.moveVertical(-90);
            sun.changeSize(60);
            sun.makeVisible();
            
            sun = new Circle();
            sun.changeColor("white");
            sun.moveHorizontal(550);
            sun.moveVertical(-90);
            sun.changeSize(60);
            sun.makeVisible();
            
            sun = new Circle();
            sun.changeColor("white");
            sun.moveHorizontal(700);
            sun.moveVertical(-90);
            sun.changeSize(60);
            sun.makeVisible();
            
            sun = new Circle();
            sun.changeColor("white");
            sun.moveHorizontal(605);
            sun.moveVertical(-90);
            sun.changeSize(100);
            sun.makeVisible();
            
            sun = new Circle();
            sun.changeColor("white");
            sun.moveHorizontal(425);
            sun.moveVertical(-130);
            sun.changeSize(130);
            sun.makeVisible();
            
            sun = new Circle();
            sun.changeColor("white");
            sun.moveHorizontal(850);
            sun.moveVertical(-90);
            sun.changeSize(60);
            sun.makeVisible();
            
            sun = new Circle();
            sun.changeColor("white");
            sun.moveHorizontal(755);
            sun.moveVertical(-90);
            sun.changeSize(100);
            sun.makeVisible();
           
            sun = new Circle();
            sun.changeColor("white");
            sun.moveHorizontal(900);
            sun.moveVertical(-90);
            sun.changeSize(100);
            sun.makeVisible();
            
            sun = new Circle();
            sun.changeColor("yellow");
            sun.moveHorizontal(440);
            sun.moveVertical(100);
            sun.changeSize(100);
            sun.makeVisible();
            
            jalan = new Box();
            jalan.changeColor("grey");
            jalan.moveVertical(180);
            jalan.moveHorizontal(380);
            jalan.changeWidth(120);
            jalan.changeHeight(600);
            jalan.makeVisible();
            
            mountain1 = new Triangle();
            mountain1.changeColor("dark goldenrod");
            mountain1.changeSize(150,600);
            mountain1.moveHorizontal(200);      
            mountain1.moveVertical(70);
            mountain1.makeVisible();
            
            mountain2 = new Triangle();
            mountain2.changeColor("dark goldenrod");
            mountain2.changeSize(150,600);
            mountain2.moveHorizontal(700);      
            mountain2.moveVertical(70);
            mountain2.makeVisible();
            
            putih = new Box();
            putih.changeColor("white");
            putih.moveVertical(220);
            putih.moveHorizontal(430);
            putih.changeWidth(20);
            putih.changeHeight(60);
            putih.makeVisible();
            
            putih = new Box();
            putih.changeColor("white");
            putih.moveVertical(350);
            putih.moveHorizontal(430);
            putih.changeWidth(20);
            putih.changeHeight(60);
            putih.makeVisible();
            
            putih = new Box();
            putih.changeColor("white");
            putih.moveVertical(470);
            putih.moveHorizontal(430);
            putih.changeWidth(20);
            putih.changeHeight(70);
            putih.makeVisible();
           
        }
    
        /**
         * Change this picture to black/white display
         */
        public void setBlackAndWhite()
        {
            if(wall != null)   // only if it's painted already...
            {
                wall.changeColor("black");
                window1.changeColor("white");
                window2.changeColor("white");
                mountain1.changeColor("black");
                mountain2.changeColor("black");
                roof.changeColor("black");
                sun.changeColor("black");
            }
        }
    
        /**
         * Change this picture to use color display
         */
        public void setColor()
        {
            if(wall != null)   // only if it's painted already...
            {
                wall.changeColor("red");
                window1.changeColor("black");
                window2.changeColor("black");
                mountain1.changeColor("green");
                mountain2.changeColor("green");
                roof.changeColor("blue");
                sun.changeColor("yellow");
            }
        }
    
    }
    
     
  • Berikut adalah Source Code untuk Circle:
    import java.awt.*;
    import java.awt.geom.*;
    
    /**
     * Nama : Isnaini Nurul KurniaSari
     * Kelas: PBO B
     * NRP  : 05111740000010
     * (Surabaya, 16 September 2018)
     */
    
    public class Circle
    {
        private int diameter;
     private int xPosition;
     private int yPosition;
     private String color;
     private boolean isVisible;
     
        /**
         * Create a new circle at default position with default color.
         */
        public Circle()
        {
      diameter = 30;
      xPosition = 20;
      yPosition = 60;
      color = "blue";
      isVisible = false;
        }
    
     /**
      * Make this circle visible. If it was already visible, do nothing.
      */
     public void makeVisible()
     {
      isVisible = true;
      draw();
     }
     
     /**
      * Make this circle invisible. If it was already invisible, do nothing.
      */
     public void makeInvisible()
     {
      erase();
      isVisible = false;
     }
     
        /**
         * Move the circle a few pixels to the right.
         */
        public void moveRight()
        {
      moveHorizontal(20);
        }
    
        /**
         * Move the circle a few pixels to the left.
         */
        public void moveLeft()
        {
      moveHorizontal(-20);
        }
    
        /**
         * Move the circle a few pixels up.
         */
        public void moveUp()
        {
      moveVertical(-20);
        }
    
        /**
         * Move the circle a few pixels down.
         */
        public void moveDown()
        {
      moveVertical(20);
        }
    
        /**
         * Move the circle horizontally by 'distance' pixels.
         */
        public void moveHorizontal(int distance)
        {
      erase();
      xPosition += distance;
      draw();
        }
    
        /**
         * Move the circle vertically by 'distance' pixels.
         */
        public void moveVertical(int distance)
        {
      erase();
      yPosition += distance;
      draw();
        }
    
        /**
         * Slowly move the circle 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 circle 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 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);
      }
     }
    
     /*
      * Erase the circle on screen.
      */
     private void erase()
     {
      if(isVisible) {
       Canvas canvas = Canvas.getCanvas();
       canvas.erase(this);
      }
     }
    }
     
    Berikut adalah Source Code untuk Box:
    import java.awt.*;
    
    /**
     * Nama : Isnaini Nurul KurniaSari
     * Kelas: PBO B
     * NRP  : 05111740000010
     * (Surabaya, 16 September 2018)
     */
    
    public class Box
    {
        private int width;
        private int height;
     private int xPosition;
     private int yPosition;
     private String color;
     private boolean isVisible;
    
        /**
         * Create a new Box at default position with default color.
         */
        public Box()
        {
      width = 30;
      height = 30;
      xPosition = 60;
      yPosition = 50;
      color = "red";
      isVisible = false;
        }
    
     /**
      * Make this Box visible. If it was already visible, do nothing.
      */
     public void makeVisible()
     {
      isVisible = true;
      draw();
     }
     
     /**
      * Make this Box invisible. If it was already invisible, do nothing.
      */
     public void makeInvisible()
     {
      erase();
      isVisible = false;
     }
     
        /**
         * Move the Box a few pixels to the right.
         */
        public void moveRight()
        {
      moveHorizontal(20);
        }
    
        /**
         * Move the Box a few pixels to the left.
         */
        public void moveLeft()
        {
      moveHorizontal(-20);
        }
    
        /**
         * Move the Box a few pixels up.
         */
        public void moveUp()
        {
      moveVertical(-20);
        }
    
        /**
         * Move the Box a few pixels down.
         */
        public void moveDown()
        {
      moveVertical(20);
        }
    
        /**
         * Move the Box horizontally by 'distance' pixels.
         */
        public void moveHorizontal(int distance)
        {
      erase();
      xPosition += distance;
      draw();
        }
    
        /**
         * Move the Box vertically by 'distance' pixels.
         */
        public void moveVertical(int distance)
        {
      erase();
      yPosition += distance;
      draw();
        }
    
        /**
         * Slowly move the Box 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 Box 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). Size must be >= 0.
         */
        public void changeWidth(int newWidth)
        {
      erase();
      width = newWidth;
      draw();
        }
        
        /**
         * Change the height to the new height (in pixels). Size 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 Box 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 Box on screen.
      */
     private void erase()
     {
      if(isVisible) {
       Canvas canvas = Canvas.getCanvas();
       canvas.erase(this);
      }
     }
    }
     
     
     Berikut adalah Source Code untuk Triangle:
    import java.awt.*;
    
    /**
     * Nama : Isnaini Nurul KurniaSari
     * Kelas: PBO B
     * NRP  : 05111740000010
     * (Surabaya, 16 September 2018)
     */
    
    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 = 30;
      width = 40;
      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(20);
        }
    
        /**
         * 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(-20);
        }
    
        /**
         * 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);
      }
     }
    }
     
    Berikut ini adalah Hasilnya setelah di compile: 
     

Comments

Popular posts from this blog

Database Akademik

The Foxes and Rabbits Simulator