我正在尝试登上游戏模拟器。我的方块是JButton,它们看起来还不错,直到我使用fillOval函数添加一个椭圆形以指示玩家在棋盘上的位置为止。

我的面板是由方形按钮构建的,这里是Square类:

public class Square extends JButton{

    private boolean occupied;
    private Player occupant;
    private int position, num;
    private Board board;

    public Square (int position) {
        this.setPreferredSize(new Dimension(85, 85));
        this.setText(Integer.toString(position + 1));
        this.setFont(new Font("Tempus Sans ITC", Font.BOLD, 20));
        this.position = position;
        occupied = false;
        occupant = null;
    }

    public int getPosition() {
        return position;
    }


    public boolean isOccupied() {
        return this.occupied;
    }


    public Player getOccupant() {
        if(this.isOccupied())
            return this.occupant;

        return null;
    }

    public void enter ( Player p) {
         this . enter (p);
         }
             public void leave ( Player p) {
         this . leave (p);
         }
    public void setOccupant(Player visitor) {
        this.occupant = visitor;
        this.occupied = true;

        }

public void paint(Graphics g) {

        if(this.isOccupied()) {

            g.setColor(occupant.getColor());
            g.fillOval(30,30,50,50);


        }
}


和GUI类

     * Create the application.
     */
    public Game() {
        initialize();
        startGame();
        players.add(human);
        players.add(computer);
        //restart();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        //Main window
        frame = new JFrame();
        frame.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\X270\\eclipse-workspace\\Snakes and Ladders\\src\\org\\snakesandladders\\logo.png"));
        frame.setBounds(100, 100, 600, 700);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.setResizable(false);

        //call  panels
        createGamePanel();
        createControlPanel();
    }

    /**
     * Creating Board Panel
     */
    private void createGamePanel() {
        gamePanel = new JPanel();
        board = new Board();
        addBoardToPanel(board, gamePanel);
        frame.getContentPane().add(gamePanel, BorderLayout.CENTER);
    }


    public void addBoardToPanel(Board b, JPanel p) {
        int rows = 5;
        int cols = 6;
        JPanel ContainerPanel = new JPanel(new GridLayout(5, 6));
        for (int r=rows; r>0; r--){
            if(r % 2 == 0) {
                int rowLeft = r * cols;
                for (int n = rowLeft, l = (rowLeft - cols); n>l; n--){
                    Square sq = b.getSquare(n-1);
                    ContainerPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, BORDER_WIDTH));
                    ContainerPanel.add(sq);
                }
            }
            else{
                int rowLeft = ((r - 1) * cols) +1;
                for (int n = rowLeft, l = (rowLeft + cols); n<l; n++){
                    Square sq =b.getSquare(n-1);
                    ContainerPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK, BORDER_WIDTH));
                    ContainerPanel.add(sq);
                }
            }
            p.add(ContainerPanel, BorderLayout.CENTER);
        }
    }

    /**
     * Creating the control panel
     */
    private void createControlPanel() {
        controlPanel = new JPanel();
        infoPanel = new JPanel();
        playerPanel = new JPanel();
        buttonPanel = new JPanel();

        controlPanel.setLayout(new BorderLayout(20, 20));
        controlPanel.setBorder(new EmptyBorder(20, 20, 20, 20));



        humanLabel = new JLabel(/*humanPlayer.getName()*/  " (You)");
        humanLabel.setPreferredSize(new Dimension(175, 40));
        humanLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
        humanLabel.setHorizontalAlignment(SwingConstants.CENTER);
        humanLabel.setBorder(new LineBorder(Color.DARK_GRAY, 2));
        humanLabel.setBackground(Color.RED);
        humanLabel.setOpaque(true);
        humanLabel.setForeground(Color.WHITE);
        playerPanel.add(humanLabel, BorderLayout.WEST);

        diceButton = new JButton("Roll Dice");
        diceButton.setPreferredSize(new Dimension(150, 40));
        diceButton.setFont(humanLabel.getFont());
        /*diceButton.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            playTurn(humanPlayer);
        }
        });*/
        playerPanel.add(diceButton, BorderLayout.CENTER);

        computerLabel = new JLabel(/*humanPlayer.getName() + */" (Computer)");
        computerLabel.setPreferredSize(humanLabel.getPreferredSize());  //new Dimension(labelWidth, labelHeight)
        computerLabel.setFont(humanLabel.getFont());
        computerLabel.setHorizontalAlignment(SwingConstants.CENTER);
        computerLabel.setBorder(humanLabel.getBorder());
        computerLabel.setBackground(Color.BLUE);
        computerLabel.setOpaque(true);
        computerLabel.setForeground(humanLabel.getForeground());
        playerPanel.add(computerLabel, BorderLayout.EAST);

        diceLabel = new JLabel("Dice Value: ?");
        diceLabel.setPreferredSize(new Dimension(515, 40));
        diceLabel.setFont(humanLabel.getFont());
        diceLabel.setHorizontalAlignment(SwingConstants.CENTER);
        diceLabel.setBorder(humanLabel.getBorder());
        diceLabel.setBackground(Color.CYAN);
        diceLabel.setOpaque(true);
        infoPanel.add(diceLabel, BorderLayout.NORTH);

        startButton = new JButton("Start New Game");
        startButton.setFont(humanLabel.getFont());
        startButton.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent r){
                restart();
            }
        });
        buttonPanel.add(startButton);

        exitButton = new JButton("Exit");
        exitButton.setFont(humanLabel.getFont());
        exitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                exit();
            }
        });
        buttonPanel.add(exitButton);

        controlPanel.add(infoPanel, BorderLayout.NORTH);
        controlPanel.add(buttonPanel, BorderLayout.SOUTH);


        controlPanel.add(playerPanel, BorderLayout.CENTER);
        frame.getContentPane().add(controlPanel, BorderLayout.SOUTH);
    }



这是添加fillOval之前的样子

java - 在Square类中创建Paint Graphics g破坏了我的GUI-LMLPHP

这是添加fillOval后GUI的外观

java - 在Square类中创建Paint Graphics g破坏了我的GUI-LMLPHP

我该如何解决?
先感谢您

最佳答案

您必须致电super.paint()

public void paint(Graphics g) {
    super.paint(g);
    if(this.isOccupied()) {
        g.setColor(occupant.getColor());
        g.fillOval(30,30,50,50);
    }
}

关于java - 在Square类中创建Paint Graphics g破坏了我的GUI,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58724191/

10-16 12:45