国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java Swing實(shí)現(xiàn)五子棋游戲

瀏覽:4日期:2022-08-19 16:01:44

本文實(shí)例為大家分享了java實(shí)現(xiàn)五子棋項(xiàng)目,供大家參考,具體內(nèi)容如下

運(yùn)行結(jié)果

視頻經(jīng)過(guò)壓縮畫(huà)質(zhì)略顯粗糙。

java Swing實(shí)現(xiàn)五子棋游戲

標(biāo)題

1)繪制網(wǎng)格棋盤(pán):

重寫(xiě)JPanel的paint(Graphics g)方法,繪制16*16網(wǎng)格。如圖:

java Swing實(shí)現(xiàn)五子棋游戲

2)代碼如下(僅包含部分代碼):

public class Board extends JPanel{ int width = Toolkit.getDefaultToolkit().getScreenSize().width; int height = Toolkit.getDefaultToolkit().getScreenSize().height; public HashMap<Point,Color> points=new HashMap<>();//存放已出的所有其中,方便繪圖和計(jì)算結(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)建畫(huà)筆 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); //畫(huà)棋盤(pán)橫線 g1.drawLine(starX+i*step, starY, starX+i*step, Max+starY); //畫(huà)棋盤(pán)豎線 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<Point,Color> 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={'開(kāi)始','悔棋','投降','幫助','重來(lái)'}; for(int i=0;i<str.length;i++) { g1.setColor(Color.white); g1.fillRect(140+190*i, starY +Max+45, 150, 50); g1.setColor(Color.black); g1.setStroke(new BasicStroke(50.0f)); g1.setFont(new Font('宋體',Font.BOLD,35)); g1.drawString(str[i], 180+190*i, starY +Max+80);//縱行 } repaint(); }}

具體運(yùn)行效果

java Swing實(shí)現(xiàn)五子棋游戲

具體項(xiàng)目代碼見(jiàn):java五子棋游戲

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 罗平县| 酒泉市| 马公市| 辽源市| 任丘市| 邯郸市| 崇信县| 旅游| 三河市| 长海县| 沙河市| 桓仁| 陇西县| 山东省| 方山县| 徐闻县| 延寿县| 靖州| 三明市| 荔浦县| 师宗县| 历史| 鄂尔多斯市| 莱阳市| 化州市| 龙江县| 陆河县| 辽中县| 合作市| 泰兴市| 东源县| 临汾市| 涿鹿县| 文昌市| 五华县| 易门县| 启东市| 博白县| 贵德县| 瑞安市| 大安市|