主观题:绘制红绿灯
绘制红绿灯,包括红黄蓝三盏灯,初始时各盏灯为灰色。点击启动按钮,则各盏灯交替点亮,即先为最上方的红色灯,然后是中间的黄色灯,最后是下方的绿色灯,时间间隔可以设为3秒,循环交替。点击停止按钮,则各盏灯变为灰色。示例界面如下图所示。
![11.png](~/1fab6b94-b557-4240-899a-da1c7ca1612a.png)
提示:可以使用Timer设定时间间隔,定时重绘界面。
答案:import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ColorChange {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
TrafficLightFrame frame = new TrafficLightFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class TrafficLightFrame extends JFrame {
private Timer timer;
private TrafficLightComponent lightComponent;
public TrafficLightFrame() {
setSize(220, 250);
setTitle("交通灯");
timer = new Timer(1000, new ChangeColorAction());
lightComponent = new TrafficLightComponent();
add(lightComponent, BorderLayout.CENTER);
JButton startButton = new JButton("启动");
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.start();
}
});
JButton stopButton = new JButton("停止");
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.stop();
lightComponent.setColorNum(3);
repaint();
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
add(buttonPanel, BorderLayout.SOUTH);
}
class ChangeColorAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
lightComponent.setColorNum((lightComponent.getColorNum() + 1) % 3);
repaint();
}
}
}
class TrafficLightComponent extends JComponent {
private int colorNum = 3;
public int getColorNum() {
return colorNum;
}
public void setColorNum(int colorNum) {
this.colorNum = colorNum;
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Ellipse2D redLight = new Ellipse2D.Double(80, 0, 40, 40);
Ellipse2D yellowLight = new Ellipse2D.Double(80, 50, 40, 40);
Ellipse2D greenLight = new Ellipse2D.Double(80, 100, 40, 40);
switch (colorNum) {
case 1:
g2.draw(yellowLight);
g2.draw(greenLight);
g2.setPaint(Color.red);
g2.fill(redLight);
break;
case 2:
g2.draw(redLight);
g2.draw(greenLight);
g2.setPaint(Color.yellow);
g2.fill(yellowLight);
break;
case 0:
g2.draw(yellowLight);
g2.draw(redLight);
g2.setPaint(Color.green);
g2.fill(greenLight);
break;
case 3:
g2.draw(redLight);
g2.draw(yellowLight);
g2.draw(greenLight);
break;
}
}
}
![11.png](~/1fab6b94-b557-4240-899a-da1c7ca1612a.png)
提示:可以使用Timer设定时间间隔,定时重绘界面。
答案:import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class ColorChange {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
TrafficLightFrame frame = new TrafficLightFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class TrafficLightFrame extends JFrame {
private Timer timer;
private TrafficLightComponent lightComponent;
public TrafficLightFrame() {
setSize(220, 250);
setTitle("交通灯");
timer = new Timer(1000, new ChangeColorAction());
lightComponent = new TrafficLightComponent();
add(lightComponent, BorderLayout.CENTER);
JButton startButton = new JButton("启动");
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.start();
}
});
JButton stopButton = new JButton("停止");
stopButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
timer.stop();
lightComponent.setColorNum(3);
repaint();
}
});
JPanel buttonPanel = new JPanel();
buttonPanel.add(startButton);
buttonPanel.add(stopButton);
add(buttonPanel, BorderLayout.SOUTH);
}
class ChangeColorAction implements ActionListener {
public void actionPerformed(ActionEvent event) {
lightComponent.setColorNum((lightComponent.getColorNum() + 1) % 3);
repaint();
}
}
}
class TrafficLightComponent extends JComponent {
private int colorNum = 3;
public int getColorNum() {
return colorNum;
}
public void setColorNum(int colorNum) {
this.colorNum = colorNum;
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Ellipse2D redLight = new Ellipse2D.Double(80, 0, 40, 40);
Ellipse2D yellowLight = new Ellipse2D.Double(80, 50, 40, 40);
Ellipse2D greenLight = new Ellipse2D.Double(80, 100, 40, 40);
switch (colorNum) {
case 1:
g2.draw(yellowLight);
g2.draw(greenLight);
g2.setPaint(Color.red);
g2.fill(redLight);
break;
case 2:
g2.draw(redLight);
g2.draw(greenLight);
g2.setPaint(Color.yellow);
g2.fill(yellowLight);
break;
case 0:
g2.draw(yellowLight);
g2.draw(redLight);
g2.setPaint(Color.green);
g2.fill(greenLight);
break;
case 3:
g2.draw(redLight);
g2.draw(yellowLight);
g2.draw(greenLight);
break;
}
}
}