9 Replies - 8135 Views - Last Post: 10 January 2013 - 07:59 AM Rate Topic: -----

#1 ShawnaInMaine  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 05-May 11

Pie Chart Applet

Posted 09 January 2013 - 11:00 AM

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();
      
    
    }

}



Is This A Good Question/Topic? 0
  • +

Replies To: Pie Chart Applet

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 864
  • View blog
  • Posts: 2,653
  • Joined: 29-July 11

Re: Pie Chart Applet

Posted 09 January 2013 - 11:12 AM

There are hundreds of examples a simple web search would bring up. This one is nice.

By the way, that's not an applet. I hope you do not submit that to your teacher. :)

This post has been edited by farrell2k: 09 January 2013 - 11:14 AM

Was This Post Helpful? 0
  • +
  • -

#3 ShawnaInMaine  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 05-May 11

Re: Pie Chart Applet

Posted 09 January 2013 - 11:18 AM

Well from my understanding of my reading, I have to create the code and then just create a html file with the class referenced. Is that totally wrong?!
Was This Post Helpful? 0
  • +
  • -

#4 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 864
  • View blog
  • Posts: 2,653
  • Joined: 29-July 11

Re: Pie Chart Applet

Posted 09 January 2013 - 12:27 PM

View PostShawnaInMaine, on 09 January 2013 - 06:18 PM, said:

Well from my understanding of my reading, I have to create the code and then just create a html file with the class referenced. Is that totally wrong?!


Kind of. :) Applets are different than desktop Swing applications.
Was This Post Helpful? 0
  • +
  • -

#5 ShawnaInMaine  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 05-May 11

Re: Pie Chart Applet

Posted 09 January 2013 - 01:19 PM

I went reading my book again and realized where I had went wrong. This is my new code but now I cannot get it display when testing so I have a feeling I've forgotten to display something somewhere. But what did i forget?

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package piechartapplet;
import java.awt.*;
import java.util.*;
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 JApplet{

    //variables
     //define the numbers to go with the crime
  
   int Murder = 26;
    int Rape = 393;
    int Burglary = 7854;
    int Theft = 25955;
    int total = Murder + Rape + Burglary + Theft;;
    float percMur =  (Murder * 100.0f) / total;
    float percRape =(Rape * 100.0f) / total;
    float PercBurg =(Burglary * 100.0f) / total;
    float PercTheft =(Theft * 100.0f) / total;
     int startPosistion, degrees; //will be used to draw pie slice
    int x;
    int y;
    int w;
    int h;
    
           /**
     * init method
     */
    public void init()
           {
           getContentPane().setBackground(Color.WHITE);
           }
          
       /**
     *
     * paint method
     */
    public void paint(Graphics g)
    {
    super.paint(g);
       Graphics2D g2d = (Graphics2D)g;
   g2d.setColor(Color.BLACK); //outside line of circle
   g2d.drawArc(x, y, w, h, startPosistion, degrees);
    
   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 = 0;
    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);
   
    }
public static void main(String args[]) {
    PieChartApplet f = new PieChartApplet();
}
}


Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: Pie Chart Applet

Posted 09 January 2013 - 03:20 PM

Are you running it as an applet or an application?

Then, consider what will draw with the values of x, y, w, and h set when the paint() method executes. What are they set to? Does that sound right?
Was This Post Helpful? 0
  • +
  • -

#7 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1156
  • View blog
  • Posts: 2,538
  • Joined: 05-May 05

Re: Pie Chart Applet

Posted 09 January 2013 - 03:21 PM

Quote

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?


This example is full of errors. The red portion isn't displayed because the arcAngle argument to fillArc is 0. See the fillArc docs. The yellow portion isn't displayed simply because the green portion is being drawn over it. Comment out the code that draws the green arc and you'll see.

This post has been edited by blackcompe: 09 January 2013 - 03:21 PM

Was This Post Helpful? 1
  • +
  • -

#8 ShawnaInMaine  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 59
  • Joined: 05-May 11

Re: Pie Chart Applet

Posted 09 January 2013 - 04:12 PM

View PostGregBrannon, on 09 January 2013 - 03:20 PM, said:

Are you running it as an applet or an application?

Then, consider what will draw with the values of x, y, w, and h set when the paint() method executes. What are they set to? Does that sound right?



This is supposed to be an applet.

I thought the values were drawn by these statements:
 startPosistion = 0;
    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);



I guess if these aren't the right drawing points then I am totally lost.
Was This Post Helpful? 0
  • +
  • -

#9 tango185  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-January 13

Re: Pie Chart Applet

Posted 10 January 2013 - 07:31 AM

You have no values assigned to x, y, w, or h when you declared them. In your original code you set the values to:

[code]x = 80, y = 80, w = 800, h = 800[\code]

This pie graph is very large, though, so I would recommend using:

[code]x = 0, y = 0, w = 200, h = 200[\code]
Was This Post Helpful? 0
  • +
  • -

#10 tango185  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-January 13

Re: Pie Chart Applet

Posted 10 January 2013 - 07:59 AM

To piggy-back on what blackcompe said, your yellow slice wasn't showing being of this code right here:

View PostShawnaInMaine, on 09 January 2013 - 04:12 PM, said:

    startPosistion = startPosistion + (degrees + degrees);
    degrees =(int)(PercTheft * 360/100);
    g.setColor(Color.GREEN);
    g.fillArc(x, y, w, h, startPosistion, degrees);



There's no need to add the degrees to the startPosistion value twice since startPosistion was already updated for the blue slice. So change that first line to:

startPosistion = startPosistion + degrees;



This will allow the yellow slice to show up.

Your red slice actually is showing up, but the slice is so small it doesn't appear to be. If you do the math you'll find that the value of percMur comes to 7.5x10^-4, or .00075. I would change the values of the crimes (murder, Rape, Burglary, Theft) to something more usable.

P.S. I apologize for the code tags in the previous post. I messed up, didn't preview, and now I can't edit the post.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1