Java实现两人五子棋游戏(四)落子动作的实现

之前的两篇文章:Java实现两人五子棋游戏(二) 画出棋盘;Java实现两人五子棋游戏(三) 画出棋子

我们提供的服务有:网站建设、网站设计、微信公众号开发、网站优化、网站认证、花溪ssl等。为1000多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的花溪网站制作公司

Java实现两人五子棋游戏(四) 落子动作的实现

前面,我们已经画好的棋盘和棋子,接下来,我们要通过鼠标点击屏幕获取落子位置并落子(先不考虑行棋方和胜负判断)。

步骤:

1)捕捉鼠标按下的位置

2)经过坐标变换(由像素位置->0-19的棋盘位置)

3)更新记录棋盘状态的二维数组

4)重新渲染绘制棋盘。

-------------落子动作代码示例如下--------------

Java实现两人五子棋游戏(四) 落子动作的实现

一个棋子类Chessman.java

package xchen.test.simpleGobang; 
 
public class Chessman { 
 private int color;//1-white,0-black 
 private boolean placed = false; 
 
 public Chessman(int color,boolean placed){ 
 this.color=color; 
 this.placed=placed; 
 } 
 
 public boolean getPlaced() { 
 return placed; 
 } 
 
 public void setPlaced(boolean placed) { 
 this.placed = placed; 
 } 
 
 public int getColor() { 
 return color; 
 } 
 
 public void setColor(int color) { 
 this.color = color; 
 } 
} 

DrawChessBoard.java

package xchen.test.simpleGobang; 
 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.RadialGradientPaint; 
import java.awt.Image; 
import java.awt.Toolkit; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.Color; 
import javax.swing.JPanel; 
 
 
public class DrawChessBoard extends JPanel implements MouseListener{ 
 final static int BLACK=0; 
 final static int WHITE=1; 
 public int chessColor = BLACK; 
 int chessman_width=30; 
 
 public Image boardImg; 
 final private int ROWS = 19; 
 Chessman[][] chessStatus=new Chessman[ROWS+1][ROWS+1]; 
 
 public DrawChessBoard() { 
 boardImg = Toolkit.getDefaultToolkit().getImage("res/drawable/chessboard2.png"); 
 if(boardImg == null) 
  System.err.println("png do not exist"); 
  
 addMouseListener(this); 
 } 
 @Override 
 protected void paintComponent(Graphics g) { 
 // TODO Auto-generated method stub 
 System.out.println("DRAW!!"); 
 super.paintComponent(g); 
  
 int imgWidth = boardImg.getHeight(this); 
 int imgHeight = boardImg.getWidth(this); 
 int FWidth = getWidth(); 
 int FHeight= getHeight(); 
  
 int x=(FWidth-imgWidth)/2; 
 int y=(FHeight-imgHeight)/2; 
  
 int span_x=imgWidth/ROWS; 
 int span_y=imgHeight/ROWS; 
  
 g.drawImage(boardImg, x, y, null); 
  
 //画横线 
 for(int i=0;i=x && point_x<=x+imgWidth && point_y>=y && point_y <= y+imgHeight) 
 { 
  System.out.println("合法"); 
  for(int i=0;i=x-chessman_width/2+1+i*span_x) 
  { 
   if(point_x<=x+chessman_width/2-1+i*span_x)//如果是width/2会在中间点出现两个匹配值 
   { 
   System.out.println("point x "+i+" "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)); 
   status_x = i; 
   } 
  } 
  } 
  for(int i=0;i=y-chessman_width/2+1+i*span_y) 
  { 
   if(point_y <= y+chessman_width/2-1+i*span_y) 
   { 
   System.out.println("point y "+i+" "+point_y+" "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)); 
   status_y = i; 
   } 
  } 
  } 
  Chessman chessman = new Chessman(BLACK, true); 
  chessStatus[status_x][status_y]=chessman; 
  repaint(); 
 } 
 System.out.println(status_x+" "+status_y+" "+chessStatus[status_x][status_y].getColor()+" "+chessStatus[status_x][status_y].getPlaced()); 
 } 
 @Override 
 //当用户按下并松开鼠标按钮时发生 
 public void mouseClicked(MouseEvent e) { 
 // TODO Auto-generated method stub 
 } 
 @Override 
 public void mouseReleased(MouseEvent e) { 
 // TODO Auto-generated method stub 
  
 } 
 @Override 
 public void mouseEntered(MouseEvent e) { 
 // TODO Auto-generated method stub 
  
 } 
 @Override 
 public void mouseExited(MouseEvent e) { 
 // TODO Auto-generated method stub 
 } 
} 

主模块代码不变

package xchen.test.simpleGobang; 
 
import java.awt.Container; 
import javax.swing.JFrame; 
 
import xchen.test.simpleGobang.DrawChessBoard; 
 
public class Main extends JFrame{ 
 private DrawChessBoard drawChessBoard; 
 public Main() { 
 drawChessBoard = new DrawChessBoard(); 
  
 //Frame标题 
 setTitle("单机五子棋"); 
  
 Container containerPane =getContentPane(); 
 containerPane.add(drawChessBoard); 
 } 
 public static void main(String[] args) { 
 Main m = new Main(); 
 m.setSize(800, 800); 
 m.setVisible(true); 
 } 
} 

运行一下

Java实现两人五子棋游戏(四) 落子动作的实现

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。


网页题目:Java实现两人五子棋游戏(四)落子动作的实现
文章链接:http://scyanting.com/article/ipopdp.html