Java Colors Generator

How to Create a Color Codes Generator In Java Netbeans

How to Create a Color Codes Generator In Java Netbeans




In this Java Tutorial we will see How To Make a simple color generator application with a graphical interface using Java Swing In Netbeans.
The application allows users to generate random colors and displays the color labels along with their corresponding HEX values. 

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.






Project Source Code:





package new_tutorials;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author 1BestCsharp
 */
public class Color_Generator_App extends JFrame{
    
    private JLabel[] colorLabels;
    private JTextField[] colorTextFields;
    private JButton generateColorsButton;
    
    public Color_Generator_App(){
        // Set the window title and properties
        setTitle("Color Generator");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(400,300);
        setLocationRelativeTo(null);
        // Initialize the user interface
        intializeGUI();
    }
    
    private void intializeGUI(){
        // Create arrays to store color labels, text fields, and the generate button
        colorLabels = new JLabel[3];
        colorTextFields = new JTextField[3];
        generateColorsButton = new JButton("Generate Colors");
        
        // Create the main panel to hold components
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(3,2,10,10));
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
        
        // Create and add color labels and text fields to the main panel
        for(int i = 0; i < 3; i++)
        {
            colorLabels[i] = new JLabel("Color " + (i+1));
            colorLabels[i].setOpaque(true);
            colorLabels[i].setBackground(Color.white);
            colorLabels[i].setBorder(BorderFactory.createLineBorder(Color.black,1));
            colorLabels[i].setHorizontalAlignment(JLabel.CENTER);
            
            colorTextFields[i] = new JTextField();
            colorTextFields[i].setEditable(false);
            colorTextFields[i].setBackground(Color.white);
            colorTextFields[i].setBorder(BorderFactory.createLineBorder(Color.black,1));
            colorTextFields[i].setHorizontalAlignment(JTextField.CENTER);
            
            mainPanel.add(colorLabels[i]);
            mainPanel.add(colorTextFields[i]);
            
        }
        
        // Add the main panel to the center of the JFrame
        add(mainPanel, BorderLayout.CENTER);
        
        // Define the action when the "Generate Colors" button is clicked
        generateColorsButton.addActionListener(((e) -> {
            // Generate random colors and update labels and text fields
            for(int i = 0; i < 3; i++)
            {
              Color randomColor = generateRandomColor();  
              String colorCode = "#" + Integer.toHexString(randomColor.getRGB()).substring(2).toUpperCase();
              colorLabels[i].setBackground(randomColor);
              colorTextFields[i].setText(colorCode);
            }
            
        }));
        
        // Configure the "Generate Colors" button
        generateColorsButton.setPreferredSize(new Dimension(150,40));
        generateColorsButton.setFont(new Font("Arial", Font.PLAIN, 14));
        generateColorsButton.setBackground(new Color(30,144,255));
        generateColorsButton.setForeground(Color.WHITE);
        generateColorsButton.setFocusPainted(false);
        generateColorsButton.setBorderPainted(false);
        
        // Create a panel for the button and add it to the bottom of the JFrame
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.add(generateColorsButton);
        add(buttonPanel,BorderLayout.SOUTH);
        
    }
    
    
    // Method to generate a random color
    private Color generateRandomColor()
    {
        int red = (int) (Math.random()*256);
        int green = (int) (Math.random()*256);
        int blue = (int) (Math.random()*256);
        
        return new Color(red,green,blue);
    }
    

    public static void main(String[] args) {
        new Color_Generator_App().setVisible(true);
    }
    
}


The Final Result:

Java Color Generator

Generate Random Colors Using Java

Random colors generator in java

Color Codes Generator In Java

Java Color Generator





Share this

Related Posts

Previous
Next Post »