-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleCanvas.java
240 lines (207 loc) · 6.43 KB
/
SimpleCanvas.java
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
/**
* This is a stripped-down version of the Canvas class from the
* BlueJ team, retaining only the most fundamental features.
*
* @author BlueJ team with modifications by Gordon Royle (July 2003) and Rachel Cardell-Oliver
* @version Feb 2011
*/
public class SimpleCanvas
{
private JFrame frame;
private CanvasPane canvas;
private Graphics2D graphic;
private Image canvasImage;
private boolean autoRepaint;
/**
* Creates and displays a SimpleCanvas of the specified size
* with a white background. The client specifies whether repainting
* after a drawing command should be manual or automatic.
*
* @param title title for the window
* @param width the desired width of the SimpleCanvas
* @param height the desired height of the SimpleCanvas
* @param autoRepaint true for automatic repainting
*
*/
public SimpleCanvas(String title, int width, int height, boolean autoRepaint) {
frame = new JFrame();
canvas = new CanvasPane();
frame.setContentPane(canvas);
frame.setTitle(title);
canvas.setPreferredSize(new Dimension(width,height));
frame.pack();
Dimension size = canvas.getSize();
canvasImage = canvas.createImage(size.width,size.height);
graphic = (Graphics2D) canvasImage.getGraphics();
graphic.setColor(Color.white);
graphic.fillRect(0,0,size.width,size.height);
graphic.setColor(Color.black);
frame.setVisible(true);
frame.setVisible(true);
frame.setVisible(true);
this.autoRepaint = autoRepaint;
}
/**
* Creates and displays a SimpleCanvas with a white background and
* with automatic repainting after drawing commands.
*
* @param title title for the window
* @param width the desired width of the SimpleCanvas
* @param height the desired height of the SimpleCanvas
*
*/
public SimpleCanvas(String title, int width, int height) {
this(title,width,height,true);
}
/**
* Creates and displays a SimpleCanvas of size 400x400 with the
* default title "SimpleCanvas" and with automatic repainting
* enabled.
*/
public SimpleCanvas() {
this("SimpleCanvas",400,400);
}
/**
* Draws a line on the SimpleCanvas between two points.
*
* @param x1 x-coordinate of the first point
* @param y1 y-coordinate of the first point
* @param x2 x-coordinate of the second point
* @param y2 y-coordinate of the second point
*
*/
public void drawLine(int x1, int y1, int x2, int y2) {
graphic.drawLine(x1,y1,x2,y2);
if (autoRepaint) {
canvas.repaint();
}
}
public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints)
{
Polygon p = new Polygon(xPoints, yPoints, nPoints);
graphic.drawPolygon(p);
graphic.fillPolygon(p);
if (autoRepaint) {
canvas.repaint();
}
}
public void drawArc(int x, int y , int w, int h, int start, int end)
{
graphic.drawArc(x,y,w,h, start, end);
graphic.fillArc(x,y,w,h, start, end);
if (autoRepaint) {
canvas.repaint();
}
}
public void drawCircle(int x, int y, int d)
{
Shape s = new Ellipse2D.Double(x, y, d, d);
graphic.draw(s);
graphic.fill(s);
if (autoRepaint) {
canvas.repaint();
}
}
/**
* Changes the colour for subsequent
* drawing on this SimpleCanvas.
*
* @param newColour the new drawing colour
*/
public void setForegroundColour(Color newColour) {
graphic.setColor(newColour);
}
/**
* Gets the colour currently used for
* drawing on this SimpleCanvas.
* @return current foreground colour
*/
public Color getForegroundColour() {
return graphic.getColor();
}
/**
* Changes the font for subsequent String
* drawing on this SimpleCanvas.
*
* @param newFont the new Font
*/
public void setFont(Font newFont) {
graphic.setFont(newFont);
}
/**
* Gets the font currently used for
* String drawing on this Canvas
* @return current font for canvas
*/
public Font getFont() {
return graphic.getFont();
}
/**
* Draws the specified String at the specified
* location on this SimpleCanvas
* @param text String to write on the canvas
* @param x horizontal coordinate (from left) to place text
* @param y vertical coordinate (from top) to place text
*/
public void drawString(String text, int x, int y) {
graphic.drawString(text, x, y);
if (autoRepaint) {
canvas.repaint();
}
}
/**
* Sets the repaint mode to either manual or automatic.
*
* @param autoRepaint automatic repainting if this is true
*/
public void setAutoRepaint(boolean autoRepaint) {
this.autoRepaint = autoRepaint;
}
/**
* If a SimpleCanvas does not automatically repaint
* after each drawing command, then this method can be
* used to cause a manual repaint.
*/
public void repaint() {
canvas.repaint();
}
/**
* Causes execution to pause for the specified amount of time.
* This is usually used to produce animations in an easy
* manner, by repeatedly drawing, pausing, and then redrawing
* an object.
* @param millis number of milliseconds to pause for
*/
public void wait(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException ie) {
System.out.println("Interrruption in SimpleCanvas: "+ie);
}
}
/**
* listener to wait for mouse clicks
* @param ml the name of the mouse listener
*/
public void addMouseListener(MouseListener ml) {
canvas.addMouseListener(ml);
}
/**
* Inner class CanvasPane - the actual canvas component contained in the
* Canvas frame. This is essentially a JPanel with added capability to
* refresh the image drawn on it.
*/
class CanvasPane extends JPanel {
/**
* refresh method
* @param g Graphics object
*/
public void paint(Graphics g) {
g.drawImage(canvasImage,0,0,null);
}
}
}