Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / examples / Simulator / DOVEBrowser / PersianVisComp.java
blob40ae109ba82cd569a3c5bf2fbef9667f87e172de
1 //
2 // = FILENAME
3 // PersianVisComp.java
4 //
5 // = AUTHOR
6 // Chris Gill <cdgill@cs.wustl.edu>
7 //
8 // = DESCRIPTION
9 // This is a Visualization Component for displaying a Persian
10 // Recursion drawing.
12 // ============================================================================
17 import java.awt.*;
18 import java.util.*;
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 ()
38 super ();
39 title_ = "";
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) {
51 int x_min;
52 int x_max;
53 int y_min;
54 int y_max;
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");
64 return;
67 if (x1 < x2)
69 x_min = x1;
70 x_max = x2;
72 else
74 x_min = x2;
75 x_max = x1;
78 if (y1 < y2)
80 y_min = y1;
81 y_max = y2;
83 else
85 y_min = y2;
86 y_max = y1;
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) {
99 title_ = 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 () {
117 return title_;
120 public void update (java.util.Observable observable, java.lang.Object obj)
122 PersianRecursion.Data data_temp_;
123 try {
124 data_temp_ = (PersianRecursion.Data) obj;
126 catch (Exception excp) {
127 data_temp_ = null;
128 System.out.println (excp);
129 System.out.println ("PR Visualization Component received exception!");
130 return;
133 Color c = Color.black;
135 if (data_temp_.line_color == PersianRecursion.Line_Color_t.BLUE)
137 c = Color.red;
138 // c = Color.blue;
140 else if (data_temp_.line_color == PersianRecursion.Line_Color_t.RED)
142 c = Color.yellow;
143 // c = Color.red;
145 else if (data_temp_.line_color == PersianRecursion.Line_Color_t.GREEN)
147 c = Color.blue;
148 // c = Color.green;
150 else if (data_temp_.line_color == PersianRecursion.Line_Color_t.YELLOW)
152 c = Color.green;
153 // c = Color.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 + "}");
163 repaint ();
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)
203 update (g);
204 // System.out.println ("painted.");