【问题标题】:JPanel Constantly Being Repainted Due To setOpaque(false) Method由于 setOpaque(false) 方法,JPanel 不断被重绘
【发布时间】:2015-03-09 00:11:24
【问题描述】:

我正在创建一个 Snake 游戏,并且遇到了必须使用 JLayeredPane 的问题。我已经使用 DrawBoard 类绘制了棋盘,该类必须根据计时器不断地重新绘制。我必须为放置在板上的 3 个水果分配随机颜色,因为我无法将其放置在 DrawBoard 类中(因为它不断被重新绘制,因此每次重新绘制时随机颜色都会不断变化)我必须制作一个单独的JPanel,c2。然后将 c2 与容器(包含 drawBoard 组件)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);
    }

在这个类中,我还有一个 actionPerformed 方法和 Timer,它为每个刻度执行该操作。没有召回其中的水果成分。

水果类

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) 还在代码中时(左下方的水果不断变色)

移除时

【问题讨论】:

  • 考虑提供一个runnable example 来证明您的问题。这将导致更少的混乱和更好的响应

标签: java swing jpanel transparent repaint


【解决方案1】:

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

第二,绘制应该绘制组件的当前状态,并且不包含任何逻辑。

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

【讨论】:

  • 但这并没有什么不同,因为无论 setColour 在哪里,它都会不断地重新绘制组件,因为我最终必须将此方法调用回paintComponent 来设置水果的颜色。跨度>
  • 另外,正如我所说,当我删除 c2.setOpaque(false) 时,它不再重新绘制自己。
  • 提供一个可运行的示例来演示您的问题。当更新一个或多个与其重叠的组件时,需要重新验证透明组件,因此您的代码的某些其他部分可能已更新并强制重新绘制它......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多