JlayeredPane 透明度 setOpaque 不工作

JlayeredPane Transparency setOpaque not working

我正在创建一个模拟类型的应用程序,我想要一个背景层和一个顶部的所有动画层。我目前正在使用 JlayeredPanes,但我无法让顶层的背景显示为透明,因此我可以看到背景,非常感谢任何帮助,代码如下:

背景图层

public class SimBackground extends JLayeredPane{

private Model theModel;
private SimulationArea simulationArea;

public SimBackground(Model theModel){
    this.theModel=theModel;

    setBackground(new Color(0, 230, 0));
    setOpaque(true);
    setPreferredSize(new Dimension(500,500));
    setVisible(true);

}
@Override
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    for(int x=0;x<50;x++){
        for(int y=0;y<50;y++){

            g.drawRect((x*10), (y*10), 10, 10);
        }
    }
}

顶层

public class SimulationArea extends JLayeredPane {

private int SPEED = 100;
private Model theModel;
Timer timer;

public SimulationArea(Model theModel){

    this.theModel = theModel;

    setPreferredSize(new Dimension(500,500));
    setLocation(0,0);

    setOpaque(false);
    setBackground(new Color(0,0,0,0));
    setVisible(true);

    //Swing Timer
    timer = new Timer(SPEED,new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent ae) {
            update();
            repaint();
            revalidate();
        }
    });     
}
private void update() {
    theModel.update();
}
@Override
public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;
    //test get 1 active object

    ArrayList<ActiveObject> activeObjects = theModel.getActiveObjects();
    //System.out.println(activeObjects.size());
    for(int i=0; i<activeObjects.size(); i++){
        ActiveObject activeObject = theModel.getActiveObjects().get(i);

        int x = activeObject.getCoordinates().getX();
        int y = activeObject.getCoordinates().getY();
        int size = activeObject.getSize();
        g2d.fillRect (x ,y , size, size);
    }
}

有人可以告诉我我在这里缺少什么吗?

不要使用 JLayeredPane,但如果您将来确实需要使用 JLayeredPane,您将需要阅读教程 here,因为根据我的评论,您根本没有正确使用它们.相反,我建议您通过在单个 JPanel 中完成所有绘制来简化,可能在构造函数中将背景绘制到 BufferedImage 中,然后在 JPanel 的 paintComponent 方法中绘制该图像和您的精灵。

例如:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.*;

public class SimExample extends JPanel {
   private static final int PREF_W = 500;
   private static final int PREF_H = PREF_W;
   private static final Color BKGD_COLOR = new Color(0, 230, 0);
   private BufferedImage bkgrnd = new BufferedImage(PREF_W, PREF_H,
         BufferedImage.TYPE_INT_ARGB);

   public SimExample() {
      Graphics2D g = bkgrnd.createGraphics();
      g.setBackground(BKGD_COLOR);
      g.clearRect(0, 0, PREF_W, PREF_H);
      g.setColor(Color.black);
      for (int x = 0; x < 50; x++) {
         for (int y = 0; y < 50; y++) {

            g.drawRect((x * 10), (y * 10), 10, 10);
         }
      }
      g.dispose();
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      Graphics2D g2d = (Graphics2D) g;
      if (bkgrnd != null) {
         g.drawImage(bkgrnd, 0, 0, null);
      }

      // draw sprites here
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      SimExample mainPanel = new SimExample();

      JFrame frame = new JFrame("SimExample");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}