当坐标表发生变化时,Repaint()不会刷新电路板。

时间:2018-04-03 21:52:16

标签: java swing jpanel paintcomponent

我会问JPanel和paintComonent。我想用Pawns创建Ludo游戏。我试图在游戏类中使用buttonPanel(负责按钮)(对整个逻辑做出响应)。油漆 - 用于打印和保持球员和Pawns的球员。在类buttonPanel中,我用JButton创建了一个名为createDiceBtn的动作监听器,用于滚动骰子和改变玩家的位置。在玩家中,我定义了方法移动,用于在方法initCoords中移动一系列声明的坐标(坐标比坐标更少,但杂乱更少)。我已经创建了按钮,我试图在Game类中重新绘制它。数组的索引正在发生变化,但我对使用重绘有点困惑,因为它不起作用。一般来说我有这种行为的问题[类游戏是保持Paint类和按钮面板。逻辑在游戏中。如果我在游戏中按下逻辑按钮,那么它将在Paint Class中重新绘制。这是什么原因? PS。 Sory对于非封锁,首先我希望它能够起作用。

播放器

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

}

1 个答案:

答案 0 :(得分:0)

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