枪神ZZ 枪神ZZ
关注数: 6 粉丝数: 15 发帖数: 388 关注贴吧数: 8
五子棋 package game.frame; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.HeadlessException; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.JFrame; import javax.swing.JOptionPane; public class FiveChessFrame extends JFrame implements MouseListener,Runnable { // 获取屏幕宽度 int width = Toolkit.getDefaultToolkit().getScreenSize().width; // 获取屏幕长度 int height = Toolkit.getDefaultToolkit().getScreenSize().height; // 背景图片 BufferedImage bgimage = null; // 保存棋子坐标 int x = 0; int y = 0; // 保存之前下过的全部棋子的座标 // 其中数据内容0 表示 这个点并没有棋子,1 表示这个点是黑子, 2表示这个点是白点 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 FiveChessFrame() { // 设置标题 this.setTitle("五子棋"); // 设置窗体大小 this.setSize(500, 500); // 设置窗体出现位置 this.setLocation((width - 500) / 2, (height - 500) / 2); // 大小不可变 this.setResizable(false); // 关闭 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 显示 this.setVisible(true); //将窗体加入监听器 this.addMouseListener(this); t.start(); t.suspend(); //刷新屏幕,防止游戏打开时 无法显示。 this.repaint(); try { bgimage = ImageIO.read(new File("F:/background.jpg")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void paint(Graphics g) { //双缓冲技术 防止画面闪烁 BufferedImage bi=new BufferedImage(500, 500,BufferedImage.TYPE_INT_ARGB ); Graphics g2=bi.createGraphics(); g2.drawImage(bgimage, 3, 22, this); g2.setFont(new Font("宋体", Font.BOLD, 20)); g2.setColor(Color.black); g2.drawString("游戏信息:"+message, 120, 60); g2.setFont(new Font("华文行楷", 0, 18)); // 设置时间界面 g2.drawString("黑方时间:"+blackMessage, 28, 470); g2.drawString("白方时间:"+whiteMessage, 250, 470); for (int i = 0; i < 19; i++) { // 画棋盘线 g2.drawLine(13, 72 + 20 * i, 373, 72 + 20 * i); g2.drawLine(13 + 20 * i, 72, 13 + 20 * i, 432); } // 标注点位 g2.fillOval(71, 130, 4, 4); g2.fillOval(71, 370, 4, 4); g2.fillOval(311, 130, 4, 4); g2.fillOval(311, 370, 4, 4); g2.fillOval(191, 250, 5, 5); g2.fillOval(71, 250, 4, 4); g2.fillOval(311, 250, 4, 4);
1 下一页