1
votes

I am trying to make an undecorated transparent JFrame, and then paint some graphics over it. If I extend JFrame, set undecorated to true, and override paint with, I can make a transparent JFrame. Like this:

public class MainFrame extends JFrame {
public static void main(String[] args) throws Exception {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                MainFrame frame = new MainFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}


public MainFrame() {
    setTitle("ASDF");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setUndecorated(true);
    setBounds(0, 0, 200, 200);      
}

public void paint(Graphics g){
    g.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 20));
    g.drawString("ASDF", 100, 100);
}
}

The problem is that when I try drawing something on it, I can't clear what is drawn for another repaint. Like in this example, the text retains the background that was there when it was drawn. So if I move a a window behind the frame, it looks weird, because the frame itself has the old background. I tried AlphaComposite.Clear, but that only made a black background. What can I do?

1
setBounds((d.width-640)/2, (d.height-480)/2, 640, 480); Can you make it break at 200x100? Do that. setContentPane(contentPane); // actually not necessary since I am overriding paint 1) Nothing is being painted in paint. So the code is more 'quashing paint' than overriding it 2) 'not necessary' becomes 'counterproductive' here. The opaque panel will almost entirely cover the custom rendering.Andrew Thompson
For better help sooner, post an SSCCE.Andrew Thompson
I edited my Q a bit, you can compile and run the example. You might not get what's happening unless you actually run it. (And move around windows behind the frame it creates, etc)DankMemes

1 Answers

0
votes

If I extend JFrame, set undecorated to true, and override paint with, I can make a transparent JFrame.

I don't think so. You just paint without caring about the background, which is quickly lost. If you want to know how to make transparent windows in java use Stackoverflow: search for [java] transparent window. This should help you creating such a window, but this is quite a complicated task: Transparent Window