// 记录鼠标松开时的位置,计算其位于哪一个格子内,返回其序号(一个整数n) // 若鼠标并未位于任何格子内,重复直到输入有效 private int input() { boolean isPressing = false; while (true) { StdDraw.show(40); // 每40毫秒扫描一次鼠标状况 if (StdDraw.mousePressed() && !isPressing) { // 第一次按下鼠标 isPressing = true; continue; } if (!StdDraw.mousePressed() && isPressing) { // 第一次放开鼠标 isPressing = false; double x = StdDraw.mouseX(), y = StdDraw.mouseY(); if (y <= 5 || y >= 165 || x <= 5 || x >= 195) continue; int i, j; if (y >= 155) i = 0; else i = (int) ((155 - y) / 17.5); if (x <= 15) j = 0; else j = (int) ((x - 15) / 20); if (inCircle(x, y, i, j)) return i * 9 + j; if (i != 8 && inCircle(x, y, i + 1, j)) return (i + 1) * 9 + j; if (j != 8 && inCircle(x, y, i, j + 1)) return i * 9 + j + 1; if (i != 8 && j != 8 && inCircle(x, y, i + 1, j + 1)) return (i + 1) * 9 + j + 1; continue; } } }
/** Handles a mouse click, placing player's mark in the square on which the user clicks. */ public static void handleMouseClick(char[][] board, char player) { while (!StdDraw.mousePressed()) { // Wait for mouse press } double x = Math.round(StdDraw.mouseX()); double y = Math.round(StdDraw.mouseY()); while (StdDraw.mousePressed()) { // Wait for mouse release } int a = (int) x; int b = (int) y; if (board[a][b] == ' ') { board[a][b] = player; } }
/** * Waits for the user to click and returns the location where they clicked (which might be one of * Game.RESERVES_LOCATIONS). For invalid locations, may return null. */ public Location waitForClick() { while (!StdDraw.mousePressed()) { // Wait for mouse press } double x = StdDraw.mouseX(); double y = StdDraw.mouseY(); while (StdDraw.mousePressed()) { // Wait for mouse release } // This catches some clicks on the background as if they were on one of // the reserve buttons, but handles valid clicks correctly if (y <= 2.0) { if (x < 5) { return FocusModel.RESERVES_LOCATIONS[FocusModel.BLACK]; } return FocusModel.RESERVES_LOCATIONS[FocusModel.WHITE]; } Location result = new Location((int) (10.0 - y), (int) (x - 1.0)); if (model.isOnBoard(result)) { return result; } return null; }
public static void clicSouris() { int cooxV = 0; int cooyV = 0; int cooxH = 0; int cooyH = 0; double mouseX = 0; double mouseY = 0; if (StdDraw.mousePressed()) { mouseX = StdDraw.mouseX(); mouseY = StdDraw.mouseY(); if ((10 < mouseX && mouseX < 30) || (50 < mouseX && mouseX < 70) || (90 < mouseX && mouseX < 110) || (130 < mouseX && mouseX < 150) || (170 < mouseX && mouseX < 190) || (210 < mouseX && mouseX < 230)) { Test1.b = true; if ((10 < mouseX && mouseX < 30)) { cooxV = 0; } if ((50 < mouseX && mouseX < 70)) { cooxV = 1; } if ((90 < mouseX && mouseX < 110)) { cooxV = 2; } if ((130 < mouseX && mouseX < 150)) { cooxV = 3; } if ((170 < mouseX && mouseX < 190)) { cooxV = 4; } if ((210 < mouseX && mouseX < 230)) { cooxV = 5; } if ((20 < mouseY && mouseY < 60)) { cooyV = 0; } if ((60 < mouseY && mouseY < 100)) { cooyV = 1; } if ((100 < mouseY && mouseY < 140)) { cooyV = 2; } if ((140 < mouseY && mouseY < 180)) { cooyV = 3; } if ((180 < mouseY && mouseY < 220)) { cooyV = 4; } if ((220 < mouseY && mouseY < 260)) { cooyV = 5; } TabManager.verticale[cooyV][cooxV] = 1; } else { Test1.b = true; if ((30 < mouseX && mouseX < 50)) { cooxH = 0; } if ((70 < mouseX && mouseX < 90)) { cooxH = 1; } if ((110 < mouseX && mouseX < 130)) { cooxH = 2; } if ((150 < mouseX && mouseX < 170)) { cooxH = 3; } if ((190 < mouseX && mouseX < 210)) { cooxH = 4; } if ((0 < mouseY && mouseY < 40)) { cooyH = 0; } if ((40 < mouseY && mouseY < 80)) { cooyH = 1; } if ((80 < mouseY && mouseY < 120)) { cooyH = 2; } if ((120 < mouseY && mouseY < 160)) { cooyH = 3; } if ((160 < mouseY && mouseY < 200)) { cooyH = 4; } if ((200 < mouseY && mouseY < 240)) { cooyH = 5; } TabManager.horizontale[cooyH][cooxH] = 1; } } }