package slider; import java.awt.*; import javax.swing.*; import slider.*; public class Slider extends JComponent { // The h_spacing determines the amount of horizontal spacing between // the min and max labels and the slider. The v_spacing determines the // amount of vertical spacing between the label for the current value and // the indicator of the current value final int LABEL_H_SPACING = 10; final int LABEL_V_SPACING = 5; SliderModel model; public Slider (SliderModel m) { model = m; } public void valueChanged (int old_v) {repaint();} public void titleChanged (String old_t) {repaint();} public void paintComponent(Graphics g) { // always call the superclass paintComponent method. It clears the // the clipping region. If you forget to call the superclass // paintComponent method then you may see ghosting. super.paintComponent(g); // Get model information for displaying the slider's label and its // current value int value = model.getValue(); String title = model.getTitle(); // Obtain dimensions of the canvas on which this slider is being drawn Dimension d = getSize(); int sliderWidth = d.width * 3 / 4; int sliderHeight = sliderWidth / 20; int minValue = model.getMinValue(); int maxValue = model.getMaxValue(); // Draw the slider's horizontal line int lineX1 = (d.width - sliderWidth) / 2; int lineY1 = d.height / 2; g.drawLine(lineX1, lineY1, lineX1 + sliderWidth, lineY1); // Reset color temporarily // Obtain current Color instance Color colorHandle = g.getColor(); g.setColor(Color.red); // Draw the slider's vertical indicator line int pointerPosition = lineX1 + sliderWidth * (value - minValue) / (maxValue - minValue); g.drawLine(pointerPosition, lineY1, pointerPosition, lineY1 - sliderHeight); // restore color g.setColor(colorHandle); // Prepare font -- the font size should be a minimum of 12 pt but should // get bigger as the window gets bigger int fontSize = (d.width / 30 < 12 ? 12 : d.width / 30); g.setFont(new Font(Font.SANS_SERIF, Font.BOLD, fontSize)); // Write title FontMetrics f = g.getFontMetrics(); int stringWidth = f.stringWidth(title); // Center the title horizontally under the slider int textLeft = (d.width - stringWidth) / 2; int textTop = lineY1 + (2 * f.getHeight()); g.drawString(title, textLeft, textTop); // Display minimum and maximum values String minValueString = Integer.toString(minValue); String maxValueString = Integer.toString(maxValue); int minValueWidth = f.stringWidth(minValueString); int maxValueWidth = f.stringWidth(maxValueString); g.drawString(minValueString, lineX1 - minValueWidth - LABEL_H_SPACING, lineY1 + f.getHeight() / 2); g.drawString(maxValueString, lineX1 + sliderWidth + LABEL_H_SPACING, lineY1 + f.getHeight() / 2); // Display current value String currentValueString = Integer.toString(value); int currentValueWidth = f.stringWidth(currentValueString); g.drawString(currentValueString, pointerPosition - currentValueWidth / 2, lineY1 - sliderHeight - LABEL_V_SPACING); } public Dimension getMinimumSize() {return new Dimension(400, 100);} public Dimension getPreferredSize() {return new Dimension(400, 100);} }