📄 test20fans.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;
public class Test20Fans
{
public static void main(String[] args)
{
FanFrame frame=new FanFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class FanFrame extends JFrame
{
private JPanel controlPanel=new JPanel();
private JButton startallButton=new JButton("全部开始"); //创建全部开始按扭
private JButton stopallButton=new JButton("全部结束"); //创建全部结束按扭
private FanPanel first=new FanPanel();
private FanPanel second=new FanPanel();
private FanPanel third=new FanPanel();
public FanFrame()
{
setTitle("风扇");
setSize(680,310);
controlPanel.add(startallButton);
controlPanel.add(stopallButton);
controlPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
add(controlPanel,"South");
add(first,"East");
add(second,"West");
add(third,"Center");
StartAllListener li1=new StartAllListener();
startallButton.addActionListener(li1);
StopAllListener li2=new StopAllListener();
stopallButton.addActionListener(li2);
}
class StartAllListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
first.GetSignal(1);
second.GetSignal(1);
third.GetSignal(1);
}
}
class StopAllListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
first.GetSignal(0);
second.GetSignal(0);
third.GetSignal(0);
}
}
}
class FanPanel extends JPanel
{
Timer timer1=new Timer(30,new StartListener());
Timer timer3=new Timer(30,new ReverseListener());
private int i,j;
private int choose=0;
public void GetSignal(int s)
{
if(s==1)
{
timer1.start();
}
else
{
timer1.stop();
timer3.stop();
}
}
public FanPanel()
{
JButton startButton = new JButton("开始");//创建开始按扭
JButton stopButton = new JButton("结束");//创建结束按扭
JButton reverseButton = new JButton("Reverse");//创建反转按扭
add(startButton);
add(stopButton);
add(reverseButton);
StartListener l1=new StartListener();
startButton.addActionListener(l1);
StopListener l2=new StopListener();
stopButton.addActionListener(l2);
ReverseListener l3=new ReverseListener();
reverseButton.addActionListener(l3);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2=(Graphics2D) g;
double xCenter=getWidth()/2; //中心点x
double yCenter=getHeight()/2; //中心点y
double radius=(int)(getWidth()*0.3); //半径
double x=xCenter-radius; //左上角x
double y=yCenter-radius+20; //左上角y
Ellipse2D ellipse1 = new Ellipse2D.Double(x-5,y-5,2*radius+10,2*radius+10);
g2.draw(ellipse1);
Arc2D arc1=new Arc2D.Double(x,y,2*radius,2*radius,0-j,30,Arc2D.PIE);
g2.fill(arc1);
Arc2D arc2=new Arc2D.Double(x,y,2*radius,2*radius,90-j,30,Arc2D.PIE);
g2.fill(arc2);
Arc2D arc3=new Arc2D.Double(x,y,2*radius,2*radius,180-j,30,Arc2D.PIE);
g2.fill(arc3);
Arc2D arc4=new Arc2D.Double(x,y,2*radius,2*radius,270-j,30,Arc2D.PIE);
g2.fill(arc4);
g2.setPaint(Color.GREEN); //风扇的颜色
g2.fill(arc1);
g2.fill(arc2);
g2.fill(arc3);
g2.fill(arc4);
}
class StartListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
timer3.stop();
timer1.start();
i+=20; //i值确定转速大小
j=i%360;
repaint();
}
}
class StopListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
timer1.stop();
timer3.stop();
repaint();
}
}
class ReverseListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
timer1.stop();
timer3.start();
i-=20; //i的值决定反向的转速
j=i%360;
repaint();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -