import java.lang.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.util.*; import javax.swing.event.*; import javax.swing.border.*; /** * The JScale class is sub-class of JSlider it takes it a max, min for the range of the scale and a decimal value * for the level of accuracy you want in the output. When using this class you should NOT use setMinimumTickSpacing, * setMinorTickSpacing, setMinimum, setMaximum, createStandardLabel, setLabelTable. There are methods implemented in * in JScale which performs the same actions as these methods use those instead they are implemented to suit the * JScale specifically. */ public class JScale extends JSlider { private int min; private int max; private int factor; /** * constructor of the JScale class * it takes as parameters a minimum, maximum, and how many decimal you want it to display */ public JScale(int min, int max,int decimal) { super(); this.min = min; this.max = max; super.setValue(min); super.setOrientation(0); init(decimal); } /** * method init takes the decimal place specified and sets the max and min appropiately */ public void init(int decimal) { int temp = decimal; int tempmax = 10; while ( temp !=1 ) { tempmax = tempmax * 10; temp = temp - 1; } this.factor = tempmax; this.setMaximum(max * tempmax); this.setMinimum(min * tempmax); } /** * this takes in an interger value and for the major tick spacing and creates it on the JScale * it also print out the tick marks on the JScale */ public void setMajorTick(int v) { v = v * this.factor; super.setMajorTickSpacing(v); super.setPaintTicks(true); } /** * this takes in an integer vlaue and for the minor tick spacing and creates it on the JScale * it prints the tick marks on the JScale */ public void setMinorTick(int v) { v = v * this.factor; super.setMinorTickSpacing(v); super.setPaintTicks(true); } /** * this takes in interger value and it creates the labels based on this value using a * hashtable with the appropiate incremented values and then prints them on the scale accordingly */ public void createLabels(int inc,int fontsize) { inc = inc * this.factor; int value = this.min * this.factor; Hashtable labelTable = new Hashtable(); int temp = (value / this.factor); labelTable.put(new Integer(value),new JLabel("" + temp) ); while ( value < (this.max * this.factor)) { value = value + inc; labelTable.put( new Integer( value), new JLabel("" + (value/this.factor) )); } this.setLabelTable( labelTable ); this.setPaintLabels(true); } /** * this will take in a title as a string and set it on the scale with a border around it */ public void title(String name,int fontsize) { this.setBackground(Color.lightGray); this.setForeground(Color.black); Border line = BorderFactory.createLineBorder(Color.black,2); Font temp1 = new Font("Impact", Font.BOLD, fontsize ); Border lscale = BorderFactory.createTitledBorder(line,name,2,3,temp1,Color.black); this.setBorder(lscale); } /** * this return the current value for the scale as a float depending on how many decimal you specified * in the parameter when creating the JScale */ public float returnValue() { float temp = super.getValue(); float value = temp/this.factor; return value; } /** * this takes in an integer value and sets it to the JScale */ public void setVal(int temp) { temp = temp * this.factor; this.setValue(temp); } }