Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 区分突出显示的棋盘格和常规棋盘格_Java - Fatal编程技术网

Java 区分突出显示的棋盘格和常规棋盘格

Java 区分突出显示的棋盘格和常规棋盘格,java,Java,我有一个棋盘,通过重写类扩展面板的paint方法来制作。我高亮显示棋子可以在棋盘上找到的所有可能的方块,并将高亮显示方块左上角的像素值存储在: private ArrayList<Integer> highlightedSquares = new ArrayList<Integer>(); 这里是一个示例实现,我在其中定义了两个类——Board和Square。电路板源自JPanel。正方形表示板上的单个正方形。我的鼠标单击侦听器将显示一条消息,指示单击的方块是否高亮显示

我有一个棋盘,通过重写类扩展面板的paint方法来制作。我高亮显示棋子可以在棋盘上找到的所有可能的方块,并将高亮显示方块左上角的像素值存储在:

private ArrayList<Integer> highlightedSquares = new ArrayList<Integer>();

这里是一个示例实现,我在其中定义了两个类——Board和Square。电路板源自JPanel。正方形表示板上的单个正方形。我的鼠标单击侦听器将显示一条消息,指示单击的方块是否高亮显示。希望这能让您很好地了解如何修改代码以获得所需的结果

public class TestMain {
    public static void main(String[] args) throws Exception {
        new TestMain().run();
    }
    public void run() {
        // create and show a JFrame containing a chess board
        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Board board = new Board();
        board.getSquare(2, 4).setHighlighted(true);
        board.getSquare(3, 4).setHighlighted(true);
        window.getContentPane().add(board, BorderLayout.NORTH);
        window.pack();
        window.setVisible(true);
    }

    // *** Board represents the chess board
    private class Board extends JPanel {
        // *** the constructor creates the squares and adds a mouse click listener
        public Board() {
            setPreferredSize(new Dimension(squareSize * 8, squareSize * 8));
            // create the squares
            boolean rowStartRedFlag = true;
            for (int row = 0; row < 8; row++) {
                boolean redFlag = rowStartRedFlag;
                for (int column = 0; column < 8; column++) {
                    squares [row] [column] = new Square(this, row, column, redFlag);
                    redFlag = !redFlag;
                }
                rowStartRedFlag = !rowStartRedFlag;
            }
            // add mouse click listener
            this.addMouseListener(new MouseClickListener());
        }
        // *** mouse click listener
        private class MouseClickListener extends MouseAdapter {
            @Override
            public void mouseClicked(MouseEvent e) {
                Square square = getSquareAt(e.getX(), e.getY());
                String msg = square.isHighlighted() ? "Square is highlighted" : "Square is not highlighted";
                JOptionPane.showMessageDialog(null, msg);
            }
        }
        // ** override paint
        @Override
        public void paint(Graphics g) {
            draw ((Graphics2D) g);
        }
        // *** draw every square on the board
        public void draw(Graphics2D g) {
            for (int row = 0; row < squares.length; row++) {
                for (int column = 0; column < squares [row].length; column++) {
                    squares [row] [column].draw(g);
                }
            }
        }
        // *** get square given row and column
        public Square getSquare(int row, int column) {
            return squares [row] [column];
        }
        // *** get square from coords
        public Square getSquareAt(int x, int y) {
            int column = getColumnAtX(x);
            int row = getRowAtY(y);
            return squares [row] [column];
        }
        // *** get column # given x
        public int getColumnAtX(int x) {
            int column = x  / squareSize;
            return Math.min(Math.max(column, 0), 7);
       }
        // *** get row # given x
        public int getRowAtY(int y) {
            int row = y / squareSize;
            return Math.min(Math.max(row, 0), 7);
        }
        // ** get left x given column
        public int getLeftFromColumn(int column) {
            return column * squareSize;
        }
        // ** get top y give row
        public int getTopFromRow(int row) {
            return row * squareSize;
        }
        // *** get size of square side
        public int getSquareSize() {
            return squareSize;
        }
        private int squareSize = 25; // length of square side
        private Square [][] squares = new Square [8][8];
    }

    // *** Squalre represents one square on the board
    private class Square {
        // ** constructor creates the square
        public Square(Board board, int row, int column, boolean redFlag) {
            this.board = board;
            this.column = column;
            this.row = row;
            if (redFlag) {
                color = Color.RED;
                colorHighlighted = Color.PINK;
            } else {
                color = Color.BLACK;
                colorHighlighted = Color.LIGHT_GRAY;
            }
        }
        // ** set highlight flag
        public void setHighlighted(boolean value) {
            highlighted = value;
        }
        // *** see if square is highlighted
        public boolean isHighlighted() {
            return highlighted;
        }
        // *** draw the square
        public void draw(Graphics2D g) {
            Color fillColor = highlighted ? colorHighlighted : color;
            g.setColor(fillColor);
            int x = board.getLeftFromColumn(column);
            int y = board.getTopFromRow(row);
            int size = board.getSquareSize();
            g.fillRect(x, y, size, size);
        }
        private Board board;
        private Color color;
        private Color colorHighlighted;
        private int column;
        private boolean highlighted = false;
        private int row;
    }
}

与其试图提取有关图形的信息,所有这些信息都应该存储在某种模型中,并根据需要更新到屏幕上……您是否可以发布所有代码,而不仅仅是您认为问题所在的代码片段?有两个原因-1能够运行这个程序并看到问题是很好的,2这个问题很可能出现在与此不同的代码段中,例如,在任何实际填充highlightedSquares的代码中。@DavidWallace如果可能的话,我宁愿不要,这将非常麻烦。然而,我只是想知道这段代码是否有意义。我打印了highlightedSquares,它包含了正确的值……对我来说,使用一个内部有一对整数的Squares类,并使用这些整数的某种数组或集合,似乎更符合逻辑,而不是单独存储坐标并试图通过q计算出哪个是哪个。但是我看不到上面代码中有任何错误。然而,人类并不善于通过阅读代码来发现错误。最好是在调试器中运行所有内容—当然,如果您不提供代码,我们就无法做到这一点。
public class TestMain {
    public static void main(String[] args) throws Exception {
        new TestMain().run();
    }
    public void run() {
        // create and show a JFrame containing a chess board
        JFrame window = new JFrame();
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Board board = new Board();
        board.getSquare(2, 4).setHighlighted(true);
        board.getSquare(3, 4).setHighlighted(true);
        window.getContentPane().add(board, BorderLayout.NORTH);
        window.pack();
        window.setVisible(true);
    }

    // *** Board represents the chess board
    private class Board extends JPanel {
        // *** the constructor creates the squares and adds a mouse click listener
        public Board() {
            setPreferredSize(new Dimension(squareSize * 8, squareSize * 8));
            // create the squares
            boolean rowStartRedFlag = true;
            for (int row = 0; row < 8; row++) {
                boolean redFlag = rowStartRedFlag;
                for (int column = 0; column < 8; column++) {
                    squares [row] [column] = new Square(this, row, column, redFlag);
                    redFlag = !redFlag;
                }
                rowStartRedFlag = !rowStartRedFlag;
            }
            // add mouse click listener
            this.addMouseListener(new MouseClickListener());
        }
        // *** mouse click listener
        private class MouseClickListener extends MouseAdapter {
            @Override
            public void mouseClicked(MouseEvent e) {
                Square square = getSquareAt(e.getX(), e.getY());
                String msg = square.isHighlighted() ? "Square is highlighted" : "Square is not highlighted";
                JOptionPane.showMessageDialog(null, msg);
            }
        }
        // ** override paint
        @Override
        public void paint(Graphics g) {
            draw ((Graphics2D) g);
        }
        // *** draw every square on the board
        public void draw(Graphics2D g) {
            for (int row = 0; row < squares.length; row++) {
                for (int column = 0; column < squares [row].length; column++) {
                    squares [row] [column].draw(g);
                }
            }
        }
        // *** get square given row and column
        public Square getSquare(int row, int column) {
            return squares [row] [column];
        }
        // *** get square from coords
        public Square getSquareAt(int x, int y) {
            int column = getColumnAtX(x);
            int row = getRowAtY(y);
            return squares [row] [column];
        }
        // *** get column # given x
        public int getColumnAtX(int x) {
            int column = x  / squareSize;
            return Math.min(Math.max(column, 0), 7);
       }
        // *** get row # given x
        public int getRowAtY(int y) {
            int row = y / squareSize;
            return Math.min(Math.max(row, 0), 7);
        }
        // ** get left x given column
        public int getLeftFromColumn(int column) {
            return column * squareSize;
        }
        // ** get top y give row
        public int getTopFromRow(int row) {
            return row * squareSize;
        }
        // *** get size of square side
        public int getSquareSize() {
            return squareSize;
        }
        private int squareSize = 25; // length of square side
        private Square [][] squares = new Square [8][8];
    }

    // *** Squalre represents one square on the board
    private class Square {
        // ** constructor creates the square
        public Square(Board board, int row, int column, boolean redFlag) {
            this.board = board;
            this.column = column;
            this.row = row;
            if (redFlag) {
                color = Color.RED;
                colorHighlighted = Color.PINK;
            } else {
                color = Color.BLACK;
                colorHighlighted = Color.LIGHT_GRAY;
            }
        }
        // ** set highlight flag
        public void setHighlighted(boolean value) {
            highlighted = value;
        }
        // *** see if square is highlighted
        public boolean isHighlighted() {
            return highlighted;
        }
        // *** draw the square
        public void draw(Graphics2D g) {
            Color fillColor = highlighted ? colorHighlighted : color;
            g.setColor(fillColor);
            int x = board.getLeftFromColumn(column);
            int y = board.getTopFromRow(row);
            int size = board.getSquareSize();
            g.fillRect(x, y, size, size);
        }
        private Board board;
        private Color color;
        private Color colorHighlighted;
        private int column;
        private boolean highlighted = false;
        private int row;
    }
}