




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、2020-7-5JavaSE實踐之單機版五子棋蝌蚪成長記目錄一、實踐目標 2二、實踐內容2三、實現(xiàn)代碼33.1 五子棋棋盤類 ChessBoard.java 33.2 棋子類 Point.java193.3 五子棋主框架類 StartChessJFrame.java 2 0四、總結26一、實踐目標1. 掌握JavaGUI界面設計2. 掌握鼠標事件的監(jiān)聽(MouseListener , MouseMotionListener)二、實踐內容設計一個簡單的五子棋程序,能夠實現(xiàn)五子棋下棋過程。如下圖所示單機版五子棋系統(tǒng)垂新開皆|退£|悔琨|三、實現(xiàn)代碼3.1五子棋棋盤類 ChessBoard
2、.javaimport javax.swi ng.*;/*五子棋-棋盤類*/public class ChessBoard exte nds JPa nel impleme nts MouseListe ner public static final int MARGIN=30;邊距public static final int GRID_SPAN=35;網格間距public static final int R0WS=15;棋盤行數public static final int C0LS=15;棋盤列數nullPoi nt chessList= new Poi nt(ROWS+1)*(COL
3、S+1);初始每個數組元素為boolean isBlack=true;默認開始是黑棋先boolea n gameOver=false;游戲是否結束int chessCou nt;當前棋盤棋子的個數int xI ndex,yl ndex;/當前剛下棋子的索引Image img;Image shadows;Color colortemp;public ChessBoard()/ setBackgro un d(Color.blue);/設置背景色為橘黃色img=Toolkit.getDefaultToolkit().getlmage("board.jpg"); shadows=
4、Toolkit.getDefaultToolkit().getlmage("shadows.jpg");addMouseListe ner(this);addMouseMoti on Liste ner(new MouseMoti on Liste ner()public void mouseDragged(MouseEve nt e)public void mouseMoved(MouseEve nt e)int x1=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;/將鼠標點擊的坐標位置轉成網格索引int y1=(e.getY()-MAR
5、GIN+GRID_SPAN/2)/GRID_SPAN;/游戲已經結束不能下/落在棋盤外不能下/x,y位置已經有棋子存在,不能下if(x1<0|x1>ROWS|y1<0|y1>COLS|gameOver|fi ndChess(x1,y1) setCursor( new Cursor(Cursor.DEFAULT_CURSOR);/設置成默認狀態(tài)else setCursor( new Cursor(Cursor.HAND_CURSOR););/繪制public void pain tComp onen t(Graphics g)super.pa in tComp onen
6、t(g);/畫棋盤int imgWidth= img.getWidth(this);int imgHeight=img.getHeight(this);獲得圖片的寬度與高度int FWidth=getWidth();int FHeight=getHeight();獲得窗口的寬度與高度int x=(FWidth-imgWidth)/2;int y=(FHeight-imgHeight)/2;g.drawlmage(img, x, y, nu II);for(i nt i=0;i<=ROWS;i+)畫橫線g.drawLi ne(MARGIN,MARGIN+i*GRID_SPAN,MARGIN
7、+COLS*GRID_SPAN,MARGIN+i*GRID_SPAN);for(i nt i=0;i<=COLS;i+)/畫豎線g.drawL in e(MARGIN+i*GRID_SPAN,MARGIN,MARGIN+i*GRID_SPAN,MARGIN+ROWS*GRID_SPAN);/畫棋子for(i nt i=0;i<chessCo un t;i+)/網格交叉點x, y坐標int xPos=chessListi.getX()*GRID_SPAN+MARGIN;int yPos=chessListi.getY()*GRID_SPAN+MARGIN;g.setColor(che
8、ssListi.getColor();設置顏色/ g.fillOval(xPos-Poi nt.DIAMETER/2, yPos-Poi nt.DIAMETER/2,/Poi nt.DIAMETER, Poi nt.DIAMETER);g.drawlmage(shadows,xPos-Poi nt.DIAMETER/2,yPos-Poi nt.DIAMETER/2,Poi nt.DIAMETER, Poi nt.DIAMETER, n ull);colortemp=chessListi.getColor();if(colortemp=Color.black)RadialGradie ntPai
9、 nt pai nt = new RadialGradie ntPa in t(xPos-Poi nt.DIAMETER/2+25, yPos-Point.DIAMETER/2+10, 20, new float0f, 1f,new ColorColor.WHITE, Color.BLACK);(Graphics2D) g).setPa in t(pai nt);(Graphics2D)g).setRe nderi ngHi nt(Re nderi ngHi nts.KEY_ANTIALIASING,Ren deri ngHi nts.VALUE_ANTIALIAS_ON);(Graphics
10、2D)g).setRe nderi ngHi nt(Re nderi ngHi nts.KEY_ALPHA_INTERPOLATION,Re nderi ngHi nts.VALUE_ALPHA_INTERPOLATION_DEFAULT);else if(colortemp=Color.white)RadialGradie ntPai nt pai nt = new RadialGradie ntPa in t(xPos-Poi nt.DIAMETER/2+25,yPos-Point.DIAMETER/2+10, 70, new floatOf, 1f,new ColorColor.WHIT
11、E, Color.BLACK);(Graphics2D) g).setPaint(paint);(Graphics2D)g).setRe nderi ngHi nt(Re nderi ngHi nts.KEY_ANTIALIASING,Ren deri ngHi nts.VALUE_ANTIALIAS_ON);(Graphics2D)g).setRe nderi ngHi nt(Re nderi ngHi nts.KEY_ALPHA_INTERPOLATION,Re nderi ngHi nts.VALUE_ALPHA_INTERPOLATION_DEFAULT);Ellipse2D e =
12、new Ellipse2D.FIoat(xPos-Poi nt.DIAMETER/2, yPos-Poi nt.DIAMETER/2,34, 35);(Graphics2D) g).fill(e);/標記最后一個棋子的紅矩形框if(i=chessCou nt-1)如果是最后一個棋子g.setColor(Color.red);g.drawRect(xPos-Poi nt.DIAMETER/2, yPos-Poi nt.DIAMETER/2,34, 35);public void mousePressed(MouseEve nt e)鼠標在組件上按下時調用/游戲結束時,不再能下if(gameOve
13、r) return;String colorName=isBlack?"黑棋":"白棋"/將鼠標點擊的坐標位置轉換成網格索引xln dex=(e.getX()-MARGIN+GRID_SPAN/2)/GRID_SPAN;yin dex=(e.getY()-MARGIN+GRID_SPAN/2)/GRID_SPAN;/落在棋盤外不能下if(xI ndex<0|xI ndex>ROWS|yl ndex<0|yl ndex>COLS)return;/如果x,y位置已經有棋子存在,不能下if(fin dChess(xI ndex,yI n
14、dex)return;/可以進行時的處理Point ch=new Poin t(x In dex, yin dex,isBlack?Color.black:Color.white);chessListchessCo un t+=ch;repai nt();通知系統(tǒng)重新繪制/如果勝出則給出提示信息,不能繼續(xù)下棋if(isWi n()", colorName);String msg=String.format(”恭喜,%s 贏了!JOpti onPan e.showMessageDialog(this, msg); gameOver=true;isBlack=!isBlack;/覆蓋mo
15、useListener 的方法public void mouseClicked(MouseEve nt e)/鼠標按鍵在組件上單擊時調用public void mouseE ntered(MouseEve nt e)/鼠標進入到組件上時調用public void mouseExited(MouseEve nt e)/鼠標離開組件時調用public void mouseReleased(MouseEve nt e)/鼠標按鈕在組件上釋放時調用/在棋子數組中查找是否有索引為x,y的棋子存在private boolean findChess(int x,int y)for(Po int c:ches
16、sList)if(c!=null &&c.getX()=x&&c.getY()=y)return true;return false;private boolea n isWi n()int con ti nueCou nt=1;連續(xù)棋子的個數/橫向向西尋找for(i nt x=x In dex-1;x>=0;x-)Color c=isBlack?Color.black:Color.white;if(getChess(x, yin dex,c)!=null)con ti nu eCo un t+;elsebreak;/橫向向東尋找for(i nt x=x
17、In dex+1;x<=C0LS;x+)Color c=isBlack?Color.black:Color.white;if(getChess(x, yin dex,c)!=null)con ti nu eCo un t+;elsebreak;if(con ti nu eCo un t>=5)return true;elsecon ti nu eCo un t=1;/繼續(xù)另一種搜索縱向/向上搜索for(i nt y=yln dex_1;y>=0;y_)Color c=isBlack?Color.black:Color.white;if(getChess(x in dex,y,
18、c)!=null)con ti nu eCo un t+;elsebreak;/縱向向下尋找for(i nt y=y In dex+1;y<=R0WS;y+)Color c=isBlack?Color.black:Color.white;if(getChess(x In dex,y,c)!=null)con ti nu eCo un t+;elsebreak;if(con ti nu eCo un t>=5)return true;elsecon ti nu eCo un t=1;/繼續(xù)另一種情況的搜索:斜向/東北尋找for(i nt x=xI ndex+1,y=yI ndex-1
19、;y>=0&& x<=COLS;x+,y-)Color c=isBlack?Color.black:Color.white;if(getChess(x,y,c)!=null)con ti nu eCo un t+;else break;/西南尋找for(i nt x=xI ndex-1,y=yl ndex+1;x>=0&&y<=ROWS;x-,y+)Color c=isBlack?Color.black:Color.white;if(getChess(x,y,c)!=null)con ti nu eCo un t+;else break;
20、if(con ti nu eCo un t>=5)return true;else continu eCo un t=1;/繼續(xù)另一種情況的搜索:斜向/西北尋找for(i nt x=x In dex-1,y=yIn dex-1;x>=0&&y>=0;x_,y_)Color c=isBlack?Color.black:Color.white;if(getChess(x,y,c)!=null)con ti nu eCo un t+;else break;/東南尋找for(i nt x=xI ndex+1,y=yl ndex+1;x<=COLS&&am
21、p; y<=ROWS;x+,y+)Color c=isBlack?Color.black:Color.white;if(getChess(x,y,c)!=null)con ti nu eCo un t+;else break;if(con ti nu eCo un t>=5)return true;else continu eCo un t=1;return false;private Point getChess(i nt xln dex,i nt yin dex,Color color) for(Po int p:chessList)if(p!=null&&p.
22、getX()=xl ndex&&p.getY()=yi ndex&&p.getColor()=color) return p;return nu II;public void restartGame()/清除棋子for(i nt i=O;i<chessList.le ngth;i+) chessListi=nu II;恢復游戲相關的變量值isBlack=true;gameOver=false; / 游戲是否結束 chessCou nt =0; / 當前棋盤棋子個數 repa in t();悔棋public void goback()if(chessCo u
23、n t=0)return ;chessListchessCo un t-1=n ull;chessCo un t-;if(chessCou nt>0)xln dex=chessListchessCo un t-1.getX();yin dex=chessListchessCo un t-1.getY();isBlack=!isBlack;repa in t();/ 矩形 Dimensionpublic Dime nsi on getPreferredSize()return new Dime nsio n(MARGIN*2+GRID_SPAN*COLS,MARGIN*2+GRID_SPA
24、N*ROWS);3.2 棋子類 Point.java*棋子類*/public class Point private int x;棋盤中的x索引private int y;棋盤中的y索引直徑private Color color;/ 顏色public static final int DIAMETER=30;public Poin t(i nt x,i nt y,Color color)this.x=x;this.y=y;this.color=color;public int getX()拿到棋盤中 x的索引return x;public in t getY()return y;public
25、Color getColor()獲得棋子的顏色retur n color;3.3 五子棋主框架類StartChessJFrame.javaimport javax.swi ng.*;/*五子棋主框架類,程序啟動類*/public class StartChessJFrame exte nds JFrame private ChessBoard chessBoard;private JPa nel toolbar;private JButton startButton,backButton,exitButton;private JMe nuBar men uBar;private JMe nu
26、sysMe nu;private JMe nultem startMe nultem,exitMe nultem,backMe nultem;/重新開始,退出,和悔棋菜單項public StartChessJFrame()setTitle(”單機版五子棋");/設置標題chessBoard=new ChessBoard();Container conten tPa ne=getC onten tPa ne();conten tPa ne.add(chessBoard);chessBoard.setOpaque(true);/創(chuàng)建和添加菜單men uBar =new JMe nuBar
27、(); 初始化菜單欄 sysMenu=new JMenu(” 系統(tǒng)");/ 初始化菜單/初始化菜單項startMenultem=new JMenultem(”重新開始");exitMenultem =new JMenultem(”退出");backMe nultem =new JMe nultem(”悔棋");/將三個菜單項添加到菜單上sysMe nu.add(startMe nultem);sysMe nu .add(exitMe nultem);sysMe nu .add(backMe nultem);/初始化按鈕事件監(jiān)聽器內部類MyltemList
28、e ner lis=new MyltemListe ner();/將三個菜單注冊到事件監(jiān)聽器上this.startMe nultem.addActio nListe ner(lis);backMe nultem.addActio nListe ner(lis);exitMe nultem.addActio nListe ner(lis);me nuBar.add(sysMe nu); 將系統(tǒng)菜單添加到菜單欄上 setJMenuBar(menuBar);/ 將 menuBar 設置為菜單欄toolbar= new JPa nel();工具面板實例化/三個按鈕初始化startButton=new JButton("重新開始");exitButton=new JButton(”退出");backButto n=new JButto n(”悔棋");/將工具面板按鈕用FlowLayou
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 合法有效裝修合同范例
- 廚房原材料合同范本
- 農村住宅建房合同范本
- 衛(wèi)材購銷合同范本
- 養(yǎng)殖設備包工合同范本
- 勞務合同范本100例
- 醫(yī)院后勤設備采購合同范本
- 學校供餐服務合同范本
- 勞務兼職培訓合同范本
- 公司裝修改造合同范本
- 全媒體運營師試題庫(含答案)
- 2024至2030年中國礦用隔爆型監(jiān)控攝像儀行業(yè)投資前景及策略咨詢研究報告
- 大學生職業(yè)素養(yǎng)訓練(第六版)課件 第二單元學習職業(yè)禮儀
- 北京市燕山區(qū)中考一模英語試題及答案
- 腦卒中-腦卒中的康復治療
- 2024至2030年中國超聲波加工機床行業(yè)深度調研及發(fā)展預測報告
- 十七個崗位安全操作規(guī)程手冊
- 疫情統(tǒng)計學智慧樹知到答案2024年浙江大學
- 三方資金轉換協(xié)議書范本
- 2024年對口升學真題模擬語文試卷及答案十四
- 初級中學語文教師資格考試學科知識與教學能力2024年下半年測試試題與參考答案
評論
0/150
提交評論