Java 实现多风扇

要求

编写程序,显示3个风扇,用控制按钮开动和停止风扇。可以同时开动和停止3个风扇,也可以分别开动和停止每一个风扇。

代码

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class Fan extends JPanel{
    private JButton start = new JButton("start");
    private JButton stop = new JButton("stop");
    private JButton reset = new JButton("reset");
    private int start_ang = 0;
    private boolean status = false;
    private boolean isRunning = true;
    Thread t = new Thread();

    public void stop_run(){
        status = false;
        isRunning = false;
    }

    public void start_run(){
        if(status == false){
            status = true;
            isRunning = true;
            t = new Thread(() -> {
                while(isRunning){
                    start_ang+=1;
                    try{
                        Thread.sleep(2);
                    }catch (InterruptedException ee){
                        ee.printStackTrace();
                    }
                    repaint();
                }
            }); 
            t.start();
        }
    }

    public Fan(){
        this.add(start);
        this.add(stop);
        this.add(reset);

        start.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                start_run();
            }
        });

        stop.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                stop_run();
            }
        });

        reset.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                start_ang = 0;
                repaint();
            }
        });
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;

        int radius = 50;

        // 画风扇的圆形部分
        g.setColor(Color.RED);
        g.fillArc(centerX - radius, centerY - radius, 2 * radius, 2 * radius, start_ang, 30);
        g.fillArc(centerX - radius, centerY - radius, 2 * radius, 2 * radius, start_ang+90, 30);
        g.fillArc(centerX - radius, centerY - radius, 2 * radius, 2 * radius, start_ang+180, 30);
        g.fillArc(centerX - radius, centerY - radius, 2 * radius, 2 * radius, start_ang+270, 30);
    }
}

class window extends JFrame{
    private JButton start_all = new JButton("satrt_all");
    private JButton stop_all = new JButton("stop_all");

    public window(){
        //设置大小位置
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenw = screenSize.width;
        int screenh = screenSize.height;
        int windowsWidth = 800;
        int windowsHeight = 300;
        int x = (screenw - windowsWidth)/2;
        int y = (screenh - windowsHeight)/2;

        Container contentPane = this.getContentPane();
        JPanel genControl = new JPanel();
        genControl.setLayout(new FlowLayout());
        genControl.add(start_all);
        genControl.add(stop_all);

        Fan fan1 = new Fan();  
        Fan fan2 = new Fan();  
        Fan fan3 = new Fan();  

        start_all.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                fan1.start_run();
                fan2.start_run();
                fan3.start_run();
            }
        });

        stop_all.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                fan1.stop_run();
                fan2.stop_run();
                fan3.stop_run();
            }
        });

        contentPane.setLayout(new BorderLayout());
        contentPane.add(fan1,BorderLayout.WEST);
        contentPane.add(fan2,BorderLayout.CENTER);
        contentPane.add(fan3,BorderLayout.EAST);
        contentPane.add(genControl,BorderLayout.SOUTH);
        this.setBounds(x,y,windowsWidth,windowsHeight);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
public class Main{
    public static void main(String [] args){
        new window();
    }
}

代码分析

  • 一生三
  • 扩展JPanel类
  • 通过Thread实现多线程
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嗯嗯你说的对

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值