⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise11_11.java

📁 java程序设计 机械工业出版社 书籍代码
💻 JAVA
字号:
// Exercise11_11.java: Display a pie chart
import java.awt.*;
import javax.swing.*;

public class Exercise11_11 extends JFrame {
  public static void main(String[] args) {
    JFrame frame = new Exercise11_11();
    frame.setSize(300, 300);
    frame.setTitle("Exercise11_11");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }

  public Exercise11_11() {
    getContentPane().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;

    // Draw the arc for projects
    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)));

    // Draw the arc for quizzes
    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)));

    // Draw the arc for midterm exams
    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)));

    // Draw the arc for the final exam
    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)));
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -