Home > Software engineering >  Java Swing button creates a dupe on the panel below it
Java Swing button creates a dupe on the panel below it

Time:05-21

We have this assignment where we make a GUI window which sets a panel's color based on RGB-Alpha values. I got it mostly working, but when I click the change color button, it duplicates (I'm not sure if its only visual bug) a clicked state of itself under it. It's not visible if alpha is 255 because it's fully opaque, but if it's transparent, the visual glitch thing is visible.

Screenshots of Window/Frame: https://imgur.com/a/Vf1Hb2B

This is my current code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
 
public class ColorCalc implements ActionListener {
    //Containers
    private JFrame f;
    private JPanel colorPanel;
    //Components
    private JLabel l1,l2,l3,l4;
    private JTextField redTf,greenTf,blueTf,alphaTf;
    private JButton bCompute,bClear;
 
    public ColorCalc()
    {
        //Containers
        f = new JFrame("My Color Calculator");
        f.setPreferredSize(new Dimension(400, 250));//Set Frame Size
 
        colorPanel = new JPanel();
        colorPanel.setPreferredSize(new Dimension(300, 200));//Set Panel Size
 
        //Components
        l1 = new JLabel("Red:");
        l2 = new JLabel("Green:");
        l3 = new JLabel("Blue:");
        l4 = new JLabel("Alpha:");
        
        redTf = new JTextField("0", 3);
        greenTf = new JTextField("0", 3);
        blueTf = new JTextField("0", 3);
        alphaTf = new JTextField("0", 3);
        
        bCompute = new JButton("Compute");
        bClear = new JButton("Clear");
        //Action Listeners
        bCompute.addActionListener(this);
        bClear.addActionListener(this);
    }
    public void startApp()
    {
        //Labels Panel
        JPanel lPanel = new JPanel();
        lPanel.setLayout(new GridLayout(5,1));
        lPanel.add(l1);
        lPanel.add(l2);
        lPanel.add(l3);
        lPanel.add(l4);
        lPanel.add(bCompute);
        //Text Fields Panel
        JPanel tfPanel = new JPanel();
        tfPanel.setLayout(new GridLayout(5,1));
        tfPanel.add(redTf);
        tfPanel.add(greenTf);
        tfPanel.add(blueTf);
        tfPanel.add(alphaTf);
        tfPanel.add(bClear);
        //Labels   TextFields Panel
        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new GridLayout(1,2));
        inputPanel.add(lPanel);
        inputPanel.add(tfPanel);
        //Add all panels to Frame
        f.setLayout(new GridLayout(2,1));
        f.add(inputPanel);
        f.add(colorPanel);
                
        f.setLocationRelativeTo(null);
        f.pack();
        f.setVisible(true);
 
    }
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == bCompute)
        {
            //Set Default Values
            int red = 255;
            int green = 255;
            int blue = 255;
            int alpha = 255;
            try 
            {
                //Get Values and Parse to Integer
                red = Integer.parseInt(redTf.getText());
                green = Integer.parseInt(greenTf.getText());
                blue = Integer.parseInt(blueTf.getText());
                alpha = Integer.parseInt(alphaTf.getText());
                
            } catch (NumberFormatException nfe)//If Parsing failed due to invalid types
            {
                red = 255; green = 255; blue = 255; alpha = 255; //Set values to white
                JOptionPane.showMessageDialog(null, "Invalid Inputs (RGB,Alpha values 0-255)");
            }
            
            if(red < 256 && green < 256 && blue < 256 && alpha < 256)//Max Value: 255
            {
              if(red > -1 && green > -1 && blue > -1 && alpha > -1)//Min Value: 0
                    colorPanel.setBackground(new Color(red, green, blue, alpha));
                else
                    JOptionPane.showMessageDialog(null, "Invalid Inputs (RGB,Alpha values 0-255)");    
            }                  
            else
                JOptionPane.showMessageDialog(null, "Invalid Inputs (RGB,Alpha values 0-255)");
        }
        else if(e.getSource() == bClear)
        {
            //Set all values to 0 and set panel to white
            redTf.setText("0");
            greenTf.setText("0");
            blueTf.setText("0");
            alphaTf.setText("0");
            colorPanel.setBackground(Color.WHITE);
        }
    }
    public static void main(String[] args) 
    {
        ColorCalc colCalc = new ColorCalc();
        colCalc.startApp();
    }
}

CodePudding user response:

Call the method f.repaint(); at the end of the actionPerformed() method

See more info here

  • Related