samer@0: /* samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.core.util.swing; samer@0: import samer.core.util.*; samer@0: import samer.core.*; samer@0: import java.awt.*; samer@0: import java.util.*; samer@0: import javax.swing.BorderFactory; samer@0: samer@0: public class Meter extends VCanvas samer@0: { samer@0: IMap map; samer@0: int lasti, nexti; samer@0: double x; samer@0: samer@0: public Meter() { map = new LinearMap(0,1); lasti=0; } samer@0: samer@0: public void exposeMap() { exposeMap(true); } samer@0: public void exposeMap(boolean reinit) { samer@0: final VMap vmap=new VMap(map,reinit); samer@0: vmap.addObserver( new Observer() { samer@0: public void update(Observable o, Object args) { samer@0: if (args==VMap.NEW_MAP) setMap(vmap.getMap()); samer@0: nexti=map.toInt(x); repaint(); samer@0: } samer@0: } ); samer@0: vmap.changed(); samer@0: exposeCommands(vmap); samer@0: } samer@0: samer@0: public void realized() { samer@0: setBorder(BorderFactory.createLineBorder(getForeground().darker().darker())); samer@0: } samer@0: samer@0: protected void paintComponent(Graphics g) { samer@0: lasti=nexti; samer@0: g.setColor(getBackground()); samer@0: g.fillRect(lasti,0,width-lasti,height); samer@0: g.setColor(getForeground()); samer@0: g.fillRect(0,0,lasti,height); samer@0: } samer@0: samer@0: public void next( double x) { samer@0: nexti=map.toInt(x); this.x=x; samer@0: if (nexti!=lasti) repaint(); samer@0: } samer@0: samer@0: public Dimension getPreferredSize() { return new Dimension(96,6); } samer@0: public Dimension getMinimumSize() { return new Dimension(32,4); } samer@0: samer@0: public IMap getMap() { return map; } samer@0: public void setMap(IMap m) { map=m; lasti=0; repaint(); } samer@0: protected void sized() { map.setIntRange(width); } samer@0: }