summaryrefslogtreecommitdiff
path: root/TAO/examples/Simulator/Sim_Display/Graph.java
blob: d8be73b54cb14e5e4bd8d82ccca2854c07b8c813 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// $Id$


import java.awt.*;
import java.util.*;

public class Graph extends Canvas
{
  private static final int MIN_SPACING = 2;
  private static final int POINT_HISTORY = 200;
  private static final Font FONT = new Font ("Dialog", Font.PLAIN, 10);
  
  private Queue plot_;
  private String title_;
  private Graphics offgraphics_;
  private Image offscreen_;
  private Dimension offscreensize_;
  private int max_value_;
  private int old_max_value_;
  private int spacing_;
  private float local_max_ = 0;
  private boolean max_increased_ = false;
  
  public Graph(String title, int max_value)
  {
    super();

    plot_ = new Queue();
    spacing_ = MIN_SPACING;
    title_ = title;
    max_value_ = max_value;
    old_max_value_ = max_value;
  }

  public Dimension getMinimumSize ()
    {
      return new Dimension (75, 75);
    }

  public Dimension getPreferredSize ()
    {
      return new Dimension (175, 175);
    }

  public void addNewData(float new_point)
  {
    Float temp = (Float)plot_.dequeue_tail();
    plot_.enqueue_head(new Float(new_point));
    
    if (new_point > local_max_)
      local_max_ = new_point;
    
    while (local_max_ > max_value_)
      max_value_ *= 2;
    
    while ((local_max_ < max_value_/2) && (max_value_ > old_max_value_))
      max_value_ /= 2;

    repaint();
  }

  public void update(Graphics g)
  {
    Dimension d = getSize ();
    float tmp, value_1, value_2;
    FontMetrics fm = g.getFontMetrics ();
    Enumeration queue_iter = plot_.forward_iterator();
    int x1 = d.width - 8, y1, x2, y2, fheight = fm.getHeight (), i;
    String value =  "Value (of " + max_value_ + "): " + String.valueOf(plot_.head());
    
    if ((offscreen_ == null) ||
	(offscreensize_.width != d.width - 8) ||
	(offscreensize_.height != d.height - 8))
      {
	offscreen_ = createImage(d.width - 8, d.height - 8);
	offscreensize_ = new Dimension(d.width - 8, d.height - 8);
	offgraphics_ = offscreen_.getGraphics();
	offgraphics_.setFont(FONT);
      }

    g.setColor (Color.lightGray);
    g.draw3DRect (0, 0, d.width - 1, d.height - 1, true);
    g.draw3DRect (1, 1, d.width - 3, d.height - 3, true);
    g.draw3DRect (2, 2, d.width - 5, d.height - 5, true);
    
    local_max_ = 0;
    offgraphics_.setColor (getBackground());
    offgraphics_.fillRect (0, 0, offscreensize_.width, offscreensize_.height);
    offgraphics_.setColor (getForeground());    
    offgraphics_.drawString(title_, 5, fheight);
    offgraphics_.drawString(value, 5, offscreensize_.height - 5);

    value_1 = ((Float)queue_iter.nextElement()).floatValue();
    while (queue_iter.hasMoreElements())
      {
	value_2 = ((Float)queue_iter.nextElement()).floatValue();
	
	if (value_1 > local_max_)
	  local_max_ = value_1;
	
        y1 = normalize(offscreensize_.height - fheight, value_1);
        y2 = normalize(offscreensize_.height - fheight, value_2);
	
	tmp = value_2;
	value_2 = value_1;
	value_1 = tmp;

        x2 = x1 - spacing_;
        offgraphics_.drawLine(x1, y1, x2, y2);
        x1 = x2;
	if (x1 <= 5)
	  break;
      }

    g.drawImage(offscreen_, 3, 3, null);
  }
  
  public void paint(Graphics g)
  {
    Dimension d = getSize ();
    int plot_length = plot_.length();
    int num_points = d.width / spacing_;
    
    if (plot_.length() < num_points)
      {
	for (int i = 0; i < num_points - plot_length; i++)
	  plot_.enqueue_tail(new Float(0));
      }
    else if (plot_.length() > num_points)
      {
	for (int i = 0; i < plot_length - num_points; i++)
	  plot_.dequeue_tail();
      }
    
    update(g);
  }

  public String getKey()
  {
    return title_;
  }

  public int getMax()
    {
      return old_max_value_;
    }

  private int normalize(int height, float coord)
  {
    float ratio = (float)coord/max_value_;
    float pixels = (float)height*ratio;
    float location = (float)height - pixels;
    
    return Math.round(location);
  }

  public void updateDisplay(float information)
    {
      addNewData(information);
      repaint();
    }
}