Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Pie Chart Applet

$
0
0
Okay I found an example an kind of figured this out, but it is not diplaying each portion. Currently it displays the blue and green but what about the red and yellow portions? Have I missed something? can you point me in the right direction or show me a few different examples I can compare my code too?





/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package piechartapplet;
import java.awt.*;
import javax.swing.*;

/**
 *
 * @author Shawna Rowe
 * 
 * 
 * Write an applet that includes a pie chart. 
 * Use a news article with statistics that are good candidates for a pie chart: 
 *  for example, political candidate preferences; percentages of those for, 
 * against, or undecided about a ballot measure; and so forth.
 * 
 * Cite the source for your input statistics.
 * Submit the applet along with an HTML file to launch it. 
 * 
 */
public class PieChartApplet extends JFrame {
    int Murder, Rape, Burglary, Theft, total;  // statistics to enter
    float percMur, percRape, PercBurg, PercTheft; // percentages
    /*
     * The layout method sets up the applet similar to
     * the set up of a constructor
     */
    public PieChartApplet()
    {
      setTitle("Pie Chart Statistics");
            setSize(1400, 1400);
            setVisible(true);
            setLayout(new GridLayout(2, 3));
            setBackground(Color.WHITE);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
 
    //Create the outer circle of the pie chart
    /**
     *
     * @param g
     */
    @Override
    public void paint(Graphics g)
    {
     
        Graphics2D g2d = (Graphics2D)g;
    g2d.setColor(Color.BLACK); //outside line of circle
   int x = 80, y = 80, w = 800, h= 800; //defines size
    
    int startPosistion, degrees; //will be used to draw pie slice
      
    
    //define the numbers to go with the crime
    
    Murder = 26;
    Rape = 393;
    Burglary = 7854;
    Theft = 25955;
    
    //calculate percentages
    total = Murder + Rape + Burglary + Theft;
    percMur =  (Murder * 100.0f) / total;
    percRape =(Rape * 100.0f) / total;
    PercBurg =(Burglary * 100.0f) / total;
    PercTheft =(Theft * 100.0f) / total;
    
    
    /*
     * Used an example found
     * http://mainline.brynmawr.edu/Courses/cs110/fall2003/Applets/PieChart/PieChart.html
     * 
     * 
     * 
     * 
     */
    //display pie chart
    startPosistion = -4;
    degrees =(int)(percMur * 360/100);
    g.setColor(Color.RED);
    g.fillArc(x, y, w, h, startPosistion, degrees);
   
    startPosistion = degrees;
    degrees =(int)(percRape * 360/100);
    g.setColor(Color.YELLOW);
    g.fillArc(x, y, w, h, startPosistion, degrees);
   
    startPosistion = startPosistion + degrees;
    degrees =(int)(PercBurg * 360/100);
    g.setColor(Color.BLUE);
    g.fillArc(x, y, w, h, startPosistion, degrees);
   
    startPosistion = startPosistion + (degrees + degrees);
    degrees =(int)(PercTheft * 360/100);
    g.setColor(Color.GREEN);
    g.fillArc(x, y, w, h, startPosistion, degrees);
   
    }
    

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        PieChartApplet f = new PieChartApplet();
      
    
    }

}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>