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 = DefaultToolkit().getScreenSize().width;
// 获取屏幕长度
int height = DefaultToolkit().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();
//刷新屏幕,防止游戏打开时无法显示。
try {
bgimage = ad(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 ateGraphics();
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);
g2.fillOval(191, 130, 4, 4);
g2.fillOval(191, 370, 4, 4);
// 繪製棋子
/*
* x= (x-13)/20*20+10; y=(y-72)/20*20+70; g.fillOval(x-4, y-4, 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 + 13;
int tempY = j * 20 + 73;
g2.fillOval(tempX - 7, tempY - 7, 14, 14);
}
if (allChess[i][j] == 2) {
/
/ 繪製白子
int tempX = i * 20 + 13;
int tempY = j * 20 + 73;
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
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
// System.out.println("X:"+e.getX());
// System.out.println("Y:"+e.getY());
if (canPlay == true) {
x = e.getX();
y = e.getY();
if (x >= 13 && x <= 373 && y >= 73 && y <= 433) {
x = (x - 13) / 20;
y = (y - 72) / 20;
if (allChess[x][y] == 0) {
// 判断现在要下什么颜的棋子
if (isBlack == true) {
allChess[x][y] = 1;
isBlack = false;
message="轮到白方";
} else {
allChess[x][y] = 2;
isBlack = true;
message="轮到黑方";
}
// 判断这个棋子是否和其他棋子连成五个,即游戏结束
boolean Winflag = this.checkWin();
if (Winflag == true) {
JOptionPane.showMessageDialog(this, "游戏结束"
+ (allChess[x][y] == 1 ? "黑方" : " 白方") + "获胜");
canPlay = false;
}
// this.checkWin();
} else {
JOptionPane.showMessageDialog(this, " 当前位置已经有子,请重新落
子!");
}
}
}
// System.out.X()+"---"+e.getY());
//点击开始按钮
X()>=404&&X()<=472&&Y()>=74&&Y()<=104){
int result=JOptionPane.showConfirmDialog(this, "是否重新开始游戏?");
if(result==0){
//现在重新开始游戏
//重新开始游戏所需的操作,1.把棋盘清空allcChess 这个数组中的数全部
归零;
//2 将游戏信息设置为初始位置;
// 3 将下一步器=棋设置为黑方开始;
for (int i=0;i<19;i++){
for(int j=0;j<19;j++){
allChess[i][j]=0;
}
}
}
//另一种方式allChess= new int [19][19];
message="黑先行";
isBlack=true ;
blackTime=maxTime;
whiteTime=maxTime;
if(maxTime>0){
blackMessage=maxTime/3600+":"
+(maxTime/60-maxTime/3600*60)+":"
+(maxTime-maxTime/60*60);
whiteMessage=maxTime/3600+":"
+(maxTime/60-maxTime/3600*60)+":"
+(maxTime-maxTime/60*60);
}else{
blackMessage="无限制";
whiteMessage="无限制";
}
简单的java游戏代码
}
/
/ 点击游戏设置
X()>=404&&X()<=472&&Y()>=124&&Y()<=154){
String input=JOptionPane.showInputDialog("请输入游戏最大时间,(单位:分
钟),如果输入0,表示时间为无限制!");
try {
maxTime=Integer.parseInt(input)*60;