この記事では、Javaに実装されたGozi Chessゲームコードについて説明します。それはあなたの参照のためにあなたと共有されます。特定のコードは次のとおりです
1。実用的な目的
1. Javaguiインターフェイスデザインをマスターします
2。マウスイベントの監視をマスターします(Mouselistener、MousemotionListener)
2。実用的なコンテンツ
Gochi Chessゲームプロセスを実現できるシンプルなGochiプログラムを設計します。下の図に示すように
1。ゴジチェスボード
パッケージcn.edu.ouc.fivechess; java.awt.colorをインポートします。 java.awt.cursorをインポートします。 java.awt.dimensionをインポートします。 java.awt.graphicsをインポートします。 java.awt.graphics2dをインポートします。 java.awt.imageをインポートします。 java.awt.radialgradientPaintをインポートします。 Java.awt.RenderingHintsをインポートします。 java.awt.toolkitをインポートします。 java.awt.event.MouseEventをインポートします。 java.awt.event.mouselistenerをインポートします。 java.awt.event.mousemotionListenerをインポートします。 java.awt.geom.ellipse2dをインポートします。 javax.swing。*; /*** GOZIQI-CHESS-CHESS-BOARD CLASS*/PUBLIC CLASS CHESSBOARD拡張JPANEL実装MOUSELISTENER {public static final int margin = 30; // public static final int grid_span = 35; //グリッド間隔public static final int rows = 15; // chessic int int colls = 15; Point [(rows+1)*(cols+1)]; //初期各配列要素はnull boolean isblack = true; //ブラックチェスで始まる最初のboolean gameover = false; //ゲームは終了しました。画像の影; Color Colortemp; public chessboard(){// setbackground(color.blue); //背景色をオレンジに設定= toolkit.getDefaultToolkit()。getImage( "board.jpg"); Shadows = toolkit.getDefaultToolkit()。getImage( "Shadows.jpg"); addmouseListener(this); AddMouseMotionListener(new MouseMotionListener(){public void mousedRagged(mouseEvent e){} public void mousemoved(mouseEvent e){int x1 =(e.getx() - margin+grid_span/2)/grid_span; // y1 =(e.gety() - マージン+grid_span/2)// cursor(cursor.default_cursor)); } // public void paintComponent(グラフィックg){super.paintComponent(g); //チェスボードint imgwidth = img.getWidth(this); int imgheight = img.getheight(this); //画像の幅と高さを取得int fwidth = getWidth(); int fheight = getheight(); //ウィンドウの幅と高さを取得x =(fwidth-imgwidth)/2; int y =(fheight-imgheight)/2; G.Drawimage(IMG、X、Y、null); for(int i = 0; i <= rows; i ++){// horizontal line g.drawline(マージン、マージン+i*grid_span、マージン+cols*grid_span、margin+i*grid_span); } for(int i = 0; i <= cols; i ++){//垂直線g.drawline(margin+i*grid_span、margin、margin+i*grid_span); } //(int i = 0; i <chesscount; i ++){// grid交差x、y座標int xpos = chesslist [i] .getx()*grid_span+margin; int ypos = chesslist [i] .gety()*grid_span+margin; G.SetColor(ChessList [i] .getColor()); //色// g.filloval(xpos-point.diameter/2、ypos-point.diameter/2、//point.diameter、point.diameter); //g.drawimage(shadows、xpos-point.diameter/2、ypos-point.diameter/2、point.diameter、point.diameter、null); colortemp = chesslist [i] .getColor(); if(colortemp == color.black){radialgradientpaint paint = new radialgradientPaint(xpos-point.diameter/2+25、ypos-point.diameter/2+10、20、new float [] {0f、1f}、new color [] {color.white、color.black}); ((Graphics2d)G).SetPaint(Paint); ((Graphics2d)g).setRenderingHint(renderinghints.key_antialiasing、renderinghints.value_antialias_on); ((graphics2d)g).setrenderinghint(renderinghints.key_alpha_interpolation、renderinghints.value_alpha_interpolation_default); } else if(colortemp == color.white){radialgradientpaint paint = new radialgradientpaint(xpos-point.diameter/2+25、ypos-point.diameter/2+10、70、new float [] {0f、1f}、new color [] {color.white、color.black}); ((Graphics2d)G).SetPaint(Paint); ((Graphics2d)g).setRenderingHint(renderinghints.key_antialiasing、renderinghints.value_antialias_on); ((graphics2d)g).setrenderinghint(renderinghints.key_alpha_interpolation、renderinghints.value_alpha_interpolation_default); } eLlipse2d e = new ellipse2d.float(xpos-point.diameter/2、ypos-point.diameter/2、34、35); ((graphics2d)g).fill(e); //最後のチェスピースをマークする赤い長方形のボックスif(i == chesscount-1){//最後のチェスピースg.setcolor(color.red); G.drawrect(xpos-point.diameter/2、ypos-point.diameter/2、34、35); }}} public void mousepressed(mousevent e){//コンポーネントに押されたときにマウスが呼び出されます//ゲームが再生できなくなりました(GameOver)return; string colorname = isblack? "black": "White Chess"; //マウスの座標位置をグリッドインデックスに変換しますxindex =(e.getx() - マージン+grid_span/2)/grid_span; yindex =(e.gety() - マージン+grid_span/2)/grid_span; //チェスボードの外に落ちた場合、(xindex <0 || xindex> rows || yindex <0 || yindex> cols)return; // XおよびYの位置にチェスのピースがある場合、(findChess(xindex、yindex))returnの場合、それを置くことができません。 //それが完了したらそれを行うことができれば、それが完了したらそれを置くことはできません。 point(xindex、yindex、isblack?color.black:color.white); chesslist [chesscount ++] = ch; Repaint(); //システムにredRawに通知しますjoptionpane.showmessagedialog(this、msg); GameOver = true; } isblack =!isblack; } // museListener public void mouseclicked(mouseevent e){//マウスボタンがコンポーネントをクリックすると呼び出される} public void mousevent e){//マウスがコンポーネントに入るときに呼ばれる}パブリックvoid exited(mouseevent exed {mousevent eaved eaved efice of the mouse eave eave efconce mouseReleased(MouseEvent e){ //Called when the mouse button is released on the component} //Called in the chess array to see if there are chess pieces with index x and y that exist private boolean findChess(int x,int y){ for(Point c:chessList){ if(c!=null&&c.getX()==x&&c.getY()==y) return true; } falseを返します。 } private boolean iswin(){int continuecount = 1; //連続チェスのピースの数//水平方向に西を見る(int x = xindex-1; x> = 0; x - ){color c = isblack?color.black:color.white; if(getChess(x、yindex、c)!= null){continuecount ++; } else break; } //(int x = xindex+1; x <= cols; x ++){color c = isblack?color.black:color.white; if(getChess(x、yindex、c)!= null){continuecount ++; } else break; } if(ContinueCount> = 5){return true; } else ContinueCount = 1; //垂直に別の検索を続行しますif(getChess(xindex、y、c)!= null){continuecount ++; } else break; } //(int y = yindex+1; y <= rows; y ++){color c = isblack?color.black:color.white; if(getChess(xindex、y、c)!= null)ContinueCount ++;それ以外の場合は休憩します。 } if(ContinueCount> = 5)trueを返します。 else continuecount = 1; //別の状況を検索し続ける:obliquely //北東検索(int x = xindex+1、y = yindex-1; y> = 0 && x <= cols; x ++、y - ){color c = isblack?color.black:color.white; if(getChess(x、y、c)!= null){continuecount ++; } else break; } //(int x = xindex-1、y = yindex+1; x> = 0 && y <= rows; x-、y ++){color c = isblack?color.black:color.white; if(getChess(x、y、c)!= null){continuecount ++; } else break; } if(ContinueCount> = 5)trueを返します。 else continuecount = 1; //別の状況で検索を続ける:oblique //ノースウェスト検索(int x = xindex-1、y = yindex-1; x> = 0 && y> = 0; x - 、y - ){color c = isblack?color.black:color.white; if(getChess(x、y、c)!= null)ContinueCount ++;それ以外の場合は休憩します。 } //南東部は(int x = xindex+1、y = yindex+1; x <= cols && y <= rows; x ++、y ++){color c = isblack?color.black:color.white; if(getChess(x、y、c)!= null)ContinueCount ++;それ以外の場合は休憩します。 } if(ContinueCount> = 5)trueを返します。 else continuecount = 1; falseを返します。 } private Point Getchess(int xindex、int yindex、color Color){for(ポイントP:Chesslist){if(p!= null && p.getx()== xindex && p.gety()== yindex && p.getcolor()== color)return p; } nullを返します。 } public void restartgame(){//(int i = 0; i <chesslist.length; i ++){chesslist [i] = null; } //ゲーム関連の変数値を復元するisblack = true; GameOver = false; //ゲームエンディングChessCount = 0です。 //現在のチェスピースのrepaint(); } // Chessへの返信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(); } isblack =!isblack; Repaint(); } //長方形寸法public dimension getPreferredSize(){return new Dimension(margin*2 +grid_span*cols、margin*2 +grid_span*rows); }} 2。チェスの断片
パッケージcn.edu.ouc.fivechess; java.awt.colorをインポートします。 /**チェスクラス*/パブリッククラスポイント{プライベートX; // Xチェスボードのインデックスプライベートインク; //チェスボードプライベートカラーのインデックスプライベートカラーカラー; this.y = y; this.color = color; } public int getX(){//チェスボードリターンxでxのインデックスを取得します。 } public int gety(){return y; } public color getColor(){//チェスピースの色を取得します。 }} 3。GOZIメインフレームクラス
パッケージcn.edu.ouc.fivechess; java.awt.event。*; java.awt。*; javax.swing。*; /* gojiメインフレームワーク、プログラム*/ public class startchessjframeはjframe {private chessboard chessboardを拡張します。プライベートJPanelツールバー。プライベートJbutton Startbutton、バックボタン、Exitbutton;プライベートJmenubar Menubar;プライベートJmenu sysmenu; private jmenuitem startmenuitem、exitmenuitem、backmenuitem; //再起動、終了、および後悔メニュー項目publicshessjframe(){settitle( "stand-alone goji"); //タイトルChessboard = new Chessboard();コンテナcontentpane = getContentPane(); ContentSpane.Add(チェスボード); Chessboard.setopaque(true); //メニューmenubar = new jmenubar();を作成して追加します。 //メニューバーの初期化sysmenu = new jmenu( "system"); //メニューの初期化//メニュー項目を初期化startmenuitem = new JMenuitem( "RestArt"); exitmenuitem = new jmenuitem( "exit"); backmenuitem = new jmenuitem( "Repent"); //メニューに3つのメニュー項目を追加しますsysmenu.add(startmenuitem); sysmenu.add(exitmenuitem); sysmenu.add(backmenuitem); //内部クラスの初期化myitemlistener lis = new myitemlistener(); //イベントリスナーthis.startmenuitem.addactionlistener(lis)に3つのメニューを登録します。 backmenuitem.addactionlistener(lis); exitmenuitem.addactionlistener(lis); menubar.add(sysmenu); //メニューメニューをメニューバーに追加します。 ExitButton = new JButton( "Exit"); Backbutton = new JButton( "Repent"); // FlowLayout Toolbar.setLayout(new flowlayout(flowlayout.left))を使用したツールパネルボタンのレイアウト; //ツールパネルToolbar.Add(StartButton)に3つのボタンを追加します。 Toolbar.Add(ExitButton); Toolbar.Add(BackButton); // 3つのボタンを登録して、イベントStartButton.AddActionListener(LIS)をリッスンします。 exitbutton.addactionlistener(lis); backbutton.addactionlistener(lis); backbutton.addactionlistener(lis); //ツールパネルをインターフェイス「南」に置きます。つまり、追加(Toolbar、Borderlayout.South);追加(チェスボード); // SetSize(800,800); pack(); // adaptive size}プライベートクラスmyitemlistenerはactionlistener {public void actionperformed(actionevent e){object obj = e.getsource(); //イベントソースを取得するif(obj == startchessjframe.this.startmenuitem || obj == instart/- startigfframe.prasteffive.prassbutton) system.out.println( "start"); chessboard.restartgame(); } else if(obj == exitmenuitem || obj == exitbutton)system.exit(0); else if(obj == backmenuitem || obj == backbutton){system.out.println( "repent chess ..."); chessboard.goback(); }}}} public static void main(string [] args){startchessjframe f = new startchessjframe(); //メインフレームf.setVisible(true); //メインフレームを表示}}}} 3。概要
1.メニューの設計と実装?
2。チェスボードでマウスがクリックした後、チェスの断片を描く方法は?設定したばかりのチェスピース用の赤い箱を描く方法は?
3.チェススコアのデータ構造とは何ですか?
上記はこの記事に関するものです。誰もがJavaプログラミングを学ぶことが役立つことを願っています。