可点击的Checkerboard applet

时间:2015-02-27 07:53:10

标签: java

目前正试图纠正一些可点击的棋盘小程序的错误(可能是由于我对某些概念的误解)。

  • 使用数组指示是否选中了棋盘格的方块(1 =选中,0 =未选中)并使用鼠标单击侦听器
  • 如果鼠标单击(选中),则在方块上添加白色边框,如果再次单击则不带边框,或者如果单击另一个正方形则保留边框
  • 注意排除坐标(0,0),因为我不想在开头选择任何方格,如果我包括(0,0)
  • 就是这种情况

错误: - 数组值已更改,但重绘不符合预期,边框/没有边框 - 如果我选择另一个选定方格旁边的方格,则另一个方格的选择将消失

对我如何向前推进的建议将不胜感激。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class ClickableCheckerboard extends Checkerboard implements MouseListener {


    @Override
    public void init() {
        initSelectionRecord();
        addMouseListener(this);
    }

    private static int 
            selectedRow, selectedCol,       // row and col of checkerboard
            x, y;                        // coordinates of a mouse click

//  Selection state of each square of the Checkerboard board.
    private static int selectionRecord[][];   // 8 x 8 array (0 to 7, row x col), 0 = not selected, 1 = selected

//  Initialise selection state of each square of the Checkerboard board.
    public void initSelectionRecord() {
        int row, col;
        selectionRecord = new int[8][8];        // Allocate memory for the array, otherwise end up with NullPointerException
        for (row = 0; row < 8; row++) {
            for (col = 0; col < 8; col++) {
                selectionRecord[row][col] = 0;
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {

        x = e.getX();
        y = e.getY();
        selectedRow = getRow(y);
        selectedCol = getCol(x);

        if (selectedRow != -1 && selectedCol != -1) {
            repaint(selectedCol * 20, selectedRow * 20, 19, 19);
            repaint(0, 161, 300, 300);
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }


    /**
     * Returns the column of the checkerboard for an integer x coordinate.
     *
     * @param x
     * @return col if inside checkerboard, or -1 if outside checkerboard
     */
    public int getCol(int x) {
        int col = -1;
        if (x <= 160 && x != 0) {
            col = (int) (x / 20);
        } 
        return col;
    }

    /**
     * Returns the row of the checkerboard for an integer y coordinate.
     *
     * @param y
     * @return row if inside checkerboard, or -1 if outside checkerboard
     */
    public int getRow(int y) {
        int row = -1;
        if (y <= 160 && y != 0) {
            row = (int) (y / 20);
        } 
        return row;
    }


    /**
     * Returns the selection state of a square of the checkerboard
     *
     * @param row
     * @param col
     * @return i - 1 if selected, 0 if unselected, -1 if not applicable
     */
    public static int getSelectionRecord(int row, int col) {
        int i = -1;
        if (row < 8 && col < 8){
            i = selectionRecord[row][col];
        }
        return i;
    }

    /**
     * Select a square by adding a white border to it, or unselect it by clearing the border 
     *
     * @param row
     * @param col
     * @param g
     */
    public void toggleSelection(int row, int col, Graphics g) {

        int evenRow, evenCol, width, height;

        evenRow = row % 2;
        evenCol = col % 2;
        width = 19;
        height = 19;

        if (getSelectionRecord(row, col) == 0) {
            selectionRecord[row][col] = 1;
            g.setColor(Color.white);
            g.drawRect(col * 20 , row * 20 , width, height);
        } else if (getSelectionRecord(row, col) == 1) {
            selectionRecord[row][col] = 0;
            if (evenRow == 0 && evenCol == 0) {
                g.setColor(Color.red);
                g.fillRect(col * 20, row * 20, width, height);
            } else if (evenRow != 0 && evenCol != 0) {
                g.setColor(Color.red);
                g.fillRect(col * 20, row * 20, width, height);
            } else if (evenRow == 0 && evenCol != 0) {
                g.setColor(Color.black);
                g.fillRect(col * 20, row * 20, width, height);
            } else if (evenRow != 0 && evenCol == 0) {
                g.setColor(Color.black);
                g.fillRect(col * 20, row * 20, width, height);
            }
        } else if (getSelectionRecord(row, col) == -1) {

        }

    }


    public void paint(Graphics g) {

        super.paint(g);

        g.drawString("x is " + x + ". y is " + y + ".", 2, 180);

        if (x != 0 && y != 0 && x <=160 && y <= 160) {
            if (selectedRow != -1 && selectedCol != -1) {
                toggleSelection(selectedRow, selectedCol, g);  
            }
        }

        g.drawString("Selection state of toggled square is " + getSelectionRecord(selectedRow, selectedCol), 2, 200);

        int i, j;
        for (i = 0; i < 8; i++) {
            for (j = 0; j < 8; j++) {
                g.drawString(getSelectionRecord(i, j) + " ", (2 + j * 20), (220 + i * 20));
            }
        }

    }

}

0 个答案:

没有答案