-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShape.java
More file actions
152 lines (139 loc) · 4.19 KB
/
Shape.java
File metadata and controls
152 lines (139 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.awt.*;
/**
* Shape.java
* Abstract class for geometric shapes.
* Provides three non-abstract methods: setColor, draw, and setCenter.
*
* Written by THC for CS 5 Lab Assignment 3.
*
* @author Thomas H. Cormen
* @author Jasper Bingham
*/
public abstract class Shape {
private Color color; // Shape's color
private Point origin; // origin of shape's creation
private Point dragPoint; // point to which shape is dragged out
public abstract void drawShape(Graphics page); // draw the Shape
public abstract boolean containsPoint(Point p); // does the Shape contain Point p?
public abstract void move(int deltaX, int deltaY); // move the Shape
public abstract Point getCenter(); // return the Shape's center
/**
* Create a Shape, setting its color.
* @param c the color you wish the shape to initially have
*/
public Shape(Color c) {
color = c;
}
/**
* Set the Shape's color.
* @param newColor the new color of the shape
*/
public void setColor(Color newColor) {
color = newColor;
}
/**
* Return the Shape's color.
* @return the Shape's color
*/
public Color getColor()
{
return color;
}
/**
* Draw the Shape.
*
* @param page the page you wish to draw the shape on
*/
public void draw(Graphics page) {
Color savedColor = page.getColor(); //save page's original color
page.setColor(color); //set page color to Shape's color
drawShape(page); //draw Shape in correct color
page.setColor(savedColor); //reset page's color
}
/**
* Move the Shape to be a given center.
*
* @param newCenter the new center of the shape
*/
public void setCenter(Point newCenter) {
Point oldCenter = getCenter(); //Shape's original center
int deltaX = newCenter.x - oldCenter.x; //horizontal movement of center
int deltaY = newCenter.y - oldCenter.y; //vertical movement of center
//move origin by correct horizontal distance
Point newOrigin = new Point(getOrigin().x + deltaX, getOrigin().y + deltaY);
setOrigin(newOrigin);
//move drag point by correct horizontal distance
Point newDragPoint = new Point(getDragPoint().x + deltaX, getDragPoint().y + deltaY);
setDragPoint(newDragPoint);
}
/**
* Set origin to new point.
* @param orig the new point
*/
public void setOrigin(Point orig)
{
origin = orig;
}
/**
* Set drag point to new point
* @param drag the new point
*/
public void setDragPoint(Point drag)
{
dragPoint = drag;
}
/**
* Return the shape's origin.
* @return the shape's origin
*/
public Point getOrigin()
{
return origin;
}
/**
* Return the shape's drag point.
* @return the shape's drag point
*/
public Point getDragPoint()
{
return dragPoint;
}
/**
* Returns the top left corner of the shape
* @return the top left corner of the shape
*/
public Point getTopLeft()
{
//top left corner will have lowest x and y values in shape
int lowX = 0;
int lowY = 0;
if(getOrigin().x < getDragPoint().x) //is origin left of drag point?
{
lowX = getOrigin().x; //if so, set top left x value to origin's x value
}
else lowX = getDragPoint().x; //otherwise set top left x value to drag point's x value
if(getOrigin().y < getDragPoint().y) //is origin above the drag point?
{
lowY = getOrigin().y; //if so, set top left y value to origin's y value
}
else lowY = getDragPoint().y; //otherwise set top left y value to drag point's y value
return new Point(lowX, lowY);
}
public Point getBottomRight()
{
//bottom right corner will have highest x and y values in shape
int highX = 0;
int highY = 0;
if(getOrigin().x > getDragPoint().x) //is origin right of drag point?
{
highX = getOrigin().x; //if so, set bottom right x value to origin's x value
}
else highX = getDragPoint().x; //otherwise set bottom right x value to drag point's x value
if(getOrigin().y > getDragPoint().y) //is origin below the drag point?
{
highY = getOrigin().y; //if so, set bottom right y value to origin's y value
}
else highY = getDragPoint().y; //otherwise set bottom right y value to drag point's y value
return new Point(highX, highY);
}
}