繁体   English   中英

将JPanel背景设置为透明,setOpaque()不允许我绘画

[英]Set JPanel background transparent, setOpaque() doesn't let me paint

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Tetris extends JFrame {

public Tetris() {

    add(new GamePanel());
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setResizable(false);
    setSize(800, 600);
    setVisible(true);
    setLocationRelativeTo(null);
    setTitle("Tetris");
}

public class GamePanel extends JPanel {


    public GamePanel(){
        TetrisBoard tetraBoard= new TetrisBoard();
        GridBagLayout layout= new GridBagLayout();
        this.setLayout(layout);
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 2;
        c.gridy = 1;
        c.ipadx = 190;
        c.ipady = 390;
        c.insets.left= 360;
        layout.setConstraints(tetraBoard, c);
        this.add(tetraBoard);
        setBackground(Color.WHITE);
    }

   @Override
   public void paint(Graphics g){
       super.paint(g);

       g.setFont(new Font("Birth Std", Font.PLAIN, 12));
       g.setColor(Color.LIGHT_GRAY);
       g.drawString("200", 36, 63);
       g.drawString("200", 36, 88);
       g.drawString("200", 36, 114);
    }

}//GamePanel class

public class TetrisBoard extends JPanel implements Runnable{
    private Thread animator= new Thread(this);
    private final int DELAY= 50;

    public TetrisBoard(){
        setFocusable(true);
        //setBackground(Color.WHITE);
        setDoubleBuffered(true);
        //this.setBackground(Color.BLACK);
        setOpaque(false);
    }

    @Override
    public void addNotify() {
    super.addNotify();
        animator = new Thread(this);
        animator.start();
    }//addNotify


   @Override
   public void paint (Graphics g){
       super.paint(g);
       g.drawRect (20, 30, 130, 50);

   }//paint

   @Override
public void run() {
    long beforeTime, timeDiff, sleep;

    beforeTime = System.currentTimeMillis();

    while (true) {
        repaint();

        timeDiff = System.currentTimeMillis() - beforeTime;
        sleep = DELAY - timeDiff;

        if (sleep < 0)
            sleep = 2;
        try {
            Thread.sleep(sleep);
        } catch (InterruptedException e) {
            System.out.println("interrupted");
        }

        beforeTime = System.currentTimeMillis();
    }
} 

}//TetrisBoard class


public static void main(String[] args) {
    Tetris t = new Tetris();
}

}

使用此代码,结果是它根本不会绘制任何内容。 我只希望背景是透明的,而不是在背景上绘制的图像,但是如果我setOpaque(false),看起来好像paint方法不会绘制。

编辑:按照要求,我发布了一个简单的代码,将TetraBoard添加到GamePanel(使用该GridBagLayout),并将GamePanel添加到框架,这3个类是单独的文件。 我希望TetraBoard具有透明的背景,以便可以看到GamePanel的背景,但是我在四边板上绘制的内容必须可见。 如果我setOpaque(false),则TetraBoard是透明的,但是它将我绘制的所有内容都设置为透明。

编辑:假设我了解您要执行的操作,请在TetrisBoard构造函数中替换以下行:

setOpaque(false);

有:

setBackground(new Color(0,0,0,0));

例如:

JPanel p = new JPanel() {
    @Override
    public void paintComponent(Graphics g) { // as suggested Andrew
        g.setColor(Color.RED);
        g.drawArc(0, 0, 100, 100, 0, 360); // arc will be painted on transparent bg
    }
};
p.setBackground(new Color(0, 0, 0, 0)); // as suggested Perry
...

因此,您必须执行两个操作:
1)覆盖paintComponent(Graphics g)
2)并将bg颜色设置为透明: new Color(0, 0, 0, 0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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