Dalam pembuatan gambar Rumah digunakan beberapa library Java sebagai penunjang pemrograman grafis seperti :
Source code Class Square
Source code Class Triangle
Source code Class Rectangles
Source code Class Canvas
Source code Class House Picture
Gambar hasil run HousePicture
- import javax.swing.*
- import java.awt.*
- import java.util.List
- import java.util.*
Lalu class - class yang digunakan dalam pembuatan gambar Rumah ini terbagi menjadi :
- Shape class, digunakan untuk mengatur bentuk yang ingin ditampilkan. Terdiri atas:
- Circle
- Square
- Triangle
- Rectangles
- Library class, bernama Canvas, berfungsi sebagai utility yang memungkinkan untuk memunculkan gambar.
- Main class, bernama HousePicture, berfungsi untuk menyatukan class-class yang ada dan melakukan function serta variable control seperti ukuran, warna, dan posisi yang ingin dimunculkan pada Canvas.
import java.awt.*;
import java.awt.geom.*;
/**
* Write a description of class Circle here.
*
* @author Satria Ade Veda Karuniawan
* @version version 1.0
*/
public class Circle
{
private int radius;
private int centerx;
private int centery;
private String color;
private boolean isVisible;
public Circle()
{
radius = 50;
centerx = 0;
centery = 0;
color = "yellow";
isVisible = true;
draw();
}
public void SetRadius(int r)
{
radius = r;
}
public void SetCenter(int x, int y)
{
centerx = x;
centery = y;
}
public double Circumference()
{
return 2*3.14*radius;
}
public double Area()
{
return 3.14*radius*radius;
}
public void moveTo(int x, int y)
{
erase();
centerx = x;
centery = y;
draw();
}
public void makeVisible()
{
isVisible = true;
draw();
}
public void makeInvisible()
{
erase();
isVisible = false;
}
public void moveRight()
{
moveHorizontal(20);
}
public void moveLeft()
{
moveHorizontal(-20);
}
public void moveUp()
{
moveVertical(-20);
}
public void moveDown()
{
moveVertical(20);
}
public void moveHorizontal(int distance)
{
erase();
centerx += distance;
draw();
}
public void moveVertical(int distance)
{
erase();
centery += 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++)
{
centerx += 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++)
{
centery += delta;
draw();
}
}
public void changeSize(int newRadius)
{
erase();
radius = newRadius;
draw();
}
public void changeColor(String newColor)
{
color = newColor;
draw();
}
private void draw()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
canvasobj.draw(this, color, new Ellipse2D.Double(centerx, centery, 2*radius, 2*radius));
canvasobj.wait(10);
}
}
private void erase()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
canvasobj.erase(this);
}
}
}
import java.awt.geom.*;
/**
* Write a description of class Circle here.
*
* @author Satria Ade Veda Karuniawan
* @version version 1.0
*/
public class Circle
{
private int radius;
private int centerx;
private int centery;
private String color;
private boolean isVisible;
public Circle()
{
radius = 50;
centerx = 0;
centery = 0;
color = "yellow";
isVisible = true;
draw();
}
public void SetRadius(int r)
{
radius = r;
}
public void SetCenter(int x, int y)
{
centerx = x;
centery = y;
}
public double Circumference()
{
return 2*3.14*radius;
}
public double Area()
{
return 3.14*radius*radius;
}
public void moveTo(int x, int y)
{
erase();
centerx = x;
centery = y;
draw();
}
public void makeVisible()
{
isVisible = true;
draw();
}
public void makeInvisible()
{
erase();
isVisible = false;
}
public void moveRight()
{
moveHorizontal(20);
}
public void moveLeft()
{
moveHorizontal(-20);
}
public void moveUp()
{
moveVertical(-20);
}
public void moveDown()
{
moveVertical(20);
}
public void moveHorizontal(int distance)
{
erase();
centerx += distance;
draw();
}
public void moveVertical(int distance)
{
erase();
centery += 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++)
{
centerx += 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++)
{
centery += delta;
draw();
}
}
public void changeSize(int newRadius)
{
erase();
radius = newRadius;
draw();
}
public void changeColor(String newColor)
{
color = newColor;
draw();
}
private void draw()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
canvasobj.draw(this, color, new Ellipse2D.Double(centerx, centery, 2*radius, 2*radius));
canvasobj.wait(10);
}
}
private void erase()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
canvasobj.erase(this);
}
}
}
Source code Class Square
import java.awt.*;
/**
* Write a description of class Square here.
*
* @author Satria Ade Veda Karuniawan
* @version version 1.0
*/
public class Square
{
private int side;
private int centerx;
private int centery;
private String color;
private boolean isVisible;
public Square()
{
side = 25;
centerx = 50;
centery = 0;
color = "blue";
isVisible = true;
draw();
}
public void SetSide(int s)
{
side = s;
}
public void SetCenter(int x, int y)
{
centerx = x;
centery = y;
}
public double Circumference()
{
return 4 * side;
}
public double Area()
{
return side * side;
}
public void moveTo(int x, int y)
{
erase();
centerx = x;
centery = y;
draw();
}
public void makeVisible()
{
isVisible = true;
draw();
}
public void makeInvisible()
{
erase();
isVisible = false;
}
public void moveRight()
{
moveHorizontal(20);
}
public void moveLeft()
{
moveHorizontal(-20);
}
public void moveUp()
{
moveVertical(-20);
}
public void moveDown()
{
moveVertical(20);
}
public void moveHorizontal(int distance)
{
erase();
centerx += distance;
draw();
}
public void moveVertical(int distance)
{
erase();
centery += 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++)
{
centerx += 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++)
{
centery += delta;
draw();
}
}
public void changeSize(int newSide)
{
erase();
side = newSide;
draw();
}
public void changeColor(String newColor)
{
color = newColor;
draw();
}
private void draw()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
canvasobj.draw(this, color, new Rectangle(centerx, centery, side, side));
canvasobj.wait(10);
}
}
private void erase()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
canvasobj.erase(this);
}
}
}
/**
* Write a description of class Square here.
*
* @author Satria Ade Veda Karuniawan
* @version version 1.0
*/
public class Square
{
private int side;
private int centerx;
private int centery;
private String color;
private boolean isVisible;
public Square()
{
side = 25;
centerx = 50;
centery = 0;
color = "blue";
isVisible = true;
draw();
}
public void SetSide(int s)
{
side = s;
}
public void SetCenter(int x, int y)
{
centerx = x;
centery = y;
}
public double Circumference()
{
return 4 * side;
}
public double Area()
{
return side * side;
}
public void moveTo(int x, int y)
{
erase();
centerx = x;
centery = y;
draw();
}
public void makeVisible()
{
isVisible = true;
draw();
}
public void makeInvisible()
{
erase();
isVisible = false;
}
public void moveRight()
{
moveHorizontal(20);
}
public void moveLeft()
{
moveHorizontal(-20);
}
public void moveUp()
{
moveVertical(-20);
}
public void moveDown()
{
moveVertical(20);
}
public void moveHorizontal(int distance)
{
erase();
centerx += distance;
draw();
}
public void moveVertical(int distance)
{
erase();
centery += 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++)
{
centerx += 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++)
{
centery += delta;
draw();
}
}
public void changeSize(int newSide)
{
erase();
side = newSide;
draw();
}
public void changeColor(String newColor)
{
color = newColor;
draw();
}
private void draw()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
canvasobj.draw(this, color, new Rectangle(centerx, centery, side, side));
canvasobj.wait(10);
}
}
private void erase()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
canvasobj.erase(this);
}
}
}
Source code Class Triangle
import static java.lang.Math.sqrt;
import java.awt.*;
/**
* Write a description of class Triangle here.
*
* @author Satria Ade Veda Karuniawan
* @version version 1.0
*/
public class Triangle
{
private int bottom;
private int height;
private double hypo;
private int centerx;
private int centery;
private String color;
private boolean isVisible;
public Triangle()
{
bottom = 100;
height = 100;
centerx = 50;
centery = 50;
color = "red";
isVisible = true;
draw();
}
public void SetBottom(int s)
{
bottom = s;
}
public void SetHeight(int h)
{
height = h;
}
public void SetCenter(int x, int y)
{
centerx = x;
centery = y;
}
public double Circumference()
{
hypo = Math.sqrt(0.5*bottom*0.5*bottom + height*height);
return 2*hypo + bottom;
}
public double Area()
{
return 0.5 * bottom * height;
}
public void moveTo(int x, int y)
{
erase();
centerx = x;
centery = y;
draw();
}
public void makeVisible()
{
isVisible = true;
draw();
}
public void makeInvisible()
{
erase();
isVisible = false;
}
public void moveRight()
{
moveHorizontal(20);
}
public void moveLeft()
{
moveHorizontal(-20);
}
public void moveUp()
{
moveVertical(-20);
}
public void moveDown()
{
moveVertical(20);
}
public void moveHorizontal(int distance)
{
erase();
centerx += distance;
draw();
}
public void moveVertical(int distance)
{
erase();
centery += 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++)
{
centerx += 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++)
{
centery += delta;
draw();
}
}
public void changeSize(int newBottom, int newHeight)
{
erase();
bottom = newBottom;
height = newHeight;
draw();
}
public void changeColor(String newColor)
{
color = newColor;
draw();
}
private void draw()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
int[] xpoints = {centerx, centerx + (bottom/2), centerx - (bottom/2)};
int[] ypoints = {centery, centery + height, centery + height};
canvasobj.draw(this, color, new Polygon(xpoints, ypoints,3));
canvasobj.wait(10);
}
}
private void erase()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
canvasobj.erase(this);
}
}
}
import java.awt.*;
/**
* Write a description of class Triangle here.
*
* @author Satria Ade Veda Karuniawan
* @version version 1.0
*/
public class Triangle
{
private int bottom;
private int height;
private double hypo;
private int centerx;
private int centery;
private String color;
private boolean isVisible;
public Triangle()
{
bottom = 100;
height = 100;
centerx = 50;
centery = 50;
color = "red";
isVisible = true;
draw();
}
public void SetBottom(int s)
{
bottom = s;
}
public void SetHeight(int h)
{
height = h;
}
public void SetCenter(int x, int y)
{
centerx = x;
centery = y;
}
public double Circumference()
{
hypo = Math.sqrt(0.5*bottom*0.5*bottom + height*height);
return 2*hypo + bottom;
}
public double Area()
{
return 0.5 * bottom * height;
}
public void moveTo(int x, int y)
{
erase();
centerx = x;
centery = y;
draw();
}
public void makeVisible()
{
isVisible = true;
draw();
}
public void makeInvisible()
{
erase();
isVisible = false;
}
public void moveRight()
{
moveHorizontal(20);
}
public void moveLeft()
{
moveHorizontal(-20);
}
public void moveUp()
{
moveVertical(-20);
}
public void moveDown()
{
moveVertical(20);
}
public void moveHorizontal(int distance)
{
erase();
centerx += distance;
draw();
}
public void moveVertical(int distance)
{
erase();
centery += 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++)
{
centerx += 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++)
{
centery += delta;
draw();
}
}
public void changeSize(int newBottom, int newHeight)
{
erase();
bottom = newBottom;
height = newHeight;
draw();
}
public void changeColor(String newColor)
{
color = newColor;
draw();
}
private void draw()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
int[] xpoints = {centerx, centerx + (bottom/2), centerx - (bottom/2)};
int[] ypoints = {centery, centery + height, centery + height};
canvasobj.draw(this, color, new Polygon(xpoints, ypoints,3));
canvasobj.wait(10);
}
}
private void erase()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
canvasobj.erase(this);
}
}
}
Source code Class Rectangles
import java.awt.*;
/**
* Write a description of class Rectangle here.
*
* @author Satria Ade Veda Karuniawan
* @version version 1.0
*/
public class Rectangles
{
private int aside;
private int bside;
private int centerx;
private int centery;
private boolean isVisible;
private String color;
public Rectangles()
{
aside = 100;
bside = 50;
centerx = 200;
centery = 200;
isVisible = true;
color = "magenta";
draw();
}
public void SetSide(int l, int s)
{
aside = l;
bside = s;
}
public void SetCenter(int x, int y)
{
centerx = x;
centery = y;
}
public double Circumference()
{
return 2*aside + 2*bside;
}
public double Area()
{
return aside * bside;
}
public void moveTo(int x, int y)
{
erase();
centerx = x;
centery = y;
draw();
}
public void makeVisible()
{
isVisible = true;
draw();
}
public void makeInvisible()
{
erase();
isVisible = false;
}
public void moveRight()
{
moveHorizontal(20);
}
public void moveLeft()
{
moveHorizontal(-20);
}
public void moveUp()
{
moveVertical(-20);
}
public void moveDown()
{
moveVertical(20);
}
public void moveHorizontal(int distance)
{
erase();
centerx += distance;
draw();
}
public void moveVertical(int distance)
{
erase();
centery += 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++)
{
centerx += 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++)
{
centery += delta;
draw();
}
}
public void changeSize(int newASide, int newBSide)
{
erase();
aside = newASide;
bside = newBSide;
draw();
}
public void changeColor(String newColor)
{
color = newColor;
draw();
}
private void draw()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
canvasobj.draw(this, color, new Rectangle(centerx, centery, aside, bside));
canvasobj.wait(10);
}
}
private void erase()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
canvasobj.erase(this);
}
}
}
/**
* Write a description of class Rectangle here.
*
* @author Satria Ade Veda Karuniawan
* @version version 1.0
*/
public class Rectangles
{
private int aside;
private int bside;
private int centerx;
private int centery;
private boolean isVisible;
private String color;
public Rectangles()
{
aside = 100;
bside = 50;
centerx = 200;
centery = 200;
isVisible = true;
color = "magenta";
draw();
}
public void SetSide(int l, int s)
{
aside = l;
bside = s;
}
public void SetCenter(int x, int y)
{
centerx = x;
centery = y;
}
public double Circumference()
{
return 2*aside + 2*bside;
}
public double Area()
{
return aside * bside;
}
public void moveTo(int x, int y)
{
erase();
centerx = x;
centery = y;
draw();
}
public void makeVisible()
{
isVisible = true;
draw();
}
public void makeInvisible()
{
erase();
isVisible = false;
}
public void moveRight()
{
moveHorizontal(20);
}
public void moveLeft()
{
moveHorizontal(-20);
}
public void moveUp()
{
moveVertical(-20);
}
public void moveDown()
{
moveVertical(20);
}
public void moveHorizontal(int distance)
{
erase();
centerx += distance;
draw();
}
public void moveVertical(int distance)
{
erase();
centery += 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++)
{
centerx += 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++)
{
centery += delta;
draw();
}
}
public void changeSize(int newASide, int newBSide)
{
erase();
aside = newASide;
bside = newBSide;
draw();
}
public void changeColor(String newColor)
{
color = newColor;
draw();
}
private void draw()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
canvasobj.draw(this, color, new Rectangle(centerx, centery, aside, bside));
canvasobj.wait(10);
}
}
private void erase()
{
if(isVisible)
{
Canvas canvasobj = Canvas.getCanvas();
canvasobj.erase(this);
}
}
}
Source code Class Canvas
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.*;
/**
* Write a description of class Canvas here.
*
* @author Satria Ade Veda Karuniawan
* @version version 1.0
*/
public class Canvas
{
private static Canvas canvasSingleton;
public static Canvas getCanvas()
{
if(canvasSingleton == null)
{
canvasSingleton = new Canvas("House", 1000, 750, Color.black);
}
canvasSingleton.setVisible(true);
return canvasSingleton;
}
private JFrame frame;
public CanvasPane canvas;
private Graphics2D graphic;
private Color backgroundColor;
private Image canvasImage;
private List<Object>objects;
private HashMap<Object, ShapeDescription> shapes;
//Fungsi untuk membuat 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));
backgroundColor = bgColour;
frame.pack();
objects = new ArrayList<Object>();
shapes = new HashMap<Object, ShapeDescription>();
}
//Memunculkan Canvas ke depan layar
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(backgroundColor);
graphic.fillRect(0, 0, size.width, size.height);
graphic.setColor(Color.black);
}
frame.setVisible(visible);
}
//Menggambar bentuk tertentu pada canvas
public void draw(Object referenceObject, String color, Shape shape)
{
objects.remove(referenceObject);
objects.add(referenceObject);
shapes.put(referenceObject, new ShapeDescription(shape, color));
redraw();
}
//Menghapus bentuk tertentu pada screen
public void erase(Object referenceObject)
{
objects.remove(referenceObject);
shapes.remove(referenceObject);
redraw();
}
//Set foreground colour
public void setForegroundColor(String colorString)
{
if(colorString.equals("white"))
{
graphic.setColor(Color.white);
}
else if(colorString.equals("magenta"))
{
graphic.setColor(Color.magenta);
}
else if(colorString.equals("green"))
{
graphic.setColor(Color.green);
}
else if(colorString.equals("yellow"))
{
graphic.setColor(Color.yellow);
}
else if(colorString.equals("blue"))
{
graphic.setColor(Color.blue);
}
else if(colorString.equals("red"))
{
graphic.setColor(Color.red);
}
else if(colorString.equals("brown"))
{
graphic.setColor(new Color(139,69,19));
}
else
{
graphic.setColor(Color.black);
}
}
//Delay
public void wait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch(Exception e)
{
}
}
//Gambar ulang
private void redraw()
{
erase();
for(Object shape : objects)
{
shapes.get(shape).draw(graphic);
}
canvas.repaint();
}
private void erase()
{
Color original = graphic.getColor();
graphic.setColor(backgroundColor);
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);
}
}
}
import java.awt.*;
import java.util.List;
import java.util.*;
/**
* Write a description of class Canvas here.
*
* @author Satria Ade Veda Karuniawan
* @version version 1.0
*/
public class Canvas
{
private static Canvas canvasSingleton;
public static Canvas getCanvas()
{
if(canvasSingleton == null)
{
canvasSingleton = new Canvas("House", 1000, 750, Color.black);
}
canvasSingleton.setVisible(true);
return canvasSingleton;
}
private JFrame frame;
public CanvasPane canvas;
private Graphics2D graphic;
private Color backgroundColor;
private Image canvasImage;
private List<Object>objects;
private HashMap<Object, ShapeDescription> shapes;
//Fungsi untuk membuat 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));
backgroundColor = bgColour;
frame.pack();
objects = new ArrayList<Object>();
shapes = new HashMap<Object, ShapeDescription>();
}
//Memunculkan Canvas ke depan layar
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(backgroundColor);
graphic.fillRect(0, 0, size.width, size.height);
graphic.setColor(Color.black);
}
frame.setVisible(visible);
}
//Menggambar bentuk tertentu pada canvas
public void draw(Object referenceObject, String color, Shape shape)
{
objects.remove(referenceObject);
objects.add(referenceObject);
shapes.put(referenceObject, new ShapeDescription(shape, color));
redraw();
}
//Menghapus bentuk tertentu pada screen
public void erase(Object referenceObject)
{
objects.remove(referenceObject);
shapes.remove(referenceObject);
redraw();
}
//Set foreground colour
public void setForegroundColor(String colorString)
{
if(colorString.equals("white"))
{
graphic.setColor(Color.white);
}
else if(colorString.equals("magenta"))
{
graphic.setColor(Color.magenta);
}
else if(colorString.equals("green"))
{
graphic.setColor(Color.green);
}
else if(colorString.equals("yellow"))
{
graphic.setColor(Color.yellow);
}
else if(colorString.equals("blue"))
{
graphic.setColor(Color.blue);
}
else if(colorString.equals("red"))
{
graphic.setColor(Color.red);
}
else if(colorString.equals("brown"))
{
graphic.setColor(new Color(139,69,19));
}
else
{
graphic.setColor(Color.black);
}
}
//Delay
public void wait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch(Exception e)
{
}
}
//Gambar ulang
private void redraw()
{
erase();
for(Object shape : objects)
{
shapes.get(shape).draw(graphic);
}
canvas.repaint();
}
private void erase()
{
Color original = graphic.getColor();
graphic.setColor(backgroundColor);
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);
}
}
}
Source code Class House Picture
/**
* Write a description of class HousePicture here.
*
* @author Satria Ade Veda Karuniawan
* @version version 1.0
*/
public class HousePicture
{
public HousePicture()
{
DrawAll();
}
public void DrawAll()
{
DrawHouse();
DrawFlag();
DrawTree();
DrawMoon();
DrawSky();
}
public void DrawHouse()
{
Square Jendela1 = new Square();
Square Jendela2 = new Square();
Rectangles Pintu = new Rectangles();
Rectangles Cerobong = new Rectangles();
Rectangles BasisRumah = new Rectangles();
int PositionX = 350;
int PositionY = 450;
BasisRumah.moveTo(PositionX, PositionY);
BasisRumah.changeSize(350,200);
BasisRumah.changeColor("yellow");
Pintu.moveTo(500,560);
Pintu.changeSize(50,100);
Pintu.changeColor("brown");
Jendela1.moveTo(380,560);
Jendela1.changeSize(70);
Jendela1.changeColor("white");
Jendela2.moveTo(600,560);
Jendela2.changeSize(70);
Jendela2.changeColor("white");
Triangle Atap2 = new Triangle();
Triangle Atap1 = new Triangle();
Cerobong.moveTo(620,340);
Cerobong.changeSize(25,70);
Cerobong.changeColor("brown");
Atap2.moveTo(520,320);
Atap2.changeSize(500,150);
Atap2.changeColor("red");
Atap1.moveTo(524,370);
Atap1.changeSize(350,100);
Atap1.changeColor("yellow");
}
public void DrawFlag()
{
Rectangles TiangBendera1 = new Rectangles();
Rectangles TiangBendera2 = new Rectangles();
Rectangles BenderaIndonesia1 = new Rectangles();
Rectangles BenderaIndonesia2 = new Rectangles();
Rectangles BenderaJepang = new Rectangles();
Circle RedJapan = new Circle();
int InitialPositionX = 250;
int InitialPositionY = 500;
int FlagSizeX = 50;
int FlagSizeY = 10;
TiangBendera1.changeSize(5,150);
TiangBendera1.moveTo(InitialPositionX,InitialPositionY);
TiangBendera1.changeColor("brown");
BenderaIndonesia1.moveTo(InitialPositionX,InitialPositionY);
BenderaIndonesia2.moveTo(InitialPositionX,InitialPositionY+FlagSizeY);
BenderaIndonesia1.changeSize(FlagSizeX,FlagSizeY);
BenderaIndonesia2.changeSize(FlagSizeX,FlagSizeY);
BenderaIndonesia1.changeColor("red");
BenderaIndonesia2.changeColor("white");
TiangBendera2.changeSize(5,150);
TiangBendera2.moveTo(InitialPositionX+500, InitialPositionY);
TiangBendera2.changeColor("brown");
BenderaJepang.moveTo(InitialPositionX + 5 + 500, InitialPositionY);
BenderaJepang.changeSize(FlagSizeX,FlagSizeY*2);
BenderaJepang.changeColor("white");
RedJapan.moveTo(InitialPositionX + FlagSizeX + FlagSizeX/3 + 453, InitialPositionY + FlagSizeY/2 - 2);
RedJapan.changeSize(FlagSizeY-3);
RedJapan.changeColor("red");
}
public void DrawTree()
{
Rectangles BatangPohon = new Rectangles();
Rectangles BatangPohon2 = new Rectangles();
int PositionX = 100;
int PositionY = 600;
int LebarPohon = 40;
int TinggiPohon = 150;
BatangPohon.moveTo(PositionX, PositionY);
BatangPohon.changeSize(LebarPohon, TinggiPohon);
BatangPohon.changeColor("brown");
BatangPohon2.moveTo(PositionX+770, PositionY);
BatangPohon2.changeSize(LebarPohon, TinggiPohon);
BatangPohon2.changeColor("brown");
Triangle DaunPohon = new Triangle();
Triangle DaunPohon2 = new Triangle();
Triangle DaunPohon3 = new Triangle();
Triangle DaunPohon4 = new Triangle();
Triangle DaunPohon5 = new Triangle();
Triangle DaunPohon6 = new Triangle();
DaunPohon.moveTo(PositionX+20, PositionY-100);
DaunPohon.changeSize(LebarPohon*4, TinggiPohon);
DaunPohon.changeColor("green");
DaunPohon2.moveTo(PositionX+20, PositionY-200);
DaunPohon2.changeSize(LebarPohon*4, TinggiPohon);
DaunPohon2.changeColor("green");
DaunPohon3.moveTo(PositionX+20, PositionY-300);
DaunPohon3.changeSize(LebarPohon*4, TinggiPohon);
DaunPohon3.changeColor("green");
DaunPohon4.moveTo(PositionX+20+770, PositionY-100);
DaunPohon4.changeSize(LebarPohon*4, TinggiPohon);
DaunPohon4.changeColor("green");
DaunPohon5.moveTo(PositionX+20+770, PositionY-200);
DaunPohon5.changeSize(LebarPohon*4, TinggiPohon);
DaunPohon5.changeColor("green");
DaunPohon6.moveTo(PositionX+20+770, PositionY-300);
DaunPohon6.changeSize(LebarPohon*4, TinggiPohon);
DaunPohon6.changeColor("green");
}
public void DrawMoon()
{
Circle Bulan = new Circle();
int PositionX = 700;
int PositionY = 50;
int Radius = 100;
Bulan.changeSize(Radius);
Bulan.moveTo(PositionX, PositionY);
Bulan.changeColor("white");
}
public void DrawSky()
{
Square Star1 = new Square();
Square Star2 = new Square();
Square Star3 = new Square();
Square Star4 = new Square();
Square Star5 = new Square();
Square Star6 = new Square();
Star1.moveTo(72,200);
Star1.changeSize(15);
Star1.changeColor("white");
Star2.moveTo(500,171);
Star2.changeSize(15);
Star2.changeColor("white");
Star3.moveTo(300,200);
Star3.changeSize(15);
Star3.changeColor("white");
Star4.moveTo(666,300);
Star4.changeSize(15);
Star4.changeColor("white");
Star5.moveTo(200,101);
Star5.changeSize(15);
Star5.changeColor("white");
Star6.moveTo(52,11);
Star6.changeSize(15);
Star6.changeColor("white");
}
}
* Write a description of class HousePicture here.
*
* @author Satria Ade Veda Karuniawan
* @version version 1.0
*/
public class HousePicture
{
public HousePicture()
{
DrawAll();
}
public void DrawAll()
{
DrawHouse();
DrawFlag();
DrawTree();
DrawMoon();
DrawSky();
}
public void DrawHouse()
{
Square Jendela1 = new Square();
Square Jendela2 = new Square();
Rectangles Pintu = new Rectangles();
Rectangles Cerobong = new Rectangles();
Rectangles BasisRumah = new Rectangles();
int PositionX = 350;
int PositionY = 450;
BasisRumah.moveTo(PositionX, PositionY);
BasisRumah.changeSize(350,200);
BasisRumah.changeColor("yellow");
Pintu.moveTo(500,560);
Pintu.changeSize(50,100);
Pintu.changeColor("brown");
Jendela1.moveTo(380,560);
Jendela1.changeSize(70);
Jendela1.changeColor("white");
Jendela2.moveTo(600,560);
Jendela2.changeSize(70);
Jendela2.changeColor("white");
Triangle Atap2 = new Triangle();
Triangle Atap1 = new Triangle();
Cerobong.moveTo(620,340);
Cerobong.changeSize(25,70);
Cerobong.changeColor("brown");
Atap2.moveTo(520,320);
Atap2.changeSize(500,150);
Atap2.changeColor("red");
Atap1.moveTo(524,370);
Atap1.changeSize(350,100);
Atap1.changeColor("yellow");
}
public void DrawFlag()
{
Rectangles TiangBendera1 = new Rectangles();
Rectangles TiangBendera2 = new Rectangles();
Rectangles BenderaIndonesia1 = new Rectangles();
Rectangles BenderaIndonesia2 = new Rectangles();
Rectangles BenderaJepang = new Rectangles();
Circle RedJapan = new Circle();
int InitialPositionX = 250;
int InitialPositionY = 500;
int FlagSizeX = 50;
int FlagSizeY = 10;
TiangBendera1.changeSize(5,150);
TiangBendera1.moveTo(InitialPositionX,InitialPositionY);
TiangBendera1.changeColor("brown");
BenderaIndonesia1.moveTo(InitialPositionX,InitialPositionY);
BenderaIndonesia2.moveTo(InitialPositionX,InitialPositionY+FlagSizeY);
BenderaIndonesia1.changeSize(FlagSizeX,FlagSizeY);
BenderaIndonesia2.changeSize(FlagSizeX,FlagSizeY);
BenderaIndonesia1.changeColor("red");
BenderaIndonesia2.changeColor("white");
TiangBendera2.changeSize(5,150);
TiangBendera2.moveTo(InitialPositionX+500, InitialPositionY);
TiangBendera2.changeColor("brown");
BenderaJepang.moveTo(InitialPositionX + 5 + 500, InitialPositionY);
BenderaJepang.changeSize(FlagSizeX,FlagSizeY*2);
BenderaJepang.changeColor("white");
RedJapan.moveTo(InitialPositionX + FlagSizeX + FlagSizeX/3 + 453, InitialPositionY + FlagSizeY/2 - 2);
RedJapan.changeSize(FlagSizeY-3);
RedJapan.changeColor("red");
}
public void DrawTree()
{
Rectangles BatangPohon = new Rectangles();
Rectangles BatangPohon2 = new Rectangles();
int PositionX = 100;
int PositionY = 600;
int LebarPohon = 40;
int TinggiPohon = 150;
BatangPohon.moveTo(PositionX, PositionY);
BatangPohon.changeSize(LebarPohon, TinggiPohon);
BatangPohon.changeColor("brown");
BatangPohon2.moveTo(PositionX+770, PositionY);
BatangPohon2.changeSize(LebarPohon, TinggiPohon);
BatangPohon2.changeColor("brown");
Triangle DaunPohon = new Triangle();
Triangle DaunPohon2 = new Triangle();
Triangle DaunPohon3 = new Triangle();
Triangle DaunPohon4 = new Triangle();
Triangle DaunPohon5 = new Triangle();
Triangle DaunPohon6 = new Triangle();
DaunPohon.moveTo(PositionX+20, PositionY-100);
DaunPohon.changeSize(LebarPohon*4, TinggiPohon);
DaunPohon.changeColor("green");
DaunPohon2.moveTo(PositionX+20, PositionY-200);
DaunPohon2.changeSize(LebarPohon*4, TinggiPohon);
DaunPohon2.changeColor("green");
DaunPohon3.moveTo(PositionX+20, PositionY-300);
DaunPohon3.changeSize(LebarPohon*4, TinggiPohon);
DaunPohon3.changeColor("green");
DaunPohon4.moveTo(PositionX+20+770, PositionY-100);
DaunPohon4.changeSize(LebarPohon*4, TinggiPohon);
DaunPohon4.changeColor("green");
DaunPohon5.moveTo(PositionX+20+770, PositionY-200);
DaunPohon5.changeSize(LebarPohon*4, TinggiPohon);
DaunPohon5.changeColor("green");
DaunPohon6.moveTo(PositionX+20+770, PositionY-300);
DaunPohon6.changeSize(LebarPohon*4, TinggiPohon);
DaunPohon6.changeColor("green");
}
public void DrawMoon()
{
Circle Bulan = new Circle();
int PositionX = 700;
int PositionY = 50;
int Radius = 100;
Bulan.changeSize(Radius);
Bulan.moveTo(PositionX, PositionY);
Bulan.changeColor("white");
}
public void DrawSky()
{
Square Star1 = new Square();
Square Star2 = new Square();
Square Star3 = new Square();
Square Star4 = new Square();
Square Star5 = new Square();
Square Star6 = new Square();
Star1.moveTo(72,200);
Star1.changeSize(15);
Star1.changeColor("white");
Star2.moveTo(500,171);
Star2.changeSize(15);
Star2.changeColor("white");
Star3.moveTo(300,200);
Star3.changeSize(15);
Star3.changeColor("white");
Star4.moveTo(666,300);
Star4.changeSize(15);
Star4.changeColor("white");
Star5.moveTo(200,101);
Star5.changeSize(15);
Star5.changeColor("white");
Star6.moveTo(52,11);
Star6.changeSize(15);
Star6.changeColor("white");
}
}
Gambar hasil run HousePicture
Comments
Post a Comment