samer@0
|
1 /*
|
samer@0
|
2 * Copyright (c) 2000, Samer Abdallah, King's College London.
|
samer@0
|
3 * All rights reserved.
|
samer@0
|
4 *
|
samer@0
|
5 * This software is provided AS iS and WITHOUT ANY WARRANTY;
|
samer@0
|
6 * without even the implied warranty of MERCHANTABILITY or
|
samer@0
|
7 * FITNESS FOR A PARTICULAR PURPOSE.
|
samer@0
|
8 */
|
samer@0
|
9
|
samer@0
|
10 package samer.core.util.swing;
|
samer@0
|
11 import samer.core.util.*;
|
samer@0
|
12 import samer.core.*;
|
samer@0
|
13 import java.awt.*;
|
samer@0
|
14 import java.util.*;
|
samer@0
|
15 import javax.swing.BorderFactory;
|
samer@0
|
16
|
samer@0
|
17 public class Meter extends VCanvas
|
samer@0
|
18 {
|
samer@0
|
19 IMap map;
|
samer@0
|
20 int lasti, nexti;
|
samer@0
|
21 double x;
|
samer@0
|
22
|
samer@0
|
23 public Meter() { map = new LinearMap(0,1); lasti=0; }
|
samer@0
|
24
|
samer@0
|
25 public void exposeMap() { exposeMap(true); }
|
samer@0
|
26 public void exposeMap(boolean reinit) {
|
samer@0
|
27 final VMap vmap=new VMap(map,reinit);
|
samer@0
|
28 vmap.addObserver( new Observer() {
|
samer@0
|
29 public void update(Observable o, Object args) {
|
samer@0
|
30 if (args==VMap.NEW_MAP) setMap(vmap.getMap());
|
samer@0
|
31 nexti=map.toInt(x); repaint();
|
samer@0
|
32 }
|
samer@0
|
33 } );
|
samer@0
|
34 vmap.changed();
|
samer@0
|
35 exposeCommands(vmap);
|
samer@0
|
36 }
|
samer@0
|
37
|
samer@0
|
38 public void realized() {
|
samer@0
|
39 setBorder(BorderFactory.createLineBorder(getForeground().darker().darker()));
|
samer@0
|
40 }
|
samer@0
|
41
|
samer@0
|
42 protected void paintComponent(Graphics g) {
|
samer@0
|
43 lasti=nexti;
|
samer@0
|
44 g.setColor(getBackground());
|
samer@0
|
45 g.fillRect(lasti,0,width-lasti,height);
|
samer@0
|
46 g.setColor(getForeground());
|
samer@0
|
47 g.fillRect(0,0,lasti,height);
|
samer@0
|
48 }
|
samer@0
|
49
|
samer@0
|
50 public void next( double x) {
|
samer@0
|
51 nexti=map.toInt(x); this.x=x;
|
samer@0
|
52 if (nexti!=lasti) repaint();
|
samer@0
|
53 }
|
samer@0
|
54
|
samer@0
|
55 public Dimension getPreferredSize() { return new Dimension(96,6); }
|
samer@0
|
56 public Dimension getMinimumSize() { return new Dimension(32,4); }
|
samer@0
|
57
|
samer@0
|
58 public IMap getMap() { return map; }
|
samer@0
|
59 public void setMap(IMap m) { map=m; lasti=0; repaint(); }
|
samer@0
|
60 protected void sized() { map.setIntRange(width); }
|
samer@0
|
61 }
|