有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java中的PieChart不显示

我在java中的piechart没有显示,对于我的类,我们将使用图形制作一个piechart,其值基于用户输入。即使尝试输入任何值,我的piechart也不会显示。 我做错了什么

我的代码:

package ass15;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.HeadlessException;
import javax.swing.JFrame;


public class PieChart extends JFrame {
    private int iNorth, iSouth, iEast, iWest, iMidWest;
    public PieChart(int North, int South, int East, int West, int MidWest) throws HeadlessException {
        super("Assignment 15");
        this.iNorth = North;
        this.iSouth = South;
        this.iEast = East;
        this.iWest = West;
        this.iMidWest = MidWest;
    }

    public void Paint(Graphics g) {

        //Integer to percent
        double dNorth,dSouth,dEast,dWest,dMidWest, total;
        dNorth = 0.00 + iNorth;
        dSouth = 0.00 + iSouth;
        dEast = 0.00 + iEast;
        dWest = 0.00 + iWest;
        dMidWest = 0.00 + iMidWest;
        total = dNorth + dSouth + dEast + dWest + dMidWest;

        //initial arc
        int startAngle = 0;
        double curValue = 0.00;
        startAngle = (int) (curValue * 360/total);
        int arcAngle = (int) (dNorth * 360/total);
        g.setColor(Color.red);
        g.fillArc(50, 50, 100, 100, startAngle, arcAngle);

        //new arc
        curValue += dNorth;
        startAngle = (int) (curValue * 360/total);
        arcAngle = (int) (dSouth * 360/total);
        g.setColor(Color.green);
        g.fillArc(50, 50, 50, 50, startAngle, arcAngle);


        //new arc
        curValue += dSouth;
        startAngle = (int) (curValue * 360/total);
        arcAngle = (int) (dEast * 360/total);
        g.setColor(Color.blue);
        g.fillArc(50, 50, 50, 50, startAngle, arcAngle);

        //new arc
        curValue += dEast;
        startAngle = (int) (curValue * 360/total);
        arcAngle = (int) (dWest * 360/total);
        g.setColor(Color.magenta);
        g.fillArc(50, 50, 50, 50, startAngle, arcAngle);

        //new arc
        curValue += dWest;
        startAngle = (int) (curValue * 360/total);
        arcAngle = (int) (dMidWest * 360/total);
        g.setColor(Color.yellow);
        g.fillArc(50, 50, 50, 50, startAngle, arcAngle);

        //background circle
        g.setColor( Color.black );
        g.drawArc( 50, 50, 50, 50, 0, 360 );
        }

    }

我的主要观点是:

package ass15;

import java.util.StringTokenizer;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class TestPieChart {

    public static void main(String[] args) {
        int north,south,east,west,midwest;
        String token;
        String in = "";
        in = JOptionPane.showInputDialog("Input sales for regions");

        StringTokenizer stk = new StringTokenizer(in, ",");
        token = stk.nextToken().trim();
        north = Integer.parseInt(token);
        token = stk.nextToken().trim();
        south = Integer.parseInt(token);
        token = stk.nextToken().trim();
        east = Integer.parseInt(token);
        token = stk.nextToken().trim();
        west = Integer.parseInt(token);
        token = stk.nextToken().trim();
        midwest = Integer.parseInt(token);
        PieChart pi = new PieChart(north,south,east,west,midwest);

        pi.setVisible(true);
        pi.setSize(500, 500);
    }

}

这是个愚蠢的错误。。。 答复:

方法

Paint(Graphics g)

应该是

paint(Graphics g)

直到我玩了密码才意识到


共 (0) 个答案