dabocaiqq 2020-05-23 10:16 采纳率: 63.3%
浏览 129
已采纳

高分悬赏:Java语言实现象棋棋盘的输出,要求在控制台的环境下完成

高分悬赏:Java语言实现象棋棋盘的输出,要求在控制台的环境下完成

  • 写回答

2条回答

  • 毕小宝 博客专家认证 2020-06-04 12:26
    关注

    控制台呈现不够形象,实现功能也不太方便,这里有一个五子棋棋盘的绘制方法,可以参考:

    public class FiveChessGame extends JFrame implements MouseListener, Runnable {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
    
        public static void main(String args[]) {
            FiveChessGame ff = new FiveChessGame();
            ff.setVisible(true);
        }
    
        int height = Toolkit.getDefaultToolkit().getScreenSize().height;
        int width = Toolkit.getDefaultToolkit().getScreenSize().width;
    
        BufferedImage bgImage = null;// 缓存图片
        int x = 0;
        int y = 0;
    
        int allChess[][] = new int[19][19];// 设置棋盘大小
        boolean isBlack = true;
        boolean canPlay = true;
    
        String message = "黑方先行";
        int maxTime = 0;
        Thread t = new Thread(this);// 创建一个线程
        int blackTime = 0;
        int whiteTime = 0;
        String blackMessage = "无限制";
        String whiteMessage = "无限制";
    
        public FiveChessGame() {
            this.setTitle("五子棋");
            this.setSize(600, 600);// 设置窗体大小
            this.setResizable(true);// 窗体是否可改变大小
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 单击窗口的关闭按钮
            this.setLocation((width - 500) / 2, (height - 500) / 2);// 离显示屏上下,左右像素
            this.addMouseListener(this);// 处理鼠标事件
            this.setVisible(true); // 窗体可视
    
            t.start();// 开始线程
            this.repaint();
    
            try {
                bgImage = ImageIO.read(new File("src/game/bgImage.jpg"));
            } catch (IOException e) {
                e.printStackTrace();// 运行时自动将io流异常初始化,并打印出程序的异常信息
            }
        }
    
        public void paint(Graphics g) {
            BufferedImage bi = new BufferedImage(500, 500, BufferedImage.TYPE_INT_ARGB);
            Graphics g2 = bi.createGraphics();
    
            g2.drawImage(bgImage, 0, 0, this);// 信息参数
            g2.setColor(Color.black);
            g2.setFont(new Font("黑体", Font.BOLD, 25));
            g2.drawString("游戏信息: " + message, 20, 60);
            g2.setFont(new Font("宋体", Font.BOLD, 20));
            g2.setColor(Color.white);
            g2.fillRect(15, 460, 185, 25);
            g2.fillRect(255, 460, 185, 25);
            g2.setColor(Color.black);
            g2.drawString("黑方时间:" + blackMessage, 20, 480);
            g2.drawString("白方时间:" + whiteMessage, 260, 480);
    
            g2.setColor(Color.yellow);
            g2.fill3DRect(390, 90, 90, 30, true);// x坐标,y坐标,长,宽,false凹 true凸
            g2.fill3DRect(390, 140, 90, 30, true);
            g2.fill3DRect(390, 190, 90, 30, true);
            g2.fill3DRect(390, 240, 90, 30, true);
            g2.fill3DRect(390, 290, 90, 30, true);
            g2.fill3DRect(390, 340, 90, 30, true);
            g2.fill3DRect(390, 395, 90, 30, true);
    
            g2.setColor(Color.red);
            g2.drawString("开始游戏", 394, 113);// 框体中的内容,x坐标。y坐标
            g2.drawString("游戏设置", 394, 163);
            g2.drawString("游戏说明", 394, 213);
            g2.drawString("暂停", 412, 263);
            g2.drawString("继续", 412, 313);
            g2.drawString("认输", 412, 363);
            g2.drawString("退出", 412, 418);
    
            g2.setColor(Color.black);
    
            //绘制棋盘,19 条横线和 19 条纵线
            for (int i = 0; i < 19; i++) {
                g2.drawLine(10, 70 + 20 * i, 370, 70 + 20 * i);
                g2.drawLine(10 + 20 * i, 70, 10 + 20 * i, 430);
            }
    
            //绘制棋盘上 8 个黑点
            g2.fillOval(66, 126, 8, 8);
            g2.fillOval(306, 126, 8, 8);
            g2.fillOval(306, 366, 8, 8);
            g2.fillOval(66, 366, 8, 8);
            g2.fillOval(306, 246, 8, 8);
            g2.fillOval(186, 126, 8, 8);
            g2.fillOval(66, 246, 8, 8);
            g2.fillOval(186, 366, 8, 8);
            g2.fillOval(186, 246, 8, 8);// 设置棋盘上的九个星
    
            for (int i = 0; i < 19; i++) {
                for (int j = 0; j < 19; j++) {
                    if (allChess[i][j] == 1) {
                        int tempX = i * 20 + 10;
                        int tempY = j * 20 + 70;
                        g2.fillOval(tempX - 8, tempY - 8, 16, 16);
                    }
                    if (allChess[i][j] == 2) {
                        int tempX = i * 20 + 10;
                        int tempY = j * 20 + 70;
                        g2.setColor(Color.white);
                        g2.fillOval(tempX - 8, tempY - 8, 16, 16);
                        g2.setColor(Color.black);
                        g2.drawOval(tempX - 8, tempY - 8, 16, 16);
                    }
                }
            }
            g.drawImage(bi, 0, 0, this);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler