Visualisasi Java - Membuat Rumah
Jadi kali ini, saya membuat sebuah visualisasi dari program java. Dengan memanfaatkan materi Pemrograman Berbasis Objek, saya dapat memvisualisasikan bangun 2 dimensi pada tugas lalu menjadi bentuk sebuah rumah. Kali ini saya masih menggunakan BlueJ sebagai compiler Javanya. Berikut hasil gambar rumah saya, beserta kode dari programnya.
Visualisasi gambar
Canvas / Background
Bentuk Jajar Genjang
Bentuk Persegi Panjang
Bentuk Segitiga
Bentuk Persegi / Bujur Sangkar
Bentuk Lingkaran
Terimakasih, semoga bermanfaat bagi yang membaca.
Visualisasi gambar
/**
* Pictures sebagai visualisasi.
*
* by : Ahmad Yahya Abdul Aziz
*/
public class Pictures
{
private PersegiPanjang wall;
private PersegiPanjang fence;
private PersegiPanjang pfence1;
private PersegiPanjang pfence2;
private PersegiPanjang tree;
private PersegiPanjang g;
private PersegiPanjang wallg2;
private PersegiPanjang window1;
private PersegiPanjang window2;
private PersegiPanjang window3;
private PersegiPanjang door;
private JajarGenjang roof1;
private Segitiga mroof;
private Lingkaran lfence1;
private Lingkaran lfence2;
private Lingkaran sun;
private Lingkaran leaf1;
private Lingkaran leaf2;
private Lingkaran leaf3;
private Lingkaran leaf4;
public void draw(){
mroof = new Segitiga();
mroof.moveHorizontal(425);
mroof.moveVertical(0);
mroof.changeSize(150,300);
mroof.changeColor("brown");
mroof.makeVisible();
roof1 = new JajarGenjang();
roof1.moveHorizontal(450);
roof1.moveVertical(250);
roof1.changeSize(100,350,true);
roof1.changeColor("brown");
roof1.makeVisible();
wall = new PersegiPanjang();
wall.moveHorizontal(300);
wall.moveVertical(350);
wall.changeSize(250,500);
wall.changeColor("green");
wall.makeVisible();
wallg2 = new PersegiPanjang();
wallg2.moveHorizontal(300);
wallg2.moveVertical(150);
wallg2.changeColor("green");
wallg2.changeSize(200,250);
wallg2.makeVisible();
g = new PersegiPanjang();
g.moveHorizontal(290);
g.moveVertical(340);
g.changeColor("brown");
g.changeSize(10,260);
g.makeVisible();
door = new PersegiPanjang();
door.moveHorizontal(500);
door.moveVertical(450);
door.changeSize(150,100);
door.changeColor("brown");
door.makeVisible();
window1 = new PersegiPanjang();
window1.moveHorizontal(390);
window1.moveVertical(200);
window1.changeSize(100,75);
window1.makeVisible();
window2 = new PersegiPanjang();
window2.moveHorizontal(390);
window2.moveVertical(390);
window2.changeSize(100,75);
window2.makeVisible();
window3 = new PersegiPanjang();
window3.moveHorizontal(670);
window3.moveVertical(390);
window3.changeSize(100,75);
window3.makeVisible();
fence = new PersegiPanjang();
fence.moveHorizontal(150);
fence.moveVertical(500);
fence.changeSize(150,800);
fence.changeColor("red");
fence.makeVisible();
pfence1 = new PersegiPanjang();
pfence1.moveHorizontal(100);
pfence1.moveVertical(490);
pfence1.changeSize(160,100);
pfence1.changeColor("red");
pfence1.makeVisible();
pfence2 = new PersegiPanjang();
pfence2.moveHorizontal(900);
pfence2.moveVertical(490);
pfence2.changeSize(160,100);
pfence2.changeColor("red");
pfence2.makeVisible();
tree = new PersegiPanjang();
tree.moveHorizontal(1100);
tree.moveVertical(250);
tree.changeSize(400,60);
tree.changeColor("brown");
tree.makeVisible();
sun = new Lingkaran();
sun.changeSize(200);
sun.changeColor("yellow");
sun.makeVisible();
leaf1 = new Lingkaran();
leaf1.changeSize(125);
leaf1.moveHorizontal(1000);
leaf1.moveVertical(250);
leaf1.changeColor("green");
leaf1.makeVisible();
leaf2 = new Lingkaran();
leaf2.changeSize(175);
leaf2.moveHorizontal(1085);
leaf2.moveVertical(150);
leaf2.changeColor("green");
leaf2.makeVisible();
leaf3 = new Lingkaran();
leaf3.changeSize(125);
leaf3.moveHorizontal(1130);
leaf3.moveVertical(250);
leaf3.changeColor("green");
leaf3.makeVisible();
leaf4 = new Lingkaran();
leaf4.changeSize(225);
leaf4.moveHorizontal(970);
leaf4.moveVertical(80);
leaf4.changeColor("green");
leaf4.makeVisible();
lfence1 = new Lingkaran();
lfence1.changeSize(100);
lfence1.moveHorizontal(100);
lfence1.moveVertical(390);
lfence1.changeColor("blue");
lfence1.makeVisible();
lfence1 = new Lingkaran();
lfence1.changeSize(100);
lfence1.moveHorizontal(900);
lfence1.moveVertical(390);
lfence1.changeColor("blue");
lfence1.makeVisible();
}
}
Canvas / Background
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.*;
import java.awt.Color;
/**
* Write a description of class Canvas here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Canvas
{
// instance variables - replace the example below with your own
private static Canvas canvasSingleton;
public static final Color brown = new Color(102,51,0);
/**
* Constructor for objects of class Canvas
*/
public static Canvas getCanvas()
{
if(canvasSingleton == null) {
canvasSingleton = new Canvas("Membuat Rumah", 1336, 768,
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;
/**
* An example of a method - replace this comment with your own
*
* @param
*
*/
public 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) {
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);
shapes.remove(referenceObject);
redraw();
}
public void setForegroundColor(String colorString)
{
if(colorString.equals("red"))
graphic.setColor(Color.red);
else if(colorString.equals("black"))
graphic.setColor(Color.black);
else if(colorString.equals("blue"))
graphic.setColor(Color.blue);
else if(colorString.equals("yellow"))
graphic.setColor(Color.yellow);
else if(colorString.equals("green"))
graphic.setColor(Color.green);
else if(colorString.equals("magenta"))
graphic.setColor(Color.magenta);
else if(colorString.equals("white"))
graphic.setColor(Color.white);
else if(colorString.equals("brown"))
graphic.setColor(new Color(139,69,19));
else
graphic.setColor(Color.black);
}
public void wait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (Exception e)
{
// ignoring exception at the moment
}
}
private void redraw()
{
erase();
for(Iterator i=objects.iterator(); i.hasNext(); ) {
((ShapeDescription)shapes.get(i.next())).draw(graphic);
}
canvas.repaint();
}
private void erase()
{
Color original = graphic.getColor();
graphic.setColor(backgroundColour);
Dimension size = canvas.getSize();
graphic.fill(new Rectangle(0, 0, size.width, size.height));
graphic.setColor(original);
}
private class CanvasPane extends JPanel
{
public void paint(Graphics g)
{
g.drawImage(canvasImage, 0, 0, null);
}
}
private class ShapeDescription
{
private Shape shape;
private String colorString;
public ShapeDescription(Shape shape, String color)
{
this.shape = shape;
colorString = color;
}
public void draw(Graphics2D graphic)
{
setForegroundColor(colorString);
graphic.fill(shape);
}
}
}
Bentuk Jajar Genjang
import java.awt.*;
/**
* Write a description of class JajarGenjang here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class JajarGenjang
{
// instance variables - replace the example below with your own
private int height;
private int width;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
private boolean hipo;
public JajarGenjang()
{
height = 30;
width = 40;
xPosition = 40;
yPosition = 40;
color = "red";
isVisible = false;
hipo=true;
}
public void makeVisible()
{
isVisible = true;
draw();
}
public void makeInvisible()
{
erase();
isVisible = false;
}
public void moveRight()
{
moveHorizontal(10);
}
public void moveLeft()
{
moveHorizontal(-10);
}
public void moveUp()
{
moveVertical(-10);
}
public void moveDown()
{
moveVertical(10);
}
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
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();
}
}
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();
}
}
public void changeSize(int newHeight, int newWidth, boolean newHipo)
{
erase();
height = newHeight;
width = newWidth;
hipo = newHipo;
draw();
}
public void changeColor(String newColor)
{
color = newColor;
draw();
}
private void draw()
{
if(isVisible) {
if(hipo){
Canvas canvas = Canvas.getCanvas();
int[] xpoints = { xPosition, xPosition + width, xPosition + height/2 +width, xPosition + height/2};
int[] ypoints = { yPosition, yPosition, yPosition + height, yPosition + height };
canvas.draw(this, color, new Polygon(xpoints, ypoints, 4));
canvas.wait(10);
}else {
Canvas canvas = Canvas.getCanvas();
int[] xpoints = { xPosition, xPosition + width, xPosition - height/2 + width, xPosition - height/2};
int[] ypoints = { yPosition, yPosition, yPosition + height, yPosition + height };
canvas.draw(this, color, new Polygon(xpoints, ypoints, 4));
canvas.wait(10);
}
}
}
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}
Bentuk Persegi Panjang
import java.awt.*;
/**
* Write a description of class PersegiPanjang here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class PersegiPanjang
{
// instance variables - replace the example below with your own
private int height;
private int width;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
/**
* Constructor for objects of class PersegiPanjang
*/
public PersegiPanjang()
{
height = 40;
width = 50;
xPosition = 40;
yPosition = 40;
color = "white";
isVisible = false;
}
public void makeVisible()
{
isVisible = true;
draw();
}
public void makeInvisible()
{
erase();
isVisible = false;
}
public void moveRight()
{
moveHorizontal(10);
}
public void moveLeft()
{
moveHorizontal(-10);
}
public void moveUp()
{
moveVertical(-10);
}
public void moveDown()
{
moveVertical(10);
}
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
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();
}
}
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();
}
}
public void changeSize(int newHeight, int newWidth)
{
erase();
height = newHeight;
width = newWidth;
draw();
}
public void changeColor(String newColor)
{
color = newColor;
draw();
}
private void draw()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.draw(this, color,
new Rectangle(xPosition, yPosition, width, height));
canvas.wait(10);
}
}
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}
Bentuk Segitiga
import java.awt.*;
/**
* Write a description of class Segitiga here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Segitiga
{
// instance variables - replace the example below with your own
private int height;
private int width;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
public Segitiga()
{
height = 30;
width = 40;
xPosition = 40;
yPosition = 40;
color = "green";
isVisible = false;
}
public void makeVisible()
{
isVisible = true;
draw();
}
public void makeInvisible()
{
erase();
isVisible = false;
}
public void moveRight()
{
moveHorizontal(10);
}
public void moveLeft()
{
moveHorizontal(-10);
}
public void moveUp()
{
moveVertical(-10);
}
public void moveDown()
{
moveVertical(10);
}
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
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();
}
}
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();
}
}
public void changeSize(int newHeight, int newWidth)
{
erase();
height = newHeight;
width = newWidth;
draw();
}
public void changeColor(String newColor)
{
color = newColor;
draw();
}
private void draw()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
int[] xpoints = { xPosition, xPosition + (width/2), xPosition - (width/2) };
int[] ypoints = { yPosition, yPosition + height, yPosition + height };
canvas.draw(this, color, new Polygon(xpoints, ypoints, 3));
canvas.wait(10);
}
}
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}
Bentuk Persegi / Bujur Sangkar
import java.awt.*;
/**
* Write a description of class Persegi here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Persegi
{
// instance variables - replace the example below with your own
private int size;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
public Persegi()
{
size = 30;
xPosition = 40;
yPosition = 40;
color = "red";
isVisible = false;
}
public void makeVisible()
{
isVisible = true;
draw();
}
public void makeInvisible()
{
erase();
isVisible = false;
}
public void moveRight()
{
moveHorizontal(10);
}
public void moveLeft()
{
moveHorizontal(-10);
}
public void moveUp()
{
moveVertical(-10);
}
public void moveDown()
{
moveVertical(10);
}
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
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();
}
}
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();
}
}
public void changeSize(int newSize)
{
erase();
size = newSize;
draw();
}
public void changeColor(String newColor)
{
color = newColor;
draw();
}
private void draw()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.draw(this, color,
new Rectangle(xPosition, yPosition, size, size));
canvas.wait(10);
}
}
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}
Bentuk Lingkaran
import java.awt.*;
import java.awt.geom.*;
/**
* Write a description of class Lingkaran here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Lingkaran
{
private int diameter;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
/**
* Constructor for objects of class Lingkaran
*/
public Lingkaran()
{
diameter = 30;
xPosition = 40;
yPosition = 40;
color = "blue";
isVisible = false;
}
/**
* An example of a method - replace this comment with your own
*
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public void makeVisible()
{
isVisible = true;
draw();
}
public void moveRight()
{
moveHorizontal(10);
}
public void moveLeft()
{
moveHorizontal(-10);
}
public void moveUp()
{
moveVertical(-10);
}
public void moveDown()
{
moveVertical(10);
}
public void moveHorizontal(int distance)
{
erase();
xPosition += distance;
draw();
}
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
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();
}
}
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();
}
}
public void changeSize(int newDiameter)
{
erase();
diameter = newDiameter;
draw();
}
public void changeColor(String newColor)
{
color = newColor;
draw();
}
private void draw()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.draw(this, color, new Ellipse2D.Double(xPosition, yPosition,
diameter, diameter));
canvas.wait(10);
}
}
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}
Terimakasih, semoga bermanfaat bagi yang membaca.
Comments
Post a Comment