Repository navigation
Expand file tree
/
Copy pathDrawingKit.java
More file actions
230 lines (210 loc) · 7.01 KB
/
Copy pathDrawingKit.java
File metadata and controls
230 lines (210 loc) · 7.01 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
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
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
/** This class draws a window on the screen using
* the Swing classes JFrame and JPanel. Internally, it uses
* a BufferedImage of the same size as the window. All
* the drawing is done on this BufferedImage, which is
* rendered to the screen when the repaint method is invoked.
* This class provides Java 2D methods such as draw, fill etc. to
* draw and color graphical shapes on the window.
*
* @author Radhika S. Grover
* @author programwithjava.com
*/
public class DrawingKit {
private JFrame window;
private JPanel drawingSheet;
private BufferedImage bufferedImage;
Graphics2D bufferedImageGraphics;
/** Constructor to create a window with size 500 x 500.
*/
public DrawingKit() {
this("", 500, 500);
}
/** Constructor to create a window with a
* title and default size of 500 by 500.
*
* @param title the window title.
*/
public DrawingKit(String title) {
this(title, 500, 500);
}
/** Constructor to create a window of the specified width and height.
*
* @param width the window width.
* @param height the window height.
*/
public DrawingKit(int width, int height) {
this("", width, height);
}
/** Constructor to create a window with the specified
* title, width and height. A BufferedImage
* object called bufferedImage of the same size as the window is
* created internally. The window is cleared and the contents of
* BufferedImage are displayed on the window each time repaint
* is called. The window is positioned in the center of the screen.
*
* @param title the title on window.
* @param width the window width.
* @param height the window height.
*/
public DrawingKit(String title, int width, int height) {
window = new JFrame(title);
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
bufferedImageGraphics = bufferedImage.createGraphics();
bufferedImageGraphics.setPaint(Color.black);
drawingSheet = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D myGraphics = (Graphics2D) g;
myGraphics.drawImage(bufferedImage, 0, 0, this);
}
};
// display the coordinates of the mouse when it is clicked on the console
addMouseEventHandler();
window.setContentPane(drawingSheet);
drawingSheet.setOpaque(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(width, height);
window.setResizable(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation((screenSize.width - width)/2, (screenSize.height - height)/2);
window.setVisible(true);
}
/** Method to display the (x, y) coordinates of a
* point on the window when it is clicked
* with the mouse.
*
*/
public void addMouseEventHandler() {
drawingSheet.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
Point p = drawingSheet.getMousePosition();
System.out.println("(" +p.getX() +", " +p.getY() +")");
}
});
}
/** Method to draw a graphics shape on the window.
*
* @param obj graphics shape to draw.
*/
public void draw(Shape obj) {
bufferedImageGraphics.draw(obj);
window.repaint();
}
/** Method to draw picture with specified width and height
* and display it in window at (x, y).
*
* @param picture the picture object to draw.
* @param x the x-coordinate of the top left corner of picture.
* @param y the y-coordinate of the top left corner of picture.
* @param width the picture width
* @param height the picture height
*/
public void drawPicture(BufferedImage picture, int x, int y, int width, int height) {
bufferedImageGraphics.drawImage(picture, x, y, width, height, null);
window.repaint();
}
/** Method to display a BufferedImage object
* in window at a specified location.
*
* @param picture is a BufferedImage object.
* @param x the picture is positioned at this x-coordinate.
* @param y the picture is positioned at this y-coordinate.
*/
public void drawPicture(BufferedImage picture, int x, int y) {
bufferedImageGraphics.drawImage(picture, x, y, picture.getWidth(), picture.getHeight(), null);
window.repaint();
}
/** Method to draw picture from given file on window.
*
* @param s the name of the file containing the picture.
*/
public void drawPicture(String s) {
File filename = new File(s);
BufferedImage picture = null;
try {
picture = ImageIO.read(filename);
} catch (IOException e) {
System.out.println("An error has occurred while reading image file. Check that file exists and is in the correct directory.");
System.exit(1);
}
bufferedImageGraphics.drawImage(picture, 0, 0, picture.getWidth(), picture.getHeight(), null);
window.repaint();
}
/** Method to write text at the specified location
* (x, y) in the window.
*
* @param s the text to be written.
* @param x the x-coordinate of the starting position of the text.
* @param y the y-coordinate of the starting position of the text.
*/
public void drawString(String s, float x, float y) {
bufferedImageGraphics.drawString(s, x, y);
window.repaint();
}
/** Method to fill an object with a color.
*
* @param obj the graphics object to be filled.
*/
public void fill(Shape obj) {
bufferedImageGraphics.fill(obj);
window.repaint();
}
/** Method to return the graphics context associated
* with the window.
*
* @return the graphics context associated with
* the BufferedImage object called bufferedImage on
* which all the drawing is done before it is rendered
* to the screen.
*/
public Graphics2D getGraphics() {
return bufferedImageGraphics;
}
/** Method to load picture from file into memory. This will
* store the picture in memory as type BufferedImage.
*
* @param f the name of the file from which to load
* the picture.
* @return the reference to the BufferedImage object.
*/
public BufferedImage loadPicture(String f) {
File filename = new File(f);
BufferedImage picture = null;
try {
picture = ImageIO.read(filename);
} catch (IOException e) {
System.out.println("An error has occurred while reading image file. Check that file exists and is in the correct directory.");
System.exit(1);
}
return picture;
}
/** Method to change the font.
*
* @param f the new font.
*/
public void setFont(Font f) {
bufferedImageGraphics.setFont(f);
}
/** Method to change the color.
*
* @param c the new color.
*/
public void setPaint(Color c) {
bufferedImageGraphics.setPaint(c);
}
/** Method to change the line thickness.
*
* @param s the new stroke.
*/
public void setStroke(BasicStroke s) {
bufferedImageGraphics.setStroke(s);
}
}