1 | /* |
---|
2 | * This program is free software; you can redistribute it and/or modify |
---|
3 | * it under the terms of the GNU General Public License as published by |
---|
4 | * the Free Software Foundation; either version 2 of the License, or |
---|
5 | * (at your option) any later version. |
---|
6 | * |
---|
7 | * This program is distributed in the hope that it will be useful, |
---|
8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | * GNU General Public License for more details. |
---|
11 | * |
---|
12 | * You should have received a copy of the GNU General Public License |
---|
13 | * along with this program; if not, write to the Free Software |
---|
14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
15 | */ |
---|
16 | |
---|
17 | /* |
---|
18 | * PostscriptGraphics.java |
---|
19 | * Copyright (C) 2003 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.gui.visualize; |
---|
24 | |
---|
25 | import java.awt.image.renderable.*; |
---|
26 | import java.awt.*; |
---|
27 | import java.awt.geom.*; |
---|
28 | import java.awt.font.*; |
---|
29 | import java.io.*; |
---|
30 | import java.util.*; |
---|
31 | import java.awt.image.*; |
---|
32 | import java.text.*; |
---|
33 | |
---|
34 | |
---|
35 | /** |
---|
36 | * The PostscriptGraphics class extends the Graphics2D class to |
---|
37 | * produce an encapsulated postscript file rather than on-screen display. |
---|
38 | * <p> |
---|
39 | * Currently only a small (but useful) subset of Graphics methods have been |
---|
40 | * implemented. |
---|
41 | * To handle the ability to Clone a Graphics object, the graphics state of the |
---|
42 | * eps is set from the graphics state of the local PostscriptGraphics before output. |
---|
43 | * To use, create a PostscriptGraphics object, and pass it to the PaintComponent |
---|
44 | * method of a JComponent. |
---|
45 | * <p> |
---|
46 | * If necessary additional font replacements can be inserted, since some fonts |
---|
47 | * might be displayed incorrectly. |
---|
48 | * |
---|
49 | * @see #addPSFontReplacement(String, String) |
---|
50 | * @see #m_PSFontReplacement |
---|
51 | * @author Dale Fletcher (dale@cs.waikato.ac.nz) |
---|
52 | * @author FracPete (fracpete at waikato dot ac dot nz) |
---|
53 | * @version $Revision: 1.5 $ |
---|
54 | */ |
---|
55 | |
---|
56 | public class PostscriptGraphics extends Graphics2D { |
---|
57 | |
---|
58 | /** |
---|
59 | * This inner class is used to maintain the graphics states of the PostScript |
---|
60 | * file and graphics context. |
---|
61 | */ |
---|
62 | private class GraphicsState { |
---|
63 | /** The current pen color */ |
---|
64 | protected Color m_currentColor; |
---|
65 | |
---|
66 | /** The current Font */ |
---|
67 | protected Font m_currentFont; |
---|
68 | |
---|
69 | /** The current Stroke (not yet used) */ |
---|
70 | protected Stroke m_currentStroke; |
---|
71 | |
---|
72 | /** x,y Translation */ |
---|
73 | protected int m_xOffset; |
---|
74 | protected int m_yOffset; |
---|
75 | |
---|
76 | /** the scale factors */ |
---|
77 | protected double m_xScale; |
---|
78 | protected double m_yScale; |
---|
79 | |
---|
80 | /** |
---|
81 | * Create a new GraphicsState with default values. |
---|
82 | */ |
---|
83 | GraphicsState(){ |
---|
84 | m_currentColor = Color.white; |
---|
85 | m_currentFont = new Font ("Courier", Font.PLAIN, 11); |
---|
86 | m_currentStroke = new BasicStroke(); |
---|
87 | m_xOffset = 0; |
---|
88 | m_yOffset = 0; |
---|
89 | m_xScale = 1.0; |
---|
90 | m_yScale = 1.0; |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Create a new cloned GraphicsState |
---|
95 | * |
---|
96 | * @param copy The GraphicsState to clone |
---|
97 | */ |
---|
98 | GraphicsState(GraphicsState copy){ |
---|
99 | m_currentColor = copy.m_currentColor; |
---|
100 | m_currentFont = copy.m_currentFont; |
---|
101 | m_currentStroke = copy.m_currentStroke; |
---|
102 | m_xOffset = copy.m_xOffset; |
---|
103 | m_yOffset = copy.m_yOffset; |
---|
104 | m_xScale = copy.m_xScale; |
---|
105 | m_yScale = copy.m_yScale; |
---|
106 | } |
---|
107 | |
---|
108 | /* Stroke Methods */ |
---|
109 | protected Stroke getStroke(){ |
---|
110 | return m_currentStroke; |
---|
111 | } |
---|
112 | |
---|
113 | protected void setStroke(Stroke s){ |
---|
114 | m_currentStroke = s; |
---|
115 | } |
---|
116 | |
---|
117 | /* Font Methods */ |
---|
118 | protected Font getFont(){ |
---|
119 | return m_currentFont; |
---|
120 | } |
---|
121 | |
---|
122 | protected void setFont(Font f){ |
---|
123 | m_currentFont = f; |
---|
124 | } |
---|
125 | |
---|
126 | /* Color Methods */ |
---|
127 | protected Color getColor(){ |
---|
128 | return m_currentColor; |
---|
129 | } |
---|
130 | |
---|
131 | protected void setColor(Color c){ |
---|
132 | m_currentColor = c; |
---|
133 | } |
---|
134 | |
---|
135 | /* Translation methods */ |
---|
136 | protected void setXOffset(int xo){ |
---|
137 | m_xOffset = xo; |
---|
138 | } |
---|
139 | |
---|
140 | protected void setYOffset(int yo){ |
---|
141 | m_yOffset = yo; |
---|
142 | } |
---|
143 | |
---|
144 | protected int getXOffset(){ |
---|
145 | return m_xOffset; |
---|
146 | } |
---|
147 | |
---|
148 | protected int getYOffset(){ |
---|
149 | return m_yOffset; |
---|
150 | } |
---|
151 | |
---|
152 | protected void setXScale(double x){ |
---|
153 | m_xScale = x; |
---|
154 | } |
---|
155 | |
---|
156 | protected void setYScale(double y){ |
---|
157 | m_yScale = y; |
---|
158 | } |
---|
159 | |
---|
160 | protected double getXScale(){ |
---|
161 | return m_xScale; |
---|
162 | } |
---|
163 | |
---|
164 | protected double getYScale(){ |
---|
165 | return m_yScale; |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | /** The bounding box of the output */ |
---|
170 | protected Rectangle m_extent; |
---|
171 | |
---|
172 | /** The output file */ |
---|
173 | protected PrintStream m_printstream; |
---|
174 | |
---|
175 | /** The current global PostScript graphics state for all cloned objects */ |
---|
176 | protected GraphicsState m_psGraphicsState; |
---|
177 | |
---|
178 | /** The current local graphics state for this PostscriptGraphics object */ |
---|
179 | protected GraphicsState m_localGraphicsState; |
---|
180 | |
---|
181 | /** whether to print some debug information */ |
---|
182 | protected final static boolean DEBUG = false; |
---|
183 | |
---|
184 | /** the font replacement */ |
---|
185 | protected static Hashtable m_PSFontReplacement; |
---|
186 | |
---|
187 | /** output if we're in debug mode */ |
---|
188 | static { |
---|
189 | if (DEBUG) |
---|
190 | System.err.println(PostscriptGraphics.class.getName() + ": DEBUG ON"); |
---|
191 | |
---|
192 | // get font replacements |
---|
193 | m_PSFontReplacement = new Hashtable(); |
---|
194 | m_PSFontReplacement.put("SansSerif.plain", "Helvetica.plain"); // SansSerif.plain is displayed as Courier in GV??? |
---|
195 | m_PSFontReplacement.put("Dialog.plain", "Helvetica.plain"); // dialog is a Sans Serif font, but GV displays it as Courier??? |
---|
196 | m_PSFontReplacement.put("Microsoft Sans Serif", "Helvetica.plain"); // MS Sans Serif is a Sans Serif font (hence the name!), but GV displays it as Courier??? |
---|
197 | m_PSFontReplacement.put("MicrosoftSansSerif", "Helvetica.plain"); // MS Sans Serif is a Sans Serif font (hence the name!), but GV displays it as Courier??? |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * Constructor |
---|
202 | * Creates a new PostscriptGraphics object, given dimensions and |
---|
203 | * output file. |
---|
204 | * |
---|
205 | * @param width The width of eps in points. |
---|
206 | * @param height The height of eps in points. |
---|
207 | * @param os File to send postscript to. |
---|
208 | */ |
---|
209 | public PostscriptGraphics(int width, int height, OutputStream os ){ |
---|
210 | |
---|
211 | m_extent = new Rectangle(0, 0, height, width); |
---|
212 | m_printstream = new PrintStream(os); |
---|
213 | m_localGraphicsState = new GraphicsState(); |
---|
214 | m_psGraphicsState = new GraphicsState(); |
---|
215 | |
---|
216 | Header(); |
---|
217 | } |
---|
218 | |
---|
219 | /** |
---|
220 | * Creates a new cloned PostscriptGraphics object. |
---|
221 | * |
---|
222 | * @param copy The PostscriptGraphics object to clone. |
---|
223 | */ |
---|
224 | PostscriptGraphics(PostscriptGraphics copy){ |
---|
225 | |
---|
226 | m_extent = new Rectangle(copy.m_extent); |
---|
227 | m_printstream = copy.m_printstream; |
---|
228 | m_localGraphicsState = new GraphicsState(copy.m_localGraphicsState); // create a local copy of the current state |
---|
229 | m_psGraphicsState = copy.m_psGraphicsState; // link to global state of eps file |
---|
230 | } |
---|
231 | |
---|
232 | /** |
---|
233 | * Finalizes output file. |
---|
234 | */ |
---|
235 | public void finished(){ |
---|
236 | m_printstream.flush(); |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * Output postscript header to PrintStream, including helper macros. |
---|
241 | */ |
---|
242 | private void Header(){ |
---|
243 | m_printstream.println("%!PS-Adobe-3.0 EPSF-3.0"); |
---|
244 | m_printstream.println("%%BoundingBox: 0 0 " + xScale(m_extent.width) + " " + yScale(m_extent.height)); |
---|
245 | m_printstream.println("%%CreationDate: " + Calendar.getInstance().getTime()); |
---|
246 | |
---|
247 | m_printstream.println("/Oval { % x y w h filled"); |
---|
248 | m_printstream.println("gsave"); |
---|
249 | m_printstream.println("/filled exch def /h exch def /w exch def /y exch def /x exch def"); |
---|
250 | m_printstream.println("x w 2 div add y h 2 div sub translate"); |
---|
251 | m_printstream.println("1 h w div scale"); |
---|
252 | m_printstream.println("filled {0 0 moveto} if"); |
---|
253 | m_printstream.println("0 0 w 2 div 0 360 arc"); |
---|
254 | m_printstream.println("filled {closepath fill} {stroke} ifelse grestore} bind def"); |
---|
255 | |
---|
256 | m_printstream.println("/Rect { % x y w h filled"); |
---|
257 | m_printstream.println("/filled exch def /h exch def /w exch def /y exch def /x exch def"); |
---|
258 | m_printstream.println("newpath "); |
---|
259 | m_printstream.println("x y moveto"); |
---|
260 | m_printstream.println("w 0 rlineto"); |
---|
261 | m_printstream.println("0 h neg rlineto"); |
---|
262 | m_printstream.println("w neg 0 rlineto"); |
---|
263 | m_printstream.println("closepath"); |
---|
264 | m_printstream.println("filled {fill} {stroke} ifelse} bind def"); |
---|
265 | |
---|
266 | m_printstream.println("%%BeginProlog\n%%EndProlog"); |
---|
267 | m_printstream.println("%%Page 1 1"); |
---|
268 | setFont(null); // set to default |
---|
269 | setColor(null); // set to default |
---|
270 | setStroke(null); // set to default |
---|
271 | } |
---|
272 | |
---|
273 | /** |
---|
274 | * adds the PS font name to replace and its replacement in the replacement |
---|
275 | * hashtable |
---|
276 | * |
---|
277 | * @param replace the PS font name to replace |
---|
278 | * @param with the PS font name to replace the font with |
---|
279 | */ |
---|
280 | public static void addPSFontReplacement(String replace, String with) { |
---|
281 | m_PSFontReplacement.put(replace, with); |
---|
282 | } |
---|
283 | |
---|
284 | /** |
---|
285 | * Convert Java Y coordinate (0 = top) to PostScript (0 = bottom) |
---|
286 | * Also apply current Translation |
---|
287 | * @param y Java Y coordinate |
---|
288 | * @return translated Y to postscript |
---|
289 | */ |
---|
290 | private int yTransform(int y){ |
---|
291 | return (m_extent.height - (m_localGraphicsState.getYOffset() + y)); |
---|
292 | } |
---|
293 | |
---|
294 | /** |
---|
295 | * Apply current X Translation |
---|
296 | * @param x Java X coordinate |
---|
297 | * @return translated X to postscript |
---|
298 | */ |
---|
299 | private int xTransform(int x){ |
---|
300 | return (m_localGraphicsState.getXOffset() + x); |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * scales the given number with the provided scale factor |
---|
305 | */ |
---|
306 | private int doScale(int number, double factor) { |
---|
307 | return (int) StrictMath.round(number * factor); |
---|
308 | } |
---|
309 | |
---|
310 | /** |
---|
311 | * scales the given x value with current x scale factor |
---|
312 | */ |
---|
313 | private int xScale(int x) { |
---|
314 | return doScale(x, m_localGraphicsState.getXScale()); |
---|
315 | } |
---|
316 | |
---|
317 | /** |
---|
318 | * scales the given y value with current y scale factor |
---|
319 | */ |
---|
320 | private int yScale(int y) { |
---|
321 | return doScale(y, m_localGraphicsState.getYScale()); |
---|
322 | } |
---|
323 | |
---|
324 | /** Set the current eps graphics state to that of the local one |
---|
325 | */ |
---|
326 | private void setStateToLocal(){ |
---|
327 | setColor(this.getColor()); |
---|
328 | setFont(this.getFont()); |
---|
329 | setStroke(this.getStroke()); |
---|
330 | } |
---|
331 | |
---|
332 | /** |
---|
333 | * returns a two hexadecimal representation of i, if shorter than 2 chars |
---|
334 | * then an additional "0" is put in front |
---|
335 | */ |
---|
336 | private String toHex(int i) { |
---|
337 | String result; |
---|
338 | |
---|
339 | result = Integer.toHexString(i); |
---|
340 | if (result.length() < 2) |
---|
341 | result = "0" + result; |
---|
342 | |
---|
343 | return result; |
---|
344 | } |
---|
345 | |
---|
346 | /***** overridden Graphics methods *****/ |
---|
347 | |
---|
348 | /** |
---|
349 | * Draw a filled rectangle with the background color. |
---|
350 | * |
---|
351 | * @param x starting x coord |
---|
352 | * @param y starting y coord |
---|
353 | * @param width rectangle width |
---|
354 | * @param height rectangle height |
---|
355 | */ |
---|
356 | public void clearRect(int x, int y, int width, int height) { |
---|
357 | setStateToLocal(); |
---|
358 | Color saveColor = getColor(); |
---|
359 | setColor(Color.white); // background color for page |
---|
360 | m_printstream.println(xTransform(xScale(x)) + " " + yTransform(yScale(y)) + " " + xScale(width) + " " + yScale(height) + " true Rect"); |
---|
361 | setColor(saveColor); |
---|
362 | } |
---|
363 | |
---|
364 | /** |
---|
365 | * Not implemented |
---|
366 | */ |
---|
367 | public void clipRect(int x, int y, int width, int height) {} |
---|
368 | |
---|
369 | /** |
---|
370 | * Not implemented |
---|
371 | */ |
---|
372 | public void copyArea(int x, int y, int width, int height, int dx, int dy) {} |
---|
373 | |
---|
374 | /** |
---|
375 | * Clone a PostscriptGraphics object |
---|
376 | */ |
---|
377 | public Graphics create() { |
---|
378 | if (DEBUG) |
---|
379 | m_printstream.println("%create"); |
---|
380 | PostscriptGraphics psg = new PostscriptGraphics(this); |
---|
381 | return(psg); |
---|
382 | } |
---|
383 | |
---|
384 | /** |
---|
385 | * Not implemented |
---|
386 | */ |
---|
387 | public void dispose(){} |
---|
388 | |
---|
389 | /** |
---|
390 | * Draw an outlined rectangle with 3D effect in current pen color. |
---|
391 | * (Current implementation: draw simple outlined rectangle) |
---|
392 | * |
---|
393 | * @param x starting x coord |
---|
394 | * @param y starting y coord |
---|
395 | * @param width rectangle width |
---|
396 | * @param height rectangle height |
---|
397 | * @param raised True: appear raised, False: appear etched |
---|
398 | */ |
---|
399 | public void draw3DRect(int x, int y, int width, int height, boolean raised){ |
---|
400 | drawRect(x,y,width,height); |
---|
401 | } |
---|
402 | |
---|
403 | /** |
---|
404 | * Not implemented |
---|
405 | */ |
---|
406 | public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle){} |
---|
407 | |
---|
408 | /** |
---|
409 | * simply calls drawString(String,int,int) |
---|
410 | * |
---|
411 | * @see #drawString(String,int,int) |
---|
412 | */ |
---|
413 | public void drawBytes(byte[] data, int offset, int length, int x, int y) { |
---|
414 | drawString(new String(data, offset, length), x, y); |
---|
415 | } |
---|
416 | |
---|
417 | /** |
---|
418 | * simply calls drawString(String,int,int) |
---|
419 | * |
---|
420 | * @see #drawString(String,int,int) |
---|
421 | */ |
---|
422 | public void drawChars(char[] data, int offset, int length, int x, int y) { |
---|
423 | drawString(new String(data, offset, length), x, y); |
---|
424 | } |
---|
425 | |
---|
426 | /** |
---|
427 | * calls drawImage(Image,int,int,int,int,Color,ImageObserver) |
---|
428 | * |
---|
429 | * @see #drawImage(Image,int,int,int,int,Color,ImageObserver) |
---|
430 | */ |
---|
431 | public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer){ |
---|
432 | return drawImage(img, x, y, img.getWidth(observer), img.getHeight(observer), bgcolor, observer); |
---|
433 | } |
---|
434 | |
---|
435 | /** |
---|
436 | * calls drawImage(Image,int,int,Color,ImageObserver) with Color.WHITE as |
---|
437 | * background color |
---|
438 | * |
---|
439 | * @see #drawImage(Image,int,int,Color,ImageObserver) |
---|
440 | * @see Color#WHITE |
---|
441 | */ |
---|
442 | public boolean drawImage(Image img, int x, int y, ImageObserver observer){ |
---|
443 | return drawImage(img, x, y, Color.WHITE, observer); |
---|
444 | } |
---|
445 | |
---|
446 | /** |
---|
447 | * PS see http://astronomy.swin.edu.au/~pbourke/geomformats/postscript/ |
---|
448 | * Java http://show.docjava.com:8086/book/cgij/doc/ip/graphics/SimpleImageFrame.java.html |
---|
449 | */ |
---|
450 | public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor, ImageObserver observer){ |
---|
451 | try { |
---|
452 | // get data from image |
---|
453 | int[] pixels = new int[width * height]; |
---|
454 | PixelGrabber grabber = new PixelGrabber(img, 0, 0, width, height, pixels, 0, width); |
---|
455 | grabber.grabPixels(); |
---|
456 | ColorModel model = ColorModel.getRGBdefault(); |
---|
457 | |
---|
458 | // print data to ps |
---|
459 | m_printstream.println("gsave"); |
---|
460 | m_printstream.println(xTransform(xScale(x)) + " " + (yTransform(yScale(y)) - yScale(height)) + " translate"); |
---|
461 | m_printstream.println(xScale(width) + " " + yScale(height) + " scale"); |
---|
462 | m_printstream.println(width + " " + height + " " + "8" + " [" + width + " 0 0 " + (-height) + " 0 " + height + "]"); |
---|
463 | m_printstream.println("{<"); |
---|
464 | |
---|
465 | int index; |
---|
466 | for (int i = 0; i < height; i++) { |
---|
467 | for (int j = 0; j < width; j++) { |
---|
468 | index = i * width + j; |
---|
469 | m_printstream.print(toHex(model.getRed(pixels[index]))); |
---|
470 | m_printstream.print(toHex(model.getGreen(pixels[index]))); |
---|
471 | m_printstream.print(toHex(model.getBlue(pixels[index]))); |
---|
472 | } |
---|
473 | m_printstream.println(); |
---|
474 | } |
---|
475 | |
---|
476 | m_printstream.println(">}"); |
---|
477 | m_printstream.println("false 3 colorimage"); |
---|
478 | m_printstream.println("grestore"); |
---|
479 | return true; |
---|
480 | } |
---|
481 | catch (Exception e) { |
---|
482 | e.printStackTrace(); |
---|
483 | return false; |
---|
484 | } |
---|
485 | } |
---|
486 | |
---|
487 | /** |
---|
488 | * calls drawImage(Image,int,int,int,int,Color,ImageObserver) with the color |
---|
489 | * WHITE as background |
---|
490 | * |
---|
491 | * @see #drawImage(Image,int,int,int,int,Color,ImageObserver) |
---|
492 | * @see Color#WHITE |
---|
493 | */ |
---|
494 | public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer){ |
---|
495 | return drawImage(img, x, y, width, height, Color.WHITE, observer); |
---|
496 | } |
---|
497 | |
---|
498 | /** |
---|
499 | * Not implemented |
---|
500 | */ |
---|
501 | public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, Color bgcolor, ImageObserver observer){ |
---|
502 | return false; |
---|
503 | } |
---|
504 | |
---|
505 | /** |
---|
506 | * calls drawImage(Image,int,int,int,int,int,int,int,int,Color,ImageObserver) |
---|
507 | * with Color.WHITE as background color |
---|
508 | * |
---|
509 | * @see #drawImage(Image,int,int,int,int,int,int,int,int,Color,ImageObserver) |
---|
510 | */ |
---|
511 | public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer){ |
---|
512 | return drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, Color.WHITE, observer); |
---|
513 | } |
---|
514 | |
---|
515 | |
---|
516 | /** |
---|
517 | * Draw a line in current pen color. |
---|
518 | * |
---|
519 | * @param x1 starting x coord |
---|
520 | * @param y1 starting y coord |
---|
521 | * @param x2 ending x coord |
---|
522 | * @param y2 ending y coord |
---|
523 | */ |
---|
524 | public void drawLine(int x1, int y1, int x2, int y2){ |
---|
525 | setStateToLocal(); |
---|
526 | m_printstream.println(xTransform(xScale(x1)) + " " + yTransform(yScale(y1)) + " moveto " + xTransform(xScale(x2)) + " " + yTransform(yScale(y2)) + " lineto stroke"); |
---|
527 | } |
---|
528 | |
---|
529 | /** |
---|
530 | * Draw an Oval outline in current pen color. |
---|
531 | * |
---|
532 | * @param x x-axis center of oval |
---|
533 | * @param y y-axis center of oval |
---|
534 | * @param width oval width |
---|
535 | * @param height oval height |
---|
536 | */ |
---|
537 | public void drawOval(int x, int y, int width, int height){ |
---|
538 | setStateToLocal(); |
---|
539 | m_printstream.println(xTransform(xScale(x)) + " " + yTransform(yScale(y)) + " " + xScale(width) + " " + yScale(height) + " false Oval"); |
---|
540 | } |
---|
541 | |
---|
542 | /** |
---|
543 | * Not implemented |
---|
544 | */ |
---|
545 | public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints){} |
---|
546 | |
---|
547 | /** |
---|
548 | * Not implemented |
---|
549 | */ |
---|
550 | public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints){} |
---|
551 | |
---|
552 | /** |
---|
553 | * Draw an outlined rectangle in current pen color. |
---|
554 | * |
---|
555 | * @param x starting x coord |
---|
556 | * @param y starting y coord |
---|
557 | * @param width rectangle width |
---|
558 | * @param height rectangle height |
---|
559 | */ |
---|
560 | public void drawRect(int x, int y, int width, int height){ |
---|
561 | setStateToLocal(); |
---|
562 | m_printstream.println(xTransform(xScale(x)) + " " + yTransform(yScale(y)) + " " + xScale(width) + " " + yScale(height) + " false Rect"); |
---|
563 | } |
---|
564 | |
---|
565 | /** |
---|
566 | * Not implemented |
---|
567 | */ |
---|
568 | public void drawRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight){} |
---|
569 | |
---|
570 | /** |
---|
571 | * Not implemented |
---|
572 | */ |
---|
573 | public void drawString(AttributedCharacterIterator iterator, int x, int y){} |
---|
574 | |
---|
575 | /** |
---|
576 | * Escapes brackets in the string with backslashes. |
---|
577 | * |
---|
578 | * @param s the string to escape |
---|
579 | * @return the escaped string |
---|
580 | */ |
---|
581 | protected String escape(String s) { |
---|
582 | StringBuffer result; |
---|
583 | int i; |
---|
584 | |
---|
585 | result = new StringBuffer(); |
---|
586 | |
---|
587 | for (i = 0; i < s.length(); i++) { |
---|
588 | if ( (s.charAt(i) == '(') || (s.charAt(i) == ')') ) |
---|
589 | result.append('\\'); |
---|
590 | result.append(s.charAt(i)); |
---|
591 | } |
---|
592 | |
---|
593 | return result.toString(); |
---|
594 | } |
---|
595 | |
---|
596 | /** |
---|
597 | * Draw text in current pen color. |
---|
598 | * |
---|
599 | * @param str Text to output |
---|
600 | * @param x starting x coord |
---|
601 | * @param y starting y coord |
---|
602 | */ |
---|
603 | public void drawString(String str, int x, int y){ |
---|
604 | setStateToLocal(); |
---|
605 | m_printstream.println(xTransform(xScale(x)) + " " + yTransform(yScale(y)) + " moveto" + " (" + escape(str) + ") show stroke"); |
---|
606 | } |
---|
607 | |
---|
608 | /** |
---|
609 | * Draw a filled rectangle with 3D effect in current pen color. |
---|
610 | * (Current implementation: draw simple filled rectangle) |
---|
611 | * |
---|
612 | * @param x starting x coord |
---|
613 | * @param y starting y coord |
---|
614 | * @param width rectangle width |
---|
615 | * @param height rectangle height |
---|
616 | * @param raised True: appear raised, False: appear etched |
---|
617 | */ |
---|
618 | public void fill3DRect(int x, int y, int width, int height, boolean raised){ |
---|
619 | fillRect(x, y, width, height); |
---|
620 | } |
---|
621 | |
---|
622 | /** |
---|
623 | * Not implemented |
---|
624 | */ |
---|
625 | public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle){} |
---|
626 | |
---|
627 | /** |
---|
628 | * Draw a filled Oval in current pen color. |
---|
629 | * |
---|
630 | * @param x x-axis center of oval |
---|
631 | * @param y y-axis center of oval |
---|
632 | * @param width oval width |
---|
633 | * @param height oval height |
---|
634 | */ |
---|
635 | public void fillOval(int x, int y, int width, int height){ |
---|
636 | setStateToLocal(); |
---|
637 | m_printstream.println(xTransform(xScale(x)) + " " + yTransform(yScale(y)) + " " + xScale(width) + " " + yScale(height) + " true Oval"); |
---|
638 | } |
---|
639 | |
---|
640 | /** |
---|
641 | * Not implemented |
---|
642 | */ |
---|
643 | public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints){} |
---|
644 | |
---|
645 | /** |
---|
646 | * Not implemented |
---|
647 | */ |
---|
648 | public void fillPolygon(Polygon p){} |
---|
649 | |
---|
650 | /** |
---|
651 | * Draw a filled rectangle in current pen color. |
---|
652 | * |
---|
653 | * @param x starting x coord |
---|
654 | * @param y starting y coord |
---|
655 | * @param width rectangle width |
---|
656 | * @param height rectangle height |
---|
657 | */ |
---|
658 | |
---|
659 | public void fillRect(int x, int y, int width, int height){ |
---|
660 | if (width == m_extent.width && height == m_extent.height) { |
---|
661 | clearRect(x, y, width, height); // if we're painting the entire background, just make it white |
---|
662 | } else { |
---|
663 | if (DEBUG) |
---|
664 | m_printstream.println("% fillRect"); |
---|
665 | setStateToLocal(); |
---|
666 | m_printstream.println(xTransform(xScale(x)) + " " + yTransform(yScale(y)) + " " + xScale(width) + " " + yScale(height) + " true Rect"); |
---|
667 | } |
---|
668 | } |
---|
669 | |
---|
670 | /** |
---|
671 | * Not implemented |
---|
672 | */ |
---|
673 | public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight){} |
---|
674 | |
---|
675 | /** |
---|
676 | * Not implemented |
---|
677 | */ |
---|
678 | public void finalize(){} |
---|
679 | |
---|
680 | /** |
---|
681 | * Not implemented |
---|
682 | */ |
---|
683 | public Shape getClip(){ |
---|
684 | return(null); |
---|
685 | } |
---|
686 | |
---|
687 | /** |
---|
688 | * This returns the full current drawing area |
---|
689 | * @return full drawing area |
---|
690 | */ |
---|
691 | public Rectangle getClipBounds(){ |
---|
692 | return(new Rectangle(0, 0, m_extent.width, m_extent.height)); |
---|
693 | } |
---|
694 | |
---|
695 | /** |
---|
696 | * This returns the full current drawing area |
---|
697 | * @return full drawing area |
---|
698 | */ |
---|
699 | public Rectangle getClipBounds(Rectangle r) { |
---|
700 | r.setBounds(0, 0, m_extent.width, m_extent.height); |
---|
701 | return r; |
---|
702 | } |
---|
703 | |
---|
704 | /** |
---|
705 | * Not implemented |
---|
706 | */ |
---|
707 | public Rectangle getClipRect() {return null;} |
---|
708 | |
---|
709 | /** |
---|
710 | * Get current pen color. |
---|
711 | * |
---|
712 | * @return current pen color. |
---|
713 | */ |
---|
714 | public Color getColor(){ |
---|
715 | return (m_localGraphicsState.getColor()); |
---|
716 | } |
---|
717 | |
---|
718 | /** |
---|
719 | * Get current font. |
---|
720 | * |
---|
721 | * @return current font. |
---|
722 | */ |
---|
723 | public Font getFont(){ |
---|
724 | return (m_localGraphicsState.getFont()); |
---|
725 | } |
---|
726 | |
---|
727 | /** |
---|
728 | * Get Font metrics |
---|
729 | * |
---|
730 | * @param f Font |
---|
731 | * @return Font metrics. |
---|
732 | */ |
---|
733 | public FontMetrics getFontMetrics(Font f){ |
---|
734 | return(Toolkit.getDefaultToolkit().getFontMetrics(f)); |
---|
735 | |
---|
736 | } |
---|
737 | |
---|
738 | /** |
---|
739 | * Not implemented |
---|
740 | */ |
---|
741 | public void setClip(int x, int y, int width, int height) {} |
---|
742 | |
---|
743 | /** |
---|
744 | * Not implemented |
---|
745 | */ |
---|
746 | public void setClip(Shape clip){} |
---|
747 | |
---|
748 | /** |
---|
749 | * Set current pen color. Default to black if null. |
---|
750 | * |
---|
751 | * @param c new pen color. |
---|
752 | */ |
---|
753 | public void setColor(Color c){ |
---|
754 | if (c != null){ |
---|
755 | m_localGraphicsState.setColor(c); |
---|
756 | if (m_psGraphicsState.getColor().equals(c)) { |
---|
757 | return; |
---|
758 | } |
---|
759 | m_psGraphicsState.setColor(c); |
---|
760 | } else { |
---|
761 | m_localGraphicsState.setColor(Color.black); |
---|
762 | m_psGraphicsState.setColor(getColor()); |
---|
763 | } |
---|
764 | m_printstream.print(getColor().getRed()/255.0); |
---|
765 | m_printstream.print(" "); |
---|
766 | m_printstream.print(getColor().getGreen()/255.0); |
---|
767 | m_printstream.print(" "); |
---|
768 | m_printstream.print(getColor().getBlue()/255.0); |
---|
769 | m_printstream.println(" setrgbcolor"); |
---|
770 | } |
---|
771 | |
---|
772 | /** |
---|
773 | * replaces the font (PS name) if necessary and returns the new name |
---|
774 | */ |
---|
775 | private static String replacePSFont(String font) { |
---|
776 | String result; |
---|
777 | |
---|
778 | result = font; |
---|
779 | |
---|
780 | // do we have to replace it? -> same style, size |
---|
781 | if (m_PSFontReplacement.containsKey(font)) { |
---|
782 | result = m_PSFontReplacement.get(font).toString(); |
---|
783 | if (DEBUG) |
---|
784 | System.out.println("switched font from '" + font + "' to '" + result + "'"); |
---|
785 | } |
---|
786 | |
---|
787 | return result; |
---|
788 | } |
---|
789 | |
---|
790 | /** |
---|
791 | * Set current font. Default to Plain Courier 11 if null. |
---|
792 | * |
---|
793 | * @param font new font. |
---|
794 | */ |
---|
795 | public void setFont(Font font){ |
---|
796 | |
---|
797 | if (font != null){ |
---|
798 | m_localGraphicsState.setFont(font); |
---|
799 | if ( font.getName().equals(m_psGraphicsState.getFont().getName()) |
---|
800 | && (m_psGraphicsState.getFont().getStyle() == font.getStyle()) |
---|
801 | && (m_psGraphicsState.getFont().getSize() == yScale(font.getSize()))) |
---|
802 | return; |
---|
803 | m_psGraphicsState.setFont(new Font(font.getName(), font.getStyle(), yScale(getFont().getSize()))); |
---|
804 | } |
---|
805 | else { |
---|
806 | m_localGraphicsState.setFont(new Font ("Courier", Font.PLAIN, 11)); |
---|
807 | m_psGraphicsState.setFont(getFont()); |
---|
808 | } |
---|
809 | |
---|
810 | m_printstream.println("/(" + replacePSFont(getFont().getPSName()) + ")" + " findfont"); |
---|
811 | m_printstream.println(yScale(getFont().getSize()) + " scalefont setfont"); |
---|
812 | } |
---|
813 | |
---|
814 | /** |
---|
815 | * Not implemented |
---|
816 | */ |
---|
817 | public void setPaintMode(){} |
---|
818 | |
---|
819 | /** |
---|
820 | * Not implemented |
---|
821 | */ |
---|
822 | public void setXORMode(Color c1){} |
---|
823 | |
---|
824 | /** |
---|
825 | * Translates the origin of the graphics context to the point (x, y) in the |
---|
826 | * current coordinate system. Modifies this graphics context so that its new |
---|
827 | * origin corresponds to the point (x, y) in this graphics context's original |
---|
828 | * coordinate system. All coordinates used in subsequent rendering operations |
---|
829 | * on this graphics context will be relative to this new origin. |
---|
830 | * |
---|
831 | * @param x the x coordinate. |
---|
832 | * @param y the y coordinate. |
---|
833 | */ |
---|
834 | public void translate(int x, int y){ |
---|
835 | if (DEBUG) |
---|
836 | System.out.println("translate with x = " + x + " and y = " + y); |
---|
837 | m_localGraphicsState.setXOffset(m_localGraphicsState.getXOffset() + xScale(x)); |
---|
838 | m_localGraphicsState.setYOffset(m_localGraphicsState.getYOffset() + yScale(y)); |
---|
839 | m_psGraphicsState.setXOffset(m_psGraphicsState.getXOffset() + xScale(x)); |
---|
840 | m_psGraphicsState.setYOffset(m_psGraphicsState.getYOffset() + yScale(y)); |
---|
841 | } |
---|
842 | /***** END overridden Graphics methods *****/ |
---|
843 | |
---|
844 | /***** START overridden Graphics2D methods *****/ |
---|
845 | |
---|
846 | public FontRenderContext getFontRenderContext(){ |
---|
847 | return (new FontRenderContext(null,true,true)); |
---|
848 | } |
---|
849 | public void clip(Shape s){} |
---|
850 | public Stroke getStroke(){ |
---|
851 | return(m_localGraphicsState.getStroke()); |
---|
852 | } |
---|
853 | |
---|
854 | public Color getBackground(){ |
---|
855 | return(Color.white); |
---|
856 | } |
---|
857 | public void setBackground(Color c){} |
---|
858 | public Composite getComposite(){ |
---|
859 | return(AlphaComposite.getInstance(AlphaComposite.SRC)); |
---|
860 | } |
---|
861 | public Paint getPaint(){ |
---|
862 | return((Paint) (new Color(getColor().getRed(),getColor().getGreen(),getColor().getBlue()))); |
---|
863 | } |
---|
864 | public AffineTransform getTransform(){ |
---|
865 | return(new AffineTransform()); |
---|
866 | } |
---|
867 | public void setTransform(AffineTransform at) {} |
---|
868 | public void transform(AffineTransform at) {} |
---|
869 | public void shear(double d1, double d2){} |
---|
870 | public void scale(double d1, double d2) { |
---|
871 | m_localGraphicsState.setXScale(d1); |
---|
872 | m_localGraphicsState.setYScale(d2); |
---|
873 | if (DEBUG) |
---|
874 | System.err.println("d1 = " + d1 + ", d2 = " + d2); |
---|
875 | } |
---|
876 | public void rotate(double d1, double d2, double d3){} |
---|
877 | public void rotate(double d1){} |
---|
878 | public void translate(double d1, double d2) {} |
---|
879 | public RenderingHints getRenderingHints(){ |
---|
880 | return(new RenderingHints(null)); |
---|
881 | } |
---|
882 | public void addRenderingHints(Map m){} |
---|
883 | public void setRenderingHints(Map m){} |
---|
884 | public Object getRenderingHint(RenderingHints.Key key){ |
---|
885 | return(null); |
---|
886 | } |
---|
887 | public void setRenderingHint(RenderingHints.Key key, Object o){} |
---|
888 | public void setStroke(Stroke s){ |
---|
889 | if (s != null){ |
---|
890 | m_localGraphicsState.setStroke(s); |
---|
891 | if (s.equals(m_psGraphicsState.getStroke())) { |
---|
892 | return; |
---|
893 | } |
---|
894 | m_psGraphicsState.setStroke(s); |
---|
895 | } else { |
---|
896 | m_localGraphicsState.setStroke(new BasicStroke()); |
---|
897 | m_psGraphicsState.setStroke(getStroke()); |
---|
898 | } |
---|
899 | // ouput postscript here to set stroke. |
---|
900 | } |
---|
901 | public void setPaint(Paint p){ |
---|
902 | } |
---|
903 | public void setComposite(Composite c){} |
---|
904 | public GraphicsConfiguration getDeviceConfiguration(){ |
---|
905 | GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
---|
906 | GraphicsDevice gd = ge.getDefaultScreenDevice(); |
---|
907 | return(gd.getDefaultConfiguration()); |
---|
908 | } |
---|
909 | public boolean hit(Rectangle r, Shape s, boolean onstroke){ |
---|
910 | return(false); |
---|
911 | } |
---|
912 | public void fill(Shape s){} |
---|
913 | public void drawGlyphVector(GlyphVector gv, float f1, float f2){} |
---|
914 | public void drawString(AttributedCharacterIterator aci, float f1, float f2){} |
---|
915 | public void drawString(String str, float x, float y){ |
---|
916 | drawString(str,(int)x, (int)y); |
---|
917 | } |
---|
918 | public void drawRenderableImage(RenderableImage ri, AffineTransform at){} |
---|
919 | public void drawRenderedImage(RenderedImage ri, AffineTransform af){} |
---|
920 | public void drawImage(BufferedImage bi, BufferedImageOp bio, int i1, int i2){} |
---|
921 | public boolean drawImage(Image im, AffineTransform at, ImageObserver io){ |
---|
922 | return(false); |
---|
923 | } |
---|
924 | public void draw(Shape s){} |
---|
925 | /***** END *****/ |
---|
926 | } |
---|