java Swing实现五子棋

25 篇文章 1 订阅
13 篇文章 1 订阅

java实现五子棋项目

运行结果

视频经过压缩画质略显粗糙。
在这里插入图片描述

标题

1)绘制网格棋盘:
重写JPanel的paint(Graphics g)方法,绘制16*16网格。
如图:
在这里插入图片描述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<>();//存放已出的所有其中,方便绘图和计算结果
    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();  // 创建画笔

        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<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={"开始","悔棋","投降","帮助","重来"};
        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();
    }
}

具体运行效果

在这里插入图片描述

具体项目代码见:https://download.csdn.net/download/qq_44140450/13609418

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值