Results 1 to 4 of 4
- 06-11-2011, 06:20 PM #1
Member
- Join Date
- May 2011
- Posts
- 1
- Rep Power
- 0
drawing string to each slice of pie
I have a complete application which I am now trying to add aesthetics to. I have an amortization table calculated from a user input interface which all works fine. In addition I have a chart which is shown when a new calculation is executed and the user clicks the "Show Chart" button. The chart is actually fine (colors might blur your vision ;-)) but I would like to add text to each slice. I feel comfortable with most other parameters I have done (you'll notice it is all without the use of the graphic designer, which is a requirement and I actually like learning that way). But I am not able to succeed in placing a string of text on (or adjacent to) each slice for a better readable view.
4 files attached in zip: classes(Main, filereader, piechart) external(loans.txt)
Tips from anyone would be wonderful!
Thank you.
Craig
Code:
-
Rather than ask folks to wade through a lot of unrelated code, you'll be more likely to get helpful responses if you could create a single small compilable program that demonstrates your problem, a so-called SSCCE.
- 06-11-2011, 09:34 PM #3
Is it a question of computing the x,y for using the Graphics class's drawString method?placing a string of text on (or adjacent to) each slice for a better readable view
Or do you need to rotate the plane of the String?
- 06-12-2011, 01:54 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Computing (x,y) for a given slice and drawing the string horizontally would be more readable (unless there were a whole bunch of small slices near the "top"). Remember that labels on the left have to *end* at the slice, so they would start at (x-len,y) where len is the length of the label.
------------
I've just peeked at your source code and a couple of things strike me about that. First you could include the label as an attribute of the PieValue class. Then it's just a matter of doing a little trig and getting the x,y-coordinates right for the drawString() calls. Secondly there is something weird in that class: it provides a method for drawing a whole array of slices (it's not a static method, but I would have made it so because it basically *is* static), but then you call this method for *each slice*. Ie in (the badly named) MyComponent:
Doesn't this draw the elements of slices over and over again, once per slice?Java Code:public void paint(Graphics g) { // Draw the pie for (int i = 0; i < slices.length; i++) { slices[i].drawPie((Graphics2D) g, getBounds(), slices);
It might make sense to have a JComponent subclass that just takes an array of PieValue as an argument in its constructor and does most of the painting logic that you currently do in PieValue. Alternatively you could use MyComponent as it it is (but why make it an inner class?) but have it coordinate the painting: ie set the colour and angles and then pass that information to a slice which can then paint itself.
Basically I'm saying separate the logic of painting the chart from that of painting a slice.
---------------
An incidental benefit of moving MyComponent to be a standalone class (PieChart?) and splitting the painting responsibilities between it and PieSlice is that you would have a simpler "subsystem" that is independent of all the other stuff in your app. And therefore just the thing for the SSCCE Fubarable mentioned if you get stuck.
Similar Threads
-
Can't understand diff between time slice and quantum in java threading
By daudiam in forum New To JavaReplies: 0Last Post: 12-21-2010, 06:09 AM -
Help with drawing a string using mouse events
By ptuckley in forum AWT / SwingReplies: 2Last Post: 12-14-2010, 11:09 AM -
Drawing an arc
By berkeleybross in forum Java 2DReplies: 10Last Post: 12-09-2010, 01:32 AM -
drawing a string vertical
By hannes in forum Java 2DReplies: 2Last Post: 01-26-2010, 05:57 AM -
Help with 2-D Drawing
By Deathmonger in forum New To JavaReplies: 4Last Post: 06-18-2008, 02:23 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks