forked from lseiter/CSJava
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPen.java
More file actions
153 lines (128 loc) · 3.48 KB
/
Copy pathPen.java
File metadata and controls
153 lines (128 loc) · 3.48 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
153
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
/**
* Class to represent a pen which has a color, width,
* and a list of path segments that it should draw.
* A pen also knows if it is up or down
*
* Copyright Georgia Institute of Technology 2004
* @author Barb Ericson ericson@cc.gatech.edu
*/
public class Pen
{
////////////////// fields //////////////////////
/** track if up or down */
private boolean penDown = true;
/** color of ink */
private Color color = Color.green;
/** width of stroke */
private int width = 1;
/** list of path segment objects to draw */
private List<PathSegment> pathSegmentList =
new ArrayList<PathSegment>();
//////////////// constructors ///////////////////
/**
* Constructor that takes no arguments
*/
public Pen() { }
/**
* Constructor that takes all the ink color, and width
* @param color the ink color
* @param width the width in pixels
*/
public Pen(Color color, int width)
{
this.color = color;
this.width = width;
}
/**
* Constructor that takes the ink color, width, and penDown flag
* @param color the ink color
* @param width the width in pixels
* @param penDown the flag if the pen is down
*/
public Pen(Color color, int width, boolean penDown)
{
// use the other constructor to set these
this(color,width);
// set the pen down flag
this.penDown = penDown;
}
////////////////// methods ///////////////////////
/**
* Method to get pen down status
* @return true if the pen is down else false
*/
public boolean isPenDown() { return penDown; }
/**
* Method to set the pen down value
* @param value the new value to use
*/
public void setPenDown(boolean value) { penDown = value; }
/**
* Method to get the pen (ink) color
* @return the ink color
*/
public Color getColor() { return color; }
/**
* Method to set the pen (ink) color
* @param color the color to use
*/
public void setColor(Color color) { this.color = color;}
/**
* Method to get the width of the pen
* @return the width in pixels
*/
public int getWidth() { return width; }
/**
* Method to set the width of the pen
* @param width the width to use in pixels
*/
public void setWidth(int width) { this.width = width; }
/**
* Method to add a path segment if the pen is down
* @param x1 the first x
* @param y1 the first y
* @param x2 the second x
* @param y2 the second y
*/
public synchronized void addMove(int x1, int y1, int x2, int y2)
{
if (penDown)
{
PathSegment pathSeg =
new PathSegment(this.color,this.width,
new Line2D.Float(x1,y1,x2,y2));
pathSegmentList.add(pathSeg);
}
}
/**
* Method to clear the path stored for this pen
*/
public void clearPath()
{
pathSegmentList.clear();
}
/**
* Metod to paint the pen path
* @param g the graphics context
*/
public synchronized void paintComponent(Graphics g)
{
Color oldcolor = g.getColor();
// loop through path segment list and
Iterator iterator = pathSegmentList.iterator();
PathSegment pathSeg = null;
// loop through path segments
while (iterator.hasNext())
{
pathSeg = (PathSegment) iterator.next();
pathSeg.paintComponent(g);
}
g.setColor(oldcolor);
}
} // end of class