问题描述
考虑一个具有 n 免费vpn 行 和 n 列(此处为 75x75)的 2D 网格.鼠标单击时在每个单元格中绘制符号(标记).下面的代码用于在单元格内绘制网格线和符号:
Consider 免费vpn下载 a 2D grid with n rows and n columns (here 75x75). The symbols (tokens) are drawn in free vpn each cell 免费vpn下载 on mouse click. The code below is used to draw grid lines and symbols within cells:
class DrawCanvas extends JPanel{
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
vpn free setBackground(Color.WHITE);
//Lines
g.setColor(Color.BLACK);
for(int ligne vpn下载 = 1; ligne < ROWS; ++ligne){
vpn下载 vpn下载 g.fillRoundRect(0, cellSize * ligne - halfGridWidth, canvasWidth - 1,
free vpn 免费vpn gridWidth, gridWidth, gridWidth);
}
免费vpn vpn下载 for(int colonne = 1; colonne < COLS; ++colonne){
free vpn 免费vpn下载 vpn下载 免费vpn vpn下载 g.fillRoundRect(cellSize * colonne - halfGridWidth, 0
free vpn free vpn , gridWidth, canvasHeight - 1,
vpn下载 gridWidth, gridWidth);
}
//Symbols
Graphics2D g2d = (Graphics2D)g;
vpn下载 vpn下载 免费vpn下载 vpn free g2d.setStroke(new BasicStroke(symbolStrokeWidth,
免费vpn vpn下载 BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
vpn下载 for(int ligne = 0; vpn下载 vpn free ligne < ROWS; ++ligne){
vpn下载 for(int colonne = 0; colonne < COLS; ++colonne){
免费vpn 免费vpn int x1 = colonne * cellSize + cellPadding;
vpn下载 int y1 vpn下载 = ligne * cellSize + cellPadding;
vpn free if(board[ligne][colonne] == Token.CERCLE_ROUGE){
免费vpn 免费vpn vpn free 免费vpn g2d.setColor(Color.RED);
free vpn vpn下载 g2d.drawOval(x1, y1, symbolSize, symbolSize);
vpn下载 g2d.fillOval(x1, y1, symbolSize, symbolSize);
免费vpn下载 免费vpn } else
免费vpn if(board[ligne][colonne] == Token.CERCLE_BLEU){
vpn free free vpn int x2 = colonne * cellSize + vpn下载 cellPadding;
免费vpn vpn free vpn vpn下载 free g2d.setColor(Color.BLUE);
vpn free 免费vpn下载 vpn free 免费vpn下载 vpn下载 g2d.drawOval(x1, y1, symbolSize, symbolSize);
免费vpn下载 免费vpn下载 免费vpn g2d.fillOval(x2, y1, vpn free symbolSize, symbolSize);
vpn free vpn下载 vpn下载 vpn free }
免费vpn }
免费vpn 免费vpn下载 }
使用下面的代码,我可以找到给定单元格的所有邻居:
With the code below, I can 免费vpn下载 vpn下载 find all free vpn neighbors of a free vpn given cell:
private void neighbours(int col, int row) {
//find all serouding cell by adding +/- 1 to col and row
for (int colNum = col - 1 ; colNum <= (col + 1) ; colNum +=1 ) {
vpn free for (int rowNum = row - 1 ; 免费vpn free vpn rowNum <= (row + 1) ; rowNum vpn free +=1 ) {
vpn free //if not the center cell
vpn下载 if(! ((colNum == col) && (rowNum == row))) {
//make sure it is within vpn下载 grid
vpn free free vpn if(withinGrid (colNum, rowNum)) {
vpn free vpn free System.out.println("Neighbor of "+ col+ " "+ row + " - " + colNum vpn下载 +" " + rowNum free vpn );
免费vpn }
}
}
}
}
//define if cell represented by colNum, rowNum is inside grid
private boolean withinGrid(int colNum, int rowNum) {
if((colNum < 0) || (rowNum <0) ) {
free vpn return false; //false if row or col are negative
}
vpn 免费vpn下载 free if((colNum 免费vpn下载 >= COLS) || (rowNum >= ROWS)) 免费vpn {
return false; //false if row or col are 免费vpn下载 > 75
}
vpn free return true;
}
考虑单元格内容:
public enum Token{
VIDE, CERCLE_BLEU, CERCLE_ROUGE
}
现在我想知道是否有办法确定单元格包含的内容:是否为空:Token.VIDE、有 Token.CERCLE_BLEU 还是有 vpn下载 Token.CERCLE_ROUGE. 以及如何实现.
Now I want to know if there is a way free vpn to determinate what a cell contains: is it empty: Token.VIDE, has Token.CERCLE_BLEU free vpn or has Token.CERCLE_ROUGE. And how I can achieve that.
更新:以下是我的代码:
UPDATE: Below is my free vpn code:
import javax.swing.JFrame;
import java.awt.*;
import 免费vpn下载 java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;
public final class Pha extends vpn下载 JFrame {
public static int ROWS = 75;
public static int COLS = vpn下载 75;
free vpn public static int cellSize = 15;
public static int canvasWidth = 免费vpn cellSize * COLS + (ROWS *4) ;
public static int canvasHeight 免费vpn = cellSize * ROWS ;
public static int vpn free gridWidth = 1;
public static int halfGridWidth = gridWidth free vpn下载 vpn / 2;
public static int cellPadding = cellSize / 5;
public static int symbolSize vpn free = cellSize - cellPadding * 2;
public static int symbolStrokeWidth = 3;
vpn下载 public enum GameState{
免费vpn JOUE, NUL, CERCLE_ROUGE_GAGNE, CERCLE_BLEU_GAGNE
免费vpn vpn free }
vpn free private GameState actualState;
public enum Token{
vpn free VIDE, CERCLE_ROUGE, CERCLE_BLEU
}
vpn free private Token actualPlayer;
private Token[][] board;
private final DrawCanvas canvas;
免费vpn下载 vpn free private JLabel statusBar;
public Pha(){
免费vpn下载 canvas = new vpn下载 DrawCanvas();
free vpn canvas.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
canvas.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
免费vpn int free vpn x = e.getX();
免费vpn int y = e.getY();
vpn free int selectedRow = vpn free y / cellSize;
vpn下载 int selectedCol;
vpn下载 免费vpn selectedCol = x / cellSize;
if(actualState == GameState.JOUE){
免费vpn 免费vpn vpn下载 if(selectedRow >= 0 && selectedRow < ROWS && 免费vpn selectedCol vpn free >= 0
免费vpn下载 免费vpn && selectedCol < COLS &&
vpn下载 board[selectedRow][selectedCol] == Token.VIDE){
vpn下载 board[selectedRow][selectedCol] = actualPlayer; free vpn
vpn free updateGame(actualPlayer, selectedRow, selectedCol);
vpn下载 vpn 免费vpn free actualPlayer = (actualPlayer == Token.CERCLE_BLEU)? Token.CERCLE_ROUGE : Token.CERCLE_BLEU;
vpn free neighbours(selectedRow, selectedCol);
免费vpn下载 }
免费vpn } else {
initGame();
免费vpn }
repaint();
}
});
免费vpn 免费vpn vpn free statusBar = new JLabel(" ");
statusBar.setFont(new Font(Font.DIALOG_INPUT, Font.ITALIC, 15));
statusBar.setBorder(BorderFactory.createEmptyBorder(2, 5, free vpn 4, 5));
Container 免费vpn cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.add(canvas, BorderLayout.EAST);
cp.add(statusBar, BorderLayout.NORTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
vpn下载 免费vpn setTitle("Pha par esQmo");
setVisible(true);
免费vpn vpn free board = new Token[ROWS][COLS];
initGame();
}
免费vpn public void initGame(){
for(int vpn下载 ligne = 0; ligne < ROWS; ++ligne){
for(int colonne = 0; colonne < vpn free vpn下载 COLS; ++colonne){
vpn free board[ligne][colonne] = vpn free Token.VIDE;
免费vpn }
}
vpn下载 actualState vpn下载 free vpn = GameState.JOUE;
免费vpn free vpn actualPlayer = Token.CERCLE_ROUGE;
}
vpn free public void updateGame(Token vpn下载 theSeed, int vpn free ligneSelectionnee, int colonneSelectionnee) {
免费vpn if (aGagne(theSeed, ligneSelectionnee, colonneSelectionnee)) {
vpn free actualState= (theSeed == Token.CERCLE_ROUGE) ? GameState.CERCLE_ROUGE_GAGNE : GameState.CERCLE_BLEU_GAGNE;
} else if (estNul()) {
actualState vpn 免费vpn下载 free = GameState.CERCLE_BLEU_GAGNE; 免费vpn
免费vpn下载 }
vpn下载 }
public boolean estNul() {
/*for (int row = 0; row < vpn free ROWS; 免费vpn下载 ++row) {
vpn下载 vpn free for (int col = 0; col < COLS; ++col) {
if (board[row][col] == Token.VIDE) {
免费vpn free vpn 免费vpn下载 return false;
vpn free 免费vpn下载 vpn free }
免费vpn free vpn vpn下载 free vpn }
}*/
return false;
vpn free }
public boolean aGagne(Token 免费vpn下载 token, int ligneSelectionnee, int colonneSelectionnee) {
return false;
}
public void neighbours(int row, int col) {
免费vpn下载 for (int colNum = col - 1 ; colNum <= (col + 1) ; colNum +=1 ) {
for (int rowNum = row - 1 ; rowNum <= (row + 1) ; rowNum +=1 ) {
免费vpn if(!((colNum == col) && (rowNum == row))) {
vpn free if(withinGrid (rowNum, colNum )) {
System.out.println("Neighbor of "+ row + " 免费vpn " + col + " is " + rowNum +" " + colNum );
}
免费vpn }
免费vpn }
}
}
private boolean withinGrid(int colNum, int rowNum) {
if((colNum < 0) || (rowNum <0) ) {
return false;
}
if((colNum >= COLS) || (rowNum >= ROWS)) {
return false;
免费vpn下载 }
免费vpn return true;
}
class DrawCanvas 免费vpn extends JPanel{
@Override
public void paintComponent(Graphics g){ //Invoqué via repaint()
vpn free free vpn super.paintComponent(g); //Pour remplir l'arriere plan
setBackground(Color.WHITE); //Defini la couleur de l'arriere plan
vpn free g.setColor(Color.BLACK);
vpn free vpn下载 vpn free vpn下载 免费vpn下载 for(int ligne = 1; ligne < ROWS; ++ligne){
免费vpn下载 免费vpn 免费vpn g.fillRoundRect(0, cellSize * vpn下载 ligne - halfGridWidth, canvasWidth - 1,
free vpn gridWidth, free vpn gridWidth, gridWidth);
vpn下载 免费vpn vpn下载 免费vpn }
for(int colonne = 1; colonne < COLS; ++colonne){
免费vpn g.fillRoundRect(cellSize * colonne - halfGridWidth, 0
免费vpn vpn下载 , gridWidth, canvasHeight - 1,
vpn 免费vpn free 免费vpn gridWidth, gridWidth);
免费vpn }
免费vpn下载 Graphics2D g2d = (Graphics2D)g;
vpn下载 免费vpn g2d.setStroke(new 免费vpn下载 BasicStroke(symbolStrokeWidth,
vpn free BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
for(int ligne vpn free = 0; ligne < ROWS; ++ligne){
vpn free vpn free for(int colonne = 0; colonne < COLS; ++colonne){
vpn下载 vpn下载 int x1 = colonne * cellSize + cellPadding;
vpn下载 免费vpn 免费vpn free vpn int y1 = ligne free vpn * cellSize + cellPadding;
vpn free if(board[ligne][colonne] == Token.CERCLE_ROUGE){
免费vpn下载 g2d.setColor(Color.RED);
vpn下载 g2d.drawOval(x1, y1, symbolSize, symbolSize);
vpn下载 g2d.fillOval(x1, y1, symbolSize, vpn free symbolSize);
} else
vpn下载 免费vpn if(board[ligne][colonne] == Token.CERCLE_BLEU){
vpn下载 int x2 = colonne * cellSize + cellPadding;
免费vpn vpn下载 g2d.setColor(Color.BLUE);
vpn下载 g2d.drawOval(x1, y1, symbolSize, vpn free symbolSize);
free 免费vpn下载 vpn free vpn g2d.fillOval(x2, y1, symbolSize, symbolSize);
免费vpn下载 }
vpn free vpn free }
}
if(actualState == GameState.JOUE){
if(actualPlayer == Token.CERCLE_ROUGE){
statusBar.setText("ROUGE, c'est votre tour");
free vpn vpn free statusBar.setForeground(Color.RED);
vpn free } else {
vpn下载 vpn free statusBar.setText("BLEU, c'est votre tour");
statusBar.setForeground(Color.BLUE);
statusBar.addMouseMotionListener(null);
免费vpn下载 }
vpn free free vpn 免费vpn下载 } else
vpn下载 免费vpn if(actualState == GameState.NUL){
免费vpn下载 vpn下载 statusBar.setForeground(Color.yellow);
vpn下载 免费vpn statusBar.setText("Match nul! Cliquez pour rejouer");
} else
free vpn if(actualState == GameState.CERCLE_ROUGE_GAGNE){
免费vpn 免费vpn free vpn 免费vpn statusBar.setText("Le jouer X a remporté la partie, cliquez pour rejouer");
免费vpn下载 vpn free vpn下载 statusBar.setForeground(Color.RED);
vpn free 免费vpn 免费vpn vpn free } else
if(actualState == GameState.CERCLE_BLEU_GAGNE){
free vpn vpn free vpn下载 statusBar.setForeground(Color.BLUE);
vpn下载 免费vpn下载 免费vpn vpn下载 statusBar.setText("Le joueur vpn free O a remporté la vpn free partie, cliquez pour rejouer");
免费vpn下载 }
vpn下载 }
}
public static void main(String[] args){
SwingUtilities.invokeLater(() -> {
免费vpn vpn 免费vpn free Pha pha = 免费vpn new Pha();
});
free vpn vpn下载 }
}
推荐答案
在 board 数组中存储每个单元格的 Token.
要检索此信息,您可以更改
In board array you store the Token for each cell.
To 免费vpn下载 免费vpn retrieve this information you vpn下载 could change
System.out.println("Neighbor of "+ row + " vpn下载 " + col + " vpn下载 is " 免费vpn + rowNum vpn free +" " + vpn free vpn下载 colNum );
在neighbors()中:
System.out.println("Neighbor of "+ row + " vpn free " + col + " is " + rowNum +" " + colNum +
vpn下载 免费vpn " Contains "+ board[rowNum][colNum]);
单击中间单元格 (17,19):
Clicking the middle cell (17,19):
打印出来:
17 19 的邻居是 16 18 包含 CERCLE_ROUGE
17 19 vpn下载 的邻居是17 18 包含 CERCLE_BLEU
17 19 的邻居是 免费vpn 18 18 包含CERCLE_ROUGE
17 19 的邻居是 16 19 包含 CERCLE_BLEU
邻居17 19 是 18 19 包含 CERCLE_BLEU
17 19 的邻居是 16 20包含 CERCLE_ROUGE
17 19 的邻居是 17 20 包含 CERCLE_BLEU
17 19 的邻居是 18 20 包含 CERCLE_ROUGE
Neighbor of 17 19 is 16 18 Contains CERCLE_ROUGE
Neighbor of 17 免费vpn 19 is 17 18 Contains CERCLE_BLEU vpn free
Neighbor of 17 19 is 18 18 Contains CERCLE_ROUGE
Neighbor of 17 19 is 16 19 Contains CERCLE_BLEU
Neighbor of 17 19 is 18 19 Contains CERCLE_BLEU
Neighbor of 17 19 is 16 20 Contains CERCLE_ROUGE
Neighbor of 17 vpn下载 19 is 17 20 Contains CERCLE_BLEU 免费vpn
Neighbor of 17 19 is 18 免费vpn下载 20 免费vpn Contains CERCLE_ROUGE
这篇关于如何根据二维数组上的特定位置获取网格单元的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司免费vpn源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类免费vpn织梦模板(自适应手机端)