6 // Chris Gill <cdgill@cs.wustl.edu>
9 // This is a Visualization Component for displaying a Persian
12 // ============================================================================
20 // This was needed to help the class loader.
21 import java
.awt
.image
.MemoryImageSource
;
23 public class PersianVisComp
extends Canvas
implements VisComp
25 private static final Font FONT
= new Font ("Dialog", Font
.PLAIN
, 10);
26 private static final int PIXELS_WIDE
= 256;
27 private static final int TOTAL_PIXELS
= PIXELS_WIDE
* PIXELS_WIDE
;
28 private static final int DRAW_OFFSET
= 32;
30 private String title_
;
31 private Graphics offgraphics_
;
32 private Image offscreen_
;
33 private Dimension offscreensize_
;
34 private int pixel_array_
[];
35 private MemoryImageSource image_source_
;
36 public PersianVisComp ()
40 this.setBackground (Color
.white
);
41 this.setForeground (Color
.black
);
42 pixel_array_
= new int [TOTAL_PIXELS
];
43 for (int i
= 0; i
< TOTAL_PIXELS
; ++i
)
44 pixel_array_
[i
] = Color
.white
.getRGB ();
45 image_source_
= new MemoryImageSource (PIXELS_WIDE
, PIXELS_WIDE
,
46 pixel_array_
, 0, PIXELS_WIDE
);
49 public void drawPixels (int x1
, int y1
, int x2
, int y2
, Color c
) {
55 int rgb_value
= c
.getRGB ();
57 if (x1
< 0 || y1
< 0 || x2
< 0 || y2
< 0
58 || x1
>= PIXELS_WIDE
|| y1
>= PIXELS_WIDE
59 || x2
>= PIXELS_WIDE
|| y2
>= PIXELS_WIDE
)
61 System
.err
.println ("coordinates out of range: ("
62 + x1
+ ", " + y1
+ ") ("
63 + x2
+ ", " + y2
+ ")\n");
89 // System.out.println ("Updating pixels: ("
90 // + x1 + ", " + y1 + ") ("
91 // + x2 + ", " + y2 + ")\n");
93 for (int x
= x_min
; x
<= x_max
; ++x
)
94 for (int y
= y_min
; y
<= y_max
; ++y
)
95 pixel_array_
[y
* PIXELS_WIDE
+ x
] = rgb_value
;
98 public void setName (String title
) {
102 public int getProperty () {
103 return Properties
.PERSIAN
;
106 public Dimension
getMinimumSize () {
107 return new Dimension (PIXELS_WIDE
+ 2 * DRAW_OFFSET
,
108 PIXELS_WIDE
+ 2 * DRAW_OFFSET
);
111 public Dimension
getPreferredSize () {
112 return new Dimension (PIXELS_WIDE
+ 2 * DRAW_OFFSET
,
113 PIXELS_WIDE
+ 2 * DRAW_OFFSET
);
116 public String
getName () {
120 public void update (java
.util
.Observable observable
, java
.lang
.Object obj
)
122 PersianRecursion
.Data data_temp_
;
124 data_temp_
= (PersianRecursion
.Data
) obj
;
126 catch (Exception excp
) {
128 System
.out
.println (excp
);
129 System
.out
.println ("PR Visualization Component received exception!");
133 Color c
= Color
.black
;
135 if (data_temp_
.line_color
== PersianRecursion
.Line_Color_t
.BLUE
)
140 else if (data_temp_
.line_color
== PersianRecursion
.Line_Color_t
.RED
)
145 else if (data_temp_
.line_color
== PersianRecursion
.Line_Color_t
.GREEN
)
150 else if (data_temp_
.line_color
== PersianRecursion
.Line_Color_t
.YELLOW
)
156 drawPixels (data_temp_
.x1
, data_temp_
.y1
,
157 data_temp_
.x2
, data_temp_
.y2
, c
);
159 // System.out.println ("PersianVisComp: drawing {"
160 // + data_temp_.x1 + ", " + data_temp_.y1 + ", "
161 // + data_temp_.x2 + ", " + data_temp_.y2 + "}");
166 public void update (Graphics g
)
168 Dimension d
= getSize ();
169 FontMetrics fm
= g
.getFontMetrics ();
170 int x1
= d
.width
- 8, y1
, x2
, y2
, fheight
= fm
.getHeight (), i
;
171 PersianRecursion
.Data data_temp_
;
173 if ((offscreen_
== null) ||
174 (offscreensize_
.width
!= d
.width
- 8) ||
175 (offscreensize_
.height
!= d
.height
- 8))
177 offscreen_
= createImage (d
.width
- 8, d
.height
- 8);
178 offscreensize_
= new Dimension (d
.width
- 8, d
.height
- 8);
179 offgraphics_
= offscreen_
.getGraphics ();
180 offgraphics_
.setFont (FONT
);
183 g
.setColor (Color
.lightGray
);
184 g
.draw3DRect (0, 0, d
.width
- 1, d
.height
- 1, true);
185 g
.draw3DRect (1, 1, d
.width
- 3, d
.height
- 3, true);
186 g
.draw3DRect (2, 2, d
.width
- 5, d
.height
- 5, true);
188 offgraphics_
.setColor (getBackground ());
189 offgraphics_
.fillRect (0, 0, offscreensize_
.width
, offscreensize_
.height
);
190 offgraphics_
.setColor (getForeground ());
191 offgraphics_
.drawString (title_
, 5, fheight
);
193 Image img
= createImage (image_source_
);
194 offgraphics_
.drawImage (img
, DRAW_OFFSET
, DRAW_OFFSET
, null);
196 g
.drawImage (offscreen_
, 3, 3, null);
198 // System.out.println ("updated.");
201 public void paint (Graphics g
)
204 // System.out.println ("painted.");