The Use of The Color Chooser (JColorChooser) and do - while Statement in Java Using Eclipse IDE

in #utopian-io7 years ago (edited)

What Will I Learn?

In this tutorial;

  • You will learn how to use the JColorChooser class.
  • You will learn how to use the chosen color in color choser.
  • You will learn the use of do - while statement.

Requirements

  • Java SE Development Kit (Not necessarily the last version) is required.
  • Eclipse IDE or any similar Interated Development Enviroment (IDE) that is designed for Java programming language is required.

Difficulty

  • Intermediate

Tutorial Contents

The purpose of this tutorial is to show the use of color choosers in Java. The demo program for the color chooser contains a single Java file (Color_Choosers_Demo.java). The following figure contains the code of the Java file.
code.JPG
In this demo program, it is expected to see a color chooser in the screen, when the program run. Then, the chosen color in this color choser determines the background color of the panel of the frame that opens after choosing a color.
To achieve these in the demo program, firstly we imported the neccessary GUI packages, defined our Java file and constructed our frame as usual with the following code.

import javax.swing.*;
import java.awt.*;
public class Color_Choosers_Demo {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Color Choosers Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

Then, we need a panel to add this frame. The chosen color will determine its background color later on. With the following code, a panel with white background color and the size of 200 rows and 250 columns is created.

        JPanel colorp = new JPanel();
        colorp.setBackground(Color.WHITE);
        colorp.setPreferredSize(new Dimension(250, 200));

With the folowing piece of code, the job is finished with constructing frame.

        frame.getContentPane().add(colorp);
        frame.pack();
        frame.setVisible(true);

Now, we need to create a color object, which is white initially, to use the chosen color. In addition to this, an integer object is neccesary to check whether the user wants to repeat the program.

        Color chosen_color = Color.WHITE;
        int repeat;
The do - while Statement

The do statement is used with while statement. The compiler starts to execute the code in the do statement. Then, the compiler checks the while statement. If the condition in while statement is true, the compile again goes to the code in the do statement. Unless the condition in the while returns false, the code in do statement is executed again and again. For instance, the following code opens a confirm dialog and ask the user whether continue to ask or not. If the user selects yes, the statement in while returns true and again confirm dialog code is executed. This process continiues until user selects no.

        do {
            repeat = JOptionPane.showConfirmDialog(null, "New Color?");
        }
        while (repeat == JOptionPane.YES_OPTION);
JColorChooser

The JColorChooser class represents a color chooser. It allows us to display a special dialog box that is prepared as color chooser. It can be directly used with the showDialog method. It takes the title of the dialog box, the frame and the initial color as parameters. If we want to see the color chooser dialog box again and again, we need to add its code in the do statement like in the following code;

        do {
            chosen_color=JColorChooser.showDialog(frame, "Choose a Color", chosen_color);
            colorp.setBackground(chosen_color);
            repeat = JOptionPane.showConfirmDialog(null, "New Color?");
        }
        while (repeat == JOptionPane.YES_OPTION);

The color chooser changes the initial color with the choosen color. Thus we can change the background color of the panel.

Results

When you run the program, the frame (left upper corner) and the color chooser opens.
r1.JPG
You can choose any color you want. If you need to choose more complicated color you can choose RGB or other color representation techniques from the tabbed pane. We choose a green color like the following figure;

r2.JPG

When the OK button pressed the background color of the frame changes and the program ask user to whether change color again or not.

r3.JPG
r4.JPG

When you press the Yes button, the color chooser opens again and you change the color again.

Ekran Alıntısı.JPG

Github

You can get this program from here.
git.JPG

Code of The Program
import javax.swing.*;
import java.awt.*;
public class Color_Choosers_Demo {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Color Choosers Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel colorp = new JPanel();
        colorp.setBackground(Color.WHITE);
        colorp.setPreferredSize(new Dimension(250, 200));
        frame.getContentPane().add(colorp);
        frame.pack();
        frame.setVisible(true);
        Color chosen_color = Color.WHITE;
        int repeat;
        do {
            chosen_color=JColorChooser.showDialog(frame, "Choose a Color", chosen_color);
            colorp.setBackground(chosen_color);
            repeat = JOptionPane.showConfirmDialog(null, "New Color?");
        }
        while (repeat == JOptionPane.YES_OPTION);
    }
}

Curriculum

You can find my other java related tutorials in below links.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @kizilelma, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

Hey @aromatheraphy I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x