Click here to Skip to main content
16,017,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Write a program that accepts one command line argument, the name of a file containing data to be drawn as a pie chart, and draws the pie chart in a window.
• Sample data file, birds.txt, contains the one-word categories of birds, and the number of each kind.
swallow 10
magpie 5
fairywren 7
osprey 2
fantail 3
• The pie chart must use a different colour for each pie segment, and display a legend that shows the labels and the percentage for each.
• Your program should support up to 10 categories.

[Added from comment]

This is what I have so far...
Java
import javax.swing.*;
import java.awt.*;

public class Pie4 {

    public static void main(String[] args) {
        JFrame f = new JFrame("Pie chart");
        f.setSize(600, 350);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new PieChart());
        f.setVisible(true);	
    }	
}

Java
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.*;

public class PieChart
extends JComponent {
    public void paintComponent(Graphics g) {

        Graphics2D g2 = (Graphics2D) g.create();
        Graphics2D g3 = (Graphics2D) g.create();
        g3.setColor(Color.BLACK);
        g2.setColor(Color.BLUE);
        //for (int i = 0; i < 5; i = i + 1) {
            //g2.fillRect(230, 20 * i + 50 , 20, 20);	
            //g3.drawString("swallow", 255, 20 * i + 65);	
            //g3.drawString("37.0%", 385, 20 * i + 65);	
        //}
        g2.fillArc(50, 50, 150, 150, 0, 360);	
    }
}


I can't figure out the rest
Posted
Updated 20-Sep-15 1:29am
v3
Comments
Wendelius 20-Sep-15 7:21am    
What have you done so far, where are you stuck at?
Richard MacCutchan 20-Sep-15 7:35am    
You need to explain exactly what the problem is. We cannoot guess what is in your mind. You may find the tutorial at http://docs.oracle.com/javase/tutorial/2d/index.html useful.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900