2つの以前の記事:Javaは2つのGozi Chessゲーム(2)を実装してチェスボードを描きます。 Javaは2つのGozi Chessゲーム(2)を実装してチェスのピースを描きます
正面には、チェスボードとチェスの断片が描かれています。次に、マウスで画面をクリックして位置を取得して移動する必要があります(チェスゲームと勝利または敗北の判断を最初に考慮しません)。
ステップ:
1)マウスが押されている位置をキャプチャします
2)座標変換後(ピクセルの位置から - > 0-19チェスボードの位置)
3)ボードのステータスを記録する2次元配列を更新する
4)図面ボードを再レンダリングします。
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Chessman.java
パッケージxchen.test.simplegobang;パブリッククラスのチェスマン{private int color; // 1-ホワイト、0ブラックプライベートブーリアン配置= 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
パッケージxchen.test.simplegobang; java.awt.graphicsをインポートします。 java.awt.graphics2dをインポートします。 java.awt.radialgradientPaintをインポートします。 java.awt.imageをインポートします。 java.awt.toolkitをインポートします。 java.awt.event.MouseEventをインポートします。 java.awt.event.mouselistenerをインポートします。 java.awt.colorをインポートします。 javax.swing.jpanelをインポートします。パブリッククラスのdrawchessboard拡張jpanel実装museListener {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.out.println( "draw !!"); 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.t.println( "+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}; 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( "ブラックチェスを描く"); 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.outln( "point y"+i+i+"+ "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y)); status_y = i; }}}チェスマンチェスマン= 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-fide method stub} @override public void mouseReleased(// todo auto-fid methood stub} @override bid mousetered(mouseventevent mousetered methoted methoted) MouseExited(MouseEvent E){// TODO自動生成方法スタブ}}メインモジュールコードは変更されていません
パッケージ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); }}それを実行します
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。