Results 1 to 7 of 7
Thread: drawing a pie chart
- 10-23-2010, 03:59 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
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; } }
-
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.
- 10-23-2010, 08:47 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
Thanks for the reply
I am still really stuck though
The exception raised was:
So I changed the line it was pointing at (line 41) from:Java Code:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at pie_shape.paintComponent(pie_window.java:41)
toJava Code:for (segment segments : parts_of_pie)
But it still raises the same exception at the same place.Java Code:for (int i = 0; i <= parts_of_pie.length; i++)
I am really stuck for what to do :(
-
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:
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:for (segment segments : parts_of_pie)
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.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;
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.
- 10-24-2010, 07:35 AM #5
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
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:
methodJava 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);
Exception thrown:
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.Java Code:Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 3 at pie_shape.paintComponent(pie_window.java:45)
More help please?
your time is very much appreciated!
-
- 10-24-2010, 12:33 PM #7
Member
- Join Date
- Aug 2010
- Posts
- 67
- Rep Power
- 0
Thank you.
I have now fixed that problem.
I have changed my paintComponent to:
But all I get is a black circle, but it's meant to be a pie chart with differnent coloured segments :SJava 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); }
Similar Threads
-
iReport Chart
By rose_mary in forum Other IDEsReplies: 0Last Post: 12-09-2008, 05:17 PM -
SWT 2D Chart: Flowchart
By Java Tip in forum SWTReplies: 0Last Post: 06-28-2008, 09:20 PM -
Demo bar chart and pie chart
By Java Tip in forum java.awtReplies: 0Last Post: 06-23-2008, 11:22 PM -
Daylight Chart 2.6
By Java Tip in forum Java SoftwareReplies: 0Last Post: 04-22-2008, 04:35 PM -
Daylight Chart 1.3
By levent in forum Java SoftwareReplies: 0Last Post: 05-17-2007, 08:22 AM


LinkBack URL
About LinkBacks


Bookmarks