Use Slider to control the color R G B value : JSlider « Swing « Java Tutorial

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Swing » JSlider 
14.30.15.Use Slider to control the color R G B valuePrevious/Next
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(500500);
    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(02550505);
    sliderG = getSlider(02550505);
    sliderB = getSlider(02550505);
    sliderH = getSlider(010051);
    sliderS = getSlider(010051);
    sliderBr = getSlider(010051);
    sliderAlpha = getSlider(0255255505);

    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(62150));

    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(350350);
      color = new Color(000);
      setBackgroundColor();
    }

    public void setBackgroundColor() {
      color = new Color(redValue, greenValue, blueValue, alphaValue);
      setBackground(color);
    }
  }

  class SliderListener implements ChangeListener {
    public void stateChanged(ChangeEvent e) {
      JSlider slider = (JSlidere.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[010));
      sliderS.setValue((int) (canvas.hsbValues[110));
      sliderBr.setValue((int) (canvas.hsbValues[210));

      rgbValue.setText(Integer.toString(canvas.color.getRGB() 0xffffff16));
    }

    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);
    }
  }
}
14.30.JSlider
14.30.1.JSlider Introduction
14.30.2.JSlider lets the user graphically select a value by sliding a knob within a bounded interval.
14.30.3.Creating a JSlider Component
14.30.4.Create a horizontal slider with custom min, max, and value
14.30.5.Create a vertical slider with min=0, max=100, value=50
14.30.6.Create a vertical slider with custom min, max, and value
14.30.7.Listening for Value Changes in a JSlider Component
14.30.8.Tracking changes to a JSlider with a ChangeListenerTracking changes to a JSlider with a ChangeListener
14.30.9.Displaying Tick Marks Within a JSliderDisplaying Tick Marks Within a JSlider
14.30.10.Snapping the JSlider Thumb into PositionSnapping the JSlider Thumb into Position
14.30.11.Labeling JSlider Positions: use text to mark JSliderLabeling JSlider Positions: use text to mark JSlider
14.30.12.Setting ThumbIcon for JSliderSetting ThumbIcon for JSlider
14.30.13.JSlider Client Properties: JSlider.isFilledJSlider Client Properties: JSlider.isFilled
14.30.14.Slider change event
14.30.15.Use Slider to control the color R G B valueUse Slider to control the color R G B value
14.30.16.AdjustmentListener and ChangeListener for JSlider
14.30.17.Move the slider by a fixed amount: the extent.
14.30.18.Get the extent
14.30.19.Getting and Setting the Values of a JSlider Component
14.30.20.Set major tick marks every 25 units
14.30.21.Set minor tick marks every 5 units
14.30.22.Set to a spot between tick marks; the value moves to closest tick mark
14.30.23.Set the value; the new value will be forced into the bar's range
14.30.24.Set the extent
14.30.25.Set all the values at once by using the model
14.30.26.Make it vertical and move bottom-to-top
14.30.27.Make it vertical and move top-to-bottom
14.30.28.Make the horizontal slider move right-to-left
14.30.29.Create a dynamic icon
14.30.30.Hide the track
14.30.31.Show tick marks
14.30.32.Change the minimum value
14.30.33.Change the maximum value
14.30.34.Snap to tick marks
14.30.35.Constraining JSlider Component Values to Tick Marks
14.30.36.Determine if currently snapping to tick marks
14.30.37.Determine if currently painting labels
14.30.38.A slider with tick marks and labels
14.30.39.The slider allows you to use an arbitrary label at any particular major tick mark.
14.30.40.Customizing a JSlider Look and Feel
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.