/* * Slider * * Version 1.1 - Java 1.1 compatible * * Dec 6 2001 * * Copyright 2001 David Joiner and the Shodor Education Foundation */ import java.awt.*; import java.awt.event.*; /** * The Slider class extends the traditional functionality * of scrollbars by provding routines that store and associate * a real value with the slider position. Sliders are designed * specifically to be used for numerical input. */ public class Slider extends Scrollbar implements AdjustmentListener { public final static int STYLE_LINEAR=0; public final static int STYLE_LOG=1; public final static int STYLE_INTEGER=2; protected double realMin; protected double realMax; protected double realValue; protected int sliderStyle; /* * Constructors */ public Slider() { this(Slider.STYLE_LINEAR,50.0,0.0,100.0); } public Slider(double realValue, double realMin, double realMax) { this(Slider.STYLE_LINEAR,realValue,realMin,realMax); } public Slider(int sliderStyle, double realValue, double realMin, double realMax) { super(Scrollbar.HORIZONTAL,50,10,0,110); this.sliderStyle = sliderStyle; setRealMax(realMax); setRealMin(realMin); setRealValue(realValue); addAdjustmentListener(this); } /* * Set and Get methods */ public int realToDisplay(double real) { if (sliderStyle == STYLE_LINEAR) { // linear scale double fraction = (real - realMin)/(realMax - realMin); return (int)(fraction*(double)(getMaximum()- getVisibleAmount()-getMinimum()))+ getMinimum(); } else if (sliderStyle==STYLE_INTEGER) { double fraction = ((double)((int)real) - realMin)/(realMax - realMin); return (int)(fraction*(double)(getMaximum()- getVisibleAmount()-getMinimum()))+ getMinimum(); } else { // logarithmic scale int n = getMaximum()-getVisibleAmount()-getMinimum(); double factor = Math.pow((realMax/realMin),1.0/(double)(n)); return (int)(Math.log(real/realMin)/Math.log(factor))+getMinimum(); } } public double displayToReal(int display) { if (sliderStyle == STYLE_LINEAR) { // linear scale double fraction = (double)(display - getMinimum())/ (double)(getMaximum()-getVisibleAmount()-getMinimum()); return fraction*(realMax-realMin)+realMin; } else if (sliderStyle == STYLE_INTEGER) { double fraction = (double)(display - getMinimum())/ (double)(getMaximum()-getVisibleAmount()-getMinimum()); return (double)((int)fraction*(realMax-realMin)+realMin); } else { // logarithmic scale int n = getMaximum()-getVisibleAmount()-getMinimum(); double factor = Math.pow((realMax/realMin),1.0/(double)(n)); return realMin*Math.pow(factor,(double)(display-getMinimum())); } } public void setRealMin(double realMin) { this.realMin = realMin; } public void setRealMax(double realMax) { this.realMax = realMax; } public void setRealValue(double realValue) { this.realValue = realValue; if (sliderStyle == STYLE_INTEGER) { this.realValue = (double)((int)this.realValue); } setValue(realToDisplay(this.realValue)); } public double getRealMin() { return realMin; } public double getRealMax() { return realMax; } public double getRealValue() { return realValue; } /* * Event handling */ public void adjustmentValueChanged(AdjustmentEvent e) { realValue = displayToReal(getValue()); } }