Question

I am writing a program with a undecorated frame and the text is not displaying properly on it. I am pretty sure this line is causing the problem but do not know why:

    setBackground(new Color(0, 0, 0, 0));

Here is what the text looks like and what it should look like

bad text

good text

Here is my code: it is the short version of my regular file so it might seem confusing. Additionally I have only been working with java for a week and a half.

    import java.awt.Dimension;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import static java.awt.GraphicsDevice.WindowTranslucency.*;



    public class MyTunesMain {

        public static void main(String[] args) {

            //MyTunes myTunes = new MyTunes();
            ShortTest myTunes = new ShortTest();

        }
    }

    ///////////////////////////////////////////

    import java.awt.*;
    import javax.swing.*;


    public class ShortTest extends JFrame {

        // id
        private static final long serialVersionUID = 1L;

        // basic inits
        private int width = 1000;
        private int height = 600;
        SoundThread music;
        Font searchFont = new Font("Calibri", Font.PLAIN, 18);
        Container content = getContentPane();

        // JFrame stuff
        JFrame jf = new JFrame();
        JPanel topPanel = new JPanel();
        JPanel mainPanel = new JPanel();
        private JLabel songPlayed;



        // //////////////////////////////////////////////
        public ShortTest() {

            // initialize window and technical properties
            super("ShortTest");

            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            //dimension
            int extendBy=30;
            setMaximumSize(new Dimension(width + extendBy, height + extendBy));
            setMinimumSize(new Dimension(width + extendBy, height + extendBy));
            setPreferredSize(new Dimension(width + extendBy, height extendBy));
            setUndecorated(true);
            setLocationRelativeTo(null);

            //setBackground(new Color(0, 0, 0));
            setBackground(new Color(0, 0, 0, 0));      // all hell breaks lose
            getContentPane().setBackground(Color.BLACK);
                    setLayout(null);


            // initialize jpanel for objects
            mainPanel.setBounds(6, 6, width, height);
            mainPanel.setLayout(null);
            mainPanel.setOpaque(true);
            mainPanel.setBackground(Color.gray);
            add (mainPanel);


            mainPanel.add(topPanel);
            topPanel.setBounds(0, 0, 1000, 50);
            topPanel.setLayout(null);

            // setup song label
            songPlayed = new JLabel("Little Wing");
            songPlayed.setFont(searchFont);
            FontMetrics fm = songPlayed.getFontMetrics(songPlayed.getFont());
            String text = songPlayed.getText();
            int textWidth = fm.stringWidth(text);
            songPlayed.setBounds(500 - textWidth / 2, 2, textWidth, 15);
            songPlayed.setHorizontalAlignment(SwingConstants.CENTER);


            // push onto top JPanel
            topPanel.add(songPlayed);


            setVisible(true);

            System.out.println("\n done with init...........");

        }

    }
Was it helpful?

Solution

The documentation for the constructor in question explains:

Creates an sRGB color with the specified red, green, blue, and alpha values in the range (0 - 255).

Parameters:

r - the red component

g - the green component

b - the blue component

a - the alpha component

The last is the important - you set the Alpha channel to 0 -which means the color is not really a color, but transparency... RGBA color space Wiki article

Solution

  • use new Color(0,0,0,255); to specify 100% opaque black
  • or use new Color(0,0,0); as from the doc: "Alpha is defaulted to 255."
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top