How to Draw Color Picker applet Java code example

import java.awt.*;

public class ColorPicker extends Panel
{

    public ColorPicker(String s, Color c)
    {
        b = new Button("...");
        p = new Panel();
        setLayout(null);
        color = c;
        l = new Label(s);
        l.setBounds(0, 0, 100, 20);
        b.setBounds(100, 0, 20, 20);
        p.setBounds(120, 0, 40, 20);
        add(l);
        add(b);
        add(p);
        setSize(160, 20);
    }

    public ColorPicker(String s, Color c, int w)
    {
        this(s, c);
        l.setBounds(0, 0, w, 20);
        b.setBounds(w, 0, 20, 20);
        p.setBounds(w + 20, 0, 40, 20);
        setSize(60 + w, 20);
    }

    public void setColor(Color c)
    {
        color = c;
        p.setBackground(c);
    }

    public Color getColor()
    {
        return color;
    }

    public boolean handleEvent(Event evt)
    {
        if(evt.target == b && evt.id == 1001)
        {
            ColorDialog cd = new ColorDialog(new Frame(), color);
            if(cd.isOK)
                setColor(cd.color);
            return true;
        } else
        {
            return super.handleEvent(evt);
        }
    }

    public static void main(String args[])
    {
        Frame f = new Frame();
        ColorPicker tp = new ColorPicker("Test color:", Color.white);
        f.add(tp);
        f.setSize(tp.getSize().width, tp.getSize().height + 40);
        f.show();
    }

    private Label l;
    private Button b;
    private Panel p;
    private Color color;
}

Comments

Recent Post

Recent Posts Widget

Popular posts from this blog

Capture image from webcam java code, examples

Shallow Copy Deep Copy Java example

Database Connection using NODE JS example