java赛马游戏

  • 格式:doc
  • 大小:189.00 KB
  • 文档页数:14

赛马游戏源代码:(截图)package com.guigu.king.core;public interface HandleListener{/*** 完成比赛*/public void gameHourse(HourseEvent e);/*** 比赛结束(线程结局)*/public void gameOver(HourseEvent e); }package com.guigu.king.core;public class HourseEvent{private Object source=null;private boolean error=false;private Exception exception=null;public HourseEvent(Object source){this.source=source;}public Object getSource() {return source;}public boolean isError() {return error;}public void setError(boolean error) {this.error = error;}public Exception getException() {return exception;}public void setException(Exception exception) { this.exception = exception;}}package com.guigu.king.core;import java.awt.Image;import java.awt.Toolkit;import java.util.Random;/*** 赛马类* @author Administrator*/public class HourseRace implements Runnable{public HandleListener handle=null;public void addHandleListener(HandleListener handle){this.handle=handle;}private String name="";//赛马名称private Image img=null;//赛马图片private int length=0;//赛马的步长(每跑一步的距离)private int[] rate=null;//赛马的步频(每一次奔跑的时间间隔)public HourseRace(String name,String imgPath,int length,int[] rate) {=name;this.img=Toolkit.getDefaultToolkit().getImage(imgPath);this.length=length;this.rate=rate;}private int x=0;//赛马当前赛跑的位置private int range=0;//赛跑的距离private int stop=0;//赛跑的距离public void setRaceData(int range,int stop){this.x=0;this.range=range;this.stop=stop;}public void run(){HourseEvent event=new HourseEvent(this);try{boolean b=true;Random r=new Random();while(true){int n=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(Exception e){event.setError(true);event.setException(e);}this.x=0;if (handle!=null) handle.gameOver(event);}public String getName() {return name;}public Image getImg() {return img;}public int getX() {return x;}}package com.guigu.king.core;/*** 赛场类* @author Administrator*/public class Match{private String name="";//赛场的名称private int range=0;//赛跑的距离private int stop=0;//停跑的距离private HourseRace[] track=null;//赛道数量private int HourseRaceCount=0;//赛马数量public Match(String name,int range,int stop,int count) {=name;this.range=range;this.stop=stop;this.track=new HourseRace[count];}public void addHourseRace(HourseRace hr){if (hr!=null){hr.setRaceData(range, stop);if (this.HourseRaceCount<this.track.length){this.track[this.HourseRaceCount]=hr;this.HourseRaceCount++;}}}public int getMatchTrack(){return this.track.length;}public HourseRace getHourseRace(int index){return this.track[index];}public String getName() {return name;}public int getRange() {return range;public int getStop() {return stop;}}package com.guigu.king.exec;public class CMain{/*** @param args*/public static void main(String[] args){MainFrame mf=new MainFrame();mf.setVisible(true);}}package com.guigu.king.exec;import java.awt.*;import javax.swing.*;import com.guigu.king.core.*;public class GraphicPanel extends JPanel{/****/private static final long serialVersionUID = 2441325657329104186L;private Match match=null;public GraphicPanel(Match match){this.match=match;this.setBackground(Color.WHITE);}//绘图函数:函数重写,该函数的调用,是由系统调用。

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

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

g.drawString(this.match.getName(), 6, 16);for (int i=0;i<this.match.getMatchTrack();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);}}package com.guigu.king.exec;import java.awt.*;import javax.swing.*;import com.guigu.king.core.*;public class MainFrame extends JFrame{/****/private static final long serialVersionUID = -5220804651454432075L;private GraphicPanel gp=null;private OperatorPanel op=null;private Match match=null;public MainFrame(){this.setTitle("赛马游戏");this.setSize(800,600);this.setResizable(false);this.setLocationRelativeTo(null);this.setDefaultCloseOperation(MainFrame.EXIT_ON_CLOSE);//================================================================= ==================this.match=new Match("赛道",700,this.getSize().width,4);this.match.addHourseRace(new HourseRace("汗血","images/hourse.gif",13,new int[]{50,80,30}));this.match.addHourseRace(new HourseRace("追风","images/hourse.gif",15,new int[]{50,80,60}));this.match.addHourseRace(new HourseRace("赤兔","images/hourse.gif",16,new int[]{50,80,80}));this.match.addHourseRace(new HourseRace("得卢","images/hourse.gif",18,new int[]{50,80,90}));//================================================================= ==================this.gp=new GraphicPanel(match);this.op=new OperatorPanel(match);this.setLayout(new GridLayout(2,1));this.getContentPane().add(gp);this.getContentPane().add(op);//this.setBackground(Color.BLUE);this.getContentPane().setBackground(Color.BLUE);}}package com.guigu.king.exec;import java.awt.*;import java.awt.event.*;import javax.swing.*;import com.guigu.king.core.*;public class OperatorPanel extends JPanel implements ActionListener,HandleListener{/****/private static final long serialVersionUID = -5609587650586439817L;private ButtonGroup bg=new ButtonGroup();private JRadioButton jrb1=new JRadioButton("汗血"); private JRadioButton jrb2=new JRadioButton("追风"); private JRadioButton jrb3=new JRadioButton("赤兔"); private JRadioButton jrb4=new JRadioButton("得卢");private JTextField txtMoney=new JTextField("");private JButton butStart=new JButton("开始游戏");private JLabel jInfo=new JLabel("请选择一匹马:"); private JLabel jMoney=new JLabel("您的资金总额:10000"); private JLabel jError=new JLabel("请输入正确金额"); private JPanel jp=new JPanel();private java.awt.List list=new java.awt.List();//界面组件private Match match=null;public OperatorPanel(Match match){this.match=match;this.setLayout(new BorderLayout());this.add(this.jp,BorderLayout.NORTH);this.add(this.list);//默认中间this.jp.setLayout(new GridLayout(4,3));this.jp.add(jInfo);this.jp.add(jrb1);this.jp.add(jMoney);this.jp.add(new Label());this.jp.add(jrb2);this.jp.add(txtMoney);this.jp.add(new Label());this.jp.add(jrb3);this.jp.add(jError);this.jp.add(new Label());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 (int i=0;i<this.match.getMatchTrack();i++){if (this.match.getHourseRace(i)!=null){this.match.getHourseRace(i).addHandleListener(this);this.length++;}}}@Overridepublic void actionPerformed(ActionEvent e){if (e.getSource()==butStart){try{if (this.txtMoney.getText().equals("")){this.jError.setText("金额不能为空");this.jError.setVisible(true);return;}int iMoney=Integer.parseInt(this.txtMoney.getText());String temp=this.jMoney.getText().split(":")[1];int tMoney=Integer.parseInt(temp);if (iMoney>0 && iMoney<tMoney){for (int i=0;i<this.match.getMatchTrack();i++){if (this.match.getHourseRace(i)!=null){new Thread(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(NumberFormatException ex){this.jError.setText("请输入正确的数字");this.jError.setVisible(true);}}}private int count=0;private int length=0;private HourseRace hr=null;@Overridepublic synchronized void gameHourse(HourseEvent e){this.count++;HourseRace hr=(HourseRace)e.getSource();this.list.add(hr.getName()+"比赛结束。