-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLine.java
More file actions
executable file
·138 lines (123 loc) · 3.27 KB
/
Line.java
File metadata and controls
executable file
·138 lines (123 loc) · 3.27 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
package com.codeforall.online.simplegraphics.graphics;
import java.awt.*;
import java.awt.geom.Line2D;
public class Line implements Shape, Colorable, Movable {
private Color color = Color.BLACK;
private double x1;
private double y1;
private double x2;
private double y2;
/**
* Constructs a line with a given starting and ending location.
*
* @param x1 the x-coordinate of the starting point
* @param y1 the y-coordinate of the starting point
* @param x2 the x-coordinate of the ending point
* @param y2 the y-coordinate of the ending point
*/
public Line(double x1, double y1, double x2, double y2) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
/**
* Gets the leftmost x-position of the line.
*
* @return the leftmost x-position
*/
public int getX() {
return (int) Math.round(Math.min(x1, x2));
}
/**
* Gets the topmost y-position of the line.
*
* @return the topmost y-position
*/
public int getY() {
return (int) Math.round(Math.min(y1, y2));
}
/**
* Gets the width of the bounding box.
*
* @return the width
*/
public int getWidth() {
return (int) Math.round(Math.abs(x2 - x1));
}
/**
* Gets the height of the bounding box.
*
* @return the height
*/
public int getHeight() {
return (int) Math.round(Math.abs(y2 - y1));
}
/**
* Moves this line by a given amount.
*
* @param dx the amount by which to move in x-direction
* @param dy the amount by which to move in y-direction
*/
public void translate(double dx, double dy) {
x1 += dx;
y1 += dy;
x2 += dx;
y2 += dy;
Canvas.getInstance().repaint();
}
/**
* Resizes this line both horizontally and vertically.
*
* @param dw the amount by which to resize the width on each side
* @param dh the amount by which to resize the height on each side
*/
public void grow(double dw, double dh) {
if (x1 <= x2) {
x1 -= dw;
x2 += dw;
} else {
x1 += dw;
x2 -= dw;
}
if (y1 <= y2) {
y1 -= dh;
y2 += dh;
} else {
y1 += dh;
y2 -= dh;
}
Canvas.getInstance().repaint();
}
/**
* Sets the color for drawing this line.
*
* @param newColor the new color
*/
public void setColor(Color newColor) {
color = newColor;
Canvas.getInstance().repaint();
}
/**
* Shows this line on the canvas.
*/
public void draw() {
Canvas.getInstance().show(this);
}
/**
* Deletes this line from the canvas.
*/
public void delete() {
Canvas.getInstance().hide(this);
}
public String toString() {
return "Line[x1=" + x1 + ",y1=" + y1 + ",x2=" + x2 + ",y2=" + y2 + "]";
}
public void paintShape(Graphics2D g2) {
if (color != null) {
g2.setColor(new java.awt.Color((int) color.getRed(), (int) color.getGreen(), (int) color.getBlue()));
Line2D.Double line = new Line2D.Double(x1, y1, x2, y2);
g2.draw(line);
}
}
}