简体   繁体   中英

Java, transparent JFrame under Canvas

I am trying to implement a fully transperent JFrame which carries a Canvas object. I tried to make the JFrame 's background transparent by calling frame.setBackground(new Color(0, 0, 0, 0)); but somehow Canvas does not show up anymore on the screen and does not draw my rectangle. However, if I comment out setBackground function it draws perfectly, but of course the JFrame is not transparent.

    canvas.setPreferredSize(new Dimension(800, 800));

    frame = new JFrame(title);
    frame.setSize(1000, 1000);
    frame.setResizable(true);
    frame.setUndecorated(true);
    frame.add(canvas);
    //frame.setBackground(new Color(0, 0, 0, 0));
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.toFront();

The buffer strategy to draw a blue rectangle on Canvas.

public void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if(bs == null){
        createBufferStrategy(3);
        return;
    }
    Graphics g = bs.getDrawGraphics();
    //////////////////////////////////////////////////
    g.setColor(Color.BLUE);
    g.fillRect(0,0,800,800);
    //////////////////////////////////////////////////
    g.dispose();
    bs.show();
}

When setBackground is commented : 图片 When setBackground is not commented : 图像2

but somehow Canvas does not show up anymore on the screen and does not draw my rectangle

I suspect that there is more going on in your code then we have access to, as I seem to be able to get it to work...oddly...

屏幕

So, this basically sets up a translucent window (for testing, I've tested it an alpha of 0 and it still works), adds a smaller Canvas to it (so I can see the window) and draws and even smaller rectangle on top of that.

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestPane pane = new TestPane();
                JFrame frame = new JFrame("Test");
                frame.setLayout(new GridBagLayout());
                frame.setSize(100, 100);
                frame.setResizable(true);
                frame.setUndecorated(true);
                frame.add(pane);
                frame.setBackground(new Color(255, 0, 0, 128));
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                frame.toFront();

                Timer timer = new Timer(40, new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        pane.render();
                    }
                });
                timer.start();
            }
        });
    }

    public class TestPane extends Canvas {

        public void render() {
            BufferStrategy bs = this.getBufferStrategy();
            if (bs == null) {
                createBufferStrategy(3);
                return;
            }
            Graphics g = bs.getDrawGraphics();
            //////////////////////////////////////////////////
            g.setColor(Color.BLUE);
            g.fillRect(0, 0, 25, 25);
            //////////////////////////////////////////////////
            g.dispose();
            bs.show();
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(50, 50);
        }

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM