I took Mark's bar_meter class and made it into a more general abstract class called Meter for us to use in the ui.
DigitalMeter is the simplest concrete class derived from Meter. It just displays the max and current values, a percentage, and a units string if that has been set. It's kind of blinky when the updates are rapid, but I figure if it gets used then we can address that. It just needs to figure out where the changing part is and only redraw there.
if we use a generic base class like Meter for these things it's real easy to pop eye candy in and out as needed. We could give the base class to io360 and let them make some really sharp graphical doodads and then we can just pop 'em in wherever we want to. I think we'll get a lot of use out of the bar meters as progress indicators since java is so slow.
there are several attributes that are settable in anything derived from Meter, like colors, innie or outie, logarithmic, and three choices for how to handle max values. for the most part this is compatable with (pronounced "stolen from") Mark's original class.
it should be pretty easy to create some other ones of these that look like speedometers or whatever. it was only about 50 lines to make Meter into a BarMeter, and the same not much more for ColoredBarMeter. Since all the work of logarithmic scaling, percentages, and adaptive maxes is done in the base class, all that's needed for a new one is to make it draw a picture based on two numbers.
here's the code that instantiates the above examples:
public void test5() { GridLayout l = new GridLayout(0,2); setLayout(l); add(new Label("DigitalMeter (max = 500")); dm = new DigitalMeter(false, Meter.FIXED_MAX); dm.set_units("frobnai"); dm.set_max(500); dm.set_showPercentage(true); dm.set_showMax(true); add(dm); add(new Label("BarMeter (max = 500)")); bm = new BarMeter(false, Meter.FIXED_MAX); bm.set_max(500); add(bm); add(new Label("DigitalMeter (adaptive, initial max = 500")); adm = new DigitalMeter(false, Meter.ADAPTIVE_MAX); adm.set_units("flodgets"); adm.set_max(500); adm.set_showPercentage(true); adm.set_showMax(true); add(adm); add(new Label("BarMeter (adaptive, initial max = 500)")); abm = new BarMeter(false, Meter.ADAPTIVE_MAX); abm.set_max(500); add(abm); add(new Label("DigitalMeter (max = 1000")); ddm = new DigitalMeter(false, Meter.FIXED_MAX); ddm.set_units("cubits"); ddm.set_max(1000); ddm.set_showPercentage(true); ddm.set_showMax(true); add(ddm); add(new Label("ColoredBarMeter (max = 1000)")); cbm = new ColoredBarMeter(false, Meter.FIXED_MAX); cbm.set_colorAt(0, Color.blue); cbm.set_colorAt(500, Color.green); cbm.set_colorAt(750, Color.yellow); cbm.set_colorAt(950, Color.red); add(cbm); cbm.set_max(1000); add(new Label("ColoredBarMeter (logarithmic, max = 1000)")); lcbm = new ColoredBarMeter(true, Meter.FIXED_MAX); lcbm.set_colorAt(0, Color.blue); lcbm.set_colorAt(500, Color.green); lcbm.set_colorAt(750, Color.yellow); lcbm.set_colorAt(950, Color.red); add(lcbm); lcbm.set_max(1000); add(new Button("go")); }
they're updated by the following:
public void run() { dm.reset(); bm.reset(); adm.reset(); abm.reset(); ddm.reset(); cbm.reset(); int i = 0; while(i <= max) { dm.set_value(i); bm.set_value(i); adm.set_value(i); abm.set_value(i); ddm.set_value(i); cbm.set_value(i); lcbm.set_value(i); try { Thread.sleep(10); } catch(InterruptedException X) { System.out.println(X); } i++; } while(i >= 0) { dm.set_value(i); bm.set_value(i); adm.set_value(i); abm.set_value(i); ddm.set_value(i); cbm.set_value(i); lcbm.set_value(i); i--; } }