Tugas Rumah 2 Pemrograman Berorientasi Objek
Tugas 2 PBO - Membuat Gambar Rumah
Nama: Donny Fitrado
NRP: 05111740000171
Kelas: PBO A
Projek ini memakai 5 class:
1. Canvas
2. Triangle
3. Circle
4. Square
5. Picture
1. Canvas
2. Triangle
3. Circle
4. Square
5. Picture
Berikut ini adalah gambarnya:
Terima kasih dan mohon maaf apabila ada kesalahan.
Nama: Donny Fitrado
NRP: 05111740000171
Kelas: PBO A
Projek ini memakai 5 class:
1. Canvas
2. Triangle
3. Circle
4. Square
5. Picture
1. Canvas
/**
* Write a description of class Canvas here.
*
* Donny Fitrado
* 1.1
*/
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.*;
public class Canvas
{
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", 900, 700,
Color.blue);
}
canvasSingleton.setVisible(true);
return canvasSingleton;
}
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
*/
public void draw(Object referenceObject, String color, Shape shape)
{
objects.remove(referenceObject);
objects.add(referenceObject);
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);
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
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)
{
// diignore
}
}
/**
* 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);
}
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);
}
}
}
2. Triangle
/**
* Write a description of class Triangle here.
*
* Donny Fitrado
* 1.1
*/
import java.awt.*;
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);
}
}
}
3. Circle
/**
* Write a description of class Circle here.
*
* @author (your name)
* @version (a version number or a date)
*/
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;
/**
* Create a new circle at default position with default color.
*/
public Circle()
{
diameter = 35;
xPosition = -170;
yPosition = 50;
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);
}
}
}
4. Square
/**
* Write a description of class Square here.
*
* Donny Fitrado
* 1.1
*/
import java.awt.*;
public class Square
{
private int size;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
private int width;
private int height;
/**
* Create a new square at default position with default color.
*/
public Square()
{
size = 30;
xPosition = 60;
yPosition = 50;
color = "blue";
isVisible = false;
}
/**
* Make this square visible. If it was already visible, do nothing.
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this square invisible. If it was already invisible, do nothing.
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Move the square a few pixels to the right.
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the square a few pixels to the left.
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the square a few pixels up.
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the square a few pixels down.
*/
public void moveDown()
{
moveVertical(20);
}
/**
* Move the square horizontally by 'distance' pixels.
*/
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
/**
* Move the square vertically by 'distance' pixels.
*/
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
/**
* Slowly move the square 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 square 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 newSize)
{
erase();
size = newSize;
draw();
}
/**
* Change the color. Valid colors are "red", "yellow", "blue", "green",
* "magenta" and "black".
*/
public void changeColor(String newColor)
{
color = newColor;
draw();
}
/*
* Draw the square with current specifications on screen.
*/
private void draw()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.draw(this, color,
new Rectangle(xPosition, yPosition, size, size));
canvas.wait(10);
}
}
/*
* Erase the square on screen.
*/
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}
5. Picture
/**
* Write a description of class Picture here.
*
* Donny Fitrado
* 1.1
*/
public class Picture
{
private Square wall;
private Square window;
private Square door;
private Square pohon;
private Triangle roof;
private Circle sun;
private Circle tanah;
private Circle tanahh;
private Circle awan1;
private Circle daun;
/**
* Constructor for objects of class Picture
*/
public Picture()
{
//tidak ada
}
/**
* Draw this picture.
*/
public void draw()
{
tanahh = new Circle();
tanahh.changeColor("green");
tanahh.moveHorizontal(480);
tanahh.moveVertical(420);
tanahh.changeSize(750);
tanahh.makeVisible();
tanah = new Circle();
tanah.changeColor("green");
tanah.moveHorizontal(60);
tanah.moveVertical(420);
tanah.changeSize(500);
tanah.makeVisible();
wall = new Square();
wall.moveVertical(300);
wall.changeSize(150);
wall.makeVisible();
wall.changeColor("red");
door = new Square();
door.changeColor("white");
door.moveHorizontal(60);
door.moveVertical(400);
door.makeVisible();
door = new Square();
door.changeColor("white");
door.moveHorizontal(60);
door.moveVertical(420);
door.makeVisible();
window = new Square();
window.changeColor("white");
window.moveHorizontal(110);
window.moveVertical(330);
window.makeVisible();
window = new Square();
window.changeColor("white");
window.moveHorizontal(10);
window.moveVertical(330);
window.makeVisible();
roof = new Triangle();
roof.changeColor("grey");
roof.changeSize(70, 180);
roof.moveHorizontal(85);
roof.moveVertical(275);
roof.makeVisible();
sun = new Circle();
sun.changeColor("yellow");
sun.moveHorizontal(130);
sun.moveVertical(-80);
sun.changeSize(150);
sun.makeVisible();
awan1 = new Circle();
awan1.changeColor("white");
awan1.moveHorizontal(700);
awan1.moveVertical(70);
awan1.changeSize(90);
awan1.makeVisible();
awan1 = new Circle();
awan1.changeColor("white");
awan1.moveHorizontal(760);
awan1.moveVertical(85);
awan1.changeSize(55);
awan1.makeVisible();
awan1 = new Circle();
awan1.changeColor("white");
awan1.moveHorizontal(670);
awan1.moveVertical(85);
awan1.changeSize(55);
awan1.makeVisible();
awan1 = new Circle();
awan1.changeColor("white");
awan1.moveHorizontal(400);
awan1.moveVertical(40);
awan1.changeSize(90);
awan1.makeVisible();
awan1 = new Circle();
awan1.changeColor("white");
awan1.moveHorizontal(460);
awan1.moveVertical(55);
awan1.changeSize(55);
awan1.makeVisible();
awan1 = new Circle();
awan1.changeColor("white");
awan1.moveHorizontal(370);
awan1.moveVertical(55);
awan1.changeSize(55);
awan1.makeVisible();
awan1 = new Circle();
awan1.changeColor("white");
awan1.moveHorizontal(900);
awan1.moveVertical(70);
awan1.changeSize(90);
awan1.makeVisible();
awan1 = new Circle();
awan1.changeColor("white");
awan1.moveHorizontal(960);
awan1.moveVertical(85);
awan1.changeSize(55);
awan1.makeVisible();
awan1 = new Circle();
awan1.changeColor("white");
awan1.moveHorizontal(870);
awan1.moveVertical(85);
awan1.changeSize(55);
awan1.makeVisible();
pohon = new Square();
pohon.changeColor("brown");
pohon.moveHorizontal(500);
pohon.moveVertical(450);
pohon.makeVisible();
pohon = new Square();
pohon.changeColor("#D2691E");
pohon.moveHorizontal(500);
pohon.moveVertical(430);
pohon.makeVisible();
pohon = new Square();
pohon.changeColor("#D2691E");
pohon.moveHorizontal(500);
pohon.moveVertical(410);
pohon.makeVisible();
pohon = new Square();
pohon.changeColor("#D2691E");
pohon.moveHorizontal(500);
pohon.moveVertical(390);
pohon.makeVisible();
pohon = new Square();
pohon.changeColor("#D2691E");
pohon.moveHorizontal(500);
pohon.moveVertical(370);
pohon.makeVisible();
daun = new Circle();
daun.changeColor("green");
daun.moveHorizontal(700);
daun.moveVertical(320);
daun.changeSize(100);
daun.makeVisible();
daun = new Circle();
daun.changeColor("green");
daun.moveHorizontal(730);
daun.moveVertical(290);
daun.changeSize(80);
daun.makeVisible();
daun = new Circle();
daun.changeColor("green");
daun.moveHorizontal(670);
daun.moveVertical(290);
daun.changeSize(80);
daun.makeVisible();
pohon = new Square();
pohon.changeColor("#964B00");
pohon.moveHorizontal(700);
pohon.moveVertical(370);
pohon.makeVisible();
pohon = new Square();
pohon.changeColor("#964B00");
pohon.moveHorizontal(700);
pohon.moveVertical(390);
pohon.makeVisible();
pohon = new Square();
pohon.changeColor("#964B00");
pohon.moveHorizontal(700);
pohon.moveVertical(410);
pohon.makeVisible();
pohon = new Square();
pohon.changeColor("#964B00");
pohon.moveHorizontal(700);
pohon.moveVertical(430);
pohon.makeVisible();
roof = new Triangle();
roof.changeColor("green");
roof.changeSize(120, 50);
roof.moveHorizontal(723);
roof.moveVertical(300);
roof.makeVisible();
pohon = new Square();
pohon.changeColor("#964B00");
pohon.moveHorizontal(620);
pohon.moveVertical(370);
pohon.makeVisible();
pohon = new Square();
pohon.changeColor("#964B00");
pohon.moveHorizontal(620);
pohon.moveVertical(390);
pohon.makeVisible();
pohon = new Square();
pohon.changeColor("#964B00");
pohon.moveHorizontal(620);
pohon.moveVertical(410);
pohon.makeVisible();
pohon = new Square();
pohon.changeColor("#964B00");
pohon.moveHorizontal(620);
pohon.moveVertical(430);
pohon.makeVisible();
roof = new Triangle();
roof.changeColor("green");
roof.changeSize(120, 50);
roof.moveHorizontal(645);
roof.moveVertical(300);
roof.makeVisible();
}
/**
* Change this picture to black/white display
*/
public void setBlackAndWhite()
{
if(wall != null)
{
wall.changeColor("black");
window.changeColor("white");
roof.changeColor("black");
sun.changeColor("black");
}
}
/**
* Change this picture to use color display
*/
public void setColor()
{
if(wall != null)
{
wall.changeColor("red");
window.changeColor("black");
roof.changeColor("green");
sun.changeColor("yellow");
}
}
}
Berikut ini adalah gambarnya:
Terima kasih dan mohon maaf apabila ada kesalahan.
Comments
Post a Comment