1 | package org.greenstone.gatherer.feedback;
|
---|
2 |
|
---|
3 | import java.awt.image.*;
|
---|
4 | import java.awt.geom.*;
|
---|
5 | import java.awt.geom.Line2D.*;
|
---|
6 | import java.awt.geom.Line2D.Double;
|
---|
7 | import java.io.*;
|
---|
8 | import javax.swing.*;
|
---|
9 | import java.awt.*;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * This is the class that will allow user to draw a same line to 2 different BufferedImage
|
---|
13 | * at the same time.
|
---|
14 | * The first BufferedImage is BufferedImage with some Image as the based and then
|
---|
15 | * we draw a line on it, and the second BufferedImage is just a transparent Image and then we
|
---|
16 | * draw a line on it. The line drawn in first BufferedImage is exactly the same with the line
|
---|
17 | * drawn in second BufferedImage.The Image drawn in the first bufferedimage is the
|
---|
18 | * screen shot image of the whole screen.
|
---|
19 | * @author Veronica Liesaputra
|
---|
20 | */
|
---|
21 | public class Graphs
|
---|
22 | {
|
---|
23 | /**
|
---|
24 | * This is the first buffered image.
|
---|
25 | * It will drawn with an image first to be its based, and a line
|
---|
26 | * that are identical to line drawn the second BufferedImage, will be drawn on it.
|
---|
27 | */
|
---|
28 | private BufferedImage I = null;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * This is the second buffered image.
|
---|
32 | * Its a transaparent buffered image and a line that are indetical to line
|
---|
33 | * drawn in the first BufferedImage, will be drawn on it.
|
---|
34 | */
|
---|
35 | private BufferedImage I2;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * This is the Graphics for the first BufferedImage.
|
---|
39 | * This will allow user to draw a line and an image on the first buffered image.
|
---|
40 | */
|
---|
41 | private Graphics2D G;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * This is the Graphics for the second BufferedImage.
|
---|
45 | * This will allow user to draw a line on the second buffered image.
|
---|
46 | */
|
---|
47 | private Graphics2D G2;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * This is the arrays of the 3 BufferedImage.
|
---|
51 | */
|
---|
52 | private BufferedImage[] screen;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * This is the graphics for screen[0] BufferedImage.
|
---|
56 | * This will allow user to draw something on the screen[0] image.
|
---|
57 | */
|
---|
58 | private Graphics2D G3;
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * This is the width of the image.
|
---|
62 | */
|
---|
63 | private double imwidth;
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * This is the height of the image.
|
---|
67 | */
|
---|
68 | private double imheight;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * This is the constructor that will initialised the BufferedImage and
|
---|
72 | * its Graphics.
|
---|
73 | * (Precondition: (sh != null))
|
---|
74 | * @param sh is the images.
|
---|
75 | */
|
---|
76 | public Graphs (BufferedImage[] sh)
|
---|
77 | {
|
---|
78 | screen = sh;
|
---|
79 |
|
---|
80 | if (screen[0] != null)
|
---|
81 | {
|
---|
82 | imwidth = screen[0].getWidth();
|
---|
83 | imheight = screen[0].getHeight();
|
---|
84 |
|
---|
85 |
|
---|
86 | if((screen[1] == null)&& (screen[2] == null))
|
---|
87 | {
|
---|
88 | I = new BufferedImage(screen[0].getWidth(),screen[0].getHeight(),
|
---|
89 | BufferedImage.TYPE_INT_RGB);
|
---|
90 | I2 = new BufferedImage(I.getWidth(),I.getHeight(),
|
---|
91 | BufferedImage.TYPE_INT_ARGB);
|
---|
92 | }
|
---|
93 | else
|
---|
94 | {
|
---|
95 | I = screen[1];
|
---|
96 | I2 = screen[2];
|
---|
97 | }
|
---|
98 | start();
|
---|
99 | }
|
---|
100 | else
|
---|
101 | System.out.println("weird");
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * This method will make sure that everything is null-ed and dispose properly.
|
---|
106 | */
|
---|
107 | public void flush()
|
---|
108 | {
|
---|
109 | G.dispose();
|
---|
110 | G = null;
|
---|
111 | G3.dispose();
|
---|
112 | G3 = null;
|
---|
113 | G2.dispose();
|
---|
114 | G2 = null;
|
---|
115 |
|
---|
116 | int i;
|
---|
117 | for ( i = 0 ; i < 3; i++)
|
---|
118 | {
|
---|
119 | if (screen[i] != null)
|
---|
120 | {
|
---|
121 | screen[i].flush();
|
---|
122 | screen[i] = null;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | I.flush();
|
---|
127 | I = null;
|
---|
128 | I2.flush();
|
---|
129 | I2 = null;
|
---|
130 | System.gc();
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * This method will cleared the scribble lines in the images.
|
---|
135 | */
|
---|
136 | public void reset()
|
---|
137 | {
|
---|
138 | I.flush();
|
---|
139 | I = null;
|
---|
140 | System.gc();
|
---|
141 | I2.flush();
|
---|
142 | I2 = null;
|
---|
143 | G.dispose();
|
---|
144 | System.gc();
|
---|
145 | G2.dispose();
|
---|
146 | G = null;
|
---|
147 | G2 = null;
|
---|
148 | System.gc();
|
---|
149 |
|
---|
150 | I = new BufferedImage(screen[0].getWidth(),
|
---|
151 | screen[0].getHeight(),
|
---|
152 | BufferedImage.TYPE_INT_RGB);
|
---|
153 | I2 = new BufferedImage(I.getWidth(),I.getHeight(),
|
---|
154 | BufferedImage.TYPE_INT_ARGB);
|
---|
155 |
|
---|
156 | screen[1] = null;
|
---|
157 | screen[2] = null;
|
---|
158 |
|
---|
159 | start();
|
---|
160 | }
|
---|
161 | /**
|
---|
162 | * This method will setup the 3 Graphics for the 3 BufferedImage.
|
---|
163 | * Here the graphics for the bufferedimage will be created and the appropriate image
|
---|
164 | * will be drawn to the first bufferedimage.
|
---|
165 | */
|
---|
166 | public void start ()
|
---|
167 | {
|
---|
168 | G3 = screen[0].createGraphics();
|
---|
169 | G3.drawImage((Image) screen[0],0,0,new JLabel());
|
---|
170 |
|
---|
171 | G = I.createGraphics();
|
---|
172 | if (screen[1] == null)
|
---|
173 | G.drawImage((Image) screen[0],0,0,new JLabel());
|
---|
174 | else
|
---|
175 | G.drawImage ((Image) screen[1],0,0,new JLabel());
|
---|
176 | G2 = I2.createGraphics();
|
---|
177 | }
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * This method will draw a red line on both BufferedImage from the source point
|
---|
181 | * to the destination point.
|
---|
182 | * @param x1 is the x-coordinate of the source point.
|
---|
183 | * @param y1 is the y-coordinate of the source point.
|
---|
184 | * @param x2 is the x-coordinate of the destination point.
|
---|
185 | * @param y2 is the y-coordinate of the destination point.
|
---|
186 | */
|
---|
187 | public void drawLines(int x1,int y1,int x2,int y2)
|
---|
188 | {
|
---|
189 | G2.setColor(Color.red);
|
---|
190 | G.setColor(Color.red);
|
---|
191 |
|
---|
192 | G.setStroke(new BasicStroke(2.5f));
|
---|
193 | G2.setStroke(new BasicStroke(2.5f));
|
---|
194 |
|
---|
195 | G2.drawLine(x1,y1,x2,y2);
|
---|
196 | G.drawLine(x1,y1,x2,y2);
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * This method will draw a white line on all 3 BufferedImage from the source point
|
---|
202 | * to the destination point.
|
---|
203 | * @param x1 is the x-coordinate of the source point.
|
---|
204 | * @param y1 is the y-coordinate of the source point.
|
---|
205 | * @param x2 is the x-coordinate of the destination point.
|
---|
206 | * @param y2 is the y-coordinate of the destination point.
|
---|
207 | */
|
---|
208 | public void eraseLines(int x1,int y1,int x2,int y2)
|
---|
209 | {
|
---|
210 | G3.setColor(Color.white);
|
---|
211 | G2.setColor(Color.white);
|
---|
212 | G.setColor(Color.white);
|
---|
213 |
|
---|
214 | G.setStroke(new BasicStroke(25.5f));
|
---|
215 | G2.setStroke(new BasicStroke(25.5f));
|
---|
216 | G3.setStroke(new BasicStroke(25.5f));
|
---|
217 |
|
---|
218 | G3.drawLine(x1,y1,x2,y2);
|
---|
219 | G2.drawLine(x1,y1,x2,y2);
|
---|
220 | G.drawLine(x1,y1,x2,y2);
|
---|
221 | }
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * This method will get the first BufferedImage.
|
---|
225 | * This is the BufferedImage where it will already be drawn with the
|
---|
226 | * screen shot image and the lines.
|
---|
227 | * @return the first BufferedImage.
|
---|
228 | */
|
---|
229 | public BufferedImage getImage ()
|
---|
230 | {
|
---|
231 | return I;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * This method will get the second BufferedImage.
|
---|
236 | * This is the BufferedImage where its base is transparent and it will
|
---|
237 | * already be drawn with the lines.
|
---|
238 | * @return the second BufferedImage.
|
---|
239 | */
|
---|
240 | public BufferedImage getImage2()
|
---|
241 | {
|
---|
242 | return I2;
|
---|
243 | }
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * This method will get the image without scribble lines only the white lines.
|
---|
247 | * @return the image without scribble lines.
|
---|
248 | */
|
---|
249 | public BufferedImage getScreenImage()
|
---|
250 | {
|
---|
251 | return screen[0];
|
---|
252 | }
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * This method will get the window out of the whole screen image.
|
---|
256 | * @param xcoord the xcoordinate of the window in the image.
|
---|
257 | * @param ycoord the ycoordinate of the window in the image.
|
---|
258 | * @param width the width of the window in the image.
|
---|
259 | * @param height the height of the window in the image.
|
---|
260 | * @return the BufferedImage of the window only from the 3 buffered image.
|
---|
261 | */
|
---|
262 | public BufferedImage[] getWindowVersion(int xcoord,int ycoord,int width,int height)
|
---|
263 | {
|
---|
264 | BufferedImage[] window;
|
---|
265 | window = new BufferedImage[3];
|
---|
266 |
|
---|
267 | Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
268 |
|
---|
269 | if (xcoord < 0 )
|
---|
270 | xcoord = 0;
|
---|
271 | if (ycoord < 0)
|
---|
272 | ycoord = 0;
|
---|
273 |
|
---|
274 | if (xcoord >= imwidth)
|
---|
275 | xcoord = (int)(imwidth - 1);
|
---|
276 | if (ycoord >= imheight)
|
---|
277 | ycoord = (int)(imheight - 1);
|
---|
278 | if ((xcoord + width) > imwidth)
|
---|
279 | width = (int)(imwidth - xcoord);
|
---|
280 | if ((ycoord + height) > imheight)
|
---|
281 | height = (int)(imheight - ycoord);
|
---|
282 |
|
---|
283 | if (width <= 0)
|
---|
284 | width = 1;
|
---|
285 | if (height <= 0)
|
---|
286 | height = 1;
|
---|
287 |
|
---|
288 | window[0] = getClipVersion(screen[0],xcoord,ycoord,width,height);
|
---|
289 |
|
---|
290 | window[1] = getClipVersion(I,xcoord,ycoord,width,height);
|
---|
291 |
|
---|
292 | window[2] = getClipVersion(I2,xcoord,ycoord,width,height);
|
---|
293 |
|
---|
294 | return window;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * This method will return the subimage of the big buffered image.
|
---|
299 | * @param tag the original buffered image.
|
---|
300 | * @param x the x location of subimage.
|
---|
301 | * @param y the y location of subimage.
|
---|
302 | * @param w the width of the subimage.
|
---|
303 | * @param h the height of the subimage.
|
---|
304 | * @return the subimage.
|
---|
305 | */
|
---|
306 | public BufferedImage getClipVersion (BufferedImage tag,int x, int y, int w, int h)
|
---|
307 | {
|
---|
308 | BufferedImage sub;
|
---|
309 | sub = tag.getSubimage(x,y,w,h);
|
---|
310 |
|
---|
311 | return sub;
|
---|
312 | }
|
---|
313 |
|
---|
314 | /**
|
---|
315 | * This method will return the screen image version of the give buffered image.
|
---|
316 | * @param sh the buffered image that we going to draw in the screen image version.
|
---|
317 | * @param xcoord the x location of subimage.
|
---|
318 | * @param ycoord the y location of subimage.
|
---|
319 | * @param w the width of the subimage.
|
---|
320 | * @param h the height of the subimage.
|
---|
321 | */
|
---|
322 | public void setScreenVersion(BufferedImage[] sh,int xcoord,int ycoord,int w, int h)
|
---|
323 | {
|
---|
324 | if (xcoord < 0 )
|
---|
325 | xcoord = 0;
|
---|
326 | if (ycoord < 0)
|
---|
327 | ycoord = 0;
|
---|
328 |
|
---|
329 | if (xcoord >= imwidth)
|
---|
330 | xcoord = (int) (imwidth - 1);
|
---|
331 | if (ycoord >= imheight)
|
---|
332 | ycoord = (int) (imheight - 1);
|
---|
333 | if ((xcoord + w) > imwidth)
|
---|
334 | w = (int) (imwidth - xcoord);
|
---|
335 | if ((ycoord + h) > imheight)
|
---|
336 | h = (int) (imheight - ycoord);
|
---|
337 |
|
---|
338 | if (w <= 0)
|
---|
339 | w = 1;
|
---|
340 | if (h <= 0)
|
---|
341 | h = 1;
|
---|
342 |
|
---|
343 | G3.drawImage(sh[0], xcoord,ycoord,new JLabel());
|
---|
344 |
|
---|
345 |
|
---|
346 | G.drawImage(sh[1], xcoord,ycoord,new JLabel());
|
---|
347 |
|
---|
348 |
|
---|
349 | G2.drawImage(sh[2], xcoord,ycoord,new JLabel());
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 |
|
---|
354 |
|
---|
355 |
|
---|
356 |
|
---|
357 |
|
---|
358 |
|
---|
359 |
|
---|
360 |
|
---|