由于setOpaque(false)方法,JPanel不断被重新绘制

时间:2015-01-10 21:36:59

标签: java swing jpanel transparent repaint

我正在创建一个Snake游戏,并遇到了必须使用JLayeredPane的问题。我使用DrawBoard类绘制了该板,它必须根据计时器重新绘制。我必须为我放在板上的3个水果分配一个随机颜色,因为我不能将它放在DrawBoard类中(因为它经常被重新绘制,因此随机颜色不断变换每次重绘)我必须制作一个单独的JPanel,c2。然后将c2与容器(包含drawBoard组件)JPanel一起放入JLayeredFrame中,以便c2可以放在容器顶部 - 有效地将水果覆盖在板上,以便每次都可以手动重新涂抹水果成分。收集水果以设置随机颜色。

当我将水果画到板上时,我必须将组件的背景和JPanel设置为透明,以便您可以看到下面的板。我已经确定的问题是,当调用c2.setOpaque(false)时,c2 JPanel会不断重新绘制,因此会为水果连续生成颜色。

然后将JLayeredPane放入JFrame。

如何让c2 JPanel具有透明背景而不调用自己?

Board Class - 这是JFrame

的地方
    public Board() {
        boardBack(g);
        startGame();
    }

    public void boardBack(Graphics g) {
        questionBox = new QuestionBox();
        drawBoard = new DrawBoard();
        points = new Points();
        fruit = new Fruit();
        filler = new Filler();

        JFrame frame = new JFrame("Snake");
        JPanel container = new JPanel();
        JPanel c2 = new JPanel();
        JLayeredPane pane = new JLayeredPane();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.addKeyListener(this);
        container.setLayout(new BorderLayout());
        c2.setLayout(new BorderLayout());
        frame.setResizable(false);

        container.add(questionBox, BorderLayout.NORTH);
        container.add(points, BorderLayout.CENTER);
        container.add(drawBoard, BorderLayout.SOUTH);

        c2.add(fruit, BorderLayout.SOUTH);
        c2.add(filler, BorderLayout.NORTH);

        filler.setOpaque(false);
        fruit.setOpaque(false);
        //This is where the problem exists, 
        //when removed I cannot see the drawBoard component below,
        //but it no longer repaints.
        c2.setOpaque(false);

        container.setBounds(0, 0, 600, 720);
        c2.setBounds(0, 0, 600, 720);

        pane.add(c2, new Integer(2));
        pane.add(container, new Integer(1));

        pane.setPreferredSize(new Dimension(600, 720));

        frame.add(pane);

        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

在这个类中,我还有一个actionPerformed方法和Timer,它为每个tick执行该操作。在此内部没有召回水果成分。

水果类

package snake;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Fruit extends JPanel {

    public static Color red = new Color(8005929);
    public static Color brown = new Color(9067566);
    public static Color purple = new Color(6684774);

    public static Dimension dim4 = new Dimension(600, 600);

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(dim4.width, dim4.height);
    }

        Random rand = new Random();
        public int selectColour;
        //The structure of the array is as follows
        // [0] - fruit1 colour
        // [1] - fruit2 colour
        // [2] - fruit3 colour
        public int[] colourSelected = new int[3];

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        //This allows us to select a random colour for the fruit to be assigned.
        //Where selectColour = 0 refers to the colour red
        //Where selectColour = 1 refers to the colour brown
        //Where selectColour = 2 refers to the colour purple
        int selectColour = rand.nextInt(3);

        switch (selectColour) {
        case 0:
            colourSelected[0] = selectColour;
            g.setColor(red);
            break;
        case 1:
            colourSelected[0] = selectColour;
            g.setColor(brown);
            break;
        case 2:
            colourSelected[0] = selectColour;
            g.setColor(purple);
            break;
        }

        //Draws the fruit at the point of the fruit
        g.fillRect(Board.fruit1.x * Board.SCALE, Board.fruit1.y * Board.SCALE,
                Board.SCALE, Board.SCALE);

        //This draws the second fruit
        g.setColor(brown);
        g.fillRect(Board.fruit2.x * Board.SCALE, Board.fruit2.y * Board.SCALE,
                Board.SCALE, Board.SCALE);

        //This draws the third fruit
        g.setColor(purple);
        g.fillRect(Board.fruit3.x * Board.SCALE, Board.fruit3.y * Board.SCALE,
                Board.SCALE, Board.SCALE);

    }

这显示了运行时的问题我在System.out.println()方法中放置了paintComponent()来显示问题。

当setOpaque(false)仍然在代码中时(左下方水果连续改变颜色)

enter image description here

删除时

enter image description here

1 个答案:

答案 0 :(得分:2)

首先,你没有控制绘画过程,绘画可能由于多种原因而发生,其中许多是你无法控制的

其次,绘画应绘制组件的当前状态,不包含任何逻辑。

将颜色生成从paintComponent移出到Timer可以调用的方法,并在调用此方法时重新绘制组件