Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-30-2009, 02:33 AM
MZA MZA is offline
Member
 
Join Date: Sep 2009
Posts: 5
Rep Power: 0
MZA is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 09-30-2009, 02:59 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,504
Rep Power: 8
Fubarable is on a distinguished road
Default
First you show us yours (code that is).
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-30-2009, 03:22 AM
MZA MZA is offline
Member
 
Join Date: Sep 2009
Posts: 5
Rep Power: 0
MZA is on a distinguished road
Default


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 . I'm a complete noob when it comes to this stuff.

Last edited by MZA; 09-30-2009 at 05:07 AM.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-30-2009, 07:05 AM
Fubarable's Avatar
Moderator
 
Join Date: Jun 2008
Posts: 6,504
Rep Power: 8
Fubarable is on a distinguished road
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...
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...
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
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]);
            }
         }
      }
__________________
When posting code, please use code tags so that your code is readable. To do this, place the tag [code] before your block of code and [/code] after your block of code.
How to use Code Tags

Last edited by Fubarable; 09-30-2009 at 07:10 AM.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-30-2009, 08:26 AM
MZA MZA is offline
Member
 
Join Date: Sep 2009
Posts: 5
Rep Power: 0
MZA is on a distinguished road
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 .
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Arc2D.Double coordinates alley Java 2D 2 11-07-2007 11:27 PM


All times are GMT +2. The time now is 08:36 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org