Results 1 to 5 of 5

Thread: Arc2D.PIE help

  1. #1
    MZA
    MZA is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default Arc2D.PIE help

    Hi there.

    I have to construct a project that is a solid color circle with four circles inside of each other (kind of like a bulls-eye) that each have a third of the circle a different color (hence the Arc2D problem)

    I'm having problems with making the inner pie pieces. I am able to construct them, but not at the angles required. Also, I'm trying to figure out how to keep them at a consistent ratio with the outer circle when I re-size the window. If someone could give me some pointers or some sample code I would be eternally grateful.

    Thanks,

    MZA

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

  3. #3
    MZA
    MZA is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    :D

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Rectangle;
    import java.awt.geom.Ellipse2D;
    import java.awt.geom.Arc2D;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    
    public class prog2
    {
       public static void main(String[] args)
       {
          JFrame frame = new JFrame();
          frame.setSize(360, 360);
          frame.setTitle("Prog2");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
          GraphicComponent component = new GraphicComponent();
          frame.add(component);
    
          frame.setVisible(true);
       }
    }
    
    class GraphicComponent extends JComponent
    {
       public void paintComponent(Graphics g)
       {
         // Recover Graphics2D
          Graphics2D g2 = (Graphics2D) g;
          Ellipse2D.Double elli;
          Arc2D.Double arc;
    
          int h=getHeight();    //h is height of component
          int w=getWidth();     //w is width of component
    
          Ellipse2D.Double circ1 = new Ellipse2D.Double(16, 16, w*9/10, h*9/10);
          g2.setColor(Color.ORANGE);
          g2.fill(circ1);
    
          Arc2D.Double arc1 = new Arc2D.Double(64, 64, w*3/5, h*3/5, 360, 480, Arc2D.PIE);
          g2.setColor(Color.GREEN);
          g2.fill(arc1);
    
          Arc2D.Double arc2 = new Arc2D.Double(64, 64, w*3/5, h*3/5, 240, 120, Arc2D.PIE);
          g2.setColor(Color.RED);
          g2.fill(arc2);
    
          Arc2D.Double arc3 = new Arc2D.Double(64, 64, w*3/5, h*3/5, 0, 120, Arc2D.PIE);
          g2.setColor(Color.BLUE);
          g2.fill(arc3);
       }
    thanks for responding, this is a pain in the arse :p. I'm a complete noob when it comes to this stuff.
    Last edited by MZA; 09-30-2009 at 04:07 AM.

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

    Default

    Myself, I'd use a 2D array of arcs, the outer index for the circles, the inner index for the arcs, and perhaps a parallel array (sorry!) of Colors. I also wouldn't hard-code the x, y, w, h of the arcs but would calculate them based on the width and height of the component displaying the arcs. You could do the calculations within a ComponentListener's componentResize() method.

    for instance,

    declarations...
    Java Code:
       private static final int CIRCLE_COUNT = 4;
       private static final int ARC_COUNT = 3;
    
       private Arc2D[][] arcs = new Arc2D[CIRCLE_COUNT][ARC_COUNT];
       private Color[][] colors = new Color[CIRCLE_COUNT][ARC_COUNT];
    the component listener...
    Java Code:
          mainPanel.addComponentListener(new ComponentAdapter() {
             public void componentResized(ComponentEvent e) {
                
                int width = mainPanel.getWidth();
                int height = mainPanel.getHeight();
                for (int i = 0; i < CIRCLE_COUNT; i++) {
                   double x = //... add code here
                   double y = //... add code here
                   double w = width - 2*x;
                   double h = height - 2*y;
    
                   for (int j = 0; j < ARC_COUNT; j++) {
                      double start = (double)j * 360 / ARC_COUNT;
                      double extent = //... add code here
                      Arc2D arc = new Arc2D.Double(x, y, w, h, start, extent, Arc2D.PIE);
                      arcs[i][j] = arc;
                   }
                   
                }
                mainPanel.repaint();
             }
          });
    and in the paintComponent
    Java Code:
          Graphics2D g2 = (Graphics2D)g;
          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
          for (int i = 0; i < arcs.length; i++) {
             for (int j = 0; j < arcs[i].length; j++) {
                if (arcs[i][j] != null) {
                   g2.setColor(colors[i][j]);
                   g2.fill(arcs[i][j]);
                }
             }
          }
    Last edited by Fubarable; 09-30-2009 at 06:10 AM.

  5. #5
    MZA
    MZA is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    Thanks a ton for the input, it helped me solve my problem. Glad to find a place where I can get help even on basic stuff :D.

Similar Threads

  1. Arc2D.Double coordinates
    By alley in forum Java 2D
    Replies: 2
    Last Post: 11-07-2007, 10:27 PM

Posting Permissions

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