/* * AnimatedSlider * * 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 AnimatedSlider class provides a slider which is capable of * running itself via animation buttons. It posts events as * an Adjustable object. It can be treated either like a * traditional scollbar by using the integer value, max, min, * and visible amount variables, or as a real value input * device. */ public class AnimatedSlider extends BufferCanvas implements MouseListener,MouseMotionListener,Runnable,Adjustable { public final static int STYLE_LINEAR=0; public final static int STYLE_LOG=1; protected double realMin; protected double realMax; protected double realValue; protected int sliderStyle; protected boolean leftAnimButtonPressed = false; protected boolean leftButtonPressed = false; protected boolean sliderMarkerPressed = false; protected int sliderMarkerAnchor; protected boolean rightButtonPressed = false; protected boolean rightAnimButtonPressed = false; protected boolean pauseButtonDisplayed = false; protected boolean stillGoing = false; protected boolean reverseAnimDirection = false; protected Thread isAnimating = null; protected int sliderValue; protected int sliderMin; protected int sliderMax; protected int sliderWidth; protected int sliderUnit; private int buttonWidth; private int leftAnimButtonPos; private int leftButtonPos; private int sliderPos; private int rightButtonPos; private int rightAnimButtonPos; private int arrowHOffset; private int arrowWidth; private int height; private int sliderOffset; private int sliderLeftSide; private int sliderRightSide; private int markerPos; private int markerWidth; private AdjustmentListener adjustmentListener; /* * Constructors */ public AnimatedSlider(int sliderMin, int sliderMax, int sliderWidth, int sliderValue, int sliderStyle, double realValue, double realMin, double realMax) { super(); addMouseListener(this); addMouseMotionListener(this); setValue(sliderValue); setMinimum(sliderMin); setMaximum(sliderMax); setSliderWidth(sliderWidth); this.realMax = realMax; this.realMin = realMin; this.realValue = realValue; this.sliderStyle = sliderStyle; setUnitIncrement(1); } public AnimatedSlider () { this(0,110,10,0); } public AnimatedSlider(int sliderMin, int sliderMax, int sliderWidth, int sliderValue) { this(sliderMin, sliderMax, sliderWidth, sliderValue, STYLE_LINEAR, (double)sliderValue, (double) sliderMin, (double)(sliderMax - sliderWidth)); } public AnimatedSlider(int sliderStyle, double realValue, double realMin, double realMax) { this(0,110,10,0,sliderStyle,realValue,realMin,realMax); } public AnimatedSlider(double realValue, double realMin, double realMax) { this(0,110,10,0,STYLE_LINEAR,realValue, realMin, realMax); } /* * Runnable routines */ public void run() { while(isAnimating != null) { try { isAnimating.sleep(75); if (!buttonPressed()&&stillGoing) { boolean noWall = false; if (reverseAnimDirection) { if (sliderValue > 0) { setValue(Math.max(sliderValue-sliderUnit,sliderMin)); noWall = true; postSliderChanged(); } } else { if (sliderValuemarkerPos&&x<(markerPos+markerWidth)) { sliderMarkerPressed = true; sliderMarkerAnchor = markerPos-x; } } else if (xmarkerPos+markerWidth) { setValue(Math.min(sliderValue+sliderWidth,sliderMax-sliderWidth)); } pauseButtonDisplayed = false; stop(); } else if (rightButtonPressed) { // Right button was released if (sliderValue