JAVA 小程序 赛马
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
contentPane.add(btnNewButton);
init();
btnNewButton.addActionListener(this);
}
public void init(){
final int endInt = 600;
horsesP = new JPanel(null);
horsesP.setBounds(10, 10, 700, 350); //马匹面板
int nowWinNum = 0;//本次已到终点的马数
int heaveRaceedNum = 0;//已赛马的次数
boolean startFlag = false;//运行中的标志为true时表示已开始
int horseRestorationNum = 4;//马匹复位的个数
public RaceHorse() {
this.horseName = horseName;
this.horseSize = horseSize;
this.horseMoveFrom = horseMoveFrom; this.horseMoveTo = horseMoveTo;
this.horseY = horseY;
this.setText(horseName);
try {
this.wait();
} catch (InterruptedException e) { e.printStackTrace(); }
}
}else if(xPosition>=(horseMoveTo-this.horseSize.width)&&
xPosition<(horseMoveTo-this.horseSize.width)+2)
}
while(true) {
xPosition += 2;
try{
long randomMoveTime = (long)(Math.random()*40);
this.horseThread.sleep(randomMoveTime);
this.horseGoto(xPosition, horseY);
this.setHorizontalTextPosition(CENTER);
this.setVerticalTextPosition(TOP);
this.setSize(horseSize);
this.setIcon(new ImageIcon(getClass().getResource("imgs/RaceHorse/horse.gif")));
}catch(Exception ex) { System.out.println("Error!");
}
if(xPosition>=horseMoveTo) {
xPosition = horseMoveFrom;
this.horseGoto(xPosition, horseY);
synchronized (this) {
public void run() {
int xPosition = horseMoveFrom; //this.horseThread.suspend();
synchronized (this) {
try {
wait();
} catch (InterruptedException e) { e.printStackTrace(); }
{//报告到达终点 //endSet(this); }
Байду номын сангаас
}
}
public synchronized void sleepUp(){
this.notify(); }
}
2、RaceHorse
public class RaceHorse extends JFrame implements ActionListener{
for(int i=0; i<4; i++) {
//horse[i].horseThread.resume(); //horse[i].notifyAll();
horse[i].sleepUp();
}
}
}
}
getContentPane().add(horsesP);
}
public static void main(String[] args) {
RaceHorse frame = new RaceHorse();
}
public void actionPerformed(ActionEvent e) {
public int horseY;//垂直位置
public Dimension horseSize;//马的大小
public Horse(String horseName ,Dimension horseSize ,int horseMoveFrom ,int horseMoveTo ,int
horseY) {
private JPanel contentPane; //界面变量
JPanel horsesP, command;//定义两个面板,马所在的和命令面板
JPanel endLine;
//全局变量
Horse horse[] = new Horse[4];
Horse winner[] = new Horse[4];
this.horseGoto(this.horseMoveFrom, this.horseY);
}
public void createThread() {
this.horseThread = new Thread(this); this.horseThread.start();
}
public void horseGoto(int x, int y) { this.setLocation(x, y); }
1、Horse
public class Horse extends JLabel implements Runnable {
public Thread horseThread;
public String horseName;
public int horseMoveFrom;//起点 public int horseMoveTo;//终点
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 518, 416);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnNewButton = new JButton("\u5F00\u59CB\u8D5B\u9A6C");
btnNewButton.setBounds(224, 354, 95, 25);
horsesP.add(endLine);
for(int i=0;i<4;i++) {//初始化马匹
horse[i] = new Horse((i+1)+"号马",new Dimension(116,80),30,endInt,80*i);
horsesP.add(horse[i]);
horse[i].createThread();
horsesP.setMaximumSize(new Dimension(10,10));
horsesP.setBackground(Color.white);
endLine = new JPanel();
endLine.setBounds(endInt,0,2, 280);
endLine.setBackground(Color.red);