Java Tutorial/Swing/JSlider

Материал из Java эксперт
Перейти к: навигация, поиск

Содержание

AdjustmentListener and ChangeListener for JSlider

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Slippery {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel main = new JPanel(new GridLayout(2, 1));
    JPanel scrollBarPanel = new JPanel();
    final JScrollBar scrollBar = new JScrollBar(JScrollBar.HORIZONTAL, 0, 48, 0, 255);
    int height = scrollBar.getPreferredSize().height;
    scrollBar.setPreferredSize(new Dimension(175, height));
    scrollBarPanel.add(scrollBar);
    main.add(scrollBarPanel);
    JPanel sliderPanel = new JPanel();
    final JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 255, 128);
    slider.setMajorTickSpacing(48);
    slider.setMinorTickSpacing(16);
    slider.setPaintTicks(true);
    sliderPanel.add(slider);
    main.add(sliderPanel);
    frame.add(main, BorderLayout.CENTER);
    scrollBar.addAdjustmentListener(new AdjustmentListener() {
      public void adjustmentValueChanged(AdjustmentEvent e) {
        System.out.println("JScrollBar"s current value = " + scrollBar.getValue());
      }
    });
    slider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        System.out.println("JSlider"s current value = " + slider.getValue());
      }
    });
    frame.pack();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}





A slider with tick marks and labels

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JSlider;
public class Main extends JFrame {
  public static void main(String[] args) {
    new Main();
  }
  public Main() {
    Container content = getContentPane();
    content.setBackground(Color.white);
    JSlider slider1 = new JSlider();
    slider1.setBorder(BorderFactory.createTitledBorder("JSlider without Tick Marks"));
    content.add(slider1, BorderLayout.NORTH);
    JSlider slider2 = new JSlider();
    slider2.setBorder(BorderFactory.createTitledBorder("JSlider with Tick Marks"));
    slider2.setMajorTickSpacing(20);
    slider2.setMinorTickSpacing(5);
    slider2.setPaintTicks(true);
    content.add(slider2, BorderLayout.CENTER);
    JSlider slider3 = new JSlider();
    slider3.setBorder(BorderFactory.createTitledBorder("JSlider with Tick Marks & Labels"));
    slider3.setMajorTickSpacing(20);
    slider3.setMinorTickSpacing(5);
    slider3.setPaintTicks(true);
    slider3.setPaintLabels(true);
    content.add(slider3, BorderLayout.SOUTH);
    setSize(300,300);
    setVisible(true);
  }
}





Change the maximum value

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    int newMax = 256;
    slider.setMaximum(newMax);
  }
}





Change the minimum value

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    int newMin = 0;
    slider.setMinimum(newMin);
  }
}





Constraining JSlider Component Values to Tick Marks

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create a horizontal slider that moves left-to-right
    JSlider slider = new JSlider();
    // Set major tick marks every 25 units
    int tickSpacing = 25;
    slider.setMajorTickSpacing(tickSpacing);
  }
}





Create a dynamic icon

import java.awt.BorderLayout;
import java.awt.ruponent;
import java.awt.Graphics;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class Main{
  Icon icon = new DynamicIcon();
  final JSlider width = new JSlider(JSlider.HORIZONTAL, 1, 150, 75);
  final JSlider height = new JSlider(JSlider.VERTICAL, 1, 150, 75);
  final JLabel dynamicLabel = new JLabel(icon);
  Main(){
    Updater updater = new Updater();
    width.addChangeListener(updater);
    height.addChangeListener(updater);
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    f.add(width, BorderLayout.NORTH);
    f.add(height, BorderLayout.WEST);
    f.add(dynamicLabel, BorderLayout.CENTER);
    f.setSize(210, 210);
    f.setVisible(true);
  }
  public static void main(String[] args) {
    new Main();
  }
  class Updater implements ChangeListener {
    public void stateChanged(ChangeEvent ev) {
      dynamicLabel.repaint();
    }
  }
  class DynamicIcon implements Icon {
    public int getIconWidth() {
      return width.getValue();
    }
    public int getIconHeight() {
      return height.getValue();
    }
    public void paintIcon(Component c, Graphics g, int x, int y) {
      g.fill3DRect(x, y, getIconWidth(), getIconHeight(), true);
    }
  }
}





Create a horizontal slider with custom min, max, and value

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    int initValue = 0;
    int minimum = 10;
    int maximum = 100;
    JSlider slider = new JSlider(minimum, maximum, initValue);
  }
}





Create a vertical slider with custom min, max, and value

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    int initValue = 0;
    int minimum = 10;
    int maximum = 100;
    JSlider slider = new JSlider(JSlider.VERTICAL, minimum, maximum, initValue);
  }
}





Create a vertical slider with min=0, max=alue=50

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider(JSlider.VERTICAL);
  }
}





Creating a JSlider Component

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create a horizontal slider with min=0, max=100, value=50
    JSlider slider = new JSlider();
    // Create a horizontal slider with custom min and max; value is set to the middle
    int minimum = -255;
    int maximum = 256;
    slider = new JSlider(minimum, maximum);
  }
}





Customizing a JSlider Look and Feel

Property StringObject TypeSlider.actionMapActionMapSlider.altTrackColorColorSlider.backgroundColorSlider.borderBorderSlider.darkShadowColorSlider.focusColorSlider.focusGradientListSlider.focusInputMapObject[ ]Slider.focusInputMap.RightToLeftInputMapSlider.focusInsetsInsetsSlider.foregroundColorSlider.gradientListSlider.highlightColorSlider.horizontalSizeDimensionSlider.horizontalThumbIconIconSlider.majorTickLengthIntegerSlider.minimumHorizontalSizeDimensionSlider.minimumVerticalSizeDimensionSlider.paintThumbArrowShapeBooleanSlider.paintValueBooleanSlider.shadowColorSlider.thumbColorSlider.thumbHeightIntegerSlider.thumbWidthIntegerSlider.tickColorColorSlider.trackBorderBorderSlider.trackWidthIntegerSlider.verticalSizeDimensionSlider.verticalThumbIconIconSliderUIString


Determine if currently painting labels

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    boolean b = slider.getPaintLabels(); 
    // Paint labels at the major ticks - 0, 25, 50, 75, and 100
    slider.setPaintLabels(true);
  }
}





Determine if currently snapping to tick marks

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create a horizontal slider that moves left-to-right
    JSlider slider = new JSlider();
    boolean b = slider.getSnapToTicks(); // false
  }
}





Displaying Tick Marks Within a JSlider

import java.awt.BorderLayout;
import javax.swing.BoundedRangeModel;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TickSliders {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Tick Slider");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JSlider jSliderOne = new JSlider();
    // Major Tick 25 - Minor 5
    jSliderOne.setMinorTickSpacing(5);
    jSliderOne.setMajorTickSpacing(25);
    jSliderOne.setPaintTicks(true);
    jSliderOne.setSnapToTicks(true);
    frame.add(jSliderOne, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





Get the extent

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    int extent = slider.getExtent();
  }
}





Getting and Setting the Values of a JSlider Component

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    // Get the current value
    int value = slider.getValue();
    // Get the minimum value
    int min = slider.getMinimum();
    // Get the maximum value
    int max = slider.getMaximum();
  }
}





Hide the track

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    slider.setPaintTrack(false);
  }
}





JSlider Client Properties: JSlider.isFilled

You can enable a client property that signals the slider to fill this track up to the point of the current value that the thumb has crossed. The name of this property is JSlider.isFilled, and a Boolean object represents the current setting.



import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JSlider;
public class TickSlidersClientProperty {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Tick Slider");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JSlider oneJSlider = new JSlider();
    oneJSlider.putClientProperty("JSlider.isFilled", Boolean.TRUE);
    JSlider anotherJSlider = new JSlider();
    // Set to default setting
    anotherJSlider.putClientProperty("JSlider.isFilled", Boolean.FALSE);
    frame.add(oneJSlider, BorderLayout.NORTH);
    frame.add(anotherJSlider, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





JSlider Introduction

There are six different constructors for JSlider:



public JSlider()
JSlider aJSlider = new JSlider();
public JSlider(int orientation)
JSlider aJSlider = new JSlider(JSlider.VERTICAL);
JSlider bJSlider = new JSlider(JSlider.HORIZONTAL);
public JSlider(int minimum, int maximum)
// Initial value midpoint / 0
JSlider aJSlider = new JSlider(-100, 100);
public JSlider(int minimum, int maximum, int value)
JSlider aJSlider = new JSlider(-100, 100, 0);
public JSlider(int orientation, int minimum, int maximum, int value)
// Vertical, initial value 6, range 1-12 (months of year)
JSlider aJSlider = new JSlider(JSlider.VERTICAL, 6, 1, 12);
public JSlider(BoundedRangeModel model)
// Data model, initial value 3, range 1-31, and extent of 0
// JSlider direction changed to vertical prior to display on screen
DefaultBoundedRangeModel model = new DefaultBoundedRangeModel(3, 0, 1, 31);
JSlider aJSlider = new JSlider(model);
aJSlider.setOrientation(JSlider.VERTICAL);





JSlider lets the user graphically select a value by sliding a knob within a bounded interval.

import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Slider {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    final JSlider slider = new JSlider(0, 150, 0);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    slider.setPreferredSize(new Dimension(150, 30));
    slider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent event) {
        int value = slider.getValue();
        if (value == 0) {
          System.out.println("0");
        } else if (value > 0 && value <= 30) {
          System.out.println("value > 0 && value <= 30");
        } else if (value > 30 && value < 80) {
          System.out.println("value > 30 && value < 80");
        } else {
          System.out.println("max");
        }
      }
    });
    f.add(slider);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}





Labeling JSlider Positions: use text to mark JSlider

import java.awt.BorderLayout;
import java.util.Hashtable;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSlider;
public class TickSlidersLabelTable {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Tick Slider");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JSlider jSliderOne = new JSlider();
    jSliderOne.setPaintLabels(true);
    
    Hashtable<Integer, JLabel> table = new Hashtable<Integer, JLabel>();
    table.put (0, new JLabel(new ImageIcon("yourFile.gif")));
    table.put (10, new JLabel("Ten"));
    table.put (25, new JLabel("Twenty-Five"));
    table.put (34, new JLabel("Thirty-Four"));
    table.put (52, new JLabel("Fifty-Two"));
    table.put (70, new JLabel("Seventy"));
    table.put (82, new JLabel("Eighty-Two"));
    jSliderOne.setLabelTable (table);
    
    frame.add(jSliderOne, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





Listening for Value Changes in a JSlider Component

import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    slider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent evt) {
        JSlider slider = (JSlider) evt.getSource();
        if (!slider.getValueIsAdjusting()) {
          int value = slider.getValue();
          System.out.println(value);
        }
      }
    });
  }
}





Make it vertical and move bottom-to-top

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    slider.setOrientation(JSlider.VERTICAL);
    slider.setInverted(false);
  }
}





Make it vertical and move top-to-bottom

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    slider.setOrientation(JSlider.VERTICAL);
    slider.setInverted(true);
  }
}





Make the horizontal slider move right-to-left

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    slider.setInverted(true);
  }
}





Move the slider by a fixed amount: the extent.

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    // Set an extent
    int extent = 10;
    slider.setExtent(extent);
  }
}





Set major tick marks every 25 units

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create a horizontal slider that moves left-to-right
    JSlider slider = new JSlider();
    int tickSpacing = 25;
    slider.setMajorTickSpacing(tickSpacing);
  }
}





Set minor tick marks every 5 units

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create a horizontal slider that moves left-to-right
    JSlider slider = new JSlider();
    int tickSpacing = 5;
    slider.setMinorTickSpacing(tickSpacing);
  }
}





Set the extent

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    int newExtent = 10;
    slider.setExtent(newExtent);
  }
}





Set the value; the new value will be forced into the bar"s range

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    int newValue = 33;
    slider.setValue(newValue);
  }
}





Setting ThumbIcon for JSlider

import java.awt.BorderLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
public class SlidersThumbIcon {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Tick Slider");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // No Ticks
    JSlider jSliderOne = new JSlider();
    Icon icon = new ImageIcon("yourFile.gif");
    UIDefaults defaults = UIManager.getDefaults();
    defaults.put("Slider.horizontalThumbIcon", icon);
    frame.add(jSliderOne, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





Set to a spot between tick marks; the value moves to closest tick mark

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    slider.setValue(27);
    int value = slider.getValue();
  }
}





Show tick marks

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create a horizontal slider that moves left-to-right
    JSlider slider = new JSlider();
    slider.setPaintTicks(true);
  }
}





Slider change event

import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Hashtable;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Test extends JFrame {
  JSlider slider1 = new JSlider();
  JSlider slider2 = new JSlider(JSlider.HORIZONTAL, -100, 100, 0);
  public Test() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().setLayout(new GridLayout(2, 1));
    slider1.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        System.out.println("Slider1: " + slider1.getValue());
      }
    });
    getContentPane().add(slider1);
    slider2.setPaintTicks(true);
    slider2.setMajorTickSpacing(50);
    slider2.setMinorTickSpacing(10);
    slider2.setPaintLabels(true);
    Hashtable ht = slider2.createStandardLabels(50);
    slider2.setLabelTable(ht);
    slider2.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
        System.out.println("Slider2: " + slider2.getValue());
      }
    });
    getContentPane().add(slider2);
    pack();
  }
  public static void main(String[] args) {
    new Test().setVisible(true);
  }
}





Snapping the JSlider Thumb into Position

import java.awt.BorderLayout;
import javax.swing.BoundedRangeModel;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class TickSlidersSnapToTicks {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Tick Slider");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JSlider jSliderOne = new JSlider();
    // Major Tick 25 - Minor 5
    jSliderOne.setMinorTickSpacing(5);
    jSliderOne.setMajorTickSpacing(25);
    jSliderOne.setPaintTicks(true);
    jSliderOne.setSnapToTicks(true);
    frame.add(jSliderOne, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





Snap to tick marks

import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    // Create a horizontal slider that moves left-to-right
    JSlider slider = new JSlider();
    slider.setSnapToTicks(true);
  }
}





The slider allows you to use an arbitrary label at any particular major tick mark.

import java.util.Dictionary;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JSlider;
public class Main {
  public static void main(String[] argv) throws Exception {
    JSlider slider = new JSlider();
    
    Dictionary table = slider.getLabelTable();
    ImageIcon icon = new ImageIcon("icon.gif");
    JLabel label = new JLabel(icon);
    // Set at desired positions
    table.put(new Integer(slider.getMinimum()), label);
    table.put(new Integer(slider.getMaximum()), label);
    // Force the slider to use the new labels
    slider.setLabelTable(table);
  }
}





Tracking changes to a JSlider with a ChangeListener

import java.awt.BorderLayout;
import javax.swing.BoundedRangeModel;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
class BoundedChangeListener implements ChangeListener {
  public void stateChanged(ChangeEvent changeEvent) {
    Object source = changeEvent.getSource();
    if (source instanceof BoundedRangeModel) {
      BoundedRangeModel aModel = (BoundedRangeModel) source;
      if (!aModel.getValueIsAdjusting()) {
        System.out.println("Changed: " + aModel.getValue());
      }
    } else if (source instanceof JSlider) {
      JSlider theJSlider = (JSlider) source;
      if (!theJSlider.getValueIsAdjusting()) {
        System.out.println("Slider changed: " + theJSlider.getValue());
      }
    } else {
      System.out.println("Something changed: " + source);
    }
  }
}
public class TickSliders {
  public static void main(String args[]) {
    JFrame frame = new JFrame("Tick Slider");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JSlider jSliderOne = new JSlider();
    jSliderOne.addChangeListener(new BoundedChangeListener());
    frame.add(jSliderOne, BorderLayout.NORTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}





Use Slider to control the color R G B value

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class ColorValueSliderControl extends JFrame {
  public ColorValueSliderControl() {
    getContentPane().add(new TColor());
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(500, 500);
    setVisible(true);
  }
  public static void main(String arg[]) {
    new ColorValueSliderControl();
  }
}
class TColor extends JPanel {
  DrawingCanvas canvas = new DrawingCanvas();
  JLabel rgbValue = new JLabel("");
  JSlider sliderR, sliderG, sliderB, sliderH, sliderS, sliderBr,
      sliderAlpha;
  public TColor() {
    sliderR = getSlider(0, 255, 0, 50, 5);
    sliderG = getSlider(0, 255, 0, 50, 5);
    sliderB = getSlider(0, 255, 0, 50, 5);
    sliderH = getSlider(0, 10, 0, 5, 1);
    sliderS = getSlider(0, 10, 0, 5, 1);
    sliderBr = getSlider(0, 10, 0, 5, 1);
    sliderAlpha = getSlider(0, 255, 255, 50, 5);
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(6, 2, 15, 0));
    panel.add(new JLabel("R-G-B Sliders (0 - 255)"));
    panel.add(new JLabel("H-S-B Sliders (ex-1)"));
    panel.add(sliderR);
    panel.add(sliderH);
    panel.add(sliderG);
    panel.add(sliderS);
    panel.add(sliderB);
    panel.add(sliderBr);
    panel.add(new JLabel("Alpha Adjustment (0 - 255): ", JLabel.RIGHT));
    panel.add(sliderAlpha);
    panel.add(new JLabel("RGB Value: ", JLabel.RIGHT));
    
    rgbValue.setBackground(Color.white);
    rgbValue.setForeground(Color.black);
    rgbValue.setOpaque(true);
    panel.add(rgbValue);
    add(panel, BorderLayout.SOUTH);
    add(canvas);
  }
  public JSlider getSlider(int min, int max, int init, int mjrTkSp, int mnrTkSp) {
    JSlider slider = new JSlider(JSlider.HORIZONTAL, min, max, init);
    slider.setPaintTicks(true);
    slider.setMajorTickSpacing(mjrTkSp);
    slider.setMinorTickSpacing(mnrTkSp);
    slider.setPaintLabels(true);
    slider.addChangeListener(new SliderListener());
    return slider;
  }
  class DrawingCanvas extends Canvas {
    Color color;
    int redValue, greenValue, blueValue;
    int alphaValue = 255;
    float[] hsbValues = new float[3];
    public DrawingCanvas() {
      setSize(350, 350);
      color = new Color(0, 0, 0);
      setBackgroundColor();
    }
    public void setBackgroundColor() {
      color = new Color(redValue, greenValue, blueValue, alphaValue);
      setBackground(color);
    }
  }
  class SliderListener implements ChangeListener {
    public void stateChanged(ChangeEvent e) {
      JSlider slider = (JSlider) e.getSource();
      if (slider == sliderAlpha) {
        canvas.alphaValue = slider.getValue();
        canvas.setBackgroundColor();
      } else if (slider == sliderR) {
        canvas.redValue = slider.getValue();
        displayRGBColor();
      } else if (slider == sliderG) {
        canvas.greenValue = slider.getValue();
        displayRGBColor();
      } else if (slider == sliderB) {
        canvas.blueValue = slider.getValue();
        displayRGBColor();
      } else if (slider == sliderH) {
        canvas.hsbValues[0] = (float) (slider.getValue() * 0.1);
        displayHSBColor();
      } else if (slider == sliderS) {
        canvas.hsbValues[1] = (float) (slider.getValue() * 0.1);
        displayHSBColor();
      } else if (slider == sliderBr) {
        canvas.hsbValues[2] = (float) (slider.getValue() * 0.1);
        displayHSBColor();
      }
      canvas.repaint();
    }
    public void displayRGBColor() {
      canvas.setBackgroundColor();
      Color.RGBtoHSB(canvas.redValue, canvas.greenValue, canvas.blueValue,canvas.hsbValues);
      sliderH.setValue((int) (canvas.hsbValues[0] * 10));
      sliderS.setValue((int) (canvas.hsbValues[1] * 10));
      sliderBr.setValue((int) (canvas.hsbValues[2] * 10));
      rgbValue.setText(Integer.toString(canvas.color.getRGB() & 0xffffff, 16));
    }
    public void displayHSBColor() {
      canvas.color = Color.getHSBColor(canvas.hsbValues[0],
          canvas.hsbValues[1], canvas.hsbValues[2]);
      canvas.redValue = canvas.color.getRed();
      canvas.greenValue = canvas.color.getGreen();
      canvas.blueValue = canvas.color.getBlue();
      sliderR.setValue(canvas.redValue);
      sliderG.setValue(canvas.greenValue);
      sliderB.setValue(canvas.blueValue);
      canvas.color = new Color(canvas.redValue, canvas.greenValue,
          canvas.blueValue, canvas.alphaValue);
      canvas.setBackground(canvas.color);
    }
  }
}