掙值分析

风·华正茂發表於2024-04-08

沒有具體計算

import javax.swing.*;
import java.awt.*;

public class EarnValueAnalysis extends JPanel {
    private double[] EV = {9, 31, 47.5};
    private double[] PV = {10, 35, 50};
    private double[] AC = {10, 36, 54};

    public EarnValueAnalysis() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);
        frame.getContentPane().add(this);
        frame.setVisible(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int margin = 30;
        int width = getWidth();
        int height = getHeight();

        int[] xPoints = {50, 150, 250};
        int[] yPointsEV = {200 - (int) (EV[0] * 2), 200 - (int) (EV[1] * 2), 200 - (int) (EV[2] * 2)};
        int[] yPointsPV = {200 - (int) (PV[0] * 2), 200 - (int) (PV[1] * 2), 200 - (int) (PV[2] * 2)};
        int[] yPointsAC = {200 - (int) (AC[0] * 2), 200 - (int) (AC[1] * 2), 200 - (int) (AC[2] * 2)};

        g2d.setColor(Color.BLACK);

        // 繪製 x 軸
        g2d.drawLine(margin, height - margin, width - margin, height - margin);
        // 繪製 y 軸
        g2d.drawLine(margin, margin, margin, height - margin);

        // 繪製 x 軸標籤
        g2d.drawString("時間(周)", width - margin - 10, height - margin + 20);
        // 繪製 y 軸標籤
        g2d.drawString("錢(元)", margin - 20, margin - 10);

        // 繪製 x 軸刻度
        g2d.drawString("1", margin + 45, height - margin + 15);
        g2d.drawString("2", margin + 145, height - margin + 15);
        g2d.drawString("3", margin + 245, height - margin + 15);

        // 繪製 y 軸刻度
        g2d.drawString("10", margin - 20, height - margin - 20);
        g2d.drawString("20", margin - 20, height - margin - 80);
        g2d.drawString("30", margin - 20, height - margin - 140);
        g2d.drawString("40", margin - 20, height - margin - 200);
        g2d.drawString("50", margin - 20, height - margin - 260);
        g2d.drawString("60", margin - 20, height - margin - 320);

        g2d.setColor(Color.RED);
        g2d.setStroke(new BasicStroke(2));
        g2d.drawPolyline(xPoints, yPointsEV, 3);

        g2d.setColor(Color.GREEN);
        g2d.setStroke(new BasicStroke(2));
        g2d.drawPolyline(xPoints, yPointsPV, 3);

        g2d.setColor(Color.BLUE);
        g2d.setStroke(new BasicStroke(2));
        g2d.drawPolyline(xPoints, yPointsAC, 3);

        // 繪製左上角的線條含義說明
        g2d.setColor(Color.BLACK);
        g2d.drawString("EV - 紅色", margin + 10, margin + 30);
        g2d.drawString("PV - 綠色", margin + 10, margin + 45);
        g2d.drawString("AC - 藍色", margin + 10, margin + 60);

        g2d.setColor(Color.BLACK);
        g2d.setFont(new Font("宋體", Font.BOLD, 12));
        g2d.drawString("", xPoints[0], yPointsEV[0]);
        g2d.drawString("", xPoints[0], yPointsPV[0]);
        g2d.drawString("", xPoints[0], yPointsAC[0]);

        g2d.drawString("淨值分析", width / 2 - 40, height - margin + 40);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(EarnValueAnalysis::new);
    }
}

相關文章