/*
 *  =====================================================================
 *  DemoColorChoosser.java : Use of JColorChooser in a simple drawing
 *  applet..
 * 
 * 
 *  =====================================================================
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class DemoColorChooser extends JApplet {
    Container container = null;
    DrawingPanel panel = null;
    JColorChooser colorChooser = null;
    JDialog dialog = null;
    int oldX, oldY, newX, newY;
    Color color = null;

    public void init() {

        // 1. Get a handle on the applet's content pane.

        container = this.getContentPane();

        // 2. Create a drawing panel and add it at the center
        // of the content pane of the applet.
        
        panel = new DrawingPanel();
        container.add(panel); 

        // 3. Add a button at the bottom portion
        // of the applet to display the color chooser pane.

        JButton showButton = new JButton("Show Color Chooser");
        showButton.addActionListener(new ButtonListener());
        Box hBox = Box.createHorizontalBox();
        hBox.add(Box.createHorizontalGlue());
        hBox.add(showButton);
        hBox.add(Box.createHorizontalGlue());
        container.add(hBox, BorderLayout.SOUTH);

        // 4. Create a color chooser object and a dialog box
        //  that displays the color chooser.

        colorChooser = new JColorChooser();
        dialog = JColorChooser.createDialog(
                            container,
                            "Color Chooser",
                            false,
                            colorChooser,
                            new ButtonListener(),
                            new ButtonListener());
   }

   // 5. This is where you perform your drawing with
   // the selected color.

   class DrawingPanel extends Panel {
        public DrawingPanel () {
            setBackground(Color.white);
            MyMouseListener mouseListener = new MyMouseListener();
            addMouseListener(mouseListener);
            addMouseMotionListener(mouseListener);
        }

        public void update(Graphics g) {
            g.setColor(color);
            paint(g);
        }

        public void paint(Graphics g) {
           g.drawLine(oldX, oldY, newX, newY);
        }
   }

   // 6. Listener class that responds to all button actions.

   class ButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JButton button = (JButton) e.getSource();

            if (button.getText().equals("Show Color Chooser")) {
                dialog.show();
            }

            if (button.getText().equals("OK")) {
                //dialog.show();
                color = colorChooser.getColor();
            }
            else if(button.getText().equals("Cancel")) {
                dialog.dispose();
            }
            // Note: Don't have to handle the actions of the
            // 'Reset' button; this button has been implemented
            // to reset the color to the previously used color.
        }
    }

    // 7. Mouse listener class to draw on the canvas.

    class MyMouseListener extends MouseAdapter
                        implements MouseMotionListener {
        public void mousePressed(MouseEvent e) {
            oldX = e.getX(); oldY = e.getY();
            newX = e.getX(); newY = e.getY();

            panel.repaint();
        }

        public void mouseDragged(MouseEvent e) {
            oldX = newX; oldY = newY;
            newX = e.getX(); newY = e.getY();

            panel.repaint();
        }

        public void mouseMoved(MouseEvent e) {}
    }
}
