piechart1.java

来自「是一个成绩管理系统」· Java 代码 · 共 61 行

JAVA
61
字号
import javax.swing.*;
import java.awt.*;

public 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.white);
	g.fillRect(0, 0, w, h);
    // 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("<60 -- 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("60-69 -- 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("70-79 -- 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.magenta);
    g.fillArc(x, y, 2 * radius, 2 * radius, (int)(60 * 360 / 100),
      (int)(23 * 360 / 100));
    g.setColor(Color.black);
    g.drawString("80-89 -- 23%",
      (int)(xCenter + radius*Math.cos(2 * Math.PI * 0.71)),
      (int)(yCenter - radius*Math.sin(2 * Math.PI * 0.71)));
      
    g.setColor(Color.cyan);
    g.fillArc(x, y, 2 * radius, 2 * radius, (int)(83 * 360 / 100),
      (int)(17 * 360 / 100));
    g.setColor(Color.black);
    g.drawString(">=90 -- 17%",
      (int)(xCenter + radius*Math.cos(2 * Math.PI * 0.9))+15,
      (int)(yCenter - radius*Math.sin(2 * Math.PI * 0.9)));  
  }
}

⌨️ 快捷键说明

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