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





Java Create Line Chart

How to Create a Custom Line Chart In Java Netbeans

How to Create a Custom Line Chart In Java Netbeans


In this Java Tutorial we will see How To Create a Custom Line Chart from scratch with labeled axes, grid lines, and data points using graphics class and jpanel in java netbeans.

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.





Project Source Code:


package linechart;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import javax.swing.JFrame;
import javax.swing.JPanel;


/**
 *
 * @author 1BestCsharp
 */
public class LineChart extends JPanel{

    // Constants for padding and labels
    private final int padding = 30;
    private final int labelPadding = 40;
    private final int axisLabelPadding = 20;
    private final int maxVerticalLabels = 5;
    private final int maxHorizontalLabels = 5;
    
    // Arrays for vertical and horizontal labels
    private String[] verticalLabels = {"0","25","50","75","100"};
    private String[] horizontalLabels = {"Label 1","Label 2","Label 3","Label 4","Label 5"};
    
    @Override
    protected void paintComponent(Graphics g){
        
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        // Calculate coordinates and dimensions
        int width = getWidth();
        int height = getHeight();
        int xStart = padding + labelPadding + axisLabelPadding;
        int yStart = height - padding - labelPadding;
        int xEnd = width - padding;
        int yEnd = padding + labelPadding;
        
        // Calculate step size for vertical and horizontal labels
        int yStep = (yStart - yEnd) / (maxVerticalLabels - 1);
        int xStep = (xEnd - xStart) / (maxHorizontalLabels - 1);
        
        // Draw vertical labels
        for(int i = 0; i < maxVerticalLabels; i++){
            int y = yStart - i * yStep;
            drawVerticalLabel(g2d, verticalLabels[i], padding / 2, y);
        }
        
        // Draw horizontal labels
        for(int i = 0; i < maxHorizontalLabels; i++){
            int x = xStart + i * xStep;
            //drawHorizontal(g2d, horizontalLabels[i], x, height - (padding / 2));
            drawHorizontalLabel(g2d, horizontalLabels[i], x, height - (padding / 2));
        }
        
        // Data for the chart
        int[] data = {40, 100, 75, 20, 55};
        int maxValue = 100;
        
        // Find the maximum value in the data
        for(int value : data){
            if(value > maxValue)
            {
                maxValue = value;
            }
        }
        
        // Draw chart background
        g2d.setColor(Color.white);
        g2d.fillRect(xStart, yEnd, xEnd - xStart, yStart - yEnd);
        
        // Draw chart border
        g2d.setColor(Color.black);
        g2d.drawRect(xStart, yEnd, xEnd - xStart, yStart - yEnd);
        
        // Draw horizontal grid lines
        int yGridCount = 5;
        for(int i = 0; i < yGridCount; i++){
            int y = yStart - i * yStep;
            g2d.setColor(Color.red);
            g2d.drawLine(xStart, y, xEnd, y);
        }
        
        // Draw vertical grid lines
        int xGridCount = data.length;
        for(int i = 0; i < xGridCount; i++){
            int x = xStart + i * xStep;
            g2d.setColor(Color.lightGray);
            g2d.drawLine(x, yEnd, x, yStart);
        }
        
        
        // Plot data points and connect with lines
        for(int i = 0; i < data.length; i++){
            g2d.setColor(Color.orange);
            int x = xStart + i * xStep;
            int y = yStart - data[i] * (yStart - yEnd) / maxValue;
            g2d.fillOval(x-5, y-5, 10, 10);
            
            // Connect data points with lines
            if( i > 0){
                int prevX = xStart + (i - 1) * xStep;
                int prevY = yStart - data[i - 1] * (yStart - yEnd) / maxValue;
                g2d.setStroke(new BasicStroke(2));
                g2d.setColor(Color.gray);
                g2d.drawLine(prevX, prevY, x, y);
            }
        }
        
        // Draw Chart Title
        g2d.setFont(new Font("Arial",Font.BOLD, 20));
        g2d.setColor(Color.red);
        String chartTitle = "Chart Title";
        g2d.drawString(chartTitle, (width - g2d.getFontMetrics().stringWidth(chartTitle)) / 2, padding);        
    }
    
    // Method to draw a vertical label with rotation
    private void drawVerticalLabel(Graphics2D g2d, String label, int x, int y){
        AffineTransform atf = new AffineTransform();
        atf.rotate(-Math.PI / 2);
        Font rotatedFont = g2d.getFont().deriveFont(atf);
        g2d.setFont(rotatedFont);
        g2d.setColor(Color.yellow);
        g2d.drawString(label, x + 30, y);
        g2d.setFont(g2d.getFont().deriveFont(atf));
    }
    
    // Method to draw a horizontal label
    private void drawHorizontalLabel(Graphics2D g2d, String label, int x, int y){
        g2d.setColor(new Color(248,239,186));
        g2d.drawString(label, x - g2d.getFontMetrics().stringWidth(label) / 2, y);
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        JFrame frame = new JFrame();
        frame.setTitle("Custom Line Chart");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        LineChart chartPanel = new LineChart();
        chartPanel.setBackground(Color.darkGray);
        frame.add(chartPanel);
        frame.setVisible(true);
        
    }

}




The Final Result:

Java Line Chart

if you want the source code click on the download button below








Java Gradient Panels

How to Create JPanels With Gradient Background Color In Java Netbeans



In this Java Tutorial we will see How To Create Gradient JPanels in Java NetBeans.

What We Are Gonna Use In This Project:

- Java Programming Language.
- NetBeans Editor.





Project Source Code:

We will create a simple form with two panels with gradient background color.


package GradientPanels;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author 1BestCsharp
 */
public class GradientPanels_V1 extends JPanel{
    
    private Color startColor;
    private Color endColor;
    
    public GradientPanels_V1(Color _startColor, Color _endColor){
        this.startColor = _startColor;
        this.endColor = _endColor;
    }

    // Override the paintComponent method to customize the rendering of the panel
    @Override
    protected void paintComponent(Graphics g){
        
        super.paintComponent(g);
        
        // Create a Graphics2D object for advanced rendering operations
        Graphics2D g2d = (Graphics2D) g.create();
        // Create a gradient paint object from the startColor to the endColor
        GradientPaint gradientPaint = new GradientPaint(
                        // Starting point of the gradient
                        new Point2D.Float(0,0), startColor,
                        // Ending point of the gradient
                        new Point2D.Float(0,getHeight()), endColor
        );
        
        // Set the paint of the Graphics2D object to the gradient paint
        g2d.setPaint(gradientPaint);
        
        // Fill a rectangle with the gradient, covering the entire panel
        g2d.fillRect(0, 0, getWidth(), getHeight());
        
        
    }
    
    public static void main(String[] args) {
        
        GradientPanels_V1 panel1 = new GradientPanels_V1(Color.RED, Color.YELLOW);
        GradientPanels_V1 panel2 = new GradientPanels_V1(Color.BLUE, Color.CYAN);
        
        panel1.setPreferredSize(new Dimension(400, 400));
        panel2.setPreferredSize(new Dimension(400, 400));
        
        JFrame frame = new JFrame("Gradient Panels V1");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.setLayout(new FlowLayout());
        
        frame.add(panel1);
        frame.add(panel2);
        
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
    }
    
}








Project Source Code:


We Will create and display custom gradient panels and displays a title and additional data text. The title is rendered in a bold font, while the data text is rendered in a plain font.


package GradientPanels;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author 1BestCsharp
 */
public class GradientPanels_V2 extends JPanel{

    private Color startColor;  // Starting color for the gradient
    private Color endColor;    // Ending color for the gradient
    private String panelTitle; // Title to be displayed on the panel
    private String panelData;  // Data to be displayed on the panel
    
    // Fonts for title and data
    private static final Font titleFont = new Font("Helvatica", Font.BOLD, 22);
    private static final Font dataFont = new Font("Arial", Font.PLAIN, 16);
    
    public GradientPanels_V2(Color startColor, Color endColor, String panelTitle, String panelData)
    {
        this.startColor = startColor;
        this.endColor = endColor;
        this.panelTitle = panelTitle;
        this.panelData = panelData;
    }
    
    // Override the paintComponent method to customize the rendering of the panel
    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        // Create a Graphics2D object for advanced rendering
        Graphics2D g2d = (Graphics2D) g.create();
        GradientPaint gradientPaint = new GradientPaint(
                            new Point2D.Float(0,0), startColor, 
                            new Point2D.Float(0,getHeight()), endColor
        );
     
        g2d.setPaint(gradientPaint);
        // Fill the panel with the gradient paint
        g2d.fillRect(0, 0, getWidth(), getHeight());
        
        g2d.setColor(Color.WHITE);
        g2d.setFont(titleFont);
        
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        // Draw the title at specified coordinates
        g2d.drawString(panelTitle, 20, 30);
        
        g2d.setColor(Color.lightGray);
        g2d.setFont(dataFont);
        // Draw the data at specified coordinates
        g2d.drawString(panelData, 20, 60);
        
        g2d.dispose();
        
    }
    
    
    public static void main(String[] args) {
        
        JFrame frame = new JFrame("Gradient Panel V2");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(2, 2));
        
        GradientPanels_V2 panel1 = new GradientPanels_V2(Color.decode("#1B5E20"), Color.BLACK, "Sales Data", "$4,753.920");
        //GradientPanels_V2 panel2 = new GradientPanels_V2(Color.RED, Color.yellow, "User State", "Active Users: 759");
        //decode("#1976D2")
        GradientPanels_V2 panel2 = new GradientPanels_V2(Color.decode("#0D47A1"), Color.CYAN, "User State", "Active Users: 759");
        GradientPanels_V2 panel3 = new GradientPanels_V2(Color.decode("#E65100"), Color.CYAN, "Product Inventory", "Available Items: 387");
        GradientPanels_V2 panel4 = new GradientPanels_V2(Color.decode("#4A148C"), Color.WHITE, "Sales Data", "Profit: $1,276");
        
        frame.add(panel1);
        frame.add(panel2);
        frame.add(panel3);
        frame.add(panel4);
        
        frame.setSize(800,600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
    }
    
    
}








Project Source Code:


We will create 4 panels with gradient background color to display labels and values.


package GradientPanels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author 1BestCsharp
 */
public class GradientPanels_V3 extends JPanel{

    private Color startColor;
    private Color endColor;
    
    public GradientPanels_V3(Color startColor, Color endColor){
        
        this.startColor = startColor;
        this.endColor = endColor;
        setLayout(new BorderLayout());
        add(createContentPanel(), BorderLayout.CENTER);
        
    }
    
    // Make a Method to create the content panel with labels and values
    private JPanel createContentPanel(){
        JPanel contentPanel = new JPanel(new GridBagLayout());
        contentPanel.setOpaque(false);
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = GridBagConstraints.RELATIVE;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(10, 0, 5, 0);
        
        Font labelFont = new Font("Arial",Font.BOLD,17);
        Font valueFont = new Font("Arial",Font.PLAIN,17);
        
        // Labels and values to be displayed
        String[] labels = {"Total Revenue: ","Expenses: ","Profit: ","Customers: "};
        String[] values = {"$10,500","$5,200","$5,300","1,200"};
        
        // Loop to add labels and values to the content panel
        for(int i = 0; i < labels.length; i++){
            JLabel label = new JLabel(labels[i]);
            label.setFont(labelFont);
            label.setForeground(Color.WHITE);
            
            JLabel value = new JLabel(values[i]);
            value.setFont(valueFont);
            value.setForeground(Color.BLACK);
            
            gbc.gridx = 0;
            contentPanel.add(label,gbc);
            
            gbc.gridx = 1;
            contentPanel.add(value,gbc);
        }
        
        return contentPanel;
 
    }
    
    
    // Override method to paint the gradient background
    @Override
    protected void paintComponent(Graphics g){
        
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        
        GradientPaint gradientPaint = new GradientPaint(
                            new Point2D.Float(0,0), startColor,
                            new Point2D.Float(0,getHeight()),  endColor
        );
        
        g2d.setPaint(gradientPaint);
        g2d.fillRect(0, 0, getWidth(), getHeight());
        g2d.dispose();
    }
    
    
    
    public static void main(String[] args) {
        
        JFrame frame = new JFrame("Gradient Panel V3");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(2,2));
        
        /*
        GradientPanels_V3 panel1 = new GradientPanels_V3(new Color(242,149,76), new Color(232,94,31));
        GradientPanels_V3 panel2 = new GradientPanels_V3(new Color(134,211,12), new Color(35,154,59));
        GradientPanels_V3 panel3 = new GradientPanels_V3(new Color(245,94,81), new Color(226,47,42));
        GradientPanels_V3 panel4 = new GradientPanels_V3(new Color(194,149,255), new Color(232,50,204));
        */
        
        GradientPanels_V3 panel1 = new GradientPanels_V3(Color.RED, Color.YELLOW);
        GradientPanels_V3 panel2 = new GradientPanels_V3(new Color(134,211,12), new Color(35,154,59));
        GradientPanels_V3 panel3 = new GradientPanels_V3(Color.BLUE, Color.CYAN);
        GradientPanels_V3 panel4 = new GradientPanels_V3(new Color(194,149,255), new Color(232,50,204));
        
        
        frame.add(panel1);
        frame.add(panel2);
        frame.add(panel3);
        frame.add(panel4);
        
        frame.setSize(800,500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
    }
    
    
}







Project Source Code:

We will creates rounded rectangle with a gradient background using panels and a semi-transparent black overlay to create a shadow effect.


package GradientPanels;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 *
 * @author 1BestCsharp
 */
public class GradientPanels_V4 extends JPanel{

    private Color startColor;
    private Color endColor;
    
    
    public GradientPanels_V4(Color startColor, Color endColor){
        this.startColor = startColor;
        this.endColor = endColor;
        setOpaque(false);
    }
    
    @Override
    protected void paintComponent(Graphics g)
    {
         super.paintComponent(g);
         
         Graphics2D g2d = (Graphics2D) g.create();
         
         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
         
         // Create a gradient paint with the specified start and end colors
         GradientPaint gradientPaint = new GradientPaint(
                 new Point2D.Float(0, 0), startColor, 
                 new Point2D.Float(0, getHeight()), endColor
         );
         
         g2d.setPaint(gradientPaint);
         g2d.fillRoundRect(0, 0, getWidth(), getHeight(), 50, 50);
         
         // Add a semi-transparent black overlay for a shadow effect
         g2d.setColor(new Color(0,0,0,100));
         g2d.fillRoundRect(5, 5, getWidth() - 10, getHeight() - 10, 50, 50);
         g2d.dispose();
         
    }
    
    
    public static void main(String[] args) {
        
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(2, 2, 10, 10));

        /*
        GradientPanels_V4 panel1 = new GradientPanels_V4(new Color(0,122,204), new Color(0,153,255));
        GradientPanels_V4 panel2 = new GradientPanels_V4(new Color(60,179,113), new Color(102,255,153));
        GradientPanels_V4 panel3 = new GradientPanels_V4(new Color(204,0,0), new Color(255,51,51));
        GradientPanels_V4 panel4 = new GradientPanels_V4(new Color(255,102,204), new Color(255,153,255));
        */
        
        GradientPanels_V4 panel1 = new GradientPanels_V4(Color.BLACK, Color.LIGHT_GRAY);
        GradientPanels_V4 panel2 = new GradientPanels_V4(Color.RED, Color.YELLOW);
        GradientPanels_V4 panel3 = new GradientPanels_V4(Color.BLUE, Color.CYAN);
        GradientPanels_V4 panel4 = new GradientPanels_V4(Color.MAGENTA, Color.GREEN);
        
        
        panel1.setLayout(new BorderLayout());
        panel2.setLayout(new BorderLayout());
        panel3.setLayout(new BorderLayout());
        panel4.setLayout(new BorderLayout());
        
        
        JLabel label1 = new JLabel("Panel 1 Data", SwingConstants.CENTER);
        JLabel label2 = new JLabel("Panel 2 Data", SwingConstants.CENTER);
        JLabel label3 = new JLabel("Panel 3 Data", SwingConstants.CENTER);
        JLabel label4 = new JLabel("Panel 4 Data", SwingConstants.CENTER);
        
        label1.setForeground(Color.WHITE);
        label2.setForeground(Color.WHITE);
        label3.setForeground(Color.WHITE);
        label4.setForeground(Color.WHITE);
        
        panel1.add(label1, BorderLayout.CENTER);
        panel2.add(label2, BorderLayout.CENTER);
        panel3.add(label3, BorderLayout.CENTER);
        panel4.add(label4, BorderLayout.CENTER);
        
        frame.add(panel1);
        frame.add(panel2);
        frame.add(panel3);
        frame.add(panel4);
        
        frame.setSize(800,600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
    }
 
}






Project Source Code:


This Java code defines a custom JPanel class called GradientPanels_V5, which is used to create panels with gradient backgrounds, titles, descriptions, and icons.


package GradientPanels;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.geom.Point2D;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author 1BestCsharp
 */
public class GradientPanels_V5 extends JPanel{

    private Color startColor;
    private Color endColor;
    private String title;
    private String description;
    private ImageIcon icon;
    
    
    public GradientPanels_V5(Color startColor, Color endColor, String title, String description, ImageIcon icon){
        
        this.startColor = startColor;
        this.endColor = endColor;
        this.title = title;
        this.description = description;
        this.icon = icon;
        
        setOpaque(false);
        
    }
    
    // Method to paint the component, filling it with a gradient color
    @Override
    protected void paintComponent(Graphics g){
        
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();
        
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        GradientPaint gradientPaint = new GradientPaint(
                      new Point2D.Float(0,0), startColor, 
                      new Point2D.Float(0,getHeight()), endColor
        );
        
        g2d.setPaint(gradientPaint);
        g2d.fillRect(0, 0, getWidth(), getHeight());
        g2d.dispose();
    }
    
    // Method to paint the border of the component 
    @Override
    protected void paintBorder(Graphics g){
        g.setColor(Color.YELLOW);
        g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
    }
    
    // Method to paint the children components, including title and description
    @Override
    protected void paintChildren(Graphics g){
        
        super.paintChildren(g);
        Graphics2D g2d = (Graphics2D) g.create();
        
        //center the icon
        int iconX = getWidth() / 2 - icon.getIconWidth() / 2;
        int iconY = getHeight()/ 2 - icon.getIconHeight() / 2;
        
        icon.paintIcon(this, g2d, iconX, iconY);
        
        g2d.setFont(new Font("Arial",Font.BOLD,16));
        g2d.setColor(Color.WHITE);
        g2d.drawString(title, 20, getHeight() - 30);
        
        g2d.setFont(new Font("Arial",Font.PLAIN,12));
        g2d.setColor(Color.YELLOW);
        g2d.drawString(description, 20, getHeight() - 10);
        
        g2d.dispose();
        
    }
    
    public static void main(String[] args) {
        
        JFrame frame = new JFrame("Gradient Panel V5");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        frame.setLayout(new GridLayout(2,2));
        
        /*
        GradientPanels_V5 panel1 = new GradientPanels_V5(new Color(54,215,183), new Color(48,187,167), "Sales", "Monthly Sales Report");
        GradientPanels_V5 panel2 = new GradientPanels_V5(new Color(241,181,73), new Color(230,157,57), "Expenses", "Monthly Expenses Report");
        GradientPanels_V5 panel3 = new GradientPanels_V5(new Color(109,169,218), new Color(91,143,188), "Projects", "Active Project Status");
        GradientPanels_V5 panel4 = new GradientPanels_V5(new Color(197,124,182), new Color(178,100,160), "Users", "User Statics");
        */
        
        // use better images
        GradientPanels_V5 panel1 = new GradientPanels_V5( Color.RED,  Color.BLACK, "Sales", "Monthly Sales Report", new ImageIcon(GradientPanels_V5.class.getResource("item100.png")));
        GradientPanels_V5 panel2 = new GradientPanels_V5( Color.BLUE,  Color.BLACK, "Expenses", "Monthly Expenses Report", new ImageIcon(GradientPanels_V5.class.getResource("stock100.png")));
        GradientPanels_V5 panel3 = new GradientPanels_V5( Color.WHITE,  Color.BLACK, "Projects", "Active Project Status", new ImageIcon(GradientPanels_V5.class.getResource("price100.png")));
        GradientPanels_V5 panel4 = new GradientPanels_V5( Color.GREEN,  Color.BLACK, "Users", "User Statics", new ImageIcon(GradientPanels_V5.class.getResource("manager.png")));
        
        
        
        frame.add(panel1);
        frame.add(panel2);
        frame.add(panel3);
        frame.add(panel4);
        
        frame.setSize(600,400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
        
        
    }
    
}






if you want the source code click on the download button below