jbutton中的Java Swing Reasign值[] []

发布于 2025-01-29 15:44:14 字数 5550 浏览 3 评论 0原文


I have this array of classes extending JButtons, and when one is clicked it registers that. Then if another one gets clicked, they should 'switch' places. So my question is: How can i implement it that is swiches the buttons (so far i got before) and (the important part) how can i 'refresh' the GUI, so the user can see the chenge visually. Following the code:
import javax.swing.*; import Pieces.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Game extends JFrame {
    private static final int width = 8;
    private static final int height = 8;

    private static Piece clicked;

    public static Piece[][] fields = new Piece[width][height];

    private JPanel main = new JPanel();

    public static void init(JPanel g) {
        for (int y = 0; y < fields.length; y++) {
            for (int x = 0;  x < fields[y].length; x++) {
                if (y == 0) {
                    switch (x) {
                        case 0,7:
                            fields[y][x] = new Rook(x,y,1);
                            break;
                        case 1,6:
                            fields[y][x] = new Knight(x,y,1);
                            break;
                        case 2,5:
                            fields[y][x] = new Bishop(x,y,1);
                            break;
                        case 3:
                            fields[y][x] = new Queen(x,y,1);
                            break;
                        case 4:
                            fields[y][x] = new King(x,y,1);
                            break;
                    }
                }
                if (y == 1) fields[y][x] = new Pawn(x, y, 1);
                else if (y >= 2 && y <= 5) fields[y][x] = new Empty(x,y,9);
                else if (y == 6) fields[y][x] = new Pawn(x, y, 0);
                else if(y == 7) {
                    switch (x) {
                        case 0,7:
                            fields[y][x] = new Rook(x,y,0);
                            break;
                        case 1,6:
                            fields[y][x] = new Knight(x,y,0);
                            break;
                        case 2,5:
                            fields[y][x] = new Bishop(x,y,0);
                            break;
                        case 3:
                            fields[y][x] = new Queen(x,y,0);
                            break;
                        case 4:
                            fields[y][x] = new King(x,y,0);
                            break;
                    }
                }

                fields[y][x].addActionListener(e -> {
                    var p = (Piece) e.getSource();
                    var pPos = p.getCell();

                    if(clicked == null) {
                        clicked = p;
                        System.out.println(fields[clicked.getCell().y][clicked.getCell().x]);
                        clicked.setForeground(Color.yellow.darker());
                        System.out.println("clicked " + pPos);

                    } else if (pPos == clicked.getCell()) {
                        clicked.setForeground(Color.white);
                        System.out.println("deselecting " + pPos);
                        clicked = null;

                    } else {
                        if (clicked.canMoveTo(fields, pPos)) {
                            fields[p.getCell().y][p.getCell().x] = clicked;
                            fields[clicked.getCell().y][clicked.getCell().x] = new Empty(clicked.getCell().x, clicked.getCell().y, 9);

                            System.out.println("moving " + clicked.getCell() + " to " + pPos);
                            clicked.setForeground(Color.white);
                        }
                        else System.out.println("can´t move there, sry");
                        clicked = null;
                    }
                    SwingUtilities.updateComponentTreeUI(g);
                });

                g.add(fields[y][x]);
            }
        }
    }

    public Game() {
        main.setBackground(Color.darkGray.darker());
        main.setLayout(new GridLayout(8,8));
        this.setSize(800,800);

        init(main);
        this.add(main);

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        var g = new Game();
    }
}
package Pieces;

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

public abstract class Piece extends JButton {
    private final int isWhite; //0 is false, 1 is true, 9 is undefined
    private Point cell;

    public Piece(int x, int y, int isWhite) {
        cell = new Point(x, y);
        this.isWhite = isWhite;
        this.setForeground(Color.white);
        this.setOpaque(false);
        //this.setContentAreaFilled(false);
    }

    public Point getCell() {
        return cell;
    }

    public int isWhite() {
        return isWhite;
    }

    public boolean canMoveTo(Piece[][] fields, Point point) {
        return true;
    }
}

*所有的零件都是这样的

package Pieces;

public class Bishop extends Piece{
    public Bishop(int x, int y, int isWhite) {
        super(x, y, isWhite);
        this.setText("Bishop");
    }
}

(是的,有时候有时会成为国际象棋)

I have this array of classes extending JButtons, and when one is clicked it registers that.
Then if another one gets clicked, they should 'switch' places. So my question is: How can i implement it that is swiches the buttons (so far i got before) and (the important part) how can i 'refresh' the GUI, so the user can see the chenge visually. Following the code:

import javax.swing.*; import Pieces.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Game extends JFrame {
    private static final int width = 8;
    private static final int height = 8;

    private static Piece clicked;

    public static Piece[][] fields = new Piece[width][height];

    private JPanel main = new JPanel();

    public static void init(JPanel g) {
        for (int y = 0; y < fields.length; y++) {
            for (int x = 0;  x < fields[y].length; x++) {
                if (y == 0) {
                    switch (x) {
                        case 0,7:
                            fields[y][x] = new Rook(x,y,1);
                            break;
                        case 1,6:
                            fields[y][x] = new Knight(x,y,1);
                            break;
                        case 2,5:
                            fields[y][x] = new Bishop(x,y,1);
                            break;
                        case 3:
                            fields[y][x] = new Queen(x,y,1);
                            break;
                        case 4:
                            fields[y][x] = new King(x,y,1);
                            break;
                    }
                }
                if (y == 1) fields[y][x] = new Pawn(x, y, 1);
                else if (y >= 2 && y <= 5) fields[y][x] = new Empty(x,y,9);
                else if (y == 6) fields[y][x] = new Pawn(x, y, 0);
                else if(y == 7) {
                    switch (x) {
                        case 0,7:
                            fields[y][x] = new Rook(x,y,0);
                            break;
                        case 1,6:
                            fields[y][x] = new Knight(x,y,0);
                            break;
                        case 2,5:
                            fields[y][x] = new Bishop(x,y,0);
                            break;
                        case 3:
                            fields[y][x] = new Queen(x,y,0);
                            break;
                        case 4:
                            fields[y][x] = new King(x,y,0);
                            break;
                    }
                }

                fields[y][x].addActionListener(e -> {
                    var p = (Piece) e.getSource();
                    var pPos = p.getCell();

                    if(clicked == null) {
                        clicked = p;
                        System.out.println(fields[clicked.getCell().y][clicked.getCell().x]);
                        clicked.setForeground(Color.yellow.darker());
                        System.out.println("clicked " + pPos);

                    } else if (pPos == clicked.getCell()) {
                        clicked.setForeground(Color.white);
                        System.out.println("deselecting " + pPos);
                        clicked = null;

                    } else {
                        if (clicked.canMoveTo(fields, pPos)) {
                            fields[p.getCell().y][p.getCell().x] = clicked;
                            fields[clicked.getCell().y][clicked.getCell().x] = new Empty(clicked.getCell().x, clicked.getCell().y, 9);

                            System.out.println("moving " + clicked.getCell() + " to " + pPos);
                            clicked.setForeground(Color.white);
                        }
                        else System.out.println("can´t move there, sry");
                        clicked = null;
                    }
                    SwingUtilities.updateComponentTreeUI(g);
                });

                g.add(fields[y][x]);
            }
        }
    }

    public Game() {
        main.setBackground(Color.darkGray.darker());
        main.setLayout(new GridLayout(8,8));
        this.setSize(800,800);

        init(main);
        this.add(main);

        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        var g = new Game();
    }
}
package Pieces;

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

public abstract class Piece extends JButton {
    private final int isWhite; //0 is false, 1 is true, 9 is undefined
    private Point cell;

    public Piece(int x, int y, int isWhite) {
        cell = new Point(x, y);
        this.isWhite = isWhite;
        this.setForeground(Color.white);
        this.setOpaque(false);
        //this.setContentAreaFilled(false);
    }

    public Point getCell() {
        return cell;
    }

    public int isWhite() {
        return isWhite;
    }

    public boolean canMoveTo(Piece[][] fields, Point point) {
        return true;
    }
}

*all the pieces are setup like this

package Pieces;

public class Bishop extends Piece{
    public Bishop(int x, int y, int isWhite) {
        super(x, y, isWhite);
        this.setText("Bishop");
    }
}

(yee, this will hopefully be chess sometimes)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文