Java 由于set不透明(false)方法,JPanel不断被重新绘制

Java 由于set不透明(false)方法,JPanel不断被重新绘制,java,swing,jpanel,transparent,repaint,Java,Swing,Jpanel,Transparent,Repaint,我正在创建一个Snake游戏,遇到了必须使用JLayeredPane的问题。我用DrawBoard类绘制了这块木板,它必须根据计时器不断地重新绘制。我必须为我放在黑板上的3个水果分配一个随机颜色,因为我不能将其放在DrawBoard类中(因为它经常被重新绘制,因此每次重新绘制时随机颜色都会不断变化),我必须制作一个单独的JPanel,c2。然后将c2与容器(包含抽屉组件)JPanel一起放入一个JLayeredFrame中,这样c2就可以坐在容器的顶部——有效地将水果覆盖在纸板上,以便在每次收集

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

当我在黑板上画水果时,我必须将组件和JPanel的背景设置为透明,以便您可以看到下面的黑板。我发现的问题是,当调用c2.setOpaque(false)时,c2 JPanel会不断地重新绘制自己,因此会不断为水果生成随机颜色

然后将JLayeredPane放置到JFrame中

如何使c2 JPanel拥有透明的背景而不调用自身

Board类——这就是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);
    }
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);

    }
在这个类中,我还有一个actionPerformed方法和计时器,它对每个滴答声都执行该操作。本报告中未提及水果成分

水果类

    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);
    }
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
可以调用的方法,并在调用此方法时重新绘制组件

,但这不会有什么区别,因为无论setcolor位于何处,它都会不断地重新绘制组件,因为我最终不得不调用此方法paintComponent用于设置水果的颜色。另外,正如我所说,当我删除c2.set不透明(false)时,它将不再重新绘制自身。请提供一个可运行的示例来演示您的问题。当一个或多个重叠的组件被更新时,透明组件需要重新验证,因此有可能更新了代码的其他部分并强制重新绘制它……考虑提供一个演示您的问题的方法。这将减少混乱和更好的响应