JlayeredPane透明度setOpaque无法正常工作

时间:2015-03-15 20:18:44

标签: 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);
    }
}

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

1 个答案:

答案 0 :(得分:2)

不要使用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();
         }
      });
   }
}