Jishan's Log 3: GUI Color Picker

 GUI Color Picker using JAVA SWING


Have you heard of RGB color? It's a combination of three colors: Red, Green, and Blue to

make a color. A single color's intensity goes from 0 to 255. The more it increases, the more

shade of that color is seen.


Let's take a brief look at it:


 

 

 

 

 

 

 

 Here, you can see the colors of combinations set by Red, Green, and Blue color.


I have developed a GUI application using Java's Swing framework to make such color

picker. 













 

The working here is simple. There are three sliders which will change the color of the JPanel

whenever scrolled, a button is there named "Get RGB Code" and upon clicking, the color code for all 3 colors between 0-255 are shown.

 

Here are the codes for it.

ColorPicker.java

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;

public class ColorPicker extends JFrame implements AdjustmentListener {

private JScrollBar red, green, blue;
private JPanel textField;
private JLabel label;
private JButton button;

public ColorPicker() {
// title
super("Color Picker");

// declare components
red = new JScrollBar(JScrollBar.HORIZONTAL,0,0,0,255);
green = new JScrollBar(JScrollBar.HORIZONTAL,0,0,0,255);
blue = new JScrollBar(JScrollBar.HORIZONTAL,0,0,0,255);
textField = new JPanel();
label = new JLabel("R: 0, G: 0, B: 0");
button = new JButton("Get RGB Code");

// component editing
textField.setBounds(50,100,300,50);
textField.setBackground(Color.BLACK);

red.setBounds(50,150,300,20);
green.setBounds(50,200,300,20);
blue.setBounds(50,250,300,20);

label.setBounds(50,300,150,50);
button.setBounds(50,350,150,20);

// layout
setLayout(null); //using own layout
add(textField);
add(red);
add(green);
add(blue);
add(label);
add(button);

// event
red.addAdjustmentListener(this);
green.addAdjustmentListener(this);
blue.addAdjustmentListener(this);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String colorCode = "R: " + Integer.toString(red.getValue()) + ", " + "G: " +Integer.toString(green.getValue()) + ", " + "B: " + Integer.toString(blue.getValue());
label.setText(colorCode);
}
});

// set visible
setVisible(true);
setSize(new Dimension(500,500));
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
textField.setBackground(new Color(red.getValue(), green.getValue(), blue.getValue()));
}

Since we are listening for a change in the scroll bar, we use the AdjustmentListener interface.

We set the background of the JPanel by creating a new color, setting the color up by getting the

values set up by our scroll bars and setting that color object as the background color of our 

JPanel. Lastly, we add an action listener (anonymous) to change the text of our label to the values

set by the scroll bars 

 

 Main.java

import javax.swing.*;

public class Main {

public static void main(String[] args) {
// write your code here
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new ColorPicker();
}
});
} 

 

 

 

 

 

Comments

Popular posts from this blog

Jishan's Log 14: Codeforces 69A solution

Jishan's Log 12: Codeforces 236A solution

Jishan's Log 13: Codeforces 96A solution