• 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();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137

    代码分析

    • 一生三
    • 扩展JPanel类
    • 通过Thread实现多线程
  • 相关阅读:
    神经网络在控制系统中的应用有哪些
    SpringCloud Alibaba
    OpenCV开发笔记(七十五):相机标定矫正中使用remap重映射进行畸变矫正
    LeetCode 1403.非递增顺序的最小子序列
    zookeeper第二章:API接口
    C/C++内存对齐
    Qt 串口通信(C++)
    【MySQL从入门到精通】【高级篇】(二十一)数据库优化步骤_查看系统性能参数
    Java基础(二)
    C++中promise和future详解
  • 原文地址:https://blog.csdn.net/weixin_61133168/article/details/134475858