📄 fivechessframe.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package fivechess.frame;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Point;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 java.util.logging.Level;import java.util.logging.Logger;import javax.imageio.ImageIO;import javax.swing.JFrame;import javax.swing.JOptionPane;public class FiveChessFrame extends JFrame implements MouseListener{ private int width = Toolkit.getDefaultToolkit().getScreenSize().width; private int height = Toolkit.getDefaultToolkit().getScreenSize().height; private BufferedImage backImage = null; private int currentX = 0; private int currentY = 0; //0 无子 1 黑子 2 白子 private int[][] allChess = new int[19][19]; //谁赢 private boolean isBlack = true; //是否继续 private boolean isGoOn = true; //提示信息 private String message = "黑方先走"; 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.addMouseListener(this); this.setVisible(true); String imagePath = System.getProperty("user.dir")+File.separator+"src"+File.separator+"resoures"+File.separator+"background.jpg"; try { backImage = ImageIO.read(new File(imagePath)); } catch (IOException ex) { Logger.getLogger(FiveChessFrame.class.getName()).log(Level.SEVERE, null, ex); } } @Override public void paint(Graphics g){ //使用双缓冲技术防止屏幕闪烁 BufferedImage bi = new BufferedImage(500,500,BufferedImage.TYPE_INT_ARGB); Graphics g2 = bi.createGraphics(); g2.setColor(Color.black); g2.drawImage(backImage,3,22,this); g2.setFont(new Font("黑体",Font.BOLD,20)); g2.drawString("游戏信息:"+message, 130, 60); g2.setFont(new Font("宋体",Font.PLAIN,12)); g2.drawString("黑方时间", 40, 470); g2.drawString("白方时间", 260, 470); //画棋盘 for(int i=0;i<19;i++){ g2.drawLine(12, 72+20*i, 370, 72+20*i); g2.drawLine(12+20*i, 72, 12+20*i, 432); } //描特定点 g2.fillOval(68, 128, 8, 8); g2.fillOval(188, 128, 8, 8); g2.fillOval(308, 128, 8, 8); g2.fillOval(68, 248, 8, 8); g2.fillOval(188, 248, 8, 8); g2.fillOval(308, 248, 8, 8); g2.fillOval(68, 368, 8, 8); g2.fillOval(188, 368, 8, 8); g2.fillOval(308, 368, 8, 8); //下子 for(int i=0;i<19;i++) for(int j=0;j<19;j++){ if(allChess[i][j]==1){ int tmpX = i * 20 + 12; int tmpY = j * 20 + 72; g2.fillOval(tmpX-7, tmpY-7, 14, 14); }else if(allChess[i][j]==2){ int tmpX = i * 20 + 12; int tmpY = j * 20 + 72; g2.setColor(Color.white); g2.fillOval(tmpX-7, tmpY-7, 14, 14); g2.setColor(Color.black); g2.drawOval(tmpX-7, tmpY-7, 14, 14); } } //一次性绘制图片 g.drawImage(bi, 0, 0, this); } @Override public void mouseClicked(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); if(isGoOn){ //判断是否棋盘内 if(x>11 && x<373 && y>71 && y<433){ if(x<22) currentX = (x-12)/20; else currentX = (x-22)/20+1; if(y<82) currentY = (y-72)/20; else currentY = (y-82)/20+1; //判断当前位置是否有子 if(allChess[currentX][currentY] == 0){ if(isBlack){ allChess[currentX][currentY] = 1; isBlack = false; message = "轮到白方"; }else{ allChess[currentX][currentY] = 2; isBlack = true; message = "轮到黑方"; } //判断是否结束 if(this.checkWin()){ JOptionPane.showMessageDialog(this, "游戏结束,"+(allChess[currentX][currentY]==1?"黑方胜!":"白方胜")); isGoOn = false; } }else{ JOptionPane.showMessageDialog(this, "当前位置已经下有棋子,请点击其他空白处落子!"); } this.repaint(); } } if(x>405 && x<470 && y>75 && y<105){ JOptionPane.showMessageDialog(this, "开始游戏"); } if(x>405 && x<470 && y>122 && y<152){ JOptionPane.showMessageDialog(this, "游戏设置"); } if(x>405 && x<470 && y>172 && y<202){ JOptionPane.showMessageDialog(this, "游戏说明"); } if(x>405 && x<470 && y>272 && y<302){ JOptionPane.showMessageDialog(this, "认输"); } if(x>405 && x<470 && y>322 && y<352){ JOptionPane.showMessageDialog(this, "关于"); } if(x>405 && x<470&& y>372 && y<402){ JOptionPane.showMessageDialog(this, "退出"); } } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } private boolean checkWin(){ boolean flag = false; int color = allChess[currentX][currentY]; //横向判断************************************* int i = 1; int count = 1; //纵向 count = this.getCount(0,1,color); if(count==5) flag = true; else{ //横向 count = this.getCount(1,0,color); if(count==5) flag = true; else{ //斜上 count = this.getCount(1,1,color); if(count==5) flag = true; else{ //斜下 count = this.getCount(1,-1,color); if(count==5) flag = true; } } } return flag; } private int getCount(int xChange, int yChange, int color){ int count = 1; int xTemp = xChange; int yTemp = yChange; //向右 while(color == allChess[currentX+xChange][currentY+yChange]){ count++; if(xChange !=0) xChange++; if(yChange !=0) if(yChange > 0) yChange++; else yChange--; } xChange = xTemp; yChange = yTemp; //向左 while(color == allChess[currentX-xChange][currentY-yChange]){ count++; if(xChange !=0) xChange++; if(yChange !=0) if(yChange > 0) yChange++; else yChange--; } return count; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -