⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 horse.java

📁 五匹马赛跑
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
/***赛马****/
public class Horse extends Applet implements ActionListener,Runnable{//设置五个线程
	Thread thread1 = new Thread(this,"horse1");
	Thread thread2 = new Thread(this,"horse2");
	Thread thread3 = new Thread(this,"horse3");
	Thread thread4 = new Thread(this,"horse4");
	Thread thread5 = new Thread(this,"horse5");
	Button b1 = new Button("start");//游戏开始按钮
	Button b2 = new Button("确定");//输入数字的确定按钮
	Label a = new Label("请依次输入horse1~5的速度(100~1000的整数,越大速度越慢,在第i个文本框内输入第i匹马的速度,速度可以相同)");
	Label b = new Label("请输入horse1~5的优先级(1~5的整数,依次输入文本框中,方法同上,输入完后,按确定键,优先级不能相同)");
	TextField c1 = new TextField("");//五个文本框,分别输入五匹马的速度
	TextField c2 = new TextField("");
	TextField c3 = new TextField("");
	TextField c4 = new TextField("");
	TextField c5 = new TextField("");
	TextField d1 = new TextField("");//五个文本框,分别输入五匹马的优先级
	TextField d2 = new TextField("");
	TextField d3 = new TextField("");
	TextField d4 = new TextField("");
	TextField d5 = new TextField("");
	Graphics g=null;
	
	public void init()//初始化,设置游戏面板
	{   
		add(a);
		add(c1);
		add(c2);
		add(c3);
		add(c4);
		add(c5);
		add(b);
		add(d1);
		add(d2);
		add(d3);
		add(d4);
		add(d5);
		add(b2);
		add(b1);
		b1.addActionListener(this);
		b2.addActionListener(this);
	}
	
	public void actionPerformed(ActionEvent e)//button键的动作
	{   
		Object temp = e.getSource();
	    if(temp==b2){//设置五个优先级
	    	String s1 = d1.getText();
	    	int i1 = Integer.parseInt(s1);
	    	thread1.setPriority(i1);
	    	String s2 = d2.getText();
	    	int i2 = Integer.parseInt(s2);
	    	thread2.setPriority(i2);
	    	String s3 = d3.getText();
	    	int i3 = Integer.parseInt(s3);
	    	thread3.setPriority(i3);
	    	String s4 = d4.getText();
	    	int i4 = Integer.parseInt(s4);
	    	thread4.setPriority(i4);
	    	String s5 = d5.getText();
	    	int i5 = Integer.parseInt(s5);
	    	thread5.setPriority(i5);
	    }
	    if(temp==b1){//开始游戏
		thread1.start();
		thread2.start();
		thread3.start();
		thread4.start();
		thread5.start();}
	}
	
	public synchronized void bridge(){//过桥,设置循环,使停顿同步
		for(int i=0;i<20;i++){
			for(int m=0; m<20000000;m++){}
		}
	}
	public void run()
	{
		g = getGraphics();
		
		g.drawLine(150,130,470,130); //画跑道
		g.drawLine(150, 630, 470, 630);
		g.drawLine(160,130,160,630);
		g.drawLine(220,130,220,630);
		g.drawLine(280,130,280,630);
		g.drawLine(340,130,340,630);   
		g.drawLine(400,130,400,630);
		g.drawLine(460,130,460,630);
		g.drawLine(150, 530, 470, 530);//画桥
		g.drawString("Bridge,20miles", 480, 530);//标注桥的位置
		g.drawString("destination,100miles", 480, 130);
		g.drawString("horse1", 160, 650);//标注马的编号
		g.drawString("horse2", 220, 650);
		g.drawString("horse3", 280, 650);
		g.drawString("horse4", 340, 650);
		g.drawString("horse5", 400, 650);
		
		int a;
		if(((Thread.currentThread()).getName()).equals("horse1")){//设置线程1,horse1的跑法
			String f1 = c1.getText();
			int j1 = Integer.parseInt(f1);//得到速度
			String s1 = d1.getText();
	    	int i1 = Integer.parseInt(s1);//得到优先级
			for(a=0; a<=500; a+=20){
				g.fillRect(190,630-a,10,10);
				if(a==100){bridge();}
				try{
					thread1.sleep(j1);//sleep的长短决定了马跑的速度
				}
				catch(InterruptedException e1){}
				
			}
			double i = 500/20*j1/100+(6-i1);//计算时间
			g.drawString(i+"分钟", 160, 120);//显示时间
			}
		
		if(((Thread.currentThread()).getName()).equals("horse2")){//设置线程2,horse2的跑法
			String s2 = d2.getText();
	    	int i2 = Integer.parseInt(s2);
			String f2 = c2.getText();
			int j2 = Integer.parseInt(f2);
			for(a=0; a<=500; a+=20){
				g.fillRect(250, 630-a, 10, 10);
				if(a==100){bridge();}
				try{
					thread2.sleep(j2);
				}catch(InterruptedException e1){}
				
			}
			double i = 500/20*j2/100+(6-i2);
			g.drawString(i+"分钟", 220, 120);		
		}
		
		if(((Thread.currentThread()).getName()).equals("horse3")){//设置线程3,horse3的跑法
			String s3 = d3.getText();
	    	int i3 = Integer.parseInt(s3);
			String f3 = c3.getText();
			int j3 = Integer.parseInt(f3);
			for(a=0; a<=500; a+=20){
				g.fillRect(310, 630-a, 10, 10);
				if(a==100){bridge();}
				try{
					thread3.sleep(j3);
				}catch(InterruptedException e1){}
				
			}
			double i = 500/20*j3/100+(6-i3);
			g.drawString(i+"分钟", 280, 120);		
		}
		
		if(((Thread.currentThread()).getName()).equals("horse4")){//设置线程4,horse4的跑法
			String s4 = d4.getText();
	    	int i4 = Integer.parseInt(s4);
			String f4 = c4.getText();
			int j4 = Integer.parseInt(f4);
			for(a=0; a<=500; a+=20){
				g.fillRect(370, 630-a, 10, 10);
				if(a==100){bridge();}
				try{
					thread4.sleep(j4);
				}catch(InterruptedException e1){}
				
			}
			double i = 500/20*j4/100+(6-i4);
			g.drawString(i+"分钟", 340, 120);		
		}
		
		if(((Thread.currentThread()).getName()).equals("horse5")){//设置线程5,horse5的跑法
			String s5 = d5.getText();
	    	int i5 = Integer.parseInt(s5);
			String f5 = c5.getText();
			int j5 = Integer.parseInt(f5);
			for(a=0; a<=500; a+=20){
				g.fillRect(430, 630-a, 10, 10);
				if(a==100){bridge();}
				try{
					thread5.sleep(j5);
				}catch(InterruptedException e1){}
			}
			double i = 500/20*j5/100+(6-i5);
			g.drawString(i+"分钟", 400, 120);		
		}
		
		}//the end of run
}//the end of class Horse

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -