PointLib & ShapeDrawing

1. create a package (PointLib)

1.1 Point 클래스를 추가한다.
PointLib

 

2. create a package (ShapeApp)

ShapeApp

2.1. RandomShape 클래스를 추가한다.

 

package ShapeApp;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;

public class RandomShape {
Shape shape;
Color color;

RandomShape(Shape s, Color c) {
shape = s;
color = c;
}

public Shape getShape() {
return shape;
}

public void draw(Graphics2D g) {
g.setColor(color);
g.fill(shape);
}

boolean contains(int x, int y) {
return shape.contains((double) x, (double) y);
}

void translate(int x, int y) {
if (shape instanceof Polygon) {
Polygon p = (Polygon) shape;
p.translate(x, y);
}
else if (shape instanceof Rectangle) {
Rectangle r = (Rectangle) shape;
r.translate(x, y);
}
else if (shape instanceof Ellipse2D) {
Ellipse2D e = (Ellipse2D) shape;
e.setFrame((double) x, (double) y, e.getWidth(), e.getHeight());
}
}
}

2.2. RandomShapeFactory 클래스를 추가한다.

package ShapeApp;

import java.awt.Color;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.util.Random;

import PointLib.Point;

public class RandomShapeFactory {

private static Random rand = new Random();

public static Color randomColor() {
float r = rand.nextFloat();
float g = rand.nextFloat();
float b = rand.nextFloat();
return new Color(r, g, b);
}

public static int randomInt(int min, int max) {
return rand.nextInt(max-min) + min;
}

public static RandomShape getInstance(Point center) {
int shapeType = rand.nextInt() % 3;
System.out.println(“ShapeType=” + shapeType);
switch (shapeType) {
case 1:
{
System.out.println(“Triangle”);
int size = randomInt(20, 30);
Polygon triangle = new Polygon();
triangle.addPoint((int) center.getX(), (int) center.getY());
triangle.addPoint((int) (center.getX()-size), (int) (center.getY()+size));
triangle.addPoint((int) (center.getX()+size), (int) (center.getY()+size));
RandomShape s = new RandomShape(triangle, randomColor());
return s;
}

case 2:
{
System.out.println(“Rectangle”);
Shape rectangle = new Rectangle((int) center.getX(), (int) center.getY(), randomInt(50, 60), randomInt(50, 60));
RandomShape s= new RandomShape(rectangle, randomColor());
return s;
}

default:
{
System.out.println(“Oval”);
Shape oval = new Ellipse2D.Double(center.getX(), center.getY(), randomInt(40, 50), randomInt(30, 40));
RandomShape s= new RandomShape(oval, randomColor());
return s;
}
}
}
}

2.3. ShapeDrawing 클래스를 추가한다.

package ShapeApp;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import java.awt.event.*;

import PointLib.Point;

// ShapeDrawing
public class ShapeDrawing extends JFrame {
ShapeDrawingPanel polyCanvas = new ShapeDrawingPanel();

public static void main(String[] args) {
    new ShapeDrawing();
}

public ShapeDrawing() {
super(“Shape Drawing Frame”);

setBounds(10, 10, 350, 350);
setResizable(false);
setVisible(true);

polyCanvas.setPreferredSize(new Dimension(300, 300));
this.add(polyCanvas);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

private class ShapeDrawingPanel extends JPanel implements MouseListener, MouseMotionListener
{
int lastX, lastY;
boolean dragging = false;

ArrayList<RandomShape> shapes = new ArrayList<RandomShape>();
RandomShape selectedShape = null;

public ShapeDrawingPanel()
{
System.out.println(“ShapeDrawingPanel”);
addMouseListener(this);
addMouseMotionListener(this);
}

@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

for (RandomShape s : shapes)
s.draw(g2);
}

RandomShape findShape(int x, int y)
{
RandomShape s = null;
for (int i = shapes.size() – 1; i >= 0; i–) {
s = (RandomShape) shapes.get(i);
if (s.contains(x, y)) {
System.out.println(“Selected”);
return s;
}
}
return null;
}

public void mouseDragged(MouseEvent e) {
if (dragging == true) {
if (selectedShape.getShape() instanceof Ellipse2D)
selectedShape.translate(e.getX(), e.getY());
else {
int deltaX = e.getX() – lastX;
int deltaY = e.getY() – lastY;
System.out.println(“x = ” + deltaX + ” y = ” + deltaY);
selectedShape.translate(deltaX, deltaY);
lastX = e.getX();
lastY = e.getY();
}
repaint();
}
}

public void mouseMoved(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void mousePressed(MouseEvent e) {
selectedShape = findShape(e.getX(), e.getY());
if (selectedShape != null) {
dragging = true;
lastX = e.getX();
lastY = e.getY();
}
else {
System.out.println(“Pressed”);
RandomShape s = RandomShapeFactory.getInstance(new Point((double) e.getX(), (double) e.getY()));
shapes.add(s);
repaint(); // calls paintComponent()
}
}

public void mouseReleased(MouseEvent e) {
if (dragging == true) {
dragging = false;
selectedShape = null;
}
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}
}
}