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

JPanel Constantly Being Repainted Due To setOpaque(false) Method

我正在创建贪吃蛇游戏,遇到了必须使用 JLayeredPane 的问题。我已经使用 DrawBoard class 绘制了板,它必须根据计时器不断地重新绘制。我必须为我放在板上的 3 个水果分配一个随机颜色,因为我不能将它放在 DrawBoard class 中(因为它不断被重新绘制,因此随机颜色在每次重新绘制时不断变化)我不得不制作一个单独的 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);
    }

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

水果class

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

    }

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

当setOpaque(false)还在代码中时(左下水果不断变色)

删除后

首先,您无法控制绘画过程,绘画可能出于多种原因而发生,其中许多原因您无法控制

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

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