2

I have a JPanel to allow the user to set the color of an object with these components:

  • TextField (R)
  • TextField (G)
  • TextField (B)
  • Slider (Opacity 1-100)

  • Button (preview of color using values from above elements)

What I'm asking is why the button get the color correctly but not the opacity.
Here's my code:

public Color getColor() {
    if (tfRed.getText().equals("") || tfGreen.getText().equals("") || tfBlue.getText().equals("")) {
            return new Color(0, 0, 0, 0);
    } else {
        if (tfRed.getText().matches("\\d+") && tfGreen.getText().matches("\\d+") && tfBlue.getText().matches("\\d+")
                && Integer.parseInt(tfRed.getText()) <= 255 && Integer.parseInt(tfGreen.getText()) <= 255 && Integer.parseInt(tfBlue.getText()) <= 255
                && Integer.parseInt(tfRed.getText()) >= 0 && Integer.parseInt(tfGreen.getText()) >= 0 && Integer.parseInt(tfBlue.getText()) >= 0) {
             return new Color(
                    Float.parseFloat(tfRed.getText()) / 255,
                    Float.parseFloat(tfGreen.getText()) / 255,
                    Float.parseFloat(tfBlue.getText()) / 255, 
                    Float.parseFloat(sOpacity.getValue() + "") / 100
                    );
        } else {
            JOptionPane.showMessageDialog(this, "Invalid rgb value");
            tfRed.setText("0");
            tfGreen.setText("0");
            tfBlue.setText("0");
            return new Color(0, 0, 0, 0);
        }
    }
}

I set the color of the button in a single event for all textfield and another event for the slider:

// on keyup
private void button_color(java.awt.event.KeyEvent evt) {
    bColor.setBackground(getColor());
}

// on mousedragged and mouseclicked
private void slider_value(java.awt.event.MouseEvent evt) {
    lOpacity.setText(sOpacity.getValue() + "");
    bColor.setBackground(getColor());
}

I debugged it and I saw that the color taken from getColor() returns only rgb values w/o opacity, but when I use getColor() with other custom components it works (rgb + opacity). Thanks for help

EDIT

Found solution:

// on mousedragged and mouseclicked
private void slider_value(java.awt.event.MouseEvent evt) {
    lOpacity.setText(sOpacity.getValue() + "");
    bColor.setBackground(getColor());  
    bColor.getParent().repaint();  <------
}
mre
  • 43,520
  • 33
  • 120
  • 170
mastaH
  • 1,234
  • 3
  • 15
  • 30
  • 1
    I'm glad you solved your own problem. But this question is very convoluted and is therefore, difficult to answer. In the future, tailor your questions such that other people will be able to understand the problem at hand. – mre Jul 14 '11 at 12:59

1 Answers1

1

I do not think setting button's background color is useful, JButton's background color set by look and feel, it's hard to change the button's color, use JLabel instead

sam sha
  • 842
  • 9
  • 18
  • yes or not too, http://stackoverflow.com/questions/6274890/java-jbutton-with-custom-shape-fill-with-metal-look-and-feel-gradient/6275382#6275382 – mKorbel Jul 15 '11 at 08:38