Pada Jurnal kali ini, saya ingin menunjukkan karya saya yaitu membuat sebuah objek dan menyusunnya sehingga terlihat seperti sebuah view yang menarik.
Susunan Class dan Output canvas.
|
Berikut adalah Code yang digunakan :
Class Picture
/**
* Main Assigment
*
*
*
* @author CarWiMa
* @version 1.00 (Beta)
*/
public class Picture
{
private Recta door;
private Recta land;
private Square sky;
private Square wall;
private Square chimney;
private Triangle roof;
private Circle sun;
private Circle smoke1;
private Circle smoke2;
/**
* Constructor for objects of class Picture
*/
public Picture()
{
// Empty field
}
public void draw()
{
sky = new Square();
sky.changeColor("blue");
sky.changeSize(500);
sky.makeVisible();
land = new Recta();
land.changeSize(50,300);
land.moveVertical(260);
land.changeColor("brown");
land.makeVisible();
wall = new Square();
wall.changeSize(100);
wall.moveHorizontal(70);
wall.moveVertical(180);
wall.changeColor("wall");
wall.makeVisible();
door = new Recta();
door.changeSize(50,30);
door.moveHorizontal(120);
door.moveVertical(230);
door.changeColor("smoke");
door.makeVisible();
smoke1 = new Circle();
smoke1.changeColor("smoke");
smoke1.moveHorizontal(100);
smoke1.moveVertical(100);
smoke1.changeSize(30);
smoke1.makeVisible();
smoke2 = new Circle();
smoke2.changeColor("smoke");
smoke2.moveHorizontal(80);
smoke2.moveVertical(120);
smoke2.changeSize(30);
smoke2.makeVisible();
chimney = new Square();
chimney.changeColor("black");
chimney.moveHorizontal(70);
chimney.moveVertical(150);
chimney.makeVisible();
roof = new Triangle();
roof.changeColor("roof");
roof.changeSize(50, 140);
roof.moveHorizontal(120);
roof.moveVertical(140);
roof.makeVisible();
sun = new Circle();
sun.changeColor("yellow");
sun.moveHorizontal(-25);
sun.moveVertical(-25);
sun.changeSize(80);
sun.makeVisible();
}
}
Class Circle
import java.awt.*;
import java.awt.geom.*;
/**
* Lingkaran.
*
* @author CarWiMa
* @version 1.00 (Beta)
*/
public class Circle
{
private int diameter;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
public Circle()
{
diameter = 30;
xPosition = 0;
yPosition = 0;
color = "blue";
isVisible = false;
}
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();
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);
}
}
}
Class Triangle
import java.awt.*;
/**
* Segitiga yang akan berada di Canvas
*
* @author CarWiMa
* @version 1.00 (Beta)
*/
public class Triangle
{
private int height;
private int width;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
public Triangle()
{
height = 30;
width = 40;
xPosition = 0;
yPosition = 0;
color = "green";
isVisible = false;
}
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();
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);
}
}
}
Class Square
import java.awt.*;
/**
* Gambar Kotak Pada Canvas.
*
* @author CarWiMa
* @version 1.00
*/
public class Square
{
private int size;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
public Square()
{
size = 30;
xPosition = 0;
yPosition = 0;
color = "red";
isVisible = false;
}
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();
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);
}
}
}
Class Rectangle
import java.awt.*;
/**
* Segiempat yang akan berada di Canvas
*
* @author CarWiMa
* @version 1.00 (Beta)
*/
public class Recta
{
private int H;
private int W;
private int xPosition;
private int yPosition;
private String color;
private boolean isVisible;
public Recta()
{
H = 100;
W = 50;
xPosition = 0;
yPosition = 0;
color = "brown";
isVisible = false;
}
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();
xPosition += distance;
draw();
}
public void moveVertical(int distance)
{
erase();
yPosition += distance;
draw();
}
public void changeSize(int newH,int newW)
{
erase();
H = newH;
W = newW;
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, W, H));
canvas.wait(10);
}
}
private void erase()
{
if(isVisible) {
Canvas canvas = Canvas.getCanvas();
canvas.erase(this);
}
}
}
Class Canvas
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.*;
/**
* Canvas adalah kelas untuk menampilkan gambar
*
* @author Carlo Win Marshal
* @version 1.00 (Beta)
*/
public class Canvas
{
private static Canvas canvasSingleton;
/**
* -Canvas-
*/
public static Canvas getCanvas()
{
if(canvasSingleton == null) {
canvasSingleton = new Canvas("BlueJ Shapes Demo", 300, 300,
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); // just in case it was already there
objects.add(referenceObject); // add at the end
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("black"))
graphic.setColor(Color.black);
else if(colorString.equals("blue"))
graphic.setColor(new Color(58,169,222));
else if(colorString.equals("roof"))
graphic.setColor(new Color(3,30,51));
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(102,51,0));
else if(colorString.equals("smoke"))
graphic.setColor(new Color(172,173,169));
else if(colorString.equals("wall"))
graphic.setColor(new Color(51,83,106));
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);
}
}
}
Tidak ada komentar:
Posting Komentar