java - JlayeredPane 透明度 setOpaque 不起作用

标签 java swing transparent jlayeredpane

我正在创建一个模拟类型应用程序,我想要一个背景层和顶部的另一层用于所有动画。我目前使用 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 方法中绘制该图像和 Sprite 。

例如:

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();
         }
      });
   }
}

关于java - JlayeredPane 透明度 setOpaque 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29065729/

相关文章:

java - "Inline"复选框列表 - 自动换行

java - 获取 Graphics2D?

python - 如何在pygame中绘制透明线

winapi - 透明的 win32 窗口和文本

java - 单击按钮时增加一个值并使用该值更新文本字段

java - Java在哪里存储最终成员和静态成员?

java - spring mvc 中图片未上传

java - Hibernate/JPA 查询连接子类的可嵌入属性

html - div 与父 div 的透明边框

java - 如何使用匹配器替换一个网址中的2个不同的字符串?