I put together a simple color chooser and havce one other thing I want to accomplish but can't figure out.

I want the individual scrollbar to change color to reflect the amount of that particular color ie red (255,0,0)

Also, in playing with this I have realized that it is not very portable. My layout manager needs is using constants and needs to work off the individual machine settings.

any thoughts?

package colorchooser;

/**
  */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.applet.Applet;

public class ColorChooser extends Applet implements ActionListener
{
    String msg="";
	Scrollbar red_slider,green_slider,blue_slider;
    Label GREEN;
    Label BLUE;
    Label RED;
    TextField RedValue;
    TextField GreenValue;
    TextField BlueValue;
    Color c;

    public void init() {
        ColorChooserLayout customLayout = new ColorChooserLayout();

        setFont(new Font("Helvetica", Font.PLAIN, 12));
        setLayout(customLayout);

        red_slider = new Scrollbar(Scrollbar.HORIZONTAL,100,10,0,265);
        add(red_slider);

        blue_slider = new Scrollbar(Scrollbar.HORIZONTAL,100,10,0,265);
        add(blue_slider);

        green_slider = new Scrollbar(Scrollbar.HORIZONTAL,100,10,0,265);
        add(green_slider);

        GREEN = new Label("green",Label.CENTER);
        add(GREEN);

        BLUE = new Label("blue",Label.CENTER);
        add(BLUE);

        RED = new Label("red",Label.CENTER);
        add(RED);

        BlueValue = new TextField(""+blue_slider.getValue());
        BlueValue.addActionListener(this);
        add(BlueValue);

        GreenValue = new TextField(""+green_slider.getValue());
        GreenValue.addActionListener(this);
        add(GreenValue);

        RedValue = new TextField(""+red_slider.getValue());
        RedValue.addActionListener(this);
        add(RedValue);
        
        c=new Color(red_slider.getValue(),green_slider.getValue(),blue_slider.getValue());
    	setBackground(c);

        setSize(getPreferredSize());

    }
    
    public boolean handleEvent(Event evtObj)
    {
    	if(evtObj.target instanceof Scrollbar)
    	{
    		Color rc = new Color (Integer.parseInt(RedValue.getText()),0,0);
        	Color gc=new Color (0,Integer.parseInt(GreenValue.getText()),0);
        	Color bc = new Color (0,0,Integer.parseInt(BlueValue.getText()));
        	
    		c=new Color(red_slider.getValue(),green_slider.getValue(),blue_slider.getValue());
        	setBackground(c);
        	BlueValue.setText(""+blue_slider.getValue());
        	RedValue.setText(""+red_slider.getValue());
        	GreenValue.setText(""+green_slider.getValue());
        	repaint();
    		return true;
    	}
    	return super.handleEvent(evtObj);
    }
    
    public boolean mouseDrag(Event evtObj,int x)
    {	    	
    	red_slider.setValue(x);
    	green_slider.setValue(x);
    	blue_slider.setValue(x);
    	return true;
    }
    
    public void actionPerformed(ActionEvent e)
    {
    	Color c=new Color(Integer.parseInt(RedValue.getText()),Integer.parseInt(GreenValue.getText()),Integer.parseInt(BlueValue.getText()));
    	
    	setBackground(c);
    	red_slider.setValue(Integer.parseInt(RedValue.getText()));
    	green_slider.setValue(Integer.parseInt(GreenValue.getText()));
    	blue_slider.setValue(Integer.parseInt(BlueValue.getText()));
    	repaint();
    }
    
    public void paint(Graphics g)
    {
    	/*Color rc = new Color (Integer.parseInt(RedValue.getText()),0,0);
    	Color gc=new Color (0,Integer.parseInt(GreenValue.getText()),0);
    	Color bc = new Color (0,0,Integer.parseInt(BlueValue.getText()));*/
    }
}

class ColorChooserLayout implements LayoutManager {

    public ColorChooserLayout() {
    }

    public void addLayoutComponent(String name, Component comp) {
    }

    public void removeLayoutComponent(Component comp) {
    }

    public Dimension preferredLayoutSize(Container parent) {
        Dimension dim = new Dimension(0, 0);

        Insets insets = parent.getInsets();
        dim.width = 1200 + insets.left + insets.right;
        dim.height = 780 + insets.top + insets.bottom;

        return dim;
    }

    public Dimension minimumLayoutSize(Container parent) {
        Dimension dim = new Dimension(0, 0);
        return dim;
    }

    public void layoutContainer(Container parent) {
        Insets insets = parent.getInsets();

        Component c;
        c = parent.getComponent(0);
        if (c.isVisible()) {c.setBounds(insets.left+12,insets.top+120,304,32);}
        c = parent.getComponent(1);
        if (c.isVisible()) {c.setBounds(insets.left+884,insets.top+120,304,32);}
        c = parent.getComponent(2);
        if (c.isVisible()) {c.setBounds(insets.left+444,insets.top+120,304,32);}
        c = parent.getComponent(3);
        if (c.isVisible()) {c.setBounds(insets.left+510,insets.top+72,168,24);}
        c = parent.getComponent(4);
        if (c.isVisible()) {c.setBounds(insets.left+940,insets.top+72,168,24);}
        c = parent.getComponent(5);
        if (c.isVisible()) {c.setBounds(insets.left+85,insets.top+72,168,24);}
        c = parent.getComponent(6);
        if (c.isVisible()) {c.setBounds(insets.left+1010,insets.top+184,30,24);}
        c = parent.getComponent(7);
        if (c.isVisible()) {c.setBounds(insets.left+580,insets.top+184,30,24);}
        c = parent.getComponent(8);
        if (c.isVisible()) {c.setBounds(insets.left+140,insets.top+184,30,24);}
    }
}

Figured it out

Scrollbar name.setBackground(color);

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.