📄 exercise14_2.java
字号:
import javax.swing.Timer;
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;
public class Exercise14_2 extends JApplet {
private JScrollBar jsbDelay = new JScrollBar();
private JButton jbStart =new JButton("Start");
private JButton jbStop=new JButton("Stop");
private JButton jbReverse=new JButton("Reverse");
public Exercise14_2() {
JPanel jp1=new JPanel();
jp1.setLayout(new GridLayout(1,3));
jp1.add(jbStart);
jp1.add(jbStop);
jp1.add(jbReverse);
add(jp1,BorderLayout.NORTH);
jsbDelay.setOrientation(JScrollBar.HORIZONTAL);
add(jsbDelay,BorderLayout.SOUTH);
add(new Fans(),BorderLayout.CENTER);
}
class Fans extends JPanel{
int X=0;
private int delay = 10; //时间间隔
private int Direction; //设定方向
protected Timer timer = new Timer(delay, new TimerListener());
public Fans (){
jbStart.addActionListener(new ActionListener(){ //开始
public void actionPerformed(ActionEvent e){
setDirection(20); //顺时针转动
timer.start();
}
});
jbStop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
timer.stop();
}
});
jbReverse.addActionListener(new ActionListener(){ //反转
public void actionPerformed(ActionEvent e){
setDirection(-20); //逆时针转动
timer.start();
}
});
jsbDelay.addAdjustmentListener(new AdjustmentListener() {
public void adjustmentValueChanged(AdjustmentEvent e) {
setDelay(jsbDelay.getMaximum() - e.getValue()); //从左到右速度增大
}
});
}
public void setDelay(int delay) { //设置时间间隔
this.delay = delay;
timer.setDelay(delay);
}
public void setDirection(int Direction){ //设置转动的方向
this.Direction=Direction;
}
class TimerListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
repaint();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int xCenter = getWidth() / 2;
int yCenter = getHeight() / 2;
int radius =(int)(Math.min(getWidth(), getHeight()) * 0.4);
int x = xCenter - radius;
int y = yCenter - radius;
g.drawOval(x-10, y-10, 2 * radius+20, 2 * radius+20);
X-=Direction;
g.setColor(Color.red);
g.fillArc(x, y, 2 * radius, 2 * radius, X, 30);
g.setColor(Color.green);
g.fillArc(x, y, 2 * radius, 2 * radius, 90+X, 30);
g.setColor(Color.blue);
g.fillArc(x, y, 2 * radius, 2 * radius, 180+X, 30);
g.setColor(Color.yellow);
g.fillArc(x, y, 2 * radius, 2 * radius, 270+X, 30);
}
}
public static void main(String[] args) {
Exercise14_2 applet =new Exercise14_2();
JFrame frame = new JFrame();
frame.setTitle("Exercise14_2");
frame.add(applet);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 300);
frame.setVisible(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -