java赛马游戏.docx

上传人:b****3 文档编号:11116307 上传时间:2023-05-29 格式:DOCX 页数:20 大小:110.65KB
下载 相关 举报
java赛马游戏.docx_第1页
第1页 / 共20页
java赛马游戏.docx_第2页
第2页 / 共20页
java赛马游戏.docx_第3页
第3页 / 共20页
java赛马游戏.docx_第4页
第4页 / 共20页
java赛马游戏.docx_第5页
第5页 / 共20页
java赛马游戏.docx_第6页
第6页 / 共20页
java赛马游戏.docx_第7页
第7页 / 共20页
java赛马游戏.docx_第8页
第8页 / 共20页
java赛马游戏.docx_第9页
第9页 / 共20页
java赛马游戏.docx_第10页
第10页 / 共20页
java赛马游戏.docx_第11页
第11页 / 共20页
java赛马游戏.docx_第12页
第12页 / 共20页
java赛马游戏.docx_第13页
第13页 / 共20页
java赛马游戏.docx_第14页
第14页 / 共20页
java赛马游戏.docx_第15页
第15页 / 共20页
java赛马游戏.docx_第16页
第16页 / 共20页
java赛马游戏.docx_第17页
第17页 / 共20页
java赛马游戏.docx_第18页
第18页 / 共20页
java赛马游戏.docx_第19页
第19页 / 共20页
java赛马游戏.docx_第20页
第20页 / 共20页
亲,该文档总共20页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

java赛马游戏.docx

《java赛马游戏.docx》由会员分享,可在线阅读,更多相关《java赛马游戏.docx(20页珍藏版)》请在冰点文库上搜索。

java赛马游戏.docx

java赛马游戏

赛马游戏源代码:

(截图)

packagecom.guigu.king.core;

publicinterfaceHandleListener

{

/**

*完成比赛

*/

publicvoidgameHourse(HourseEvente);

/**

*比赛结束(线程结局)

*/

publicvoidgameOver(HourseEvente);

}

packagecom.guigu.king.core;

publicclassHourseEvent

{

privateObjectsource=null;

privatebooleanerror=false;

privateExceptionexception=null;

publicHourseEvent(Objectsource)

{

this.source=source;

}

publicObjectgetSource(){

returnsource;

}

publicbooleanisError(){

returnerror;

}

publicvoidsetError(booleanerror){

this.error=error;

}

publicExceptiongetException(){

returnexception;

}

publicvoidsetException(Exceptionexception){

this.exception=exception;

}

}

packagecom.guigu.king.core;

importjava.awt.Image;

importjava.awt.Toolkit;

importjava.util.Random;

/**

*赛马类

*@authorAdministrator

*/

publicclassHourseRaceimplementsRunnable

{

publicHandleListenerhandle=null;

publicvoidaddHandleListener(HandleListenerhandle)

{

this.handle=handle;

}

privateStringname="";//赛马名称

privateImageimg=null;//赛马图片

privateintlength=0;//赛马的步长(每跑一步的距离)

privateint[]rate=null;//赛马的步频(每一次奔跑的时间间隔)

publicHourseRace(Stringname,StringimgPath,intlength,int[]rate)

{

this.name=name;

this.img=Toolkit.getDefaultToolkit().getImage(imgPath);

this.length=length;

this.rate=rate;

}

privateintx=0;//赛马当前赛跑的位置

privateintrange=0;//赛跑的距离

privateintstop=0;//赛跑的距离

publicvoidsetRaceData(intrange,intstop)

{

this.x=0;

this.range=range;

this.stop=stop;

}

publicvoidrun()

{

HourseEventevent=newHourseEvent(this);

try

{

booleanb=true;

Randomr=newRandom();

while(true)

{

intn=r.nextInt(rate.length);

if(b&&this.x>=this.range-116)

{

b=false;

if(handle!

=null)handle.gameHourse(event);

}

this.x=this.x+this.length;

Thread.sleep(rate[n]);

if(x>=this.stop)

{

break;

}

}

}

catch(Exceptione)

{

event.setError(true);

event.setException(e);

}

this.x=0;

if(handle!

=null)handle.gameOver(event);

}

publicStringgetName(){

returnname;

}

publicImagegetImg(){

returnimg;

}

publicintgetX(){

returnx;

}

}

packagecom.guigu.king.core;

/**

*赛场类

*@authorAdministrator

*/

publicclassMatch

{

privateStringname="";//赛场的名称

privateintrange=0;//赛跑的距离

privateintstop=0;//停跑的距离

privateHourseRace[]track=null;//赛道数量

privateintHourseRaceCount=0;//赛马数量

publicMatch(Stringname,intrange,intstop,intcount)

{

this.name=name;

this.range=range;

this.stop=stop;

this.track=newHourseRace[count];

}

publicvoidaddHourseRace(HourseRacehr)

{

if(hr!

=null)

{

hr.setRaceData(range,stop);

if(this.HourseRaceCount

{

this.track[this.HourseRaceCount]=hr;

this.HourseRaceCount++;

}

}

}

publicintgetMatchTrack()

{

returnthis.track.length;

}

publicHourseRacegetHourseRace(intindex)

{

returnthis.track[index];

}

publicStringgetName(){

returnname;

}

publicintgetRange(){

returnrange;

}

publicintgetStop(){

returnstop;

}

}

packagecom.guigu.king.exec;

publicclassCMain

{

/**

*@paramargs

*/

publicstaticvoidmain(String[]args)

{

MainFramemf=newMainFrame();

mf.setVisible(true);

}

}

packagecom.guigu.king.exec;

importjava.awt.*;

importjavax.swing.*;

importcom.guigu.king.core.*;

publicclassGraphicPanelextendsJPanel

{

/**

*

*/

privatestaticfinallongserialVersionUID=2441325657329104186L;

privateMatchmatch=null;

publicGraphicPanel(Matchmatch)

{

this.match=match;

this.setBackground(Color.WHITE);

}

//绘图函数:

函数重写,该函数的调用,是由系统调用。

其参数Graphics就是我们所说的画笔。

publicvoidpaint(Graphicsg)

{

super.paint(g);//调用其父类的绘图函数,向透明面板上刷背景色。

g.drawString(this.match.getName(),6,16);

for(inti=0;i

{

g.drawString(Integer.toString(i+1)+".",10,55+i*65);

if(this.match.getHourseRace(i)!

=null)

{

g.drawString(this.match.getHourseRace(i).getName(),22,55+i*65);

g.drawImage(this.match.getHourseRace(i).getImg(),this.match.getHourseRace(i).getX(),26+i*65,this);

}

}

g.setColor(Color.RED);

g.fillRect(this.match.getRange(),0,3,330);

}

}

packagecom.guigu.king.exec;

importjava.awt.*;

importjavax.swing.*;

importcom.guigu.king.core.*;

publicclassMainFrameextendsJFrame

{

/**

*

*/

privatestaticfinallongserialVersionUID=-5220804651454432075L;

privateGraphicPanelgp=null;

privateOperatorPanelop=null;

privateMatchmatch=null;

publicMainFrame()

{

this.setTitle("赛马游戏");

this.setSize(800,600);

this.setResizable(false);

this.setLocationRelativeTo(null);

this.setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);

//===================================================================================

this.match=newMatch("赛道",700,this.getSize().width,4);

this.match.addHourseRace(newHourseRace("汗血","images/hourse.gif",13,newint[]{50,80,30}));

this.match.addHourseRace(newHourseRace("追风","images/hourse.gif",15,newint[]{50,80,60}));

this.match.addHourseRace(newHourseRace("赤兔","images/hourse.gif",16,newint[]{50,80,80}));

this.match.addHourseRace(newHourseRace("得卢","images/hourse.gif",18,newint[]{50,80,90}));

//===================================================================================

this.gp=newGraphicPanel(match);

this.op=newOperatorPanel(match);

this.setLayout(newGridLayout(2,1));

this.getContentPane().add(gp);

this.getContentPane().add(op);

//this.setBackground(Color.BLUE);

this.getContentPane().setBackground(Color.BLUE);

}

}

packagecom.guigu.king.exec;

importjava.awt.*;

importjava.awt.event.*;

importjavax.swing.*;

importcom.guigu.king.core.*;

publicclassOperatorPanelextendsJPanelimplementsActionListener,HandleListener

{

/**

*

*/

privatestaticfinallongserialVersionUID=-5609587650586439817L;

privateButtonGroupbg=newButtonGroup();

privateJRadioButtonjrb1=newJRadioButton("汗血");

privateJRadioButtonjrb2=newJRadioButton("追风");

privateJRadioButtonjrb3=newJRadioButton("赤兔");

privateJRadioButtonjrb4=newJRadioButton("得卢");

privateJTextFieldtxtMoney=newJTextField("");

privateJButtonbutStart=newJButton("开始游戏");

privateJLabeljInfo=newJLabel("请选择一匹马:

");

privateJLabeljMoney=newJLabel("您的资金总额:

10000");

privateJLabeljError=newJLabel("请输入正确金额");

privateJPaneljp=newJPanel();

privatejava.awt.Listlist=newjava.awt.List();//界面组件

privateMatchmatch=null;

publicOperatorPanel(Matchmatch)

{

this.match=match;

this.setLayout(newBorderLayout());

this.add(this.jp,BorderLayout.NORTH);

this.add(this.list);//默认中间

this.jp.setLayout(newGridLayout(4,3));

this.jp.add(jInfo);

this.jp.add(jrb1);

this.jp.add(jMoney);

this.jp.add(newLabel());

this.jp.add(jrb2);

this.jp.add(txtMoney);

this.jp.add(newLabel());

this.jp.add(jrb3);

this.jp.add(jError);

this.jp.add(newLabel());

this.jp.add(jrb4);

this.jp.add(butStart);

this.bg.add(jrb1);this.bg.add(jrb2);this.bg.add(jrb3);this.bg.add(jrb4);

this.jrb1.setSelected(true);

this.jError.setForeground(Color.RED);

this.jError.setVisible(false);

this.butStart.addActionListener(this);

for(inti=0;i

{

if(this.match.getHourseRace(i)!

=null)

{

this.match.getHourseRace(i).addHandleListener(this);

this.length++;

}

}

}

@Override

publicvoidactionPerformed(ActionEvente)

{

if(e.getSource()==butStart)

{

try

{

if(this.txtMoney.getText().equals(""))

{

this.jError.setText("金额不能为空");

this.jError.setVisible(true);

return;

}

intiMoney=Integer.parseInt(this.txtMoney.getText());

Stringtemp=this.jMoney.getText().split(":

")[1];

inttMoney=Integer.parseInt(temp);

if(iMoney>0&&iMoney

{

for(inti=0;i

{

if(this.match.getHourseRace(i)!

=null)

{

newThread(this.match.getHourseRace(i)).start();

}

}

this.count=0;

this.end=0;

this.list.removeAll();

this.jrb1.setEnabled(false);

this.jrb2.setEnabled(false);

this.jrb3.setEnabled(false);

this.jrb4.setEnabled(false);

this.txtMoney.setEnabled(false);

this.butStart.setEnabled(false);

this.jError.setVisible(false);

}

else

{

this.jError.setText("输入的数字操作有效范围");

this.jError.setVisible(true);

}

}

catch(NumberFormatExceptionex)

{

this.jError.setText("请输入正确的数字");

this.jError.setVisible(true);

}

}

}

privateintcount=0;

privateintlength=0;

privateHourseRacehr=null;

@Override

publicsynchronizedvoidgameHourse(HourseEvente)

{

this.count++;

HourseRacehr=(HourseRace)e.getSource();

this.list.add(hr.getName()+"比赛结束。

比赛名次:

第"+this.count+"名");

if(this.list.getItemCount()==1)

{

this.hr=hr;

}

}

privateintend=0;

@Override

publicsynchronizedvoidgameOver(HourseEvente)

{

if(e.isError())

{

HourseRacehr=(HourseRace)e.getSource();

this.list.add(hr.getName()+"受伤,已退出比赛。

");

}

this.end++;

if(this.end==this.length)

{

this.list.add(this.hr.getName()+"是冠军,赢得比赛。

");

inti=-1;

if(this.jrb1.isSelected())

{

i=0;

}

elseif(this.jrb2.isSelected())

{

i=1;

}

elseif(this.jrb3.isSelected())

{

i=2;

}

elseif(this.jrb4.isSelected())

{

i=3;

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 小学教育 > 语文

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2