📄 testfan.java
字号:
//:TestFan.java
/**
*The programme used to show a rotating fan controled by an independent "Thread"
*@author HaiXian Wu in Software College Grade 2003 Class 2 ID:1033710201
*@version 1.0
*Program Time:2004-12-2
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestFan extends JFrame
{
private FanControlPanel panel;
public TestFan(String s)
{
this.setTitle(s);
panel = new FanControlPanel();
this.getContentPane().add(panel,BorderLayout.CENTER);
}
public static void main(String args[])
{
TestFan frame = new TestFan("Controling fan rotating");
frame.setSize(400,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class FanControlPanel extends JPanel implements ActionListener,AdjustmentListener
{
private Fan fan = new Fan();
private JButton startButton,stopButton,reverseButton;
private JScrollBar scrollBar;
private boolean suspended = true;
private boolean isStart = false;
public FanControlPanel()
{
startButton = new JButton("Start");
startButton.setMnemonic('s');
stopButton = new JButton("Stop");
stopButton.setMnemonic('o');
reverseButton = new JButton("Reverse");
reverseButton.setMnemonic('r');
scrollBar = new JScrollBar(JScrollBar.HORIZONTAL,20,2,0,40);
JPanel jp= new JPanel(new GridLayout(1,3));
jp.add(startButton);
jp.add(stopButton);
jp.add(reverseButton);
this.setLayout(new BorderLayout());
this.add(jp,BorderLayout.NORTH);
this.add(fan,BorderLayout.CENTER);
this.add(scrollBar,BorderLayout.SOUTH);
//Add listener
startButton.addActionListener(this);
stopButton.addActionListener(this);
reverseButton.addActionListener(this);
scrollBar.addAdjustmentListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == startButton)
{
if (!isStart)
{
fan.start();
isStart = true;
}
else
fan.resume();
}
else if (e.getSource() == stopButton)
fan.suspend();
else if (e.getSource() == reverseButton)
{
if (fan.getDirection() == fan.CLOCKWISE)
fan.setDirection(fan.COUNTER_CLOCKWISE);
else
fan.setDirection(fan.CLOCKWISE);
}
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
fan.setSpeed(scrollBar.getValue());
}
}
class Fan extends JPanel implements Runnable
{
public final int CLOCKWISE = 1;
public final int COUNTER_CLOCKWISE = 0;
private int direction = CLOCKWISE;
private int angel = 0;
private int speed = 20;
private Thread fanThread;
private boolean suspended;
public Fan()
{
fanThread = new Thread(this);
}
public void setDirection(int direction)
{
this.direction = direction;
}
public int getDirection()
{
return direction;
}
public void setSpeed(int speed)
{
this.speed = speed;
}
public void start()
{
fanThread.start();
}
// Resume the fan
public synchronized void resume()
{
if (suspended)
{
suspended = false;
notify();
}
}
// Suspend the fan
public synchronized void suspend()
{
suspended = true;
}
public void run()
{
while (true)
{
try
{
fanThread.sleep(100);
repaint();
synchronized (this)
{
while (suspended)
wait();
}
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int xCenter = this.getSize().width/2;
int yCenter = this.getSize().height/2;
int radius = (int)(Math.min(getSize().width,getSize().height) * 0.35);
//Draw the circle
g.drawOval(xCenter - radius - 20,yCenter - radius - 20,(radius+20) * 2,(radius+20) * 2);
g.setColor(Color.red);
if (direction == CLOCKWISE)
{
//Draw the fan
g.fillArc(xCenter - radius,yCenter - radius,radius * 2,radius *2,angel,30);
g.fillArc(xCenter - radius,yCenter - radius,radius * 2,radius *2,angel + 90,30);
g.fillArc(xCenter - radius,yCenter - radius,radius * 2,radius *2,angel + 180,30);
g.fillArc(xCenter - radius,yCenter - radius,radius * 2,radius *2,angel - 90,30);
//Decreae the startangel to show that the fan rotate clockwisely
angel = ((angel == -180 ? 180 : angel) - speed);
}
else
{
//Draw the fan
g.fillArc(xCenter - radius,yCenter - radius,radius * 2,radius *2,angel,30);
g.fillArc(xCenter - radius,yCenter - radius,radius * 2,radius *2,angel + 90,30);
g.fillArc(xCenter - radius,yCenter - radius,radius * 2,radius *2,angel + 180,30);
g.fillArc(xCenter - radius,yCenter - radius,radius * 2,radius *2,angel - 90,30);
//Increase the startangel to show that the fan rotate counter_clockwisely
angel = ((angel == 180 ? -180 : angel) + speed);
}
}
}///:~
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -