模拟 4×100 米接力和多线程程序,程序中应用了 Java 图形用户界面和多线程编程方法

本实验的思路,利用button按钮的移动来模仿4×100 接力的效果,采用子线程的方式来修改button的显示位置,程序运行后,按 S 键开始模拟 4 个人移动接力,按下Q,结束。

话不多说,直接上代码:

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Point;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

//界面的创建
class lampView extends JFrame {
    private static final long serialVersionUID = 1L;
    private JPanel panelLamp1 = new JPanel();
    private JPanel panelLamp2 = new JPanel();
    private JPanel panelLamp3 = new JPanel();
    private JPanel panelLamp4 = new JPanel();
    public lampView() {
        super("线程同步----4*100接力");
        Container con = getContentPane();
        con.setLayout(null);
        //this.SetPosition(new Point(0,100),new Point(130,100),new Point(260,100),new Point(390,100));
        con.add(this.lamp1(panelLamp1,Color.blue));
        con.add(this.lamp1(panelLamp2,Color.green));
        con.add(this.lamp1(panelLamp3,Color.red));
        con.add(this.lamp1(panelLamp4,Color.yellow));
        JLabel Notice=new JLabel("按 S 开始接力,按 Q 停止接力");
        Notice.setLocation(200, 200);
        Notice.setSize(200,100);
        con.add(Notice);
        this.setSize(600, 350);
        this.setVisible(true);
        this.setLocation(200, 200);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭软件后,关闭线程
    }
    
    //给外部访问修改的数据
    public void SetPosition(Point p1,Point p2,Point p3,Point p4) {
        this.panelLamp1.setLocation(p1);
        this.panelLamp2.setLocation(p2);
        this.panelLamp3.setLocation(p3);
        this.panelLamp4.setLocation(p4);
        //this.validate();
    }

    // 模拟人的创建
    public JPanel lamp1(JPanel panel,Color color) {        
        Dimension size = new Dimension(30, 30);
        JButton lamp1 = new JButton();
        panel.setLayout(new FlowLayout());
        lamp1.setBackground(color);
        lamp1.setPreferredSize(size);
        panel.add(lamp1);
        panel.setSize(30, 30);
        panel.setLocation(0,0);
        return panel;
    }
}

//线程类

class ThreadLamp extends Thread {
    private Point p1, p2, p3, p4;
    private boolean SunFlag = true;
    lampView lampView = new lampView();
    public boolean isSunFlag() {
        return SunFlag;
    }

    public void setSunFlag(boolean sunFlag) {
        SunFlag = sunFlag;
    }

    public ThreadLamp(String name, int meter) {
        super(name);
        p1 = new Point(0, 100);
        p2 = new Point(130, 100);
        p3 = new Point(260, 100);
        p4 = new Point(390, 100);
        lampView.SetPosition(p1, p2, p3, p4);
    }

    // 重写run
    public void run() {
        System.out.println("开始" + getName()+SunFlag);
                for (int i = 0; i < 400; i++) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    this.dataSet(i);
                    if(SunFlag==false)break;
                    if(i==399) 
                        System.out.println("接力完成!");
                }        
    }

// 给图形赋值
    public void dataSet(int i) {
        
        if (i < 100) {
            p1.x = i;
            lampView.SetPosition(p1, p2, p3, p4);
            // lampView.lamp2.setLocation(p1);
        } else if (i < 200) {
            p2.x = i + 30;
            lampView.SetPosition(p1, p2, p3, p4);
        } else if (i < 300) {
            p3.x = i + 60;
            lampView.SetPosition(p1, p2, p3, p4);
        } else if (i < 400) {
            p4.x = i + 90;
            lampView.SetPosition(p1, p2, p3, p4);
        }
    }

}

//主函数

public class lamp {

    public static void main(String[] args) {
    
          ThreadLamp lampGo=new ThreadLamp("接力开始", 0);
        while(true) //等待按键按下
        {
            @SuppressWarnings("resource")
            Scanner scon=new Scanner(System.in);
            String strin=scon.nextLine();    
            System.out.println(strin);
            if(strin.equals("S")||strin.equals("s"))
            {
                System.out.println("开始");
                lampGo.start();
            }    
            else if(strin.equals("Q")||strin.equals("Q"))
            {
                //lampGo.stop();
                System.out.println("停止");
                lampGo.setSunFlag(false);
            }
            else {
                System.out.println("输入有误!请重新输入");
            }
        }
    }

}


运行结果:

 

  • 0
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个使用Java线程实现多人接力跑1000米的示例代码。每个线程代表一个选手,每个线程运行时打印出当前跑了多少米,并在每10米处打印出信息。 ``` public class RelayRace implements Runnable { private static final int TOTAL_DISTANCE = 1000; private static final int INTERVAL = 10; private static int currentDistance = 0; private static int currentRunner = 1; private final int runnerNumber; public RelayRace(int runnerNumber) { this.runnerNumber = runnerNumber; } @Override public void run() { while (currentDistance < TOTAL_DISTANCE) { currentDistance += 1; if (currentDistance % INTERVAL == 0) { System.out.println("Runner " + runnerNumber + " ran " + currentDistance + " meters."); } if (currentDistance % 100 == 0) { System.out.println("Runner " + runnerNumber + " passed the baton to runner " + (currentRunner + 1)); currentRunner += 1; } try { Thread.sleep(10); // simulate running time } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Runner " + runnerNumber + " finished the race!"); } public static void main(String[] args) { Thread[] runners = new Thread[10]; for (int i = 0; i < runners.length; i++) { runners[i] = new Thread(new RelayRace(i + 1)); } for (Thread runner : runners) { runner.start(); } } } ``` 在这个示例代码中,我们定义了一个常量`TOTAL_DISTANCE`表示总距离为1000米,常量`INTERVAL`表示每10米打印一次信息。变量`currentDistance`表示当前总距离,变量`currentRunner`表示当前接力棒所在的选手编号。 在`RelayRace`类的`run()`方法中,我们在每个线程运行时先把当前跑的距离`currentDistance`加1,然后判断是否到达了10米的整数倍,如果是则打印出当前选手跑了多少米的信息。同时,如果到达了100米的整数倍,就打印出当前选手将接力棒交给下一个选手的信息,并将`currentRunner`增加1。 在每个线程运行时,我们还使用`Thread.sleep(10)`来模拟选手跑步的时间。 在`main()`方法中,我们创建了10个线程,每个线程代表一个选手,然后启动这些线程。 当所有线程都执行完毕后,程序就结束了。此时我们可以看到每个选手跑了多少米,并且在每10米处都打印出了信息,每100米处也打印出了交接棒的信息。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值