so I have been make a JFrame background transparent by setting the background alpha to 0, however if I run the program in linux the JFrame background is white why is this?
ok I found its got something to do with rendering graphics to the frame, this method is old I don't need it in the program anymore but i would still like to know why this happens (its just in Linux works find in windows)
here is a runnable example
import java.awt.*;
import java.awt.Color;
import javax.swing.*;
class Main extends JFrame{
private void init() {
this.setPreferredSize(new Dimension(1420, 820));
this.setUndecorated(true);
this.setResizable(false);
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
this.setBackground(new Color(0, 255, 0, 0));
this.requestFocus();
this.setVisible(true);
this.validate();
this.pack();
Color color = UIManager.getColor("activeCaptionBorder");
this.getRootPane().setBorder(BorderFactory.createLineBorder(color, 1));
paintInfo();
}
private void paintInfo() {
Graphics g = this.getGraphics();
g.setColor(new Color(222, 222, 222, 4));
g.setFont(new Font("Arial Black", Font.BOLD, 15));
g.setColor(Color.BLACK);
g.drawString("test String ",this.getWidth()/2, this.getHeight()/2);
g.dispose();
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
new Main().init();
}
}
Let's start with...
Isn't how painting is done in
Swing
and disposing of aGraphics
context you didn't create will lead to manner of strange things...Instead, create your frame, set up it's transparency and then add another component to it, which does the actual painting that you want, for example...