最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)兩人五子棋游戲(四) 落子動(dòng)作的實(shí)現(xiàn)

 更新時(shí)間:2018年03月26日 16:39:02   作者:v_xchen_v  
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)兩人五子棋游戲,落子動(dòng)作的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

之前的兩篇文章:Java實(shí)現(xiàn)兩人五子棋游戲(二) 畫出棋盤;Java實(shí)現(xiàn)兩人五子棋游戲(三) 畫出棋子

前面,我們已經(jīng)畫好的棋盤和棋子,接下來,我們要通過鼠標(biāo)點(diǎn)擊屏幕獲取落子位置并落子(先不考慮行棋方和勝負(fù)判斷)。

步驟:

1)捕捉鼠標(biāo)按下的位置

2)經(jīng)過坐標(biāo)變換(由像素位置->0-19的棋盤位置)

3)更新記錄棋盤狀態(tài)的二維數(shù)組

4)重新渲染繪制棋盤。

-------------落子動(dòng)作代碼示例如下--------------

一個(gè)棋子類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<ROWS;i++) 
 { 
  g.drawLine(x, y+i*span_y, FWidth-x,y+i*span_y); 
 } 
 //畫豎線 
 for(int i=0;i<ROWS;i++) 
 { 
  g.drawLine(x+i*span_x, y, x+i*span_x,FHeight-y); 
 } 
  
 //畫棋子 
 for(int i=0;i<ROWS+1;i++) 
 { 
  for(int j=0;j<ROWS+1;j++) 
  { 
  if(chessStatus[i][j]!=null&&chessStatus[i][j].getPlaced()==true) 
  { 
   System.out.println("draw chessman "+i+" "+j); 
   int pos_x=x+i*span_x; 
   int pos_y=y+j*span_y; 
   float radius_b=40; 
   float radius_w=80; 
   float[] fractions = new float[]{0f,1f}; 
   java.awt.Color[] colors_b = new java.awt.Color[]{Color.BLACK,Color.WHITE}; 
   Color[] colors_w = new Color[]{Color.WHITE,Color.BLACK}; 
   RadialGradientPaint paint; 
   if(chessStatus[i][j].getColor()==1) 
   { 
   System.out.println("draw white chess"); 
   paint = new RadialGradientPaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, fractions, colors_w); 
   }else{ 
   System.out.println("draw black chess"); 
   paint = new RadialGradientPaint(pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, fractions, colors_b); 
   } 
   ((Graphics2D)g).setPaint(paint); 
   
   ((Graphics2D)g).fillOval(pos_x-chessman_width/2,pos_y-chessman_width/2,chessman_width,chessman_width); 
  } 
  } 
 } 
 } 
 
 @Override 
 //當(dāng)用戶按下鼠標(biāo)按鈕時(shí)發(fā)生 
 public void mousePressed(MouseEvent e) { 
 int point_x=e.getX(); 
 int point_y=e.getY(); 
  
 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; 
  
 System.out.println("press"); 
 int status_x = 0; 
 int status_y = 0; 
 if(point_x>=x && point_x<=x+imgWidth && point_y>=y && point_y <= y+imgHeight) 
 { 
  System.out.println("合法"); 
  for(int i=0;i<ROWS+1;i++) 
  { 
  if(point_x>=x-chessman_width/2+1+i*span_x) 
  { 
   if(point_x<=x+chessman_width/2-1+i*span_x)//如果是width/2會(huì)在中間點(diǎn)出現(xiàn)兩個(gè)匹配值 
   { 
   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<ROWS+1;i++) 
  { 
  if(point_y>=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 
 //當(dāng)用戶按下并松開鼠標(biāo)按鈕時(shí)發(fā)生 
 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標(biāo)題 
 setTitle("單機(jī)五子棋"); 
  
 Container containerPane =getContentPane(); 
 containerPane.add(drawChessBoard); 
 } 
 public static void main(String[] args) { 
 Main m = new Main(); 
 m.setSize(800, 800); 
 m.setVisible(true); 
 } 
} 

運(yùn)行一下

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之共享租車信息管理系統(tǒng)的實(shí)現(xiàn)

    Java畢業(yè)設(shè)計(jì)實(shí)戰(zhàn)之共享租車信息管理系統(tǒng)的實(shí)現(xiàn)

    這是一個(gè)使用了java+Jsp+Servlet+Jdbc+Mysql開發(fā)的共享租車信息管理系統(tǒng),是一個(gè)畢業(yè)設(shè)計(jì)的實(shí)戰(zhàn)練習(xí),具有租車管理該有的所有功能,感興趣的朋友快來看看吧
    2022-02-02
  • Java基礎(chǔ)之ClassLoader詳解

    Java基礎(chǔ)之ClassLoader詳解

    這篇文章主要介紹了Java基礎(chǔ)之ClassLoader詳解,文中對(duì)ClassLoader有非常詳細(xì)的解說,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • Java實(shí)現(xiàn)八個(gè)常用的排序算法:插入排序、冒泡排序、選擇排序、希爾排序等

    Java實(shí)現(xiàn)八個(gè)常用的排序算法:插入排序、冒泡排序、選擇排序、希爾排序等

    這篇文章主要介紹了Java如何實(shí)現(xiàn)八個(gè)常用的排序算法:插入排序、冒泡排序、選擇排序、希爾排序 、快速排序、歸并排序、堆排序和LST基數(shù)排序,需要的朋友可以參考下
    2015-07-07
  • IDEA項(xiàng)目使用SpringBoot+MyBatis-Plus的方法

    IDEA項(xiàng)目使用SpringBoot+MyBatis-Plus的方法

    這篇文章主要介紹了IDEA項(xiàng)目使用SpringBoot+MyBatis-Plus的方法,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 基于JDK動(dòng)態(tài)代理原理解析

    基于JDK動(dòng)態(tài)代理原理解析

    這篇文章主要介紹了基于JDK動(dòng)態(tài)代理原理解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Java之SpringCloudAlibaba Sentinel組件案例講解

    Java之SpringCloudAlibaba Sentinel組件案例講解

    這篇文章主要介紹了Java之SpringCloudAlibaba Sentinel組件案例講解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • spring?data?jpa查詢一個(gè)實(shí)體類的部分屬性方式

    spring?data?jpa查詢一個(gè)實(shí)體類的部分屬性方式

    這篇文章主要介紹了spring?data?jpa查詢一個(gè)實(shí)體類的部分屬性方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Spring Data分頁(yè)與排序的實(shí)現(xiàn)方法

    Spring Data分頁(yè)與排序的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于Spring Data分頁(yè)與排序的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • Spring手動(dòng)獲取bean的四種方式

    Spring手動(dòng)獲取bean的四種方式

    本文主要介紹了Spring手動(dòng)獲取bean的四種方式,包括BeanFactoryPostProcessor接口,ApplicationContextAware接口,注解 @PostConstruct 初始化時(shí)獲取,啟動(dòng)類ApplicationContext獲取這四種方法,感興趣的可以了解一下
    2024-01-01
  • MyBatis-plus批量插入的通用方法使用

    MyBatis-plus批量插入的通用方法使用

    mybatis-plus的IService接口默認(rèn)提供saveBatch批量插入,也是唯一一個(gè)默認(rèn)批量插入,在數(shù)據(jù)量不是很大的情況下可以直接使用,本文帶你詳細(xì)了解MyBatis-plus 批量插入的通用方法及使用方法,需要的朋友可以參考一下
    2023-04-04

最新評(píng)論

东安县| 昌吉市| 东乌珠穆沁旗| 天全县| 江达县| 松桃| 平乡县| 井陉县| 肇东市| 姚安县| 永吉县| 山东| 伊宁市| 汝州市| 濮阳市| 大荔县| 方城县| 长岭县| 定襄县| 化德县| 新津县| 西充县| 方山县| 云安县| 聊城市| 融水| 石河子市| 许昌县| 玛沁县| 昆山市| 浦北县| 师宗县| 民丰县| 惠州市| 威海市| 新安县| 营山县| 元朗区| 许昌县| 祁阳县| 西乌珠穆沁旗|