1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.range2;
21
22 import java.awt.CardLayout;
23 import java.awt.Color;
24 import java.awt.FontMetrics;
25 import java.awt.Graphics;
26
27 import javax.swing.JComponent;
28 import javax.swing.JFrame;
29
30 import com.cosylab.util.PrintfFormat;
31
32
33
34
35
36
37
38
39 public class SimpleDemoDisplayer extends JComponent implements TickParameters
40 {
41 private static final long serialVersionUID = 1L;
42
43 private class Updater implements RangedValueListener
44 {
45
46
47
48
49
50 public void valueChanged(RangedValueEvent event)
51 {
52 repaint();
53 }
54 }
55
56 private RangedValueController rv;
57 private PrintfFormat pf = new PrintfFormat("%3.2f");
58
59
60
61
62 public SimpleDemoDisplayer()
63 {
64 rv = new RangedValueController();
65 rv.setRange(new LogarithmicRange());
66 rv.setValue(0, 10, 2);
67
68 rv.addRangedValueListener(new Updater());
69
70 rv.setPolicy(new RescalingValuePolicy());
71 }
72
73
74
75
76
77
78 public void setMinimum(double min)
79 {
80 rv.setValue(min, rv.getMaximum(), rv.getValue());
81 }
82
83
84
85
86
87
88 public void setMaximum(double max)
89 {
90 rv.setValue(rv.getMinimum(), max, rv.getValue());
91 }
92
93
94
95
96
97
98 public void setValue(double val)
99 {
100 rv.setValue(rv.getMinimum(), rv.getMaximum(), val);
101 }
102
103
104
105
106
107
108 public double getMinimum()
109 {
110 return rv.getMinimum();
111 }
112
113
114
115
116
117
118 public double getMaximum()
119 {
120 return rv.getMaximum();
121 }
122
123
124
125
126
127
128 public double getValue()
129 {
130 return rv.getValue();
131 }
132
133
134
135
136
137
138 public void paintComponent(Graphics g)
139 {
140 super.paintComponent(g);
141
142 g.setColor(Color.BLACK);
143 g.fillRect(0, 0, getWidth(), getHeight());
144
145 g.setColor(Color.WHITE);
146 g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
147
148 Tick[] ticks = rv.calculateTicks(getWidth(),this);
149
150 for (int i = 0; i < ticks.length; i++) {
151 Tick t = ticks[i];
152 if (t==null) continue;
153 double x = t.proportional * getWidth();
154
155 if (t.major) {
156 g.drawString(t.text,
157 (int)x - g.getFontMetrics().stringWidth(t.text) / 2,
158 getHeight() / 4 - 2);
159 g.drawLine((int)x, getHeight(), (int)x, getHeight() / 4);
160 } else {
161 g.drawLine((int)x, getHeight(), (int)x, getHeight() / 2);
162 }
163 }
164
165 g.setColor(Color.RED);
166
167 double x = rv.getRelativeValue() * getWidth();
168 g.drawLine((int)x, getHeight(), (int)x, 0);
169 }
170
171
172
173
174
175 public int measureTick(double position, String text)
176 {
177 FontMetrics fm = getFontMetrics(getFont());
178
179 return fm.stringWidth(text);
180 }
181
182
183
184
185
186 public String formatNumber(double value) {
187 return pf.sprintf(value);
188 }
189
190
191
192
193
194
195 public static void main(String[] args)
196 {
197 JFrame jf = new JFrame();
198 jf.setSize(200, 80);
199 jf.getContentPane().setLayout(new CardLayout());
200
201 SimpleDemoDisplayer sdd = new SimpleDemoDisplayer();
202 sdd.setMinimum(0.2);
203
204
205 sdd.setMaximum(2);
206 jf.getContentPane().add(sdd, "SimpleDemoDisplayer");
207
208 jf.setVisible(true);
209
210 while (true) {
211 sdd.setValue(Math.random() * 9);
212
213 try {
214 Thread.sleep(100);
215 } catch (InterruptedException e) {
216 }
217 }
218 }
219 }
220
221