How to use the JColorChooser | Progressive Java



Do ou know those color options, transparency, hue, brightness, etc., that you see on image programs out there?

Yes you can put in your Java application too.
But don't fear, is quite simple. Everything is ready, and the name of it is JColorChooser.



Graphics objects contain methods to design, manipulate fonts, colors, graphics and other things.
It is an abstract class, ie can not be instantiated, that because of portability, since each device draws its own way.

To use the Graphics, added:
import java.awt.Graphics;

For inserting graphics components as lines, rectangles, etc., use an instance of Graphics:


public void paintComponent( Graphics g)
{
    //drawing methods here
}



The colors are formed by three levels of color, red (R, red), green (G, green) and blue (B, blue).
The famous RGB.

For the colors we use, we add:
import java.awt.Color;

public Color (int r, int g, int b): integer ranging from 0 to 255
public Color (float r, float g, float b): float from 0.0 to 1.0


- More methods

public int getRed() ;
public int getGreen();
public int getBlue();
public Color getColor();
public void setColor( Color c);



We will show an application that shows the JColorChooser, a GUI component that displays all the standard colors in JColorChosser dialog.
When we choose a color, the color changes JPanel.


----------------ColorsFrame.java


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JColorChooser;
import javax.swing.JPanel;

public class ShowColors2JFrame extends JFrame
{
    private JButton changeColorJButton;
    private Color color = Color.LIGHT_GRAY;
    private JPanel colorJPanel;
   
    public ShowColors2JFrame()
    {
        super( "Using the JColorChooser" );
       
        colorJPanel = new JPanel();
        colorJPanel.setBackground( color );
       
        changeColorJButton = new JButton( "Choose the color" );
        changeColorJButton.addActionListener(
                new ActionListener()
                {
                    public void actionPerformed( ActionEvent event )
                    {
                        color = JColorChooser.showDialog(
                                ShowColors2JFrame.this, "Choose the color", color );
                       
                        if( color == null )
                            color = Color.LIGHT_GRAY;
                        colorJPanel.setBackground( color );
                    }
                }
                );
       
        add( colorJPanel, BorderLayout.CENTER );
        add( changeColorJButton, BorderLayout.SOUTH );
       
        setSize( 400, 130 );
        setVisible( true );
    }
}

----------------Colors.java
import javax.swing.JFrame;

public class Colors
{
    public static void main(String[] args)
    {
        ColorsFrame application = new ColorsFrame();
        application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }
}

The result:
How to use the JColorChooser | Progressive Java
"Escolher a cor" means "Choose the color"

How to use the JColorChooser | Progressive Java

How to use the JColorChooser | Progressive Java

How to use the JColorChooser | Progressive Java

How to use the JColorChooser | Progressive Java



How to use the JColorChooser | Progressive Java

How to use the JColorChooser | Progressive Java


No comments: