import java.text.DecimalFormat; import java.awt.*; import java.awt.event.*; /** A component that combines a Scrollbar and a Label, to adjust and display a * parameter of type double. Not very pretty, robust, or general, but gets the * job done for most purposes, without too much complexity. A typical use is: * *
* Frame myFrame = new Frame("Controls");
* myFrame.setLayout(new GridLayout(0,1)); // arrange components in a vertical column
* DoubleScroller myScroller = new DoubleScroller("Angle in degrees = ",0,90,0.5,45);
* myFrame.add(myScroller);
* // (create and add more instances if needed)
* theAngle = myScroller.getValue(); // call this whenever the value is needed
*
*
* @author Dan Schroeder
*/
public class DoubleScroller extends Panel implements AdjustmentListener {
double theValue, minValue, maxValue, stepSize; // scrollbar parameters
String labelText; // explanatory text to display
Label theLabel; // includes explanatory text and the numerical value
DecimalFormat labelFormat; // format for displaying the value
Scrollbar theScrollbar;
/** Construct a new DoubleScroller given the minimum value, maximum value, step size,
initial value, and text label to display to the left of the current value. */
public DoubleScroller(String label, double min, double max, double step, double initial) {
minValue = min; maxValue = max; stepSize = step; theValue = initial;
labelText = label;
if (decimalPlaces(stepSize) <= 0) {
labelFormat = new DecimalFormat("0");
} else {
StringBuffer pattern = new StringBuffer().append("0.");
for (int i=0; i