這篇文章將為大家詳細(xì)講解有關(guān)怎么在java中利用Swing實現(xiàn)一個五子棋小游戲,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
成都創(chuàng)新互聯(lián)專注于蓬安企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計,成都商城網(wǎng)站開發(fā)。蓬安網(wǎng)站建設(shè)公司,為蓬安等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站建設(shè),專業(yè)設(shè)計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)實現(xiàn)代碼如下:
public class Board extends JPanel{ int width = Toolkit.getDefaultToolkit().getScreenSize().width; int height = Toolkit.getDefaultToolkit().getScreenSize().height; public HashMappoints=new HashMap<>();//存放已出的所有其中,方便繪圖和計算結(jié)果 Board(){ this.setVisible(true); drawBoard(); } public int starX=300,starY=40,step=40,piecesR=28; BufferedImage buf; @Override public void paint(Graphics g){ g.drawImage(buf, 0, 0,this); } void drawL(Point p,Graphics2D g1,int length,int bound){ int x1=p.x-bound; int x2=p.x-bound-length; int x3=p.x+bound; int x4=p.x+bound+length; int y1=p.y-bound; int y2=p.y-bound-length; int y3=p.y+bound; int y4=p.y+bound+length; g1.drawLine(x1,y1,x2,y1); g1.drawLine(x1,y1,x1,y2); g1.drawLine(x1,y3,x2,y3); g1.drawLine(x1,y3,x1,y4); g1.drawLine(x3,y3,x3,y4); g1.drawLine(x3,y3,x4,y3); g1.drawLine(x3,y1,x4,y1); g1.drawLine(x3,y1,x3,y2); } public void drawBoard(){ int Max=step*15; buf = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB); Graphics2D g1 = buf.createGraphics(); // 創(chuàng)建畫筆 g1.addRenderingHints((Map)(Toolkit.getDefaultToolkit().getDesktopProperty("awt.font.desktophints"))); g1.setColor(new Color(46,139,87));//60,179,113//0,255,127 g1.fillRect(0,0,this.width,this.height); g1.setColor(new Color(106,90,205)); g1.fill3DRect(starX, starY, Max, Max, true); for (int i = 0; i <=15; i++) { g1.setColor(Color.WHITE); g1.drawLine(starX, starY+i*step, Max+starX, starY+i*step); //畫棋盤橫線 g1.drawLine(starX+i*step, starY, starX+i*step, Max+starY); //畫棋盤豎線 g1.setColor(Color.black); g1.setFont(new Font("楷體",Font.BOLD,15)); g1.drawString(numToCh(i),starX-((numToCh(i).length()==1)?25:35),starY+i*step+5);//縱行 g1.drawString(numToCh(i),starX+i*step-((numToCh(i).length()==1)?5:15),starY-8);//橫行 g1.setColor(Color.BLACK); g1.setStroke(new BasicStroke(2.0f)); if(i==4) { drawL(new Point(starX+i*step,starY+i*step),g1,8,4); drawL(new Point(starX+i*step,starY+12*step),g1,8,4); } if(i==12){ drawL(new Point(starX+i*step,starY+i*step),g1,8,4); drawL(new Point(starX+i*step,starY+4*step),g1,8,4); } Iterator iterator=points.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry entry=(Map.Entry)iterator.next(); g1.setColor(entry.getValue()); g1.fillOval(starX+(entry.getKey().x)*step-17,starY+(entry.getKey().y)*step-17,34,34); } String []str={"開始","悔棋","投降","幫助","重來"}; for(int i=0;i
新聞標(biāo)題:怎么在java中利用Swing實現(xiàn)一個五子棋小游戲-創(chuàng)新互聯(lián)
本文鏈接:http://weahome.cn/article/jsigj.html