import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.applet.*;
import java.net.*;

/** Program to demonstrate the sliders.<p>
*
* Screen snap...<br>
* <center><img src="DemoSlider-1.gif"></center><p>
*
* @see <a href="DemoSlider.java">source code</a>
* @author Scott MacKenzie, 2002
*/
public class DemoSlider
{
   public static void main(String[] args)
   {
      // commented out (Java sliders look better!)
      //{
      //   UIManager.setLookAndFeel(
      //      UIManager.getSystemLookAndFeelClassName());
      //} catch (Exception e) {}

      DemoSliderFrame frame = new DemoSliderFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setTitle("DemoSlider");
      frame.pack(); 
      frame.show();      
   }
}

class DemoSliderFrame extends JFrame
implements ChangeListener, KeyListener, FocusListener
{
   private JTextField messageInput;
   private JTextField messageDisplay;

   private JSlider redF;
   private JSlider greenF;
   private JSlider blueF;
   private JSlider redB;
   private JSlider greenB;
   private JSlider blueB;

   private JTextField redFValue;
   private JTextField greenFValue;
   private JTextField blueFValue;
   private JTextField redBValue;
   private JTextField greenBValue;
   private JTextField blueBValue;

   // constructor

   public DemoSliderFrame()
   {
      // ----------------------------------
      // construct and configure components
      // ----------------------------------

      final int MESSAGE_SIZE = 20;
      messageInput = new JTextField(MESSAGE_SIZE);
      messageInput.setEditable(true);

      messageDisplay = new JTextField(MESSAGE_SIZE);
      messageDisplay.setPreferredSize(new Dimension(1, 100));
      messageDisplay.setText("(enter message above)");
      messageDisplay.setFont(new Font("serif", Font.PLAIN, 24));
      messageDisplay.setEditable(false);
      messageDisplay.setBackground(new Color(0, 0, 0));
      messageDisplay.setForeground(new Color(255, 0, 0));
      messageDisplay.setHorizontalAlignment(SwingConstants.CENTER);

      // create the sliders

      redF = new JSlider(SwingConstants.VERTICAL, 0, 255, 255);
      greenF = new JSlider(SwingConstants.VERTICAL, 0, 255, 0);
      blueF = new JSlider(SwingConstants.VERTICAL, 0, 255, 0);
      redB = new JSlider(SwingConstants.VERTICAL, 0, 255, 0);
      greenB = new JSlider(SwingConstants.VERTICAL, 0, 255, 0);
      blueB = new JSlider(SwingConstants.VERTICAL, 0, 255, 0);

      // configure major and minor tick marks for sliders

      redF.setMajorTickSpacing(50);
      redF.setMinorTickSpacing(10);
      redF.setPaintTicks(true);
      redF.setPaintLabels(true);
      greenF.setMajorTickSpacing(50);
      greenF.setMinorTickSpacing(10);
      greenF.setPaintTicks(true);
      greenF.setPaintLabels(true);
      blueF.setMajorTickSpacing(50);
      blueF.setMinorTickSpacing(10);
      blueF.setPaintTicks(true);
      blueF.setPaintLabels(true);

      redB.setMajorTickSpacing(50);
      redB.setMinorTickSpacing(10);
      redB.setPaintTicks(true);
      redB.setPaintLabels(true);
      greenB.setMajorTickSpacing(50);
      greenB.setMinorTickSpacing(10);
      greenB.setPaintTicks(true);
      greenB.setPaintLabels(true);
      blueB.setMajorTickSpacing(50);
      blueB.setMinorTickSpacing(10);
      blueB.setPaintTicks(true);
      blueB.setPaintLabels(true);

      // add some space between slider (looks better!)

      final int GAP = 10;
      redF.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, GAP));
      greenF.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, GAP));
      blueF.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, GAP));
      redB.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, GAP));
      greenB.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, GAP));
      blueB.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, GAP));

      // create value fields for sliders

      final int SIZE = 4;
      redFValue = new JTextField(SIZE);
      greenFValue = new JTextField(SIZE);
      blueFValue = new JTextField(SIZE);
      redBValue = new JTextField(SIZE);
      greenBValue = new JTextField(SIZE);
      blueBValue = new JTextField(SIZE);

      redFValue.setEditable(false);
      greenFValue.setEditable(false);
      blueFValue.setEditable(false);
      redBValue.setEditable(false);
      greenBValue.setEditable(false);
      blueBValue.setEditable(false);      

      redFValue.setHorizontalAlignment(SwingConstants.CENTER);
      greenFValue.setHorizontalAlignment(SwingConstants.CENTER);
      blueFValue.setHorizontalAlignment(SwingConstants.CENTER);
      redBValue.setHorizontalAlignment(SwingConstants.CENTER);
      greenBValue.setHorizontalAlignment(SwingConstants.CENTER);
      blueBValue.setHorizontalAlignment(SwingConstants.CENTER);

      redFValue.setBackground(new Color(225, 225, 225));
      greenFValue.setBackground(new Color(225, 225, 225));
      blueFValue.setBackground(new Color(225, 225, 225));
      redBValue.setBackground(new Color(225, 225, 225));
      greenBValue.setBackground(new Color(225, 225, 225));
      blueBValue.setBackground(new Color(225, 225, 225));

      update();

      // -------------
      // add listeners
      // -------------

      messageInput.addKeyListener(this);
      messageInput.addFocusListener(this);

      redF.addChangeListener(this);
      greenF.addChangeListener(this);
      blueF.addChangeListener(this);
      redB.addChangeListener(this);
      greenB.addChangeListener(this);
      blueB.addChangeListener(this);

      // -----------------
      // layout components
      // -----------------

      // add components to panels

      JPanel messageInputPanel = new JPanel();
      messageInputPanel.add(messageInput);
      messageInputPanel.setBorder(new TitledBorder(new EtchedBorder(),
         "Please enter a message"));

      JPanel f1 = new JPanel();
      f1.setLayout(new GridLayout(1, 0));
      f1.add(new JLabel("Red", SwingConstants.CENTER));
      f1.add(new JLabel("Green", SwingConstants.CENTER));
      f1.add(new JLabel("Blue", SwingConstants.CENTER));

      JPanel f2 = new JPanel();
      f2.setLayout(new GridLayout(1, 0));
      f2.add(redF);
      f2.add(greenF);
      f2.add(blueF);

      JPanel f3 = new JPanel();
      f3.setLayout(new GridLayout(1, 0));
      f3.add(redFValue);
      f3.add(greenFValue);
      f3.add(blueFValue);

      JPanel foregroundPanel = new JPanel();
      foregroundPanel.setLayout(new BorderLayout());
      foregroundPanel.add(f1, "North");
      foregroundPanel.add(f2, "Center");
      foregroundPanel.add(f3, "South");
      foregroundPanel.setBorder(new TitledBorder(new EtchedBorder(),
         "Foreground colour"));

      JPanel b1 = new JPanel();
      b1.setLayout(new GridLayout(1, 0));
      b1.add(new JLabel("Red ", SwingConstants.CENTER));
      b1.add(new JLabel("Green ", SwingConstants.CENTER));
      b1.add(new JLabel("Blue ", SwingConstants.CENTER));

      JPanel b2 = new JPanel();
      b2.setLayout(new GridLayout(1, 0));
      b2.add(redB);
      b2.add(greenB);
      b2.add(blueB);

      JPanel b3 = new JPanel();
      b3.setLayout(new GridLayout(1, 0));
      b3.add(redBValue);
      b3.add(greenBValue);
      b3.add(blueBValue);

      JPanel backgroundPanel = new JPanel();
      backgroundPanel.setLayout(new BorderLayout());
      backgroundPanel.add(b1, "North");
      backgroundPanel.add(b2, "Center");
      backgroundPanel.add(b3, "South");
      backgroundPanel.setBorder(new TitledBorder(new EtchedBorder(),
         "Background colour"));

      JPanel colourPanel = new JPanel();
      colourPanel.add(foregroundPanel);
      colourPanel.add(backgroundPanel);

      // arrange panels

      JPanel top = new JPanel();
      top.setLayout(new BoxLayout(top, BoxLayout.Y_AXIS));
      top.add(messageInputPanel);
      top.add(colourPanel);

      JPanel contentPane = new JPanel();
      contentPane.setLayout(new BorderLayout());
      contentPane.add(top, "Center");
      contentPane.add(messageDisplay, "South");

      // make panel this JFrame's content pane

      this.setContentPane(contentPane);
   }

   // ---------------------------------
   // implement KeyListener methods (3)
   // ---------------------------------

   public void keyReleased(KeyEvent ke) {}
   public void keyTyped(KeyEvent ke) {}
   public void keyPressed(KeyEvent ke)
   {
      // for in-class demo...
      //System.out.println(ke.paramString());

      // We're only interested in the Enter key
      if (ke.getKeyCode() != KeyEvent.VK_ENTER)
         return;

      messageDisplay.setText(messageInput.getText());
   }

   // -------------------------------
   // implement FocusListener methods
   // -------------------------------

   public void focusGained(FocusEvent fe) {}
   public void focusLost(FocusEvent fe)
   {
      // for in-class demo...
      //System.out.println(fe.paramString());

      Object source = fe.getSource();

      if (source == messageInput && messageInput.getText().length() > 0)
         messageDisplay.setText(messageInput.getText());
   }

   // -------------------------------
   // implement ChangeListener method
   // -------------------------------

   public void stateChanged(ChangeEvent ce)
   {
      JSlider source = (JSlider)ce.getSource();

      // for in-class demo...
      //System.out.println(source.getValue());

      update();
   }

   // -------------
   // Other methods
   // -------------

   /** Update the message display, based on the slider positions
   */
   public void update()
   {
      // get RGB values from sliders
      Color fore = new Color(redF.getValue(), greenF.getValue(), blueF.getValue());
      Color back = new Color(redB.getValue(), greenB.getValue(), blueB.getValue());

      // update the displays below the sliders
      redFValue.setText("" + redF.getValue());
      greenFValue.setText("" + greenF.getValue());
      blueFValue.setText("" + blueF.getValue());
      redBValue.setText("" + redB.getValue());
      greenBValue.setText("" + greenB.getValue());
      blueBValue.setText("" + blueB.getValue());

      // update the colours for the displayed message
      messageDisplay.setForeground(fore);
      messageDisplay.setBackground(back);
   }
}

