java课设五子棋悔棋重玩计时Word格式.docx
《java课设五子棋悔棋重玩计时Word格式.docx》由会员分享,可在线阅读,更多相关《java课设五子棋悔棋重玩计时Word格式.docx(13页珍藏版)》请在冰豆网上搜索。
![java课设五子棋悔棋重玩计时Word格式.docx](https://file1.bdocx.com/fileroot1/2022-11/28/afb75ae3-33d2-4b63-ae28-1e32f8147618/afb75ae3-33d2-4b63-ae28-1e32f81476181.gif)
三、系统运行及调试
}packageorg.liky.game.frame;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.Toolkit;
importjava.awt.event.MouseEvent;
importjava.awt.event.MouseListener;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjavax.imageio.ImageIO;
importjavax.swing.JFrame;
importjavax.swing.JOptionPane;
publicclassFiveChessFrameextendsJFrameimplementsMouseListener,Runnable{
privatestaticfinallongserialVersionUID=1L;
//取得屏幕的宽度
intwidth=Toolkit.getDefaultToolkit().getScreenSize().width;
//取得屏幕的高度
intheight=Toolkit.getDefaultToolkit().getScreenSize().height;
//背景图片
BufferedImagebgImage=null;
//保存棋子的坐标
intx=0;
inty=0;
//保存之前下过的全部棋子的坐标
//其中数据内容0:
表示这个点并没有棋子,1:
表示这个点是黑子,2:
表示这个点是白子
int[][]allChess=newint[19][19];
//标识当前应该黑棋还是白棋下下一步
booleanisBlack=true;
//标识当前游戏是否可以继续
booleancanPlay=true;
//保存显示的提示信息
Stringmessage="
黑房先行"
;
//保存最多拥有多少时间】
intmaxTime=0;
//做倒计时的线程类
Threadt=newThread(this);
//保存黑房与白方的剩余时间
intblackTime=0;
intwhiteTime=0;
//保存双方的剩余时间的显示信息
StringblackMessage="
无限制"
StringwhiteMessage="
//privateintx1;
//publicFiveChessFrame()throwsHeadlessException{
//@SuppressWarnings("
deprecation"
)
@SuppressWarnings("
publicFiveChessFrame(){
//设置标题
this.setTitle("
我的五子棋"
);
//设置窗体的大小
this.setSize(500,500);
//设置窗体处出现的位置
//this.setLocation(500/2,500/2);
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();
//StringimagePath="
"
try{
//imagePath=System.getProperty("
user.dir"
)+"
/bin/image/background.jpg"
StringimagePath="
D:
/mywokspace"
+File.separator+"
psbaaaa.jpg"
bgImage=ImageIO.read(newFile(imagePath.replaceAll("
\\\\"
"
/"
)));
}catch(IOExceptione){
//TDDOAuto-generatedcatchblock
e.printStackTrace();
}
this.repaint();
publicvoidpaint(Graphicsg){
//双缓冲技术防止屏幕闪烁
BufferedImagebi=newBufferedImage(500,500,
BufferedImage.TYPE_INT_RGB);
Graphicsg2=bi.createGraphics();
//g2.setColor(Color.BLACK);
g2.setColor(Color.RED);
//绘制背景
g2.drawImage(bgImage,1,20,this);
//输出标题信息
g2.setFont(newFont("
黑体"
Font.BOLD,20));
g2.drawString("
游戏信息:
+message,130,60);
//输出时间信息
宋体"
0,14));
黑方时间:
+blackMessage,30,470);
白方时间:
+whiteMessage,260,470);
g2.drawRect(400,70,70,30);
重玩"
420,90);
/*
g2.setColor(BLACK);
//setColor(BLACK);
g2.fillRect(400,70,70,30);
*/
//buttonbuta=newbutton();
g2.drawRect(400,120,70,30);
游戏设置"
410,140);
g2.drawRect(400,270,70,30);
认输"
420,290);
g2.drawRect(400,170,70,30);
游戏说明"
410,190);
g2.drawRect(400,320,70,30);
关于"
420,340);
g2.drawRect(400,370,70,30);
退出"
420,390);
//绘制棋盘
for(inti=1;
i<
19;
i++){
g2.drawLine(10,70+20*i,370,70+20*i);
g2.drawLine(10+20*i,70,10+20*i,430);
//g2.setFont(newFont("
//g2.drawString("
游戏信息:
+message,)
//标注标点
//可能是无用的
g2.fillOval(68,128,4,4);
g2.fillOval(308,128,4,4);
g2.fillOval(308,368,4,4);
g2.fillOval(68,368,4,4);
g2.fillOval(308,248,4,4);
g2.fillOval(188,128,4,4);
g2.fillOval(68,248,4,4);
g2.fillOval(188,366,4,4);
g2.fillOval(188,248,4,4);
*绘制棋子x=(x-10)/20*20+10;
y=(y-70)/20*20+70;
*黑子g.fillOval(x-7,y-7,14,14);
白子g.setColor(Color.WHITE);
*g.fillOval(x-7,y-7,14,14);
g.setColor(Color.BLACK);
*g.drawOval(x-7,y-7,14,14);
//绘制全部棋子
for(inti=0;
i<
for(intj=0;
j<
j++){
if(allChess[i][j]==1){
//黑子
inttempX=i*20+10;
inttempY=j*20+70;
g2.setColor(Color.BLACK);
g2.fillOval(tempX-7,tempY-7,14,14);
//g2.drawOval(tempX-7,tempY-7,14,14);
if(allChess[i][j]==2){
//白子
g2.setColor(Color.WHITE);
g2.fillOval(tempX-7,tempY-7,14,14);
//g2.setColor(Color.BLACK);
g.drawImage(bi,0,0,this);
//(@Override
publicvoidmouseClicked(MouseEvente){
//TODOAuto-generatedmethodstub
//注释下面一行
//JOptionPane.showMessageDialog(this,this.getX());
//JOptionPane.showMessageDialog(this,this.getY());
//JOptionPane.showMessageDialog(this,"
落子"
@Override
publicvoidmousePressed(MouseEvente){
*System.out.println("
X:
+e.getX());
Y:
+e.getY());
if(canPlay==true){
x=e.getX();
y=e.getY();
//JOptionPane.showMessageDialog(this,x+"
+++++"
+y);
if(x>
=10&
&
x<
370&
y>
=70&
y<
=430){
//判断棋子连接的数量
privateintcheckCount(intxChange,intyChange,intcolor){
intcount=1;
inttempX=xChange;
inttempY=yChange;
while(x+xChange>
=0&
x+xChange<
=18&
y+yChange>
=0
y+yChange<
=18
color==allChess[x+xChange][y+yChange]){
count++;
if(xChange!
=0)
xChange++;
if(yChange!
=0){
yChange++;
else{
yChange--;
xChange=tempX;
yChange=tempY;
while(x-xChange>
x-xChange<
y-yChange>
y-yChange<
color==allChess[x-xChange][y-yChange]){
if(yChange>
0)
returncount;
//没有下两个只有run
publicvoidmouseEntered(MouseEvente){
//e.getX();
//e.getY();
//JOptionPane.showMessageDialog,e.getX());
publicvoidmouseExited(MouseEvente){
publicvoidrun(){
//判断是否有时间限制
if(maxTime>
while(true){
if(isBlack){
blackTime--;
if(blackTime==0){
JOptionPane.showMessageDialog(this,"
黑方时间超时,游戏结束!
}else{
whiteTime--;
if(whiteTime==0){
JOptionPane.showMessageDialog(this,"
白方时间超时,游戏结束!
四、总结
通过java的课程设计,是我进一步掌握了java的各项功能作用,熟练了java的运行方法,对于将来从事java行业打下了一定的基础,在课程设计的过程中,我们组遇到了各种各样的问题,通过参考相关书籍和文献使我们对java有了更深一步的了解,克服困难后,使我们有了前所未有的满足感,真正投入到设计中,才会真正感受到快乐和成就。