~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/gui/visualize/PostscriptGraphics.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.4 $
 
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
   * Draw text in current pen color.
 
577
   *
 
578
   * @param str Text to output
 
579
   * @param x starting x coord
 
580
   * @param y starting y coord
 
581
   */
 
582
  public void drawString(String str, int x, int y){
 
583
    setStateToLocal();
 
584
    m_printstream.println(xTransform(xScale(x)) + " " + yTransform(yScale(y)) + " moveto" + " (" + str + ") show stroke");
 
585
  }
 
586
  
 
587
  /**
 
588
   * Draw a filled rectangle with 3D effect in current pen color.
 
589
   * (Current implementation: draw simple filled rectangle)
 
590
   *
 
591
   * @param x starting x coord
 
592
   * @param y starting y coord
 
593
   * @param width rectangle width
 
594
   * @param height rectangle height
 
595
   * @param raised True: appear raised, False: appear etched
 
596
   */
 
597
  public void fill3DRect(int x, int y, int width, int height, boolean raised){
 
598
    fillRect(x, y, width, height);
 
599
  }
 
600
  
 
601
  /**
 
602
   * Not implemented
 
603
   */
 
604
  public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle){}
 
605
  
 
606
  /**
 
607
   * Draw a filled Oval in current pen color.
 
608
   *
 
609
   * @param x x-axis center of oval
 
610
   * @param y y-axis center of oval
 
611
   * @param width oval width
 
612
   * @param height oval height
 
613
   */
 
614
  public void fillOval(int x, int y, int width, int height){
 
615
    setStateToLocal();
 
616
    m_printstream.println(xTransform(xScale(x)) + " " + yTransform(yScale(y)) + " " + xScale(width) + " " + yScale(height) + " true Oval");
 
617
  }
 
618
  
 
619
  /**
 
620
   * Not implemented
 
621
   */
 
622
  public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints){}
 
623
  
 
624
  /**
 
625
   * Not implemented
 
626
   */
 
627
  public void fillPolygon(Polygon p){}
 
628
  
 
629
  /**
 
630
   * Draw a filled rectangle in current pen color.
 
631
   *
 
632
   * @param x starting x coord
 
633
   * @param y starting y coord
 
634
   * @param width rectangle width
 
635
   * @param height rectangle height
 
636
   */
 
637
  
 
638
  public void fillRect(int x, int y, int width, int height){
 
639
    if (width == m_extent.width && height == m_extent.height) {
 
640
      clearRect(x, y, width, height); // if we're painting the entire background, just make it white
 
641
    } else {
 
642
      if (DEBUG)
 
643
        m_printstream.println("% fillRect");
 
644
      setStateToLocal();
 
645
      m_printstream.println(xTransform(xScale(x)) + " " + yTransform(yScale(y)) + " " + xScale(width) + " " + yScale(height) + " true Rect");
 
646
    }
 
647
  }
 
648
  
 
649
  /**
 
650
   * Not implemented
 
651
   */
 
652
  public void fillRoundRect(int x, int y, int width, int height, int arcWidth, int arcHeight){}
 
653
  
 
654
  /**
 
655
   * Not implemented
 
656
   */
 
657
  public void finalize(){}
 
658
  
 
659
  /**
 
660
   * Not implemented
 
661
   */
 
662
  public Shape getClip(){
 
663
    return(null);
 
664
  }
 
665
  
 
666
  /**
 
667
   * This returns the full current drawing area
 
668
   * @return full drawing area
 
669
   */
 
670
  public Rectangle getClipBounds(){
 
671
    return(new Rectangle(0, 0, m_extent.width, m_extent.height));
 
672
  }
 
673
  
 
674
  /**
 
675
   * This returns the full current drawing area
 
676
   * @return full drawing area
 
677
   */
 
678
  public Rectangle getClipBounds(Rectangle r) {
 
679
    r.setBounds(0, 0, m_extent.width, m_extent.height);
 
680
    return r;
 
681
  }
 
682
  
 
683
  /**
 
684
   * Not implemented
 
685
   */
 
686
  public Rectangle getClipRect() {return null;}
 
687
  
 
688
  /**
 
689
   * Get current pen color.
 
690
   *
 
691
   * @return current pen color.
 
692
   */
 
693
  public Color getColor(){
 
694
    return (m_localGraphicsState.getColor());
 
695
  }
 
696
  
 
697
  /**
 
698
   * Get current font.
 
699
   *
 
700
   * @return current font.
 
701
   */
 
702
  public Font getFont(){
 
703
    return (m_localGraphicsState.getFont());
 
704
  }
 
705
  
 
706
  /**
 
707
   * Get Font metrics
 
708
   *
 
709
   * @param f Font 
 
710
   * @return Font metrics.
 
711
   */
 
712
  public FontMetrics getFontMetrics(Font f){
 
713
    return(Toolkit.getDefaultToolkit().getFontMetrics(f));  
 
714
    
 
715
  }
 
716
  
 
717
  /**
 
718
   * Not implemented
 
719
   */
 
720
  public void setClip(int x, int y, int width, int height) {}
 
721
  
 
722
  /**
 
723
   * Not implemented
 
724
   */
 
725
  public void setClip(Shape clip){}
 
726
  
 
727
  /**
 
728
   * Set current pen color. Default to black if null.
 
729
   *
 
730
   * @param c new pen color.
 
731
   */
 
732
  public void setColor(Color c){
 
733
    if (c != null){
 
734
      m_localGraphicsState.setColor(c);
 
735
      if (m_psGraphicsState.getColor().equals(c)) {
 
736
        return;
 
737
      }
 
738
      m_psGraphicsState.setColor(c);
 
739
    } else {
 
740
      m_localGraphicsState.setColor(Color.black);
 
741
      m_psGraphicsState.setColor(getColor());
 
742
    }
 
743
    m_printstream.print(getColor().getRed()/255.0);
 
744
    m_printstream.print(" ");
 
745
    m_printstream.print(getColor().getGreen()/255.0);
 
746
    m_printstream.print(" ");
 
747
    m_printstream.print(getColor().getBlue()/255.0);
 
748
    m_printstream.println(" setrgbcolor");
 
749
  }
 
750
  
 
751
  /**
 
752
   * replaces the font (PS name) if necessary and returns the new name
 
753
   */
 
754
  private static String replacePSFont(String font) {
 
755
    String      result;
 
756
    
 
757
    result = font;
 
758
    
 
759
    // do we have to replace it? -> same style, size
 
760
    if (m_PSFontReplacement.containsKey(font)) {
 
761
      result = m_PSFontReplacement.get(font).toString();
 
762
      if (DEBUG)
 
763
        System.out.println("switched font from '" + font + "' to '" + result +  "'");
 
764
    }
 
765
    
 
766
    return result;
 
767
  }
 
768
  
 
769
  /**
 
770
   * Set current font. Default to Plain Courier 11 if null.
 
771
   *
 
772
   * @param font new font.
 
773
   */
 
774
  public void setFont(Font font){
 
775
    
 
776
    if (font != null){
 
777
      m_localGraphicsState.setFont(font);
 
778
      if (   font.getName().equals(m_psGraphicsState.getFont().getName())
 
779
          && (m_psGraphicsState.getFont().getStyle() == font.getStyle())
 
780
          && (m_psGraphicsState.getFont().getSize() == yScale(font.getSize())))
 
781
        return;
 
782
      m_psGraphicsState.setFont(new Font(font.getName(), font.getStyle(), yScale(getFont().getSize())));
 
783
    } 
 
784
    else {
 
785
      m_localGraphicsState.setFont(new Font ("Courier", Font.PLAIN, 11));
 
786
      m_psGraphicsState.setFont(getFont());
 
787
    }
 
788
    
 
789
    m_printstream.println("/(" + replacePSFont(getFont().getPSName()) + ")" + " findfont");
 
790
    m_printstream.println(yScale(getFont().getSize()) + " scalefont setfont");        
 
791
  }
 
792
  
 
793
  /**
 
794
   * Not implemented
 
795
   */
 
796
  public void setPaintMode(){}
 
797
  
 
798
  /**
 
799
   * Not implemented
 
800
   */
 
801
  public void setXORMode(Color c1){}
 
802
  
 
803
  /**
 
804
   * Translates the origin of the graphics context to the point (x, y) in the 
 
805
   * current coordinate system. Modifies this graphics context so that its new 
 
806
   * origin corresponds to the point (x, y) in this graphics context's original 
 
807
   * coordinate system. All coordinates used in subsequent rendering operations 
 
808
   * on this graphics context will be relative to this new origin.
 
809
   * 
 
810
   * @param x the x coordinate.
 
811
   * @param y the y coordinate.
 
812
   */
 
813
  public void translate(int x, int y){
 
814
    if (DEBUG)
 
815
      System.out.println("translate with x = " + x + " and y = " + y);
 
816
    m_localGraphicsState.setXOffset(m_localGraphicsState.getXOffset() + xScale(x));
 
817
    m_localGraphicsState.setYOffset(m_localGraphicsState.getYOffset() + yScale(y));
 
818
    m_psGraphicsState.setXOffset(m_psGraphicsState.getXOffset() + xScale(x));
 
819
    m_psGraphicsState.setYOffset(m_psGraphicsState.getYOffset() + yScale(y));
 
820
  }
 
821
  /***** END overridden Graphics methods *****/
 
822
  
 
823
  /***** START overridden Graphics2D methods *****/
 
824
  
 
825
  public FontRenderContext getFontRenderContext(){
 
826
    return (new FontRenderContext(null,true,true));
 
827
  }
 
828
  public void clip(Shape s){}
 
829
  public Stroke getStroke(){
 
830
    return(m_localGraphicsState.getStroke());
 
831
  }
 
832
  
 
833
  public Color getBackground(){
 
834
    return(Color.white);
 
835
  }
 
836
  public void setBackground(Color c){}
 
837
  public Composite getComposite(){
 
838
    return(AlphaComposite.getInstance(AlphaComposite.SRC));
 
839
  }
 
840
  public Paint getPaint(){
 
841
    return((Paint) (new Color(getColor().getRed(),getColor().getGreen(),getColor().getBlue())));
 
842
  }
 
843
  public AffineTransform getTransform(){
 
844
    return(new AffineTransform());
 
845
  }
 
846
  public void setTransform(AffineTransform at) {}
 
847
  public void transform(AffineTransform at) {}
 
848
  public void shear(double d1, double d2){}
 
849
  public void scale(double d1, double d2) {
 
850
    m_localGraphicsState.setXScale(d1);
 
851
    m_localGraphicsState.setYScale(d2);
 
852
    if (DEBUG)
 
853
      System.err.println("d1 = " + d1 + ", d2 = " + d2);
 
854
  }
 
855
  public void rotate(double d1, double d2, double d3){}
 
856
  public void rotate(double d1){}
 
857
  public void translate(double d1, double d2) {}
 
858
  public RenderingHints getRenderingHints(){
 
859
    return(new RenderingHints(null));
 
860
  }
 
861
  public void addRenderingHints(Map m){}
 
862
  public void setRenderingHints(Map m){}
 
863
  public Object getRenderingHint(RenderingHints.Key key){
 
864
    return(null);
 
865
  }
 
866
  public void setRenderingHint(RenderingHints.Key key, Object o){}
 
867
  public void setStroke(Stroke s){
 
868
    if (s != null){
 
869
      m_localGraphicsState.setStroke(s);
 
870
      if (s.equals(m_psGraphicsState.getStroke())) {
 
871
        return;
 
872
      }
 
873
      m_psGraphicsState.setStroke(s); 
 
874
    } else {
 
875
      m_localGraphicsState.setStroke(new BasicStroke());
 
876
      m_psGraphicsState.setStroke(getStroke());
 
877
    }
 
878
    // ouput postscript here to set stroke.
 
879
  }
 
880
  public void setPaint(Paint p){
 
881
  }
 
882
  public void setComposite(Composite c){}
 
883
  public GraphicsConfiguration getDeviceConfiguration(){
 
884
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
 
885
    GraphicsDevice gd = ge.getDefaultScreenDevice();
 
886
    return(gd.getDefaultConfiguration()); 
 
887
  }
 
888
  public boolean hit(Rectangle r, Shape s, boolean onstroke){
 
889
    return(false);
 
890
  }
 
891
  public void fill(Shape s){}
 
892
  public void drawGlyphVector(GlyphVector gv, float f1, float f2){} 
 
893
  public void drawString(AttributedCharacterIterator aci, float f1, float f2){}
 
894
  public void drawString(String str, float x, float y){
 
895
    drawString(str,(int)x, (int)y);
 
896
  }
 
897
  public void drawRenderableImage(RenderableImage ri, AffineTransform at){}
 
898
  public void drawRenderedImage(RenderedImage ri, AffineTransform af){}
 
899
  public void drawImage(BufferedImage bi, BufferedImageOp bio, int i1, int i2){}
 
900
  public boolean drawImage(Image im, AffineTransform at, ImageObserver io){
 
901
    return(false);
 
902
  }
 
903
  public void draw(Shape s){}
 
904
  /***** END *****/
 
905
}