この小さなゲームは、妹と私のJavaコースのデザインであり、私がやった最初のJavaプロジェクトでもあります。初心者に適しています。 Javaコースのデザインに悩まされている子供たちを助けることができることを願っています~~~
1.ゲームを実装する必要があります
1.メインフレームとインターフェイスを設計します。
2. ActionListenerインターフェイスを使用して、ボタンイベントの監視を実現します。
3。関数の実装を再起動します。
4。チェス機能の実装。
5。出口関数の実装。
6.チェスボード内のチェスピースドットの定義。
7. Mouselistenerインターフェイスを使用して、イベント監視を実装し、インターフェイス内のすべてのメソッドを実装します。
8.マウスがボード上の交差点に移動し、ポイントにチェスの断片がない場合、それは小さな手の形になる可能性があります。
9.チェスボードをクリックするときは、IFステートメントを使用して、ポイントが交差点にあるかどうかを判断し、チェスクラスのforeachステートメントとgetx()およびgety()メソッドを使用して、各チェスピースの位置を横断して、ポイントにチェスピースがあるかどうかを判断します。
10.クリックポイントにサブを配置することができると判断されたら、ピースを描くときに、forループを使用して既存のポイントをすべて通過し、グラフィックスクラスのセットカラーを使用して色を設定し、グラフィッククラスのフィロバルメソッドを使用して形状サイズを設定します。
11.チェスのピースを描くときは、勝者を判断し、時間内に負ける必要があります。インデックスを使用し、ループを使用して、最後のチェスピースの方向を横断します。同じ直線にチェスピースがある場合、5つ以上のチェスピースが5つ以上のチェスピースがある場合、それは現在のチェスピースで表されるものです。
12。勝者が決定されると、対応する情報をポップアップできます。
2。機能コードの実装
2.1ゲームに入ります
public static void main(string [] args){startchessjframe f = new startchessjframe(); //メインフレームf.setVisibleを作成(true); //メインフレームを表示}2.2初期化、使用するいくつかの数量を定義します。
プライベートチェスボードチェスボード。
2.3インターフェイス構築方法(ゲームフレームワーク)
public StartchessJFrame(){settitle( "Standalone goji"); //タイトルを設定したChessboard = new Chessboard(); backbutton = new Button( "Reply"); ExitButton = new Button( "Exit"); // Toolbar.setLayout(new FlowLayout.Left)); // FlowLayoutを使用してToolbar.Add(BackButton)をレイアウトします。 Toolbar.Add(StartButton); toolbar.add(exitbutton); // 3つのボタンをツールパネルに追加するstartbutton.addactionlistener(lis); backbutton.addactionlistener(lis); Exitbutton.AddActionListener(lis); // 3つのボタンイベントを登録してイベントを聞く(ツールバー、borderlayout.South); //ツールパネルをインターフェイスの南にレイアウトします。 サイズ}2.4ボタンの実装と監視(内部建設方法)
myItemlistener lis = new MyItemListener(); //ボタンイベントの内部クラスを初期化しますイベントリスナーツールバー= newパネル(); backbutton = new Button( "Reply"); ExitButton = new Button( "exit"); // toolbar.setlayout(new flowlayout.left)); // Toolbar.add(backbutton)をレイアウトします。 Toolbar.Add(StartButton); toolbar.add(exitbutton); // 3つのボタンをツールパネルに追加するstartbutton.addactionlistener(lis); backbutton.addactionlistener(lis); ExitButton.AddActionListener(LIS); // 3つのボタンイベントを登録して、イベントを聞きます
2.5ボタンイベントのリスニング
プライベートクラスmyitemlistenerはactionlistener {public void actionperformed(actionevent e){object obj = e.getSource(); //イベントソースを取得するif(obj == startbutton){system.out.print.println( "start ..."); // restart // jfiveframe.this innerclage reference references chessboard(); } else if(obj == exitbutton){system.exit(0); // exped the application} else if(obj == backbutton){system.out.println( "repent chess ..."); // repent chessboard.goback(); }}}2.6ボタン機能の実装を再起動します
public void restartgame(){//(int i = 0; i <chesslist.length; i ++)chesslist [i] = null; /*ゲーム関連の変数値を復元*/ isback = true; GameOver = false; //ゲームがChessCount = 0; //現在のボード上のチェスピースの数はrepaint()です。 }2.7チェスボタンの機能実装
public void goback(){if(chesscount == 0)return; chesslist [chesscount-1] = null; ChessCount--; if(chesscount> 0){xindex = chesslist [chesscount-1] .getx(); yindex = chesslist [chesscount-1] .gety(); } isback =!isback; Repaint(); }2.8チェスボードが必要に応じて大きくなったり小さくなったりすると、それに応じてウィンドウが変更されます。
//寸法:長方形のチェスボードクラスの内部パブリックディメンションgetPreferredSize(){return new dimension(margin*2+grid_span*cols、margin*2+grid_span*rows); } pack(); // startchessboardクラス内の適応サイズ2.9チェスピースの定義
java.awt。*;パブリッククラスポイント{プライベートint x; // xチェスボードのチェスピースのインデックス値プライベートint y; //チェスボードのチェスピースのインデックス値プライベートカラー。 this.y = y; this.color = color; } //チェスボードのチェスピースのxインデックス値を取得しますpublic int getx(){return x; } //チェスボードのチェスピースのyインデックス値を取得しますpublic int gety(){return y; } //チェスピースを取得する色のパブリックカラーgetColor(){return color; }} 3。機能パーツコードの実装
3.1初期化、使用するいくつかの数量を定義します。
public static int margin = 30;ブラックチェスファーストゲームオーバー= false; //ゲームはint chesscountを終了しますか?
3.2チェスボードオブジェクトの構築方法
public chessboard(){setbackground(color.light_gray); //背景色を灰色にaddmouseListener(this); //イベントリスナーaddmousemotionListener(){// anonymous internal class @override public void mousemoved(int int int int x1 =(e.getx() - マージン+grid_span/2)/grid_span; (x1 <0 || x1> rows || y1 <0 || y1> cols || findchess(x1、y1)){//ゲームが終了し、チェスボードの外に落ちることはできません。 shape} else {setCursor(new cursor(cursor.hand_cursor)); //手の形に設定}} @Override public void mousedRagged(mouseEvent e){}}); }3.3マウスリスナーをセットアップして、手を小さくします(建設方法内)
addmousemotionListener(new MousemotionListener(){//匿名内部クラス@Override public void mousemoved(mouseEvent e){int x1 =(e.getx() - margin+grid_span/2)/grid_span; (x1 <0 || x1> rows || y1> cols || findchess(x1、y1)){//は、チェスボードの外側に落ちることはできません。 cursor(cursor.default_cursor)); //デフォルト形状に設定} else {setcursor(new cursor(cursor.hand_cursor)); //手形に設定}} @Override public void mousedragged(mousevent e){}});3.4チェスボードをクリックするときのマウスプレスイベント
public void mousepressed(mousevent e){// if(gameover)がコンポーネントでマウスボタンを押したときに呼び出されます//ゲームが終了し、返されません。文字列colorname = isback? 「黒」:「白いチェス」; xindex =(e.getx() - マージン+grid_span/2)/grid_span; yindex =(e.gety() - マージン+grid_span/2)/grid_span; //マウスの座標位置をグリッドインデックスにコンバージョンします(xindex <|| xindex> rows || yindex> cols) if(findChess(xindex、yindex))//チェスのピースはすでにx、y位置に存在するため、返されることはできません。 Point Ch = new Point(xindex、yindex、isback?color.black:color.white); chesslist [chesscount ++] = ch; Repaint(); //システムにrepaint if(iswin()){string msg = string.format( "おめでとう、%s win〜"、coloroname); joptionpane.showmessagedialog(this、msg); GameOver = true; } else if(chesscount ==(cols+1)*(rows+1)){string msg = string.format( "チェスドラムは非常に良い、グレート〜"); joptionpane.showmessagedialog(this、msg); GameOver = true; } isback =!isback; }3.5チェスボード、チェスの断片、赤いフレームを描きます
public void paintComponent(グラフィックスG){super.paintComponent(g); //(int i = 0; i <= rows; i ++)for(int i = 0; i <rows; i ++){//水平線g.drawline(マージン+i*grid_span、margin+cols*grid_span、margin+i*grid_span); } for(int i = 0; i <= cols; i ++){//直線g.drawline(マージン+i*grid_span、マージン、マージン+i*grid_span、margin+rows*grid_span); }/*チェスピースを描画*/for(int i = 0; i <chesscount; i ++){int xpos = chesslist [i] .getx()*grid_span+マージン; //グリッド交差int ypos = chesslist [i] .gety()。 G.SetColor(Chesslist [i] .getColor()); //色G.filloval(xpos-point.diameter/2、ypos-point.diameter/2、point.diameter、point.diameter)を設定します。 if(i == chesscount-1){g.setcolor(color.red); //最後のピースを赤いg.drawrect(xpos-point.diameter/2、ypos-point.diameter/2、point.diameter、point.diameter)としてマークします。 }}3.6裁判官の勝利または損失
/*どのサイドが勝つかを判断*/private boolean iswin(){int ContinueCount = 1; // color.black:color.white; if(getChess(x、yindex、c)!= null){continuecount ++; } else break; } for(int x = xindex+1; x <= rows; x ++){//色c = isbackを探しますか? color.black:color.white; if(getChess(x、yindex、c)!= null){continuecount ++; } else break; } if(continueCount> = 5){//レコードの数は5以上であることを判断します。 } else ContinueCount = 1; // color.black:color.white; if(getChess(xindex、y、c)!= null){continuecount ++; } else break; } for(int y = yindex+1; y <= rows; y ++){//垂直方向に下向きに見えますColor c = isback? color.black:color.white; if(getChess(xindex、y、c)!= null){continuecount ++; } else break; } if(continueCount> = 5){//レコードの数は5以上であることを判断します。 } else ContinueCount = 1; //(int x = xindex+1、y = yindex-1; y> = 0 && x <= cols; x ++、y - ){//右下の色を見下ろすc = isback? color.black:color.white; if(getChess(x、y、c)!= null){continuecount ++; } else break; } for(int x = xindex-1、y = yindex+1; y <= rows && x> = 0; x-、y ++){//色c = isbackを探しますか? color.black:color.white; if(getChess(x、y、c)!= null){continuecount ++; } else break; } if(continueCount> = 5){//レコードの数は5以上の場合、この側が勝つことを意味します。 trueを返します。 } else ContinueCount = 1; //(int x = xindex-1、y = yindex-1; y> = 0 && x> = 0; x-、y - ){//色c = isbackを探しますか? color.black:color.white; if(getChess(x、y、c)!= null){continuecount ++; } else break; } for(int x = xindex+1、y = yindex+1; y <= rows && x <= cols; x ++、y ++){//色c = isbackを探しますか? color.black:color.white; if(getChess(x、y、c)!= null){continuecount ++; } else break; } if(continueCount> = 5){//レコードの数は5以上であることを判断します。 } else ContinueCount = 1; falseを返します。 }3.7対応するメッセージボックスがポップアップします(マウスプレス機能内)
if(iswin()){string msg = string.format( "おめでとう、%s win〜"、coloroname); joptionpane.showmessagedialog(this、msg); GameOver = true; } else if(chesscount ==(cols+1)*(rows+1))// track {string msg = string.format( "チェスドラムは非常に良い、グレート〜"); joptionpane.showmessagedialog(this、msg); GameOver = true; }3.8特定のポイントにチェスの断片があるかどうかを判断するために上記で使用される関数
private boolean findChess(int x、int y){for(point c:chesslist){if(c!= null && c.getx()== x && c.gety()== y)true; } falseを返します。 }3.9このチェスボードクラスは、マウスリスニングインターフェイスMonselistenerを実装するため、インターフェイス内のすべての方法を書き直す必要があります。他の方法は次のとおりです
@Override public void mouseclicked(mousevent e){//マウスボタンがクリックされたときに呼び出されたコンポーネント} @Override public void mouseReeded(mouseevent e){///マウスボタンがコンポーネントでリリースされたときに呼び出されます} @Override public void mouseExited(mousevent e){//マウスがコンポーネントを離れるときに呼び出されます}4。操作結果
V.コードの概要
ゲームには合計3つのクラスがあり、1つはインターフェイスStartchessJFrame、1つはチェスボードクラスのチェスボード、もう1つはチェスサブジェクトクラスポイントです
5.1StartChessJFrameクラス
パッケージchess.lcc.com; javax.swing。*; java.awt.event。*; java.awt。*; /** Goziのメインフレームワーク、プログラムスタートアップクラス*/パブリッククラスStartchessJFrameはjframeを拡張します{プライベートチェスボードチェスボード; //ファイトパネルプライベートパネルツールバー。 goji "); //タイトルを設定しましたchessboard = new Chessboard(); //パネルオブジェクトを初期化し、メニューmyitemlistener lis = new myitemlistener(); //ボタンイベントリスナー内部クラスツールバーを初期化します。 backbutton = new Button( "Reply"); ExitButton = new Button( "Exit"); // Toolbar.setLayout(new FlowLayout.Left)); // FlowLayoutを使用してToolbar.Add(BackButton)をレイアウトします。 Toolbar.Add(StartButton); toolbar.add(exitbutton); // 3つのボタンをツールパネルに追加するstartbutton.addactionlistener(lis); backbutton.addactionlistener(lis); Exitbutton.AddActionListener(lis); // 3つのボタンイベントを登録してイベントをリッスンする(ツールバー、borderlayout.South); //ツールパネルをインターフェイスの南にレイアウトします。サイズ}プライベートクラスmyitemlistenerを実装するactionlistener {public void actionperformed(actionevent e){object obj = e.getsource(); //イベントソースを取得するif(obj == startbutton){system.out.println( "restart ..."); } else if(obj == exitbutton){system.exit(0); // exped the application} else if(obj == backbutton){system.out.println( "Reply chess ..."); // reny chessboard.goback(); }}}} public static void main(string [] args){startchessjframe f = new startchessjframe(); //メインフレームf.setVisible(true); //メインフレームを表示}}}} 5.2ChessBoardクラス
パッケージchess.lcc.com; javax.swing。*; java.awt。*; java.awt.event.mouselistenerをインポートします。 java.awt.event.mousemotionListenerをインポートします。 java.awt.event.MouseEventをインポートします。 /*Group-Chess-Chessboardクラス*/パブリッククラスチェスボード拡張jpanel Implements museListener {public static int margin = 30; // public static int grid_span = 35; // grid spacing public static int rows = 15; //チェスボードの数Point [(rows+1)*(cols+1)]; //各配列要素をnull booleanに初期化Isback = true; //デフォルトの開始はブラックチェスファーストゲームオーバー= false; //現在のチェスボードint xindex、yindex; // setbackground(color.light_gray); //背景色を黄色に設定する(this); //イベントリスナーaddmousemotionListener(){//匿名の内部クラス@Override public void mousemoved(mousevent e){int x1 =(e.getx()(e.get()(e.get()(e.get()) y1 =(e.gety() - マージン+grid_span/2)/grid_span; XとYの位置にあるチェスの断片なので、setCursor(new cursor(cursor.default_cursor)); //デフォルト形状に設定することはできません} }/*draw*/public void paintComponent(グラフィックg){super.paintComponent(g); //(int i = 0; i <= rows; i ++){// draw horizontal g.drawline(マージン、マージン+i*grid_span、cols+cols*grid_span、magne+i*grid_span); } for(int i = 0; i <= cols; i ++){//直線g.drawline(margin+i*grid_span、margin、margin+i*grid_span、margin+rows*grid_span); }/*チェスピースを描画*/for(int i = 0; i <chesscount; i ++){int xpos = chesslist [i] .getx()*grid_span+margin; // x座標G.SetColor(Chesslist [i] .getColor()); //色G.filloval(xpos-point.diameter/2、ypos-point.diameter/2、point.diameter、point.diameter)を設定します。 if(i == chesscount-1){g.setcolor(color.red); //最後のピースを赤いg.drawrect(xpos-point.diameter/2、ypos-point.diameter/2、point.diameter、point.diameter)としてサインアップします。 }}} @Override public void mousepressed(mousevent e){// if(gameover)が呼び出された場合、マウスボタンがコンポーネントに押されたとき//ゲームが終了した場合、返すことはできません。文字列colorname = isback? 「黒」:「白いチェス」; xindex =(e.getx() - マージン+grid_span/2)/grid_span; yindex =(e.gety() - マージン+grid_span/2)/grid_span; //マウスの座標位置をグリッドインデックスにコンバージョンします(xindex <|| xindex> rows || yindex> cols) if(findChess(xindex、yindex))//チェスのピースはすでにx、y位置に存在するため、返されることはできません。 Point Ch = new Point(xindex、yindex、isback?color.black:color.white); chesslist [chesscount ++] = ch; Repaint(); //システムにrepaint if(iswin()){string msg = string.format( "おめでとう、%s win〜"、coloroname); joptionpane.showmessagedialog(this、msg); GameOver = true; } else if(chesscount ==(cols+1)*(rows+1)){string msg = string.format( "チェスドラムは非常に良い、グレート〜"); joptionpane.showmessagedialog(this、msg); GameOver = true; } isback =!isback; } @Override public void mouseClicked(mouseEvent e){//マウスボタンがコンポーネントでクリックされたときに呼び出されます} @Override public void mouseReeded(MouseEvent e){///マウスボタンがコンポーネントにリリースされたときに呼び出されます} component} @Override public void mouseExited(mouseEvent e){//マウスがコンポーネントを離れるときに呼び出されます} private boolean findchess(int x、int y){for(c!= null&c.getx()= = x && c.gety()= = y)returt true; } falseを返します。 }/*どのサイドウィンを判断します*/private boolean iswin(){int ContinueCount = 1; // color.black:color.white; if(getChess(x、yindex、c)!= null){continuecount ++; } else break; } for(int x = xindex+1; x <= rows; x ++){//色c = isbackを探しますか? color.black:color.white; if(getChess(x、yindex、c)!= null){continuecount ++; } else break; } if(continueCount> = 5){//レコードの数は5以上であることを判断します。 } else ContinueCount = 1; // color.black:color.white; if(getChess(xindex、y、c)!= null){continuecount ++; } else break; } for(int y = yindex+1; y <= rows; y ++){//垂直方向に下向きに見えますColor c = isback? color.black:color.white; if(getChess(xindex、y、c)!= null){continuecount ++; } else break; } if(continueCount> = 5){//レコードの数は5以上であることを判断します。 } else ContinueCount = 1; //(int x = xindex+1、y = yindex-1; y> = 0 && x <= cols; x ++、y - ){//右下の色を見下ろすc = isback? color.black:color.white; if(getChess(x、y、c)!= null){continuecount ++; } else break; } for(int x = xindex-1、y = yindex+1; y <= rows && x> = 0; x-、y ++){//色c = isbackを探しますか? color.black:color.white; if(getChess(x、y、c)!= null){continuecount ++; } else break; } if(continueCount> = 5){//レコードの数は5以上の場合、この側が勝つことを意味します。 trueを返します。 } else ContinueCount = 1; //(int x = xindex-1、y = yindex-1; y> = 0 && x> = 0; x-、y - ){//色c = isbackを探しますか? color.black:color.white; if(getChess(x、y、c)!= null){continuecount ++; } else break; } for(int x = xindex+1、y = yindex+1; y <= rows && x <= cols; x ++、y ++){//色c = isbackを探しますか? color.black:color.white; if(getChess(x、y、c)!= null){continuecount ++; } else break; } if(continueCount> = 5){//レコードの数が5以上であると判断します。これは、この側がtrueを勝ち取ることを意味します。 } else ContinueCount = 1; falseを返します。 } private Point getChess(int xindex、int yindex、color){for(ポイントC:chesslist){if(c!= null && c.getx()== xindex && c.gety()== yindex && c.getcolor()== color)return c; } nullを返します。 } public void restartgame(){//チェスのピースをクリアします(int i = 0; i <chesslist.length; i ++)chesslist [i] = null; /*ゲーム関連の変数値を復元*/ isback = true; GameOver = false; //ゲームがChessCount = 0; //現在のボード上のチェスピースの数はrepaint()です。 } public void goback(){if(chesscount == 0)return; chesslist [chesscount-1] = null; ChessCount--; if(chesscount> 0){xindex = chesslist [chesscount-1] .getx(); yindex = chesslist [chesscount-1] .gety(); } isback =!isback; Repaint(); } //ディメンション:長方形のパブリックディメンションgetPreferredSize(){return new dimension(margin*2+grid_span*cols、margin*2+grid_span*rows); }} 5.3ポイントクラス
パッケージchess.lcc.com; java.awt。*;パブリッククラスポイント{プライベートint x; // xチェスボードのチェスピースのインデックス値プライベートint y; //チェスボードのチェスピースのインデックス値プライベートカラー。 this.y = y; this.color = color; } //チェスボードのチェスピースのxインデックス値を取得しますpublic int getx(){return x; } //チェスボードのチェスピースのyインデックス値を取得しますpublic int gety(){return y; } //チェスピースを取得する色のパブリックカラーgetColor(){return color; }}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。