PBO A - Tugas Membuat Rumah
Pada kesempatan kali ini, saya berlatih membuat gambar rumah menggunakan BlueJ.
Nama : Ifta Jihan N
NRP : 05111740000034
PBO-A
Untuk membuat gambar rumah Class yang saya gunakan yaitu:
1. Canvas, sebagai media untuk menampilkan object-object yang telah dibuat.
2. Shape Class, terdapat objek Rectangle, Triangle, Square, dan Circle.
Berikut adalah source codenya
1. Canvas
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.*;
public class Canvas
{
private static Canvas canvasSingleton;
public static final Color skyblue = new Color(135,206,235);
public static final Color sienna = new Color(160,82,45);
public static final Color firebrick = new Color(178, 34, 34);
public static final Color green = new Color(34,139,34);
public static final Color biru = new Color(32, 178, 170);
public static final Color plum = new Color(221, 160, 221);
public static final Color lightyellow = new Color(255, 255, 224);
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("biru"))
graphic.setColor(biru);
else if(colorString.equals("lightyellow"))
graphic.setColor(lightyellow);
else if(colorString.equals("green"))
graphic.setColor(green);
else if(colorString.equals("white"))
graphic.setColor(Color.white);
else if(colorString.equals("firebrick"))
graphic.setColor(firebrick);
else if(colorString.equals("skyblue"))
graphic.setColor(skyblue);
else if(colorString.equals("sienna"))
graphic.setColor(sienna);
else if(colorString.equals("plum"))
graphic.setColor(plum);
else if(colorString.equals("gray"))
graphic.setColor(Color.gray);
else if(colorString.equals("yellow)"))
graphic.setColor(Color.yellow);
else
graphic.setColor(Color.white);
}
public void wait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (Exception e)
{
}
}
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);
}
}
}
2. Rectangle
import java.awt.*;
public class rectangle
{
private int p;
private int l;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
/**
* Create new rectangle
*/
public rectangle()
{
p = 200;
l = 100;
xPosition = 200;
yPosition = 250;
color = "green";
isVisible = false;
}
/**
* Make this rectangle visible
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this rectangle invisible
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Move the rectangle to the right
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the rectangle to the left
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the rectangle up
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the rectangle down
*/
public void moveDown()
{
moveVertical(20);
}
/**
* Move the rectangle horizontally by 'distance' pixels
*/
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
/**
* Move the rectangle vertically by 'distance' pixels
*/
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
/**
* Slowly move the rectangle 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 rectangle 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 newP, int newL)
{
erase();
p = newP;
l = newL;
draw();
}
/**
* Change the color.
*/
public void changeColor(String newColor)
{
color = newColor;
draw();
}
/**
* Draw the rectangle with current specifications on screen.
*/
private void draw()
{
if(isVisible)
{
Canvas canvas = Canvas.getCanvas();
canvas.draw(this, color, new Rectangle(xPosition, yPosition, p, l));
canvas.wait(10);
}
}
/**
* Erase the rectangle on screen
*/
private void erase()
{
if(isVisible)
{
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}
3. Triangle
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 new Triangle
*/
public Triangle()
{
height = 30;
width = 40;
xPosition = 50;
yPosition = 20;
color = "red";
isVisible = false;
}
/**
* Make this Triangle visible
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this Triangle invisible
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Move the Triangle to the right
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the Triangle to the left
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the Triangle up
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the Triangle 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.
*/
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);
}
}
}
4. Circle
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 new circle
*/
public Circle()
{
diameter = 30;
xPosition = 20;
yPosition = 60;
color = "blue";
isVisible = false;
}
/**
* Make this circle visible
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this circle invisible
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Move the circle to the right
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the circle to the left
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the circle up
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the circle 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.
*/
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);
}
}
}
5. Square
import java.awt.*;
public class Square
{
private int size;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
/**
* Create new Square
*/
public Square()
{
size = 100;
xPosition = 200;
yPosition = 250;
color = "yellow";
isVisible = false;
}
/**
* Make this Square visible
*/
public void makeVisible()
{
isVisible = true;
draw();
}
/**
* Make this Square invisible
*/
public void makeInvisible()
{
erase();
isVisible = false;
}
/**
* Move the Square to the right
*/
public void moveRight()
{
moveHorizontal(20);
}
/**
* Move the Square to the left
*/
public void moveLeft()
{
moveHorizontal(-20);
}
/**
* Move the Square up
*/
public void moveUp()
{
moveVertical(-20);
}
/**
* Move the Square 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.
*/
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);
}
}
}
Dari beberapa class tersebut saya membuat beberapa objek sebagai berikut:
Terima kasih.
Comments
Post a Comment