作者:商洛学院-杨小军 子类代码
package org.wuziqi;
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; 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][20]; //标识当前应该是黑棋还是白棋下棋 boolean isBlack=true; //标识当前游戏是否可以继续 boolean canPlay=true; //保存显示提示信息
String message =\黑方先行\; //倒计时,保存最大时间值(秒) int maxTime=0;
1 / 12
//做倒计时的线程类
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.addMouseListener(this);
this.setVisible(true); t.start(); t.suspend(); //刷新屏幕防止开始游戏时出现屏幕加载延时过长 this.repaint(); try {
bgimage = ImageIO.read(new File(\)); } 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, 0, 20,this);
g2.setFont(new Font(\宋体\,Font.BOLD,20)); g2.setColor(Color.RED);
g2.drawString(\木易小舟\, 10, 60); g2.setColor(Color.BLACK);
2 / 12
g2.drawString(\——游戏信息:\+message, 100, 60); g2.setFont(new Font(\黑体\,0,14));
g2.drawString(\黑方时间:\+blackMessage, 30, 475); g2.drawString(\白方时间:\+whiteMessage, 260, 475); //绘制棋盘
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); }
g2.fillOval(69,128,4,4); g2.fillOval(308,128,4,4); g2.fillOval(68,388,4,4); g2.fillOval(308,388,4,4); g2.fillOval(189,248,4,4); g2.fillOval(189,128,4,4); g2.fillOval(189,388,4,4); g2.fillOval(308,248,4,4); g2.fillOval(68,248,4,4); //g.fillOval(x,y,10,10); 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.setColor(Color.BLACK);
g2.fillOval(tempX-7,tempY-7,14,14); }
if(allChess[i][j]==2){ //白子
int tempX=i*20+10; int tempY=j*20+70; g2.setColor(Color.WHITE);
g2.fillOval(tempX-7, tempY-7,14,14); g2.setColor(Color.BLACK);
g2.drawOval(tempX-7,tempY-7, 14, 14); } } }
g.drawImage(bi,0,0,this); } @Override
public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub
3 / 12