风扇。编写程序,显示3个风扇,用控制按钮开动和停止风扇。可以同时开动和停止3个风扇,也可以分别开动和停止每一个风扇。输出示例如图所示。
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Arc2D;
import java.awt.geom.Ellipse2D;
public class Main {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
MyFrame frame = new MyFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
});
}
}
class MyFrame extends JFrame {
public MyFrame(){
setTitle("Controlling a Group of Fans");
MyPanel[] myPanels = new MyPanel[3];
JPanel panel = new JPanel(new GridLayout(1, 3));
for(int i = 0; i < 3; i++){
MyPanel myPanel = new MyPanel();
panel.add(myPanel);
myPanels[i] = myPanel;
}
add(panel, "North");
JPanel buttonPanel = new JPanel();
JButton startAll = new JButton("Start All");
JButton stopAll = new JButton("Stop All");
buttonPanel.add(startAll);
buttonPanel.add(stopAll);
add(buttonPanel, "South");
pack();
startAll.addActionListener(e -> {
for (MyPanel myPanel : myPanels){
myPanel.start();
}
});
stopAll.addActionListener(e -> {
for (MyPanel myPanel : myPanels){
myPanel.stop();
}
});
}
}
class MyComponent extends JComponent implements Runnable{
public static Double RADIUS = 150.0;
private static final int DEFAULT_WIDTH=400;
private static final int DEFAULT_HEIGHT=400;
private Arc2D[] arcs = new Arc2D[4];
private int OFFSET = 0;
private int STEP = 10;
private boolean run = true;
@Override
public void paintComponent(Graphics g){
setBorder(BorderFactory.createEtchedBorder());
Graphics2D g2 = (Graphics2D) g;
double x = getWidth() / 2.0 - RADIUS;
double y = getHeight() / 2.0 - RADIUS;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Ellipse2D circle = new Ellipse2D.Double();
circle.setFrameFromCenter(getWidth() / 2.0, getHeight() / 2.0, getWidth() / 2.0 + RADIUS + 10, getHeight() / 2.0 + RADIUS + 10);
g2.draw(circle);
Arc2D arc2D;
for(int i = 0, j = OFFSET; i < 4; i++, j += 90){
arc2D = new Arc2D.Double(x, y, 2 * RADIUS, 2 * RADIUS, j,30,Arc2D.PIE);
g2.setPaint(Color.RED);
g2.fill(arc2D);
g2.draw(arc2D);
arcs[i] = arc2D;
}
}
@Override
public Dimension getPreferredSize()
{
return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
}
public void rotate(){
OFFSET += STEP;
repaint();
}
public void reverse(){
STEP = -STEP;
}
public void stop(){
run = false;
}
public void start(){
new Thread(this).start();
}
@Override
public void run() {
run = true;
while (run){
rotate();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class MyPanel extends JPanel{
private static final int DEFAULT_WIDTH=400;
private static final int DEFAULT_HEIGHT=500;
private JButton start;
private JButton stop;
MyComponent component;
public MyPanel(){
setBorder(BorderFactory.createEtchedBorder());
JPanel panel = new JPanel();
start = new JButton("Start");
stop = new JButton("Stop");
JButton reverse = new JButton("Reverse");
panel.add(start);
panel.add(stop);
panel.add(reverse);
add(panel, "North");
component = new MyComponent();
add(component, "Center");
start.addActionListener(e -> component.start());
stop.addActionListener(e -> component.stop());
reverse.addActionListener(e -> component.reverse());
}
@Override
public Dimension getPreferredSize()
{
return new Dimension(DEFAULT_WIDTH,DEFAULT_HEIGHT);
}
public void start(){
component.start();
}
public void stop(){
component.stop();
}
}