交通信号灯

来源:互联网 发布:华为p8淘宝打不开 编辑:程序博客网 时间:2024/06/11 19:39

编写程序,模拟交通信号灯。程序让用户从红、黄、绿三色灯中选择一种。当选

择一个单选按钮后,相应的灯被打开,并且一次只能亮一种灯。如下图所示:

import java.awt.*;import javax.swing.*;public class Car extends JFrame {  public static void main(String[] args) {    JFrame frame = new Car();    frame.setSize(300, 300);    frame.setTitle("Circle");    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    frame.setLocationRelativeTo(null); // Center the frame    frame.setVisible(true);  }  public Car() {    add(new PieChart1());  }}class PieChart1 extends JPanel {  protected void paintComponent(Graphics g) {    super.paintComponent(g);    int w = getWidth();    int h = getHeight();    int xCenter = w / 2;    int yCenter = h / 2;    int radius = (int)(Math.min(w, h) * 0.8 / 2);    int x = xCenter - radius;    int y = yCenter - radius;    g.setColor(Color.red);    g.fillArc(x, y, 2 * radius, 2 * radius, 0, (int)(20 * 360 / 100));    g.setColor(Color.black);    g.drawString("Projects -- 20%",    (int)(xCenter + radius*Math.cos(2 * Math.PI * 0.1)),    (int)(yCenter - radius*Math.sin(2 * Math.PI * 0.1)));    g.setColor(Color.blue);    g.fillArc(x, y, 2 * radius, 2 * radius, (int)(20 * 360 / 100),    (int)(10 * 360 / 100));    g.setColor(Color.black);    g.drawString("Quizzes -- 10%",    (int)(xCenter + radius * Math.cos(2 * Math.PI * 0.25)),    (int)(yCenter - radius * Math.sin(2 * Math.PI * 0.25)));    g.setColor(Color.green);    g.fillArc(x, y, 2 * radius, 2 * radius, (int)(30*360/100),      (int)(30 * 360 / 100));    g.setColor(Color.black);    g.drawString("Midterms -- 30%",      (int)(xCenter + radius*Math.cos(2 * Math.PI * 0.45)) - 40,      (int)(yCenter - radius*Math.sin(2 * Math.PI * 0.45)));    g.setColor(Color.white);    g.fillArc(x, y, 2 * radius, 2 * radius, (int)(60 * 360 / 100),      (int)(40 * 360 / 100));    g.setColor(Color.black);    g.drawString("Final -- 40%",      (int)(xCenter + radius*Math.cos(2 * Math.PI * 0.8)),      (int)(yCenter - radius*Math.sin(2 * Math.PI * 0.8)));  }}

0 0
原创粉丝点击