สองบทความก่อนหน้านี้: Java ใช้สองเกม Gozi (สอง) เพื่อวาดกระดานหมากรุก; Java ใช้เกม Gozi สองเกม (สาม) เพื่อวาดหมากรุก Java ใช้สองเกม Gozi (สี่) เพื่อตระหนักถึงการเคลื่อนไหว; Java ใช้สองเกม Gozi (หก) เพื่อแปลงด้านหมากรุกคุณสามารถคลิกเพื่อดู
ฟังก์ชั่นถัดไปที่เราต้องการนำไปใช้คือการแจ้งข้อมูลบนหน้าจอเมื่อมีลูกปัดติดต่อกันห้าตัว เพียงแค่ใช้ showmessagedialog ในส่วน Iswin ของฟังก์ชั่น mousepressed
รหัสฟังก์ชัน mousepressed มีดังนี้:
@Override // โมฆะสาธารณะ mousepressed (MouseEvent E) {int point_x = e.getx (); int point_y = e.gety (); int imgwidth = boardimg.getheight (นี่); int imgheight = boardimg.getWidth (นี่); 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_y = 0; if (point_x> = x && point_x <= x+imgwidth && point_y> = y && point_y <= y+imgheight) {//system.out.println("legal "); สำหรับ (int i = 0; i <rows+1; i ++) {ถ้า (point_x> = x-chessman_width/2+1+i*span_x) {ถ้า (point_x <= x+chessman_width/2-1+i*span_x) // "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)); สถานะ _x = i; }}} สำหรับ (int i = 0; i <rows+1; i ++) {ถ้า (point_y> = y-chessman_width/2+1+i*span_y) {ถ้า (point_y <= y+achessman_width/2-1+i*span_y) "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)); สถานะ _y = i; }}} if (chessstatus [status_x] [status_y] == null || chessstatus [status_x] [status_y] .getPlaced () == FALSE) {Chessman Chessman = ใหม่ Chessman (ChessColor, True); chessstatus [status_x] [status_y] = Chessman; System.out.println ("CHESS COLOR:"+ChessColor); if (ChessColor == Black) {ChessColor = White; } else {ChessColor = Black; } repaint (); // ถ้าชนะให้ข้อความแจ้งถ้า (iswin (status_x, status_y, chessstatus)) {system.out.println ("win !!!"); ผู้ชนะสตริง; // ถ้าเด็กคนต่อไปเป็นสีขาวคราวนี้มันเป็นสีดำถ้า (ChessColor == White) ผู้ชนะ = "Black"; ผู้ชนะอื่น = "ว่าง"; String mstring = string.format ("ขอแสดงความยินดี, %s win !!!", ผู้ชนะ); joptionpane.showmessagedialog (นี่, mstring); -เรียกใช้:
ณ จุดนี้เราได้ทำฟังก์ชั่น Gozi ขั้นพื้นฐานที่สุดแล้ว ฉันจะโพสต์รหัสทั้งหมดด้านล่าง:
Chessman.java
แพ็คเกจ xchen.test.simplegobang; หมากรุกระดับสาธารณะ {สี int ส่วนตัว; // 1-white, 0-black private boolean วาง = false; int matchCount = 1; หมากรุกสาธารณะ (สี int, วางบูลีน) {this.color = color; this.placed = วาง; } บูลีนสาธารณะ getPlaced () {return วาง; } โมฆะสาธารณะ setplaced (วางบูลีน) {this.placed = วาง; } public int getColor () {return color; } โมฆะสาธารณะ setColor (สี int) {this.color = color; - Main.java
แพ็คเกจ xchen.test.simplegobang; นำเข้า Java.awt.Container; นำเข้า Javax.swing.jframe; นำเข้า xchen.test.simplegobang.drawchessboard; ชั้นเรียนสาธารณะ Main ขยาย JFrame {Private Drawchessboard Drawchessboard; Public Main () {drawchessboard = new Drawchessboard (); // ชื่อเฟรม Settitle ("Stand-Alone Goji"); container containerPane = getContentPane (); containerpane.add (drawchessboard); } โมฆะคงที่สาธารณะหลัก (สตริง [] args) {main m = new main (); M.Setsize (800, 800); M.SetVisible (จริง); - drawchessboard.java
แพ็คเกจ xchen.test.simplegobang; นำเข้า Java.awt.Color; นำเข้า java.awt.graphics; นำเข้า java.awt.graphics2d; นำเข้า java.awt.image; นำเข้า Java.awt.RadialgradientPaint; นำเข้า java.awt.toolkit; นำเข้า java.awt.event.mousevent; นำเข้า java.awt.event.mouselistener; นำเข้า Javax.swing.joptionpane; นำเข้า Javax.swing.jpanel; Public Class Drawchessboard ขยาย JPanel ใช้ Mouselistener {สุดท้าย int คงที่สีดำ = 0; int คงสุดท้าย int สีขาว = 1; Public Int ChessColor = Black; int chessman_width = 30; ภาพสาธารณะ Boardimg; แถว int ส่วนตัวสุดท้าย = 19; Chessman [] [] chessstatus = ใหม่ Chessman [แถว+1] [แถว+1]; Public Drawchessboard () {boardimg = toolkit.getdefaulttoolkit (). getImage ("res/drawable/chessboard2.png"); if (boardimg == null) system.err.println ("png ไม่มีอยู่"); addmouselistener (นี่); } @Override Void PaintComponent (กราฟิก g) {// วิธีการที่สร้างขึ้นอัตโนมัติ todo stub super.paintComponent (G); int imgwidth = boardimg.getheight (นี่); int imgheight = boardimg.getWidth (นี่); 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); // วาดเส้นแนวนอนสำหรับ (int i = 0; i <rows; i ++) {g.drawline (x, y+i*span_y, fwidth-x, y+i*span_y); } // วาดเส้นแนวตั้งสำหรับ (int i = 0; i <rows; i ++) {g.drawline (x+i*span_x, y, x+i*span_x, fheight-y); } // วาดชิ้นหมากรุกสำหรับ (int i = 0; i <rows+1; i ++) {สำหรับ (int j = 0; j <rows+1; j ++) {ถ้า (chessstatus [i] [j]! = null && chessstatus [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 = ใหม่ java.awt.color [] {color.black, color.white}; สี [] colors_w = สีใหม่ [] {color.white, color.black}; สีรัศมี ถ้า (chessstatus [i] [j] .getColor () == 1) {//system.out.println("Draw White หมากรุก "); สี = ใหม่ radialgradientPaint (pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_w*2, เศษส่วน, colors_w); } else {//system.out.println("Draw Black Chess "); สี = ใหม่ radialgradientPaint (pos_x-chessman_width/2f, pos_y-chessman_width/2f, radius_b*2, เศษส่วน, colors_b); } ((graphics2d) g) .setPaint (สี); ((graphics2d) g) .filloval (pos_x-chessman_width/2, pos_y-chessman_width/2, chessman_width, chessman_width); }}}} @Override // โมฆะสาธารณะ mousepressed (MouseEvent E) {int point_x = e.getx (); int point_y = e.gety (); int imgwidth = boardimg.getheight (นี่); int imgheight = boardimg.getWidth (นี่); 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_y = 0; if (point_x> = x && point_x <= x+imgwidth && point_y> = y && point_y <= y+imgheight) {//system.out.println("legal "); สำหรับ (int i = 0; i <rows+1; i ++) {ถ้า (point_x> = x-chessman_width/2+1+i*span_x) {ถ้า (point_x <= x+chessman_width/2-1+i*span_x) // "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x)); สถานะ _x = i; }} สำหรับ (int i = 0; i <rows+1; i ++) {ถ้า (point_y> = y-chessman_width/2+1+i*span_y) {ถ้า (point_y <= y+achessman_width/2-1+i*span_y) "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)); สถานะ _y = i; }}} if (chessstatus [status_x] [status_y] == null || chessstatus [status_x] [status_y] .getPlaced () == FALSE) {Chessman Chessman = ใหม่ Chessman (ChessColor, True); chessstatus [status_x] [status_y] = Chessman; System.out.println ("CHESS COLOR:"+ChessColor); if (ChessColor == Black) {ChessColor = White; } else {ChessColor = Black; } repaint (); // ถ้าคุณชนะให้ข้อความแจ้งถ้า (iswin (status_x, status_y, chessstatus)) {system.out.println ("win !!!!"); ผู้ชนะสตริง; // ถ้าเด็กคนต่อไปเป็นสีขาวคราวนี้มันเป็นสีดำถ้า (ChessColor == White) ผู้ชนะ = "Black"; ผู้ชนะอื่น = "ขาว"; String mstring = string.format ("ขอแสดงความยินดี, %s win !!!", ผู้ชนะ); joptionpane.showmessagedialog (นี่, mstring); }}}} @Override // โมฆะสาธารณะ mouseclicked (MouseEvent E) {// toDo วิธีการที่สร้างอัตโนมัติ auto-generated} @Override โมฆะสาธารณะ mouserleased (MouseEvent e) {// toDo วิธีการที่สร้างขึ้นอัตโนมัติ @Override โมฆะสาธารณะ Mouseentered (MouseEvent E) {// วิธีการที่สร้างขึ้นอัตโนมัติ stub} @Override โมฆะสาธารณะ mouseexited (MouseEvent E) {// toDo วิธีการที่สร้างอัตโนมัติ {int i = 0) สำหรับ (int j = 0; j <rows+1; j ++) {// การค้นหาด้านยาวถ้า (chessstatus [i] [j]! = null && chessstatus [i] [j] .getplaced () == จริง) {int matchcolor = chessstatus [i] [j] // มองหา (int n = 1; n <= 4; n ++) {ถ้า ((i+n> = 0) && (i+n) <= แถว) {ถ้า (chessstatus [i+n] [j]! = null && chessstatus [i+n] [j] {chessstatus [i] [j] .MatchCount ++; System.out.println ("pos:"+i+""+j+"จำนวนขวา ++:"+(i+n)+""+j+"นับ:"+chessstatus [i] [j] .matchcount); ถ้า (chessstatus [i] [j] .matchCount == 5) {return true; }} else {break; }}} // มองหา (int n = 1; n <= 4; n ++) {ถ้า ((ใน> = 0) && (ใน) <= แถว) {ถ้า (chessstatus [ใน] [j]! = null && chessstatus [ใน] [j] chessstatus [i] [j] .MatchCount ++; System.out.println ("pos:"+i+""+j+""+"จำนวนซ้าย ++:"+(in)+""+j+"นับ:"+chessstatus [i] [j] .matchcount); ถ้า (chessstatus [i] [j] .matchCount == 5) {return true; }} else {ถ้า (chessstatus [ใน] [j]! = null) {chessstatus [i] [j] .matchcount = 1; } หยุดพัก; }}}} ChessStatus [i] [j] .MatchCount = 1; // จำนวนรีเฟรช}}} สำหรับ (int i = 0; i <rows+1; i ++) {สำหรับ (int j = 0; j <rows+1; j ++) if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getPlaced () == true) {int matchColor = chessStatus [i] [j] .getColor (); // มองลงมุมซ้ายบนคือแหล่งที่มาพิกัดทิศทางบวกของแกน y จะลงสำหรับ (int n = 1; n <= 4; n ++) {ถ้า ((j+n> = 0) && (j+n) <= แถว) { if (chessstatus [i] [j+n]! = null && chessstatus [i] [j+n] .getplaced () == true && chessstatus [i] [j+n] .getColor () == MatchColor) System.out.println ("pos:"+i+""+j+"count ++:"+(i)+""+(j+n)+"นับ:"+chessstatus [i] [j] .matchcount); ถ้า (chessstatus [i] [j] .matchCount == 5) {return true; }} else {break; }}} // ค้นหา (int n = 1; n <= 4; n ++) {ถ้า ((jn> = 0) && (jn) <= แถว) { if (chessstatus [i] [jn]! = null && chessstatus [i] [jn] .getPlaced () == true && chessstatus [i] [jn] .getColor () == MatchColor) System.out.println ("pos:"+i+""+j+""+"จำนวนซ้าย ++:"+(i)+""+(jn)+"นับ:"+chessstatus [i] [j] .matchcount); ถ้า (chessstatus [i] [j] .matchCount == 5) {return true; }} else {ถ้า (chessstatus [i] [jn]! = null) {chessstatus [i] [j] .matchcount = 1; } หยุดพัก; }}} ChessStatus [i] [j] .MatchCount = 1; // จำนวนการรีเฟรช}}} // ทิศทาง: ซ้ายบนและล่างขวาสำหรับ (int i = 0; i <rows+1; i ++) {สำหรับ (int j = 0; if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getPlaced () == true) {int matchColor = chessStatus [i] [j] .getColor (); // มองลงมุมบนซ้ายคือแหล่งที่มาพิกัดและทิศทางบวกของแกน y จะลงสำหรับ (int n = 1; n <= 4; n ++) {ถ้า ((jn> = 0) && (jn) <= แถว && (ใน)> = 0 && (ใน) <= แถว) { if (chessstatus [ใน] [jn]! = null && chessstatus [ใน] [jn] .getPlaced () == true && chessstatus [ใน] [jn] .getColor () == MatchColor) System.out.println ("pos:"+i+""+j+"up count ++:"+(in)+""+(jn)+"นับ:"+chessstatus [i] [j] .matchcount); ถ้า (chessstatus [i] [j] .matchCount == 5) {return true; }} else {break; }}} // ด้านล่างขวาสำหรับ (int n = 1; n <= 4; n ++) {ถ้า ((j+n> = 0) && (j+n) <= แถว && (i+n)> = 0 && (i+n) <= แถว) { if (chessstatus [i+n] [j+n]! = null && chessstatus [i+n] [j+n]. getplaced () == true && chessstatus [i+n] [j+n] .getColor () == MatchColor) System.out.println ("pos:"+i+""+j+""+"จำนวนซ้าย ++:"+(i+n)+""+(j+n)+"นับ:"+chessstatus [i] [j] .matchcount); ถ้า (chessstatus [i] [j] .matchCount == 5) {return true; }} else {ถ้า (chessstatus [i+n] [j+n]! = null) {chessstatus [i] [j] .matchCount = 1; } หยุดพัก; }}} ChessStatus [i] [j] .MatchCount = 1; // จำนวนรีเฟรช}}} // ทิศทาง: ล่างซ้ายขวาบนขวาสำหรับ (int i = 0; i <rows+1; i ++) {สำหรับ (int j = 0; j <rows+1; j ++) if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getPlaced () == true) {int matchColor = chessStatus [i] [j] .getColor (); // มองลงมุมบนซ้ายคือจุดกำเนิดพิกัดและทิศทางบวกของแกน y นั้นลงสำหรับ (int n = 1; n <= 4; n ++) {ถ้า ((j+n> = 0) && (j+n) <= แถว && (ใน)> = 0 && (ใน) if (chessstatus [ใน] [j+n]! = null && chessstatus [ใน] [j+n] .getplaced () == true && chessstatus [ใน] [j+n] .getColor () == MatchColor) System.out.println ("pos:"+i+""+j+"up count ++:"+(in)+""+(j+n)+"นับ:"+chessstatus [i] [j] .matchcount ++); ถ้า (chessstatus [i] [j] .matchCount == 5) {return true; }} else {break; }}} // ด้านบนขวาสำหรับ (int n = 1; n <= 4; n ++) {ถ้า ((jn> = 0) && (jn) <= แถว && (i+n)> = 0 && (i+n) <= แถว) { if (chessstatus [i+n] [jn]! = null && chessstatus [i+n] [jn] .getplaced () == true && chessstatus [i+n] [jn] .getColor () == MatchColor) System.out.println ("pos:"+i+""+j+""+"จำนวนซ้าย ++:"+(i+n)+""+(jn)+"นับ:"+chessstatus [i] [j] .matchcount); ถ้า (chessstatus [i] [j] .matchCount == 5) {return true; }} else {ถ้า (chessstatus [i+n] [jn]! = null) {chessstatus [i] [j] .matchcount = 1; } หยุดพัก; }}} ChessStatus [i] [j] .MatchCount = 1; // จำนวนรีเฟรช}}} ส่งคืนเท็จ; -สำหรับเกมที่น่าตื่นเต้นยิ่งขึ้นโปรดดูหัวข้อพิเศษ "Java Classic Games"
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น