Membuat Pemandangan
- Canvas
/**
* Canvas is a class that used to draw a simple graphics.
*
* @author (Christine Amelia)
* @version (4-16/08/2018)
*/
import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.*;
public class Canvas
{
private static Canvas canvasSingleton;
//get the canvas singleton object
public static Canvas getCanvas()
{
if(canvasSingleton == null) {
canvasSingleton = new Canvas("BlueJ Shapes Demo", 300, 300,
Color.white);
}
canvasSingleton.setVisible(true);
return canvasSingleton;
}
// ----- instance part -----
private JFrame frame;
private CanvasPane canvas;
private Graphics2D graphic;
private Color backgroundColor;
private Image canvasImage;
private List<Object> objects;
private HashMap<Object, ShapeDescription> shapes;
/**
*title that appear in Canvas frame
*width that we want for the canvas
*height that we want for the canvas
*bgColor for the background color that we want for the canvas
*/
private Canvas(String title, int height, int width, Color bgColor)
{
frame = new JFrame();
canvas = new CanvasPane();
frame.setContentPane(canvas);
frame.setTitle(title);
canvas.setPreferredSize(new Dimension(width, height));
backgroundColor = bgColor;
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(backgroundColor);
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();
}
//Set the foreground color 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.
public void wait(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (Exception e)
{
// ignoring exception at the moment
}
}
//Redraw ell shapes currently on the Canvas.
private void redraw()
{
erase();
for(Object shape : objects) {
shapes.get(shape).draw(graphic);
}
canvas.repaint();
}
//Erase the whole canvas. (Does not 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);
}
}
}
- Circle
/**
* Write a description of class circle here.
*
* @author (Christine Amelia)
* @version (4-16/09/2018)
*/
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 = 30;
xPosition = 0;
yPosition = 0;
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 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)
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);
}
}
}
- Triangle
/**
* A triangle that can be manipulated and draws itself on the canvas
*
* @author (Chrsitine Amelia)
* @version (4-16/09/2018)
*/
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 = 40;
width = 50;
xPosition = 0;
yPosition = 0;
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 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 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)
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);
}
}
}
- Picture
/**
* My picture
*
* @author (Christine Amelia)
* @version (4-16/09/2018)
*/
public class Picture
{
private Triangle gunung1, gunung2, jalan;
private Circle matahari, awan1, awan2, awan3, awan4, awan5, awan6;
public Picture()
{
}
public void draw()
{
matahari = new Circle();
matahari.changeSize(80);
matahari.moveHorizontal(20);
matahari.moveVertical(20);
matahari.changeColor("yellow");
matahari.makeVisible();
gunung1 = new Triangle();
gunung1.changeSize(100, 200);
gunung1.moveHorizontal(75);
gunung1.moveVertical(65);
gunung1.changeColor("green");
gunung1.makeVisible();
gunung2 = new Triangle();
gunung2.changeSize(100, 200);
gunung2.moveHorizontal(225);
gunung2.moveVertical(65);
gunung2.changeColor("green");
gunung2.makeVisible();
awan1 = new Circle();
awan1.changeSize(30);
awan1.moveHorizontal(80);
awan1.moveVertical(0);
awan1.changeColor("blue");
awan1.makeVisible();
awan2 = new Circle();
awan2.changeSize(30);
awan2.moveHorizontal(60);
awan2.moveVertical(10);
awan2.changeColor("blue");
awan2.makeVisible();
awan3 = new Circle();
awan3.changeSize(30);
awan3.moveHorizontal(80);
awan3.moveVertical(20);
awan3.changeColor("blue");
awan3.makeVisible();
awan4 = new Circle();
awan4.changeSize(30);
awan4.moveHorizontal(100);
awan4.moveVertical(20);
awan4.changeColor("blue");
awan4.makeVisible();
awan5 = new Circle();
awan5.changeSize(30);
awan5.moveHorizontal(100);
awan5.moveVertical(0);
awan5.changeColor("blue");
awan5.makeVisible();
awan6 = new Circle();
awan6.changeSize(30);
awan6.moveHorizontal(120);
awan6.moveVertical(10);
awan6.changeColor("blue");
awan6.makeVisible();
}
}
Tidak ada komentar:
Posting Komentar