交通信号灯

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

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

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)));
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值