2つの以前の記事:Javaは2つのGoziゲーム(2)を実装してチェスボードを描きます。 Javaは、チェスのピースを描くために2つのGoziゲーム(3)を実装しています。 Javaは、2つのGoziゲーム(4)を実装して、動きを実現します。 Javaは2つのGoziゲーム(6)を実装してチェス側を変換し、クリックして表示できます。
次に実装したい機能は、5つの連続したビーズがある場合に画面上の情報を促すことです。マウスプレーション関数のiSwin部分でshowmessagedialogを使用するだけです。
マウスプレーション関数コードは次のとおりです。
@Override // public void mousepressed(mousevent e){int point_x = e.getx(); int point_y = e.gety(); int imgwidth = boardimg.getheigh(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/+i*span_x)// "+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.out.out.out.Out.Out "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)); status_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( "Chess Color:"+Chesscolor); if(chesscolor == black){chesscolor = white; } else {chesscolor = black; } Repaint(); // winの場合、迅速なメッセージを挙げてください(iswin(status_x、status_y、chessstatus)){system.out.println( "win !!!");文字列の勝者。 //次の子供が白い場合、今回は黒です。 else winner = "blank"; string mstring = string.format( "おめでとう、%s win !!!"、winner); joptionpane.showmessagedialog(this、mstring); }}}}実行:
この時点で、最も基本的なGozi関数を完了しました。以下に完全なコードを投稿します。
Chessman.java
パッケージxchen.test.simplegobang;パブリッククラスのチェスマン{private int color; // 1-ホワイト、0ブラックプライベートブーリアン配置= false; int matchcount = 1; 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; }} main.java
パッケージxchen.test.simplegobang; Import Java.awt.Container; javax.swing.jframeをインポートします。 xchen.test.simplegobang.drawchessboardをインポートします。 Public Class Mainはjframe {private Drawchessboard Drawchessboardを拡張します。 public main(){drawchessboard = new DrawChessBoard(); //フレームタイトルSettitle( "Stand-Alone Goji"); container 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; java.awt.colorをインポートします。 java.awt.graphicsをインポートします。 java.awt.graphics2dをインポートします。 java.awt.imageをインポートします。 java.awt.radialgradientPaintをインポートします。 java.awt.toolkitをインポートします。 java.awt.event.MouseEventをインポートします。 java.awt.event.mouselistenerをインポートします。 javax.swing.joptionpaneをインポートします。 javax.swing.jpanelをインポートします。パブリッククラスのドローチェスボード拡張jpanel explences mouselistener {final static int black = 0;最終的な静的intホワイト= 1; public int chesscolor = black; int chessman_width = 30;パブリックイメージボード。最終的なプライベートインクロウ= 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(グラフィックスG){// todo auto-enerated method stub super.paintcomponent(g); int imgwidth = boardimg.getheigh(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 ++){for(chessstatus [i] [j]!= null && chessstatus [i] [j] .getplaced()== true){ / /system.out.print.print.print.print.print。 "+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ペイント。 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 // public void mousepressed(mousevent e){int point_x = e.getx(); int point_y = e.gety(); int imgwidth = boardimg.getheigh(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/+i*span_x)// "+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.out.out.out "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)); status_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( "Chess Color:"+Chesscolor); if(chesscolor == black){chesscolor = white; } else {chesscolor = black; } Repaint(); //勝った場合、迅速なメッセージを送信する場合(iswin(status_x、status_y、chessstatus)){system.out.println( "win !!!!");文字列の勝者。 //次の子供が白い場合、今回は黒です。 else winner = "white"; string mstring = string.format( "おめでとう、%s win !!!"、winner); joptionpane.showmessagedialog(this、mstring); }}}} @Override // public void mouseClicked(mouseEvent e){// todo auto-fide method stub} @override public void mousereleased(mouseevent e){// todo auto-generated method stub} @Override public void mouseentered(mousevent e){// todo auto-enerated method stub} @override public void mouseexited(mouseevent e){// todo auto-generated method stub} boolean iswin(int point_x、int point_y、chessman [] for(int j = 0; j <rows+1; j ++){//長いside検索if(chessstatus [i] [j]!= null && chessstatus [i] [j] .getplaced()== true){int matchcolor = chessstatus [i] [j] .getcolor(); //(int n = 1; n <= 4; n ++){if((i+n> = 0)&&(i+n)<= rows){if(chessstatus [i+n] [j]!= null && chessstatus [i+n] [j] = = = true && chessstatus [i+n] {chessstatus [i] [j] .matchcount ++; System.out.println( "pos:"+i+""+j+"right 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)<rows){if(chessstatus [in] [j]!= null && chessstatus [in] [j] .getplaced()= = true && chessstatus [in] [in] [in] [in] [in] [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; // reffresse count}}}}} 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){int matchcolor = chessstatus [i] [j] .getColor(); //下を見下ろすと、左上隅が座標の原点であり、y軸の正の方向は(int n = 1; n <= 4; n ++){if((j+n> = 0)&&(j+n)<= rows){((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] .matchount++; 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+""+"left 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; // reffresse count}} //方向:左上および右下(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){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] 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){rows)<= rows((j+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] .matchount ++; System.out.println( "pos:"+i+""+j+""+"left 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; // refresh count}} //方向:(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){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] [j+n] .getPlaced()== true && chessstatus [in] [j+n] .getColor()== matchcolor){chessstatus [i] [J] .matchount++; 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()== matchcolor){chessstatus [i] [J] .matchount++; System.out.println( "pos:"+i+""+j+""+"left 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; // reffresh count}}} return false; }}よりエキサイティングなゲームについては、特別なトピック「Java Classic Games」を参照してください
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。