前の記事では、JavaがChessboardを描く2つのGozi Chessゲーム(2)を実装することについて説明しています。チェスボードが描かれています。次に、制御関数、主な関数を実装する必要があります。
1)チェスのピースを選択します
2)チェスの断片を描きます
3)勝者を判断します
4)チェスを交換します
最初にチェスピースの描画の一部を実現します
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
まず、チェスピースクラスを定義します。このクラスには2つの属性があります:ピースの色(0-は黒と1-は白)と移動を行うかどうか(チェスピースのターン情報を保存するために2次元配列を使用する予定です)
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; }}次に、前の部品のチェスボードのコード部分を描き、チェスピースを描くためのコードを追加しました。チェスのピースをテストするために、チェスボード[8、8]、[7、7]にある1つの白と1つの黒)を使用しました。
drawchessboard.java
パッケージxchen.test.simplegobang; java.awt.graphicsをインポートします。 java.awt.graphics2dをインポートします。 java.awt.radialgradientPaintをインポートします。 java.awt.imageをインポートします。 java.awt.toolkitをインポートします。 java.awt.colorをインポートします。 javax.swing.jpanelをインポートします。 public class drawchessboardはjpanelを拡張します{final static int black = 0;最終的な静的intホワイト= 1; public int chesscolor = black;パブリックイメージボード。最終的なプライベートインクロウ= 19; Chessman [] [] Chessstatus = new Chessman [rows] [rows]; public DrawChessboard(){boardimg = toolkit.getDefaultToolkit()。getImage( "res/drawable/chessboard2.png"); if(boardimg == null)system.err.println( "pngは存在しません"); //テスト描画チェスマンパートシンプルチェスマンチェスマン=新しいチェスマン(0、真); Chessstatus [7] [7] = Chessman; Chessman Chessman2 = New Chessman(1、True); Chessstatus [8] [8] = Chessman2; // Chessman Part Simple} @Override Protected Void PaintComponent(Graphics 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; G.drawimage(boardimg、x、y、null); int margin = x; int span_x = imgwidth/rows; int span_y = imgheight/rows; //(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; i ++){for(int j = 0; j <rows; j ++){for(chessstatus [i] [j]!= null && chessstatus [i] [j] .getplaced()== true){system.out.out.out.outln( "+i+i+i+i+i+j) int pos_x = x+i*span_x; int pos_y = y+j*span_y; int chessman_width = 20; float radius_b = 20; float radius_w = 50; 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); }}}}}}}}}メインモジュールコードは変更されていません
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); }}実行してください!
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。