DOCX

FanControlPane

By Cynthia Anderson,2014-05-28 14:11
14 views 0
FanControlPane

import java.awt.*;

    import java.applet.*;

    import java.awt.event.*;

    import javax.swing.*;

    import java.io.*;

    public class FanControlPanel extends JFrame implements ActionListener{

     Fan vt;

     Thread th;

     JPanel mu = new JPanel(); //放置按钮的面板

     JButton b[] = new JButton[4];

     String buttonTitle[]={"start","stop","reverse","accelerate"};

     public FanControlPanel()

     {

     for(int i=0; i<4; i++) {

     b[i] = new JButton(buttonTitle[i]);

     mu.add(b[i]);

     b[i].addActionListener(this);

     }

     vt = new Fan();

     this.getContentPane().add(vt,"Center");

     this.getContentPane().add(mu,"North");

     setTitle("电风扇");

     setSize(500,500);

     setVisible(true);

     }

     public static void main(String args[])

     {

     new FanControlPanel();

     }

     public void actionPerformed(ActionEvent e) {

     String s =e.getActionCommand();

     if(s.equals("start"))//启用多线程

     {

     vt.flag=1;

     vt.speed=1000;

     th= new Thread(vt);

     th.start();

     }

     else if(s.equals("stop")) {//停止多线程

     if(th!=null){

     th.stop();

     th=null;

     }

     }

     else if(s.equals("reverse")) {//re来控制正反角度,从而再给reverse设置监听

     if(vt.flag==1)

     {

     vt.re=-1;

     vt.flag=0;

     }

     else

     {

     vt.re=1;

     vt.flag=1;

     }

     }

     else if(s.equals("accelerate")) {

     if(vt.speed>10)

     vt.speed=vt.speed/10;

     if(vt.speed<=10)

     vt.speed=vt.speed/1;

     }

     }

}

class Fan extends JPanel implements Runnable

    {

     int ax=0;

     public int re = 1;

     public int speed = 1000;//睡眠时间初始设定,之后每除以10来改变速度,相当于调速度档

     public int flag ;//flag是记录正反向的标识符

     public void run()

     {

     while(true) {

     try{

     ax=ax+re*10;

     repaint();

     Thread.sleep(speed);

     }catch(Exception e){}

     }

     }

     public void paintComponent(Graphics g1)

     {

     super.paintComponent(g1);

     Graphics2D g = (Graphics2D)g1;

     g.drawOval(100,60,300,300);

     g.setColor(Color.red);

     g.fillArc(110,68,280,280,ax,30);

     g.fillArc(110,68,280,280,ax+90,30);

     g.fillArc(110,68,280,280,ax+180,30);

     g.fillArc(110,68,280,280,ax+270,30);

     }

}

Report this document

For any questions or suggestions please email
cust-service@docsford.com