Monday, June 27, 2011

Mixing of Colors : A Sample Java Swing Application


// Mixing of Colors

import java.awt.Window;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.event.AdjustmentListener;
import java.awt.event.AdjustmentEvent;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JTextField;
import javax.swing.UIManager;

class Colors extends JFrame implements AdjustmentListener
{
JPanel panTop, panBottom;

JScrollBar redSB, greenSB, blueSB;

JTextField redTF, greenTF, blueTF;

public Colors(String str)
{
super(str);

setBounds(100,100,500,400);
centerWindow(this);
getContentPane().setLayout(new GridLayout(2,1));
setResizable(false);

panTop = new JPanel();
panBottom = new JPanel();

topPanel();

panTop.setBackground(Color.LIGHT_GRAY);
panBottom.setBackground(Color.BLACK);

getContentPane().add(panTop);
getContentPane().add(panBottom);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public void centerWindow(Window w)
{
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension d = tk.getScreenSize();
setLocation((d.width - w.getWidth())/2, (d.height - w.getHeight())/2);
}

public void topPanel()
{
panTop.setLayout(null);

addRedSB();
addGreenSB();
addBlueSB();
}

public void addRedSB()
{

redSB = new JScrollBar(JScrollBar.HORIZONTAL, 0, 5, 0, 260);
redSB.setBounds(50, 25, 300, 20);
redSB.addAdjustmentListener(this);
redSB.setBackground(Color.RED);

redTF = new JTextField("0");
redTF.setBounds(400, 25, 30, 20);
redTF.setEditable(false);
redTF.setHorizontalAlignment(JTextField.CENTER);
redTF.setBackground(Color.RED);
redTF.setForeground(Color.WHITE);

panTop.add(redSB);
panTop.add(redTF);
}

public void addGreenSB()
{

greenSB = new JScrollBar(JScrollBar.HORIZONTAL, 0, 5, 0, 260);
greenSB.setBounds(50, 75, 300, 20);
greenSB.addAdjustmentListener(this);
greenSB.setBackground(Color.GREEN);

greenTF = new JTextField("0");
greenTF.setBounds(400, 75, 30, 20);
greenTF.setEditable(false);
greenTF.setHorizontalAlignment(JTextField.CENTER);
greenTF.setBackground(Color.GREEN);
// greenTF.setForeground(Color.WHITE);

panTop.add(greenSB);
panTop.add(greenTF);
}

public void addBlueSB()
{

blueSB = new JScrollBar(JScrollBar.HORIZONTAL, 0, 5, 0, 260);
blueSB.setBounds(50, 125, 300, 20);
blueSB.addAdjustmentListener(this);
blueSB.setBackground(Color.BLUE);

blueTF = new JTextField("0");
blueTF.setBounds(400, 125, 30, 20);
blueTF.setEditable(false);
blueTF.setHorizontalAlignment(JTextField.CENTER);
blueTF.setBackground(Color.BLUE);
blueTF.setForeground(Color.WHITE);

panTop.add(blueSB);
panTop.add(blueTF);
}

public void adjustmentValueChanged(AdjustmentEvent ae)
{

redTF.setText(String.valueOf(redSB.getValue()));
greenTF.setText(String.valueOf(greenSB.getValue()));
blueTF.setText(String.valueOf(blueSB.getValue()));

panBottom.setBackground(new Color(redSB.getValue(), greenSB.getValue(),
                                                                                                        blueSB.getValue()));
}

public static void main(String[] args)
{

try
{

UIManager.setLookAndFeel(

UIManager.getCrossPlatformLookAndFeelClassName() // Java Look and Feel
// UIManager.getSystemLookAndFeelClassName() // current platform

// "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"
// "javax.swing.plaf.metal.MetalLookAndFeel"
// "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"
// "com.sun.java.swing.plaf.motif.MotifLookAndFeel"

);
}
catch (Exception e)
{ }

JFrame frame = new Colors("Mixing of Colors");
frame.setVisible(true);
}
}