두 개의 이전 기사 : Java는 체스 보드를 그리는 두 개의 Gozi 게임 (2)을 구현합니다. Java는 체스 조각을 그리기 위해 두 개의 Gozi 게임 (3)을 구현합니다. Java는 2 개의 Gozi 게임 (4)을 구현하여 움직임을 실현합니다. Java는 2 개의 Gozi 게임 (6)을 구현하여 체스 쪽을 변형 시키려면 클릭하여 볼 수 있습니다.
우리가 구현하려는 다음 기능은 5 개의 연속 구슬이있을 때 화면에 정보를 프롬프트하는 것입니다. 무소한 기능의 iswin 부분에서 showmessagedialog를 사용하십시오.
근절 된 기능 코드는 다음과 같습니다.
@override // 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("legal "); 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) // 두 개의 일치 값은 중간 지점에서 나타납니다. "+point_x+" "+(x-chessman_width/2+i*span_x)+"+(x+chessman_width/2+i*span_x); 상태 _x = i; }}} for (int i = 0; i <rows+1; i ++) {if (point_y> = ychessman_width/2+i*span_y) {if (point_y <= y+chessman_width/2-1+i*span_y) {//system.out.println y "+i+"+i+""+i+" "+(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 = New Chessman (Chesscolor, True); Chessstatus [status_x] [status_y] = Chessman; System.out.println ( "체스 컬러 :"+chesscolor); if (chesscolor == black) {chesscolor = 흰색; } else {chesscolor = 검은 색; } Repaint (); // 이기면 (iswin (status_x, status_y, chessstatus)) {system.out.println ( "win !!!"); 문자열 수상자; // 다음 아이가 흰색 인 경우, 이번에는 검은 색 (chesscolor == white) winner = "black"; else winner = "blank"; 문자열 mstring = string.format ( "축하, %s win !!!", 승자); joptionpane.showmessagedialog (this, mstring); }}}}실행 :
이 시점에서 우리는 가장 기본적인 고지 기능을 완료했습니다. 아래에 전체 코드를 게시하겠습니다.
Chessman.java
패키지 xchen.test.simplegobang; 공개 클래스 Chessman {Private Int Color; // 1- 백인, 0- 블랙 개인 부울 = 거짓; int matchcount = 1; Public Chessman (int color, 부울 배치) {this.color = color; this.placed = 배치; } public boolean getPlaced () {반환 배치; } public void setplaced (부울 배치) {this.placed = 배치; } public int getColor () {return color; } public void setColor (int color) {this.color = color; }} Main.java
패키지 xchen.test.simplegobang; import java.awt.container; import javax.swing.jframe; xchen.test.simplegobang.drawchessboard 가져 오기; Public Class Main은 Jframe {Private DrawChessboard DrawChessboard; public main () {drawchessboard = New DrawChessboard (); // 프레임 제목 settitle ( "독립형 고지"); 컨테이너 containerpane = getContentPane (); ContainerPane.add (DrawChessboard); } public static void main (String [] args) {main m = new Main (); M.SetSize (800, 800); M.setvisible (true); }} Drawchessboard.java
패키지 xchen.test.simplegobang; import java.awt.color; java.awt.graphics 가져 오기; import java.awt.graphics2d; import java.awt.image; import java.awt.radialgradientPaint; java.awt.toolkit import; java.awt.event.mouseevent import; import java.awt.event.mouselistener; import javax.swing.joptionpane; import javax.swing.jpanel; Public Class DrawChessboard 확장 jpanel 구현 MousElistener {Final Static int black = 0; 최종 정적 int 화이트 = 1; 공개 int chesscolor = 검은 색; int chessman_width = 30; 공개 이미지 Boardimg; 최종 개인 int 행 = 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는 존재하지 않습니다"); AddMousElistener (this); } @Override Protected Void PaintComponent (Graphics G) {// TODO 자동 생성 메소드 스터브 SUPER.PAINTCOMPONTE (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); // (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 ++) {for (int j = 0; j <rows+1; j ++) {if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getplaced () == true) {//system.out.println * "+j"+j "+j"+j "+j"+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}; 방사형 그레이드 페인트 페인트; 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, 분수, 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, 분수, colors_b); } ((Graphics2d) g) .setPaint (페인트); ((Graphics2d) g) .filloval (pos_x-chessman_width/2, pos_y-chessman_width/2, chessman_width, chessman_width); }}}} @override // 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("legal "); 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) // 두 개의 일치 값은 중간 지점에서 나타납니다. "+point_x+" "+(x-chessman_width/2+i*span_x)+"+(x+chessman_width/2+i*span_x); 상태 _x = i; }} for (int i = 0; i <rows+1; i ++) {if (point_y> = ychessman_width/2+1+i*span_y) {if (point_y <= y+chessman_width/2-1+i*span_y) {//system.out.printlne y "+i+"+I+" "+(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 = New Chessman (Chesscolor, True); Chessstatus [status_x] [status_y] = Chessman; System.out.println ( "체스 컬러 :"+chesscolor); if (chesscolor == black) {chesscolor = 흰색; } else {chesscolor = 검은 색; } Repaint (); // 이기면 (iswin (status_x, status_y, chessstatus)) {system.out.println ( "win !!!!"); 문자열 수상자; // 다음 아이가 흰색 인 경우, 이번에는 검은 색 (chesscolor == white) winner = "black"; else winner = "화이트"; 문자열 mstring = string.format ( "축하, %s win !!!", 승자); joptionpane.showmessagedialog (this, mstring); }}}} @override // public void mouseclicked (mouseevent e) {// todo 자동 생성 메소드 스터브} @override public void mouserEleled (mouseevent e) {// to auto-auto-void mouseReLeased (mouseev e) {mouseev e) {// to-greented method e) {// @override public void mouseentered (mouseevent e) {// todo auto-auto-kenerated method stub} @override public void mouseexited (mouseevent e) {// a auto-rened method stub} boolean iswin (int point_x, int point_y, chessman [] cm) {int i = 0; i <rows+). {for (int j = 0; j <Rows+1; // (int n = 1; n <= 4; n ++) {if ((i+n> = 0) && (i+n) <= 행) {if (chessstatus [i+n] [j]! = null && chessstatus [i+n] [j] .getplaced () == true && chessstatus [i+n] [J]. {chessstatus [i] [j] .matchcount ++; System.out.println ( "pos :"+i+""+j+"오른쪽 count ++ :"+(i+n)+""+j+"count :"+chessstatus [i] [j] .matchcount); if (chessstatus [i] [j] .matchcount == 5) {return true; }} else {break; }}} // (int n = 1; n <= 4; n ++) {if (in> = 0) && (in) <= 행) {if (chessstatus [in] [j]! = null && chessstatus [in] [j] .getplaced () == true && chessstatus [j]. Chessstatus [i] [J] .MatchCount ++; System.out.println ( "pos :"+i+""+j+""+"left count ++ :"+(in)+""+j+"count :"+Chessstatus [i] [j] .matchcount); if (chessstatus [i] [j] .matchcount == 5) {return true; }} else {if (chessstatus [in] [j]! = null) {chessstatus [i] [j] .matchcount = 1; } 부서지다; }}}}} chessstatus [i] [j] .matchCount = 1; // Crofresh Count}} for (int i = 0; i <rows+1; i ++) {for (int j = 0; j <rows+1; j ++) {// perpendical if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getPlaced () == true) {int matchcolor = chessstatus [i] [j] .getColor (); // 왼쪽 상단 코너는 좌표 원점이며, y 축의 양의 방향은 (int n = 1; n <= 4; n ++) {if ((j+n> = 0) && (j+n) <= rows) { if (chessstatus [i] [j+n]! = null && chessstatus [i] [j+n] .getPlaced () == true && chessstatus [i] [j+n] .getColor () == matchcolor) {chessstatus [i] [j] .matchcount ++; System.out.println ( "pos :"+i+""+j+"up count ++ :"+(i)+"+(j+n)+"count : "+chessstatus [i] [j] .matchcount); if (chessstatus [i] [j] .matchcount == 5) {return true; }} else {break; }}} // (int n = 1; n <= 4; n ++) {if ((jn> = 0) && (jn) <= rows) { if (chessstatus [i] [jn]! = null && chessstatus [i] [jn] .getPlaced () == true && chessstatus [i] [jn] .getColor () == matchcolor) {chessstatus [i] [j] .matchcount ++; System.out.println ( "pos :"+i+""+j+""+"왼쪽 count ++ :"+(i) "+(jn)+"count : "+chessstatus [i] [j] .matchcount); if (chessstatus [i] [j] .matchcount == 5) {return true; }} else {if (chessstatus [i] [jn]! = 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 ++) {if ((jn> = 0) && (jn) <= rows && (in)> = 0 && (in) <= rows) { if (chessstatus [in] [jn]! = null && chessstatus [in] [jn] .getPlaced () == true && chessstatus [in] [jn] .getColor () == matchcolor) {chessstatus [i] [j] .matchcount ++; System.out.println ( "pos :"+i+""+j+"up count ++ :"+(in)+"+(jn)+"count : "+chessstatus [i] [j] .matchcount); if (chessstatus [i] [j] .matchcount == 5) {return true; }} else {break; }}} // (int n = 1; n <= 4; n ++) {if ((j+n> = 0) && (j+n) <= rows && (i+n)> = 0 && (i+n) <= rows) if (chessstatus [i+n] [j+n]! = null && chessstatus [i+n] [j+n] .getPlaced () == true && chessstatus [i+n] [j+n] .getColor () == matchcolor) {chessstatus [i] [j] .matchcount ++; System.out.println ( "pos :"+i+""+j+""+"왼쪽 count ++ :"+(i+n)+"+(j+n)+"count : "+chessstatus [i] [j] .matchcount); if (chessstatus [i] [j] .matchcount == 5) {return true; }} else {if (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; if (chessstatus [i] [j]! = null && chessstatus [i] [j] .getPlaced () == true) {int matchcolor = chessstatus [i] [j] .getColor (); // 왼쪽 상단 모서리는 좌표 원점이고 y 축의 양의 방향은 (int n = 1; n <= 4; n ++) {if ((j+n> = 0) & (j+n) <= rows && (in)> = 0 && (in) <= rows) { if (chessstatus [in] [j+n]! = null && chessstatus [in]. System.out.println ( "pos :"+i+""+j+"up count ++ :"+(in)+"+(j+n)+"count : "+chessstatus [i] [j] .matchcount ++); if (chessstatus [i] [j] .matchcount == 5) {return true; }} else {break; }}} // (int n = 1; n <= 4; n ++) {if ((jn> = 0) && (jn) <= rows && (i+n)> = 0 && (i+n) <= rows) { if (chessstatus [i+n] [jn]! = null && chessstatus [i+n] [jn] .getplaced () == true && chessstatus [i+n] [jn] .getColor () == mutcolor) {chessstatus [i] [j] .matchcount ++; System.out.println ( "pos :"+i+""+j+""+"왼쪽 count ++ :"+(i+n)+"+(jn)+"count : "+chessstatus [i] [j] .matchcount); if (chessstatus [i] [j] .matchcount == 5) {return true; }} else {if (chessstatus [i+n] [jn]! = null) {chessstatus [i] [j] .matchcount = 1; } 부서지다; }}} chessstatus [i] [j] .MatchCount = 1; // Comper Count}}} return false; }}더 흥미로운 게임은 특별한 주제 "Java Classic Games"를 참조하십시오.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.