0

I just finished the java swing course and I am trying to make a login page with java and java swing. I designed a login page with Figma but I couldn't convert it to the java swing code so I decided to code something similar to the design in Figma with java Swing. I set 'LoginNameText' 's color to RGB(30,30,30) with 59% opacity in Figma but I can not do it in java Swing. I asked ChatGPT for it and it gave me this code but it did not work either. How can I solve it ?

        int alpha=(int) 0.59*255;
        
        JLabel LoginNameText=new JLabel();
        LoginNameText.setBounds(0,170,500,100);
        LoginNameText.setText("Name");
        LoginNameText.setFont(new Font("Arials",Font.BOLD,30));
        LoginNameText.setHorizontalAlignment(JLabel.CENTER);
        LoginNameText.setForeground(new Color(30,30,30,alpha));
        
        
  • 1
    `int alpha = (int)(0.59 * 255)` or `int alpha = 255 * 59 / 100` (evaluation order **is** important) – user16320675 Aug 16 '23 at 13:04
  • But also note that Swing does not handle semi-transparency very well or with ease. For example, please check out [MadProgrammer's answer here](https://stackoverflow.com/a/20876300/) – Hovercraft Full Of Eels Aug 16 '23 at 13:47
  • Also, please note that ChatGPT is a very convincing bullshit generator and its output should not be trusted. For example, the code above is in error (as noted both above and below) and recommends use of known poor Swing practices, such as calling `setBounds(...)` on Swing components. – Hovercraft Full Of Eels Aug 16 '23 at 13:50

1 Answers1

1

Change alpha from an int to a double then add a int cast when creating the color else the value of alpha is going to be 0. Try this ->

double alpha= 0.59*255;
        
JLabel LoginNameText=new JLabel();
LoginNameText.setBounds(0,170,500,100);
LoginNameText.setText("Name");
LoginNameText.setFont(new Font("Arials",Font.BOLD,30));
LoginNameText.setForeground(new Color(30,30,30,(int) alpha));