view src/samer/core_/util/heavy/Meter.java @ 8:5e3cbbf173aa tip

Reorganise some more
author samer
date Fri, 05 Apr 2019 22:41:58 +0100
parents bf79fb79ee13
children
line wrap: on
line source
/*
 *	Copyright (c) 2000, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS iS and WITHOUT ANY WARRANTY; 
 *	without even the implied warranty of MERCHANTABILITY or 
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.core.util.heavy;
import  samer.core.util.*;
import  samer.core.*;
import  java.awt.*;
import  java.util.*;

public class Meter extends VCanvas
{
	IMap	map;
	int		lasti, nexti;
	double	x=0;

	public Meter() { map = new LinearMap(0,1); lasti=0; }

	public void exposeMap() { exposeMap(true); }
	public void exposeMap(boolean reinit) {
		final VMap	vmap=new VMap(map,reinit);
		vmap.addObserver( new Observer() {
			public void update(Observable o, Object args) {
				if (args==VMap.NEW_MAP) setMap(vmap.getMap());
				nexti=map.toInt(x); repaint(); 
			}
		} );
		vmap.changed();
		exposeCommands(vmap);
	}
	
	public void paint(Graphics g)
	{
		lasti=nexti=map.toInt(x);
		g.setColor(getBackground());
		g.fillRect(lasti,0,width-lasti,height);
		g.setColor(getForeground());
		g.fillRect(0,0,lasti,height);
	}

	public void update(Graphics g)
	{
		if (nexti>lasti) {
			g.setColor(getForeground());
			g.fillRect(lasti,0,nexti-lasti,height);
		} else if (nexti<lasti) {
			g.setColor(getBackground());
			g.fillRect(nexti,0,lasti-nexti,height);
		}	
		lasti=nexti;
	}

	protected void realized() {}
	public void next( double x) { 
		nexti=map.toInt(x); this.x=x;
		if (nexti!=lasti) repaint(); 
	}

	public Dimension getPreferredSize() { return new Dimension(96,6); }
	public Dimension getMinimumSize() { return new Dimension(32,4); }

	public IMap getMap() { return map; }
	public void setMap(IMap m) { map=m; lasti=0; repaint(); }
	protected void sized() { map.setIntRange(width); }
}