0

我想在背景图像上显示一个网格。为此,我使用了 JLayeredPane。这是代码的相关部分:

JLayeredPane layers = new JLayeredPane();
layers.setPreferredSize( new Dimension( GRID_SIZE, GRID_SIZE ) );
layers.setLayout(new FlowLayout());

BufferedImage backgroundImage = null;
            try{ backgroundImage = ImageIO.read(new File("resources/Background.png"));}
            catch(IOException ex){
                System.out.println(ex);
            }

JLabel bImage = new JLabel(new ImageIcon(backgroundImage));
JPanel grid = createGrid(); //creates a grid the same size as the image (500,500)

grid.setBackground(new Color(0,0,0,125));

layers.add(bImage, new Integer(1));
layers.add(grid, new Integer(2));

然后显示的是网格,黑色 50% alpha,在框架背景的顶部,背景图像无处可见。

如果我注释掉

//layers.add(grid, new Integer(2));

我可以看到图像,所以我知道它在那里。

我将如何在图像上显示网格?

谢谢,

4

0 回答 0