【问题标题】:Repaint() doesn't refresh the board when table of coordinates is changing.当坐标表发生变化时,Repaint() 不会刷新棋盘。
【发布时间】:2018-04-03 21:52:15
【问题描述】:

我会询问 JPanel 和paintComonent。我想用 Pawns 创建 Ludo 游戏。我试图在游戏类(负责整个逻辑)中初始化按钮面板(负责按钮)。 Paint - 用于打印,Player 用于保留玩家和 Pawn。在类 buttonPanel 中,我使用名为 createDiceBtn 的 JButton 创建动作侦听器,用于掷骰子和改变玩家的位置。在播放器中,我定义了 move 方法,用于在方法 initCoords 中通过声明的坐标数组移动(坐标比循环中的坐标少,但杂乱程度更低)。我已经创建了按钮,并尝试在 Game 类中重新绘制它。数组的索引正在改变,但我对使用重绘有点困惑,因为它不起作用。一般来说,我对这种行为有疑问[类游戏保持油漆类和按钮面板。逻辑在游戏中。如果我在游戏中按下带有逻辑的按钮,这将在 Paint Class 中重新绘制。这是什么原因?附言。对不起,非封装,首先我希望它能够工作。

播放器

    package proj2;

import java.util.Random;

public class Player {

    public boolean active;

     Paw[] PawYellow;
     Paw[] PawRed;
     Paw[] PawBlue;
     Paw[] PawGreen;
    public Colors color;
    public Game game;
    ButtonPanel buttonPanel;
  //  public Game game;

    private Colors[]colors=Colors.values(); // enum of colors. 

    public Player() {
        this.active=active; //variable for chceking if this field is active. 

         PawYellow=new Paw[49];
         PawRed=new Paw[49];
         PawBlue=new Paw[49];
         PawGreen=new Paw[49];
         initCords();
    }

    public void move(int r,Paw paw[] )
    {
        //if (players.get(currentPlayer).equals(this.players.get(1))) {
        for (int i = 0; i < 4; i++) {
            if (paw[i].is_active == true && r == 6) {
                paw[i] = paw[4];
                paw[i].is_active = true;
                paw[i].is_active = false;
                System.out.println( paw[i].getX());
                System.out.println( paw[i].getY());
                paw[i].setX(paw[i].getX());
                paw[i].setY(paw[i].getY());
                buttonPanel.repaint();

                break;
            }
        }
        for (int i = 4; i < 49; i++) {
            if (paw[i].is_active == true) {
                paw[i] = paw[i + r];
                paw[i].is_active = false;
                paw[i + r].is_active = true;
                System.out.println( paw[i].getX());
                System.out.println( paw[i].getY());
                paw[i].setX(paw[i].getX());
                paw[i].setY(paw[i].getY());
              //  buttonPanel.repaint();
                break;
            }
        }
    }

    public void initCords() {
        PawYellow[0] = new Paw(140, 700, true);
        PawYellow[1] = new Paw(200, 700, true);
        PawYellow[2] = new Paw(140, 640, true);
        PawYellow[3] = new Paw(200, 640, true);
        PawYellow[4] = new Paw(380, 700, false);
        PawYellow[5] = new Paw(380, 635, false);
        PawYellow[6] = new Paw(380, 570, false);
        PawYellow[7] = new Paw(380, 505, false);
        PawYellow[8] = new Paw(380, 505, false);
        PawYellow[9] = new Paw(380, 440, false);
        PawYellow[10] = new Paw(320, 440, false);
        PawYellow[11] = new Paw(260, 440, false);

        PawRed[0] = new Paw(140, 50, true);
        PawRed[1] = new Paw(200, 110, true);
        PawRed[2] = new Paw(140, 110, true);
        PawRed[3] = new Paw(200, 50, true);
        PawRed[4] = new Paw(140, 310, false);
        PawRed[5] = new Paw(200, 310, false);
        PawRed[6] = new Paw(260, 310, false);
        PawRed[7] = new Paw(320, 310, false);
        PawRed[8] = new Paw(380, 310, false);
        PawRed[9] = new Paw(380, 245, false);
        PawRed[10] = new Paw(380, 180, false);
        PawRed[11] = new Paw(380, 115, false);

        PawBlue[0] = new Paw(740, 50, true);
        PawBlue[1] = new Paw(740, 110, true);
        PawBlue[2] = new Paw(680, 110, true);
        PawBlue[3] = new Paw(680, 50, true);

        PawBlue[4] = new Paw(500, 115, false);
        PawBlue[5] = new Paw(500, 180, false);
        PawBlue[6] = new Paw(500, 245, false);
        PawBlue[7] = new Paw(500, 310, false);
        PawBlue[8] = new Paw(560, 310, false);
        PawBlue[9] = new Paw(620, 310, false);
        PawBlue[10] = new Paw(680, 310, false);
        PawBlue[11] = new Paw(740, 310, false);

        PawGreen[0] = new Paw(740, 700, true);
        PawGreen[1] = new Paw(740, 640, true);
        PawGreen[2] = new Paw(680, 700, true);
        PawGreen[3] = new Paw(680, 640, true);
        PawGreen[4] = new Paw(740, 440, false);// start
        PawGreen[5] = new Paw(680, 440, false);
        PawGreen[6] = new Paw(620, 440, false);
        PawGreen[7] = new Paw(560, 440, false);
        PawGreen[8] = new Paw(500, 440, false);
        PawGreen[9] = new Paw(500, 505, false);
        PawGreen[10] = new Paw(500, 570, false);
        PawGreen[11] = new Paw(500, 635, false);

    }
}

 class Paw {
        int x;int y; boolean is_active;

        public Paw(int x, int y, boolean is_active) {
            this.x = x;
            this.y = y;
            this.is_active = is_active;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

        public boolean isIs_active() {
            return is_active;
        }

        public void setIs_active(boolean is_active) {
            this.is_active = is_active;
        }

}

///////////////////////////////////////////////////////////////////////////////////////////////////

// 游戏。

package proj2;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class Game {
    public Paw[] crdGreen;
    public Paw[] crdBlue;
    public Paw[] crdRed;
    public Paw[] crdYellow;
    List<Player> players = new ArrayList<>();

    public Player play1;
    public Player play2;
    public Player play3;
    public Player play4;
    public int currentPlayer = 0;

    ButtonPanel buttonPanel;


    public Game() {

        play1 = new Player();
        play2 = new Player();
        play3 = new Player();
        play4 = new Player();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {

                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {

        Game g = new Game();
        Paint paint = new Paint(g);
        Board board = new Board();
        ButtonPanel buttonPanel = new ButtonPanel(paint, g);
        g.buttonPanel = buttonPanel;

        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Drawss On Image");
        frame.setSize(1400, 900);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(paint);
        frame.getContentPane().add(buttonPanel, BorderLayout.EAST);
        // frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        g.initPlayers();
    }

    public void initPlayers() {

        Player play1 = new Player();
        play1.color = Colors.Green;
        this.players.add(play1);

        Player play2 = new Player();
        play2.color = Colors.Blue;
        this.players.add(play2);

        Player play3 = new Player();
        play3.color = Colors.Red;
        this.players.add(play3);

        Player play4 = new Player();
        play4.color = Colors.Yellow;
        this.players.add(play4);

}

    public void diceResult(int r) {

        if (r != 6) {

            currentPlayer++;
            if (currentPlayer == (players.size())) {
                currentPlayer = 0;

            }

        }

        if (players.get(currentPlayer).equals(this.players.get(0))) {
            players.get(0).move(r,play1.PawGreen);
            }

         else if (players.get(currentPlayer).equals(this.players.get(1))) {
            players.get(1).move(r,play2.PawBlue);
            }

        else  if (players.get(currentPlayer).equals(this.players.get(2))) {
            play3.move(r,play3.PawRed);
            }
        else  if (players.get(currentPlayer).equals(this.players.get(3))) {
            play4.move(r,play4.PawYellow);
        }

buttonPanel.repaint();

    }

///////////////////////////////////////////////////////////////
Button panel 
package proj2;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ThreadLocalRandom;

public class ButtonPanel extends JPanel implements ActionListener {
    private Paint paint;
    final Dice dice = new Dice();
    JButton diceBtn = new JButton("Throw Dice");
    public Game game;

    int temp;

    public ButtonPanel(Paint paint, Game g) {
        this.paint = paint;
        game = g;
        add(createDiceBtn());
    }

    public JButton getDiceBtn() {
        return diceBtn;
    }

    public JButton createDiceBtn() {
        JButton diceBtn = new JButton();
        JButton diceBtn1 = new JButton();
        diceBtn.setBackground(Color.BLUE);
        diceBtn.addActionListener(this);
        //        //        //  diceBtn.s
        diceBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int r = dice.randomGenerator(); // method for rolling a dice

                diceBtn.setText(" Wylosowano: " + r+ "Obecny gracz:" +game.currentPlayer);

                game.diceResult(r);
            }

        });
        return diceBtn;
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }
}

/////////////////////////////////////////////////

油漆。 包 proj2;

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class Paint extends JPanel {
    Paint paint;
    ButtonPanel buttonPanel;
    Game game;

    public Paint(Game g)
    {
        game=g;
    }

    @Override
    public void paintComponent(Graphics g) {
       // super.paintComponent(g);
        g.setColor(Color.BLACK);
        g.drawOval(380, 635, 40, 40);
        g.drawOval(380, 570, 40, 40);
        g.drawOval(380, 505, 40, 40);
        g.drawOval(380, 440, 40, 40);
        g.drawOval(380, 310, 40, 40);
        g.drawOval(380, 245, 40, 40);
        g.drawOval(380, 180, 40, 40);
        .......
        /// way of painting static board. 

        // /////////////////
        // repainting on this board my pawns on 4 first positions
        Color customColor = new Color(0,102,0);
        g.setColor(customColor);
        g.fillOval( game.play1.PawGreen[0].getX(), game.play1.PawGreen[0].getY(),20,20);
        g.fillOval( game.play1.PawGreen[1].getX(), game.play1.PawGreen[1].getY(),20,20);
        g.fillOval( game.play1.PawGreen[2].getX(), game.play1.PawGreen[2].getY(),20,20);
        g.fillOval( game.play1.PawGreen[3].getX(), game.play1.PawGreen[3].getY(),20,20);

        Color customColor1 = new Color(102,0,0);
        g.setColor(customColor1);
        g.fillOval( game.play1.PawRed[0].getX(), game.play1.PawRed[0].getY(),20,20);
        g.fillOval( game.play1.PawRed[1].getX(), game.play1.PawRed[1].getY(),20,20);
        g.fillOval( game.play1.PawRed[2].getX(), game.play1.PawRed[2].getY(),20,20);
        g.fillOval( game.play1.PawRed[3].getX(), game.play1.PawRed[3].getY(),20,20);

        Color customColor2 = new Color(0,0,102);
        g.setColor(customColor2);
        g.fillOval( game.play1.PawBlue[0].getX(), game.play1.PawBlue[0].getY(),20,20);
        g.fillOval( game.play1.PawBlue[1].getX(), game.play1.PawBlue[1].getY(),20,20);
        g.fillOval( game.play1.PawBlue[2].getX(), game.play1.PawBlue[2].getY(),20,20);
        g.fillOval( game.play1.PawBlue[3].getX(), game.play1.PawBlue[3].getY(),20,20);

        Color customColor3 = new Color(255,204,0);
        g.setColor(customColor3);
        g.fillOval( game.play1.PawYellow[0].getX(), game.play1.PawYellow[0].getY(),20,20);
        g.fillOval( game.play1.PawYellow[1].getX(), game.play1.PawYellow[1].getY(),20,20);
        g.fillOval( game.play1.PawYellow[2].getX(), game.play1.PawYellow[2].getY(),20,20);
        g.fillOval( game.play1.PawYellow[3].getX(), game.play1.PawYellow[3].getY(),20,20);

}

【问题讨论】:

  • // super.paintComponent(g); 坏主意
  • 您在 Player 的多个实例中混合状态 - 每个 Player 似乎都在玩自己的游戏,独立于其他人 - 相反,每个玩家都应该共享相同的“状态”信息
  • 所以如果我让玩家 1 玩家 2... 类 Player 包含所有数组,如 PawGreen,Paw Yellow ...。在一个类中,它们不共享相同的状态?您对在一块板上保留 4 种不同颜色的坐标有什么想法吗?我在保留典当方面遇到了一点问题。
  • A Player 应该管理它自己的状态——而不是所有玩家的所有状态。它需要维护多少信息是我无法回答的问题,因为我没有完整的可运行示例。 Player 可能会与 Board 共享此信息(或反之亦然)

标签: java swing jpanel paintcomponent


【解决方案1】:

尝试在Paint 组件上调用repaint(),而不是在buttonPanel 上。

【讨论】:

  • 它不起作用。但是当我创建类似这样的东西时 public void diceResult(int r) { play1.PawGreen[0].setX(play1.PawGreen[0].getX()-20*r); buttonPanel.repaint();对于给定的“r”,它正在与移动的爪子一起工作
猜你喜欢
  • 1970-01-01
  • 2014-04-03
  • 2015-05-21
  • 2016-06-26
  • 2022-07-15
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
相关资源
最近更新 更多