Results 1 to 4 of 4
  1. #1
    fetch is offline Member
    Join Date
    May 2011
    Rep Power

    Default 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.

    Attached Files Attached Files

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    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.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power


    placing a string of text on (or adjacent to) each slice for a better readable view
    Is it a question of computing the x,y for using the Graphics class's drawString method?
    Or do you need to rotate the plane of the String?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power


    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:

    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);
    Doesn't this draw the elements of slices over and over again, once per slice?

    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

  1. Replies: 0
    Last Post: 12-21-2010, 07:09 AM
  2. Help with drawing a string using mouse events
    By ptuckley in forum AWT / Swing
    Replies: 2
    Last Post: 12-14-2010, 12:09 PM
  3. Drawing an arc
    By berkeleybross in forum Java 2D
    Replies: 10
    Last Post: 12-09-2010, 02:32 AM
  4. drawing a string vertical
    By hannes in forum Java 2D
    Replies: 2
    Last Post: 01-26-2010, 06:57 AM
  5. Help with 2-D Drawing
    By Deathmonger in forum New To Java
    Replies: 4
    Last Post: 06-18-2008, 03:23 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