Results 1 to 7 of 7
  1. #1
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default drawing a pie chart

    Hey there, I am following a tutorial on how to draw a pie chart using JPanels and stuff.
    I have spent ages trying to do this, and my my code does not work. All I get is a window with a mostly black background.
    Can someone please tell me what I can do to my code to get it working and display a pie chart? thanks
    Java Code:
    import java.awt.*;
    
    import javax.swing.*;
    
    public class pie_window extends JFrame {
    	public static void main(String[] args) {
    		pie_window p = new pie_window();
    
    	}
    
    	public pie_window() {
    		pie_shape z = new pie_shape();
    		super.add(z);
    		super.setVisible(true);
    		this.pack();
    	}
    
    }
    
    class pie_shape extends JPanel {
    	Color color;
    	segment[] parts_of_pie;
    	int initial_position_of_first_arc;
    	int extension_of_arc;
    	float total_portion;
    
    	public pie_shape() {
    		segment[] parts_of_pie = { new segment(0.5f, Color.RED),
    				new segment(0.3f, Color.BLUE) };
    
    		for (segment segments : parts_of_pie) {
    			total_portion += segments.portion;
    		}
    
    	}
    
    	protected void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		for (segment segments : parts_of_pie) {
    			g.setColor(segments.color);
    			g.drawArc(
    					this.getWidth() / 2,
    					this.getHeight() / 2,
    					this.getWidth() / 2,
    					this.getHeight() / 2,
    					initial_position_of_first_arc + extension_of_arc,
    					(initial_position_of_first_arc + extension_of_arc + (int) (((segments.portion * 360) / total_portion))));
    			extension_of_arc += (segments.portion * 360) / total_portion;
    		}
    
    	}
    
    }
    
    class segment {
    	Float portion;
    	Color color;
    
    	public segment(Float in_portion, Color in_color) {
    		portion = in_portion;
    		color = in_color;
    	}
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You may be ignoring the most important result of running your code -- you also get a NullPointerException because of variable shadowing, and this is causing most of your problems. You are re-declaring a field in a constructor shadowing the class field and not initializing the class field. Solution: don't re-declare the variable.

  3. #3
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    Thanks for the reply
    I am still really stuck though
    The exception raised was:
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at pie_shape.paintComponent(pie_window.java:41)
    So I changed the line it was pointing at (line 41) from:
    Java Code:
    for (segment segments : parts_of_pie)
    to
    Java Code:
    for (int i = 0; i <= parts_of_pie.length; i++)
    But it still raises the same exception at the same place.
    I am really stuck for what to do :(

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your change is cosmetic only and will do nothing (as you're finding out) because it doesn't fix the primary problem -- an object is null, and you're trying to use it. Since the error occurs on this line:
    Java Code:
    for (segment segments : parts_of_pie)
    then the null object is being used on that line, and that can only mean one variable, parts_of_pie, is the null one. So you have to look into why this class field is null, and in fact I mentioned the reason above (though perhaps I wasn't clear) -- you are shadowing this variable.

    Java Code:
    class pie_shape extends JPanel {
    	Color color;
    	[color="red"][b]segment[] parts_of_pie; // #1 [/b][/color]
    	int initial_position_of_first_arc;
    	int extension_of_arc;
    	float total_portion;
    
    	public pie_shape() {
    		[color="red"][b]segment[] parts_of_pie = { new segment(0.5f, Color.RED),
    				new segment(0.3f, Color.BLUE) }; // #2 [/b][/color]
    
    		for (segment segments : parts_of_pie) {
    			total_portion += segments.portion;
    On line // #1 above you declare the parts_of_pie class field, but then in the constructor, on line //#2 (actually it's 2 lines here) you re-declare this variable in the pie_shape constructor. So you construct the object only in the constructor, but since the shadowed variable was declared in the constructor it is only visible in the constructor.

    Solution: don't re-declare this variable in the constructor.

    There are other problems in your code though, but you must solve this one first to move on.

  5. #5
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    Okay thankyou
    I think I have solved that particular problem:

    I have changed my code from:
    Java Code:
    class pie_shape extends JPanel {
    	Color color;
    	segment[] parts_of_pie; // #1 
    	int initial_position_of_first_arc;
    	int extension_of_arc;
    	float total_portion;
    
    	public pie_shape() {
    		segment[] parts_of_pie = { new segment(0.5f, Color.RED),
    				new segment(0.3f, Color.BLUE) }; // #2 
    
    		for (segment segments : parts_of_pie) {
    			total_portion += segments.portion;


    to:
    Java Code:
    	segment[] parts_of_pie;
    	int initial_position_of_first_arc;
    	int extension_of_arc;
    	float total_portion;
    
    	public pie_shape() {
    		this.parts_of_pie = new segment[]{ new segment(0.5f, Color.RED),
    				new segment(0.3f, Color.BLUE) };

    But a new exception is being thrown in this method:
    Java Code:
    protected void paintComponent(Graphics g) {
    		super.paintComponent(g);
    
    		for (int i = 0; i <= parts_of_pie.length; i++) {
    			g.setColor(parts_of_pie[i].color);
    method

    Exception thrown:
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
    	at pie_shape.paintComponent(pie_window.java:45)
    But I can't see why it's telling me I'm trying to access elements in the array that don't exist, because I added them to the array in the pie_shape constructor.
    More help please?
    your time is very much appreciated!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by dewitrydan View Post
    But a new exception is being thrown in this method:
    Java Code:
    protected void paintComponent(Graphics g) {
    		super.paintComponent(g);
    
    		for (int i = 0; i <= parts_of_pie.length; i++) {
    			g.setColor(parts_of_pie[i].color);
    method

    Exception thrown:
    Java Code:
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3
    	at pie_shape.paintComponent(pie_window.java:45)
    But I can't see why it's telling me I'm trying to access elements in the array that don't exist, because I added them to the array in the pie_shape constructor.
    simple error and you'll kick yourself for it...
    i goes from 0 to what? How many items are there in the array? when i is the maximum value is there an array item that corresponds to it?

  7. #7
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default

    Thank you.
    I have now fixed that problem.
    I have changed my paintComponent to:
    Java Code:
    protected void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		// extension_of_arc = 0;
    
    		for (int i = 0; i <= parts_of_pie.length - 1; i++) {
    			System.out.println(parts_of_pie.length);
    
    			 System.out.println("extension_of_arc = " + extension_of_arc);
    
    			g.setColor(parts_of_pie[i].color);
    
    			
    
    			g.fillArc(0, 0, 200, 200, extension_of_arc,
    					(int) (extension_of_arc + (parts_of_pie[i].portion * 360)
    							/ total_portion));
    			extension_of_arc += (extension_of_arc + (parts_of_pie[i].portion * 360)
    					/ total_portion);
    
    		}
    But all I get is a black circle, but it's meant to be a pie chart with differnent coloured segments :S

Similar Threads

  1. iReport Chart
    By rose_mary in forum Other IDEs
    Replies: 0
    Last Post: 12-09-2008, 05:17 PM
  2. SWT 2D Chart: Flowchart
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 06-28-2008, 09:20 PM
  3. Demo bar chart and pie chart
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-23-2008, 11:22 PM
  4. Daylight Chart 2.6
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 04-22-2008, 04:35 PM
  5. Daylight Chart 1.3
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-17-2007, 08:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •