6 // Seth Widoff (core graph functionality)
7 // Michael Kircher (mk1@cs.wustl.edu)
10 // This is a Visualization Component for displaying doubles.
12 // ============================================================================
20 public class DoubleVisComp
extends Canvas
implements VisComp
22 private static final int MIN_SPACING
= 2;
23 private static final int POINT_HISTORY
= 200;
24 private static final Font FONT
= new Font ("Dialog", Font
.PLAIN
, 10);
27 private String title_
;
28 private Graphics offgraphics_
;
29 private Image offscreen_
;
30 private Dimension offscreensize_
;
31 private int max_value_
;
32 private int old_max_value_
;
34 private boolean max_increased_
= false;
35 private static boolean monotonic_scale_
= false;
37 private float local_max_
= 0;
38 private static float local_max_values_
[] = null;
39 private static int local_max_value_count_
= 0;
40 private int local_max_value_index_
= 0;
42 public DoubleVisComp()
46 // Re-initialize the global array of local maxima.
50 spacing_
= MIN_SPACING
;
53 old_max_value_
= max_value_
;
55 java
.util
.Random rand
= new java
.util
.Random (System
.currentTimeMillis());
56 float hue_
= rand
.nextFloat();
57 float brightness
= rand
.nextFloat();
64 if (brightness
> 0.75)
67 Color new_color
= Color
.getHSBColor(hue_
, 1, brightness
);
69 this.setBackground(new_color
);
70 this.setForeground(Color
.white
);
73 public static synchronized void monotonic_scale (boolean b
) {
77 public static synchronized boolean monotonic_scale () {
78 return monotonic_scale_
;
81 public void setName (String title
) {
85 public int getProperty () {
86 return Properties
.DOUBLE
;
89 public Dimension
getMinimumSize () {
90 return new Dimension (75, 75);
93 public Dimension
getPreferredSize () {
94 return new Dimension (175, 175);
97 public String
getName() {
101 public int getMax() {
102 return old_max_value_
;
105 public void update(java
.util
.Observable observable
, java
.lang
.Object obj
)
109 double_temp_
= (Double
) obj
;
111 catch (Exception excp
) {
112 double_temp_
= new Double (0.0);
113 System
.out
.println (excp
);
114 System
.out
.println ("Visualization Component received wrong data type!");
117 float new_point
= double_temp_
.floatValue();
118 Float temp
= (Float
)plot_
.dequeue_tail();
119 plot_
.enqueue_head(new Float(new_point
));
121 if (new_point
> local_max_
)
123 local_max_
= new_point
;
124 local_max_values_
[local_max_value_index_
] = local_max_
;
127 if (monotonic_scale_
)
129 float global_max
= 0;
130 global_max
= global_max_value ();
132 while (global_max
> max_value_
)
135 while ((global_max
< max_value_
/2) && (max_value_
> old_max_value_
))
140 while (local_max_
> max_value_
)
143 while ((local_max_
< max_value_
/2) && (max_value_
> old_max_value_
))
150 public void update(Graphics g
)
152 Dimension d
= getSize ();
153 float tmp
, value_1
, value_2
;
154 FontMetrics fm
= g
.getFontMetrics ();
155 Enumeration queue_iter
= plot_
.forward_iterator();
156 int x1
= d
.width
- 8, y1
, x2
, y2
, fheight
= fm
.getHeight (), i
;
157 String value
= "Value (of " + max_value_
+ "): " + String
.valueOf(plot_
.head());
159 if ((offscreen_
== null) ||
160 (offscreensize_
.width
!= d
.width
- 8) ||
161 (offscreensize_
.height
!= d
.height
- 8))
163 offscreen_
= createImage(d
.width
- 8, d
.height
- 8);
164 offscreensize_
= new Dimension(d
.width
- 8, d
.height
- 8);
165 offgraphics_
= offscreen_
.getGraphics();
166 offgraphics_
.setFont(FONT
);
169 g
.setColor (Color
.lightGray
);
170 g
.draw3DRect (0, 0, d
.width
- 1, d
.height
- 1, true);
171 g
.draw3DRect (1, 1, d
.width
- 3, d
.height
- 3, true);
172 g
.draw3DRect (2, 2, d
.width
- 5, d
.height
- 5, true);
176 offgraphics_
.setColor (getBackground());
177 offgraphics_
.fillRect (0, 0, offscreensize_
.width
, offscreensize_
.height
);
178 offgraphics_
.setColor (getForeground());
179 offgraphics_
.drawString(title_
, 5, fheight
);
180 offgraphics_
.drawString(value
, 5, offscreensize_
.height
- 5);
182 value_1
= ((Float
)queue_iter
.nextElement()).floatValue();
183 while (queue_iter
.hasMoreElements())
185 value_2
= ((Float
)queue_iter
.nextElement()).floatValue();
187 if (value_1
> local_max_
)
188 local_max_
= value_1
;
190 y1
= normalize(offscreensize_
.height
- fheight
, value_1
);
191 y2
= normalize(offscreensize_
.height
- fheight
, value_2
);
198 offgraphics_
.drawLine(x1
, y1
, x2
, y2
);
204 local_max_values_
[local_max_value_index_
] = local_max_
;
206 g
.drawImage(offscreen_
, 3, 3, null);
209 public void paint(Graphics g
)
211 Dimension d
= getSize ();
212 int plot_length
= plot_
.length();
213 int num_points
= d
.width
/ spacing_
;
215 if (plot_
.length() < num_points
)
217 for (int i
= 0; i
< num_points
- plot_length
; i
++)
218 plot_
.enqueue_tail(new Float(0));
220 else if (plot_
.length() > num_points
)
222 for (int i
= 0; i
< plot_length
- num_points
; i
++)
223 plot_
.dequeue_tail();
229 private static synchronized float global_max_value () {
232 for (int i
= 0; i
< local_max_value_count_
; ++i
)
234 if (result
< local_max_values_
[i
])
236 result
= local_max_values_
[i
];
243 private synchronized void local_max_init () {
245 // Create a new, larger, array to hold the local maxima
246 float new_max_values
[] =
247 new float [local_max_value_count_
+ 1];
249 // Copy the previously stored maxima (if any) into the new array.
250 for (int i
= 0; i
< local_max_value_count_
; ++i
)
252 new_max_values
[i
] = local_max_values_
[i
];
255 // Replace the old array with the new one.
256 local_max_values_
= new_max_values
;
258 // Store the local index for this object, bump up the count.
259 local_max_value_index_
= local_max_value_count_
;
260 local_max_value_count_
++;
264 private int normalize(int height
, float coord
)
266 float ratio
= (float)coord
/max_value_
;
267 float pixels
= (float)height
*ratio
;
268 float location
= (float)height
- pixels
;
270 return Math
.round(location
);