Results 1 to 5 of 5
  1. #1
    GenkiSudo is offline Member
    Join Date
    Sep 2008
    Posts
    6
    Rep Power
    0

    Default Beginner Java graphics - filling concentric circles with color

    Hello everyone.

    I have to do an exercise in which I'm supposed to draw olympic circles and color them in olympic color.
    I don't have a problem with drawing them (I drew them as concentric circles).
    My problem is coloring them.

    I have 3 classes:

    1.OlympicRing - it draws the outer and the inner circle

    2.OlympicComponent - extends JComponent, creates 5 OlympicRing objects with different positions and colors

    3.OlympicRingViewer (main method that creates a frame and adds the olympic component to it)

    Now, I tried filling the outer ring with olympic color and inner ring with frame background color (code is from OlympicRing class):
    Java Code:
    g2.draw(outerRing);
    g2.setColor(ringColor);
    g2.fill(outerRing);
    	
    g2.draw(innerRing);
    g2.setColor(g2.getBackground());
    g2.fill(innerRing);
    This works with one ring. But if there's more of them and they intersect, their intersection areas get covered with background color.

    Is there a way to give boundaries to the fill method ? Am I making any sense?
    Any suggestions?

  2. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    Use fillArc(x, y, w, h, startAngle, arcAngle) or/and drawArc. Real pain in the ass though.
    I die a little on the inside...
    Every time I get shot.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default

    their intersection areas get covered with background color.
    I would think that the last one drawn is the one you see. The earlier ones will be overwritten. So since you have set the color to be the background color for the last fill, that is the color it would be.
    Would fillPolygon allow you to select the parts of your display to be filled?

  4. #4
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    6

    Default

    with fillPolygon you can give the exact coordinates so it could work, but drawing a circle with fillpolygon...? good luck.
    I die a little on the inside...
    Every time I get shot.

  5. #5
    GenkiSudo is offline Member
    Join Date
    Sep 2008
    Posts
    6
    Rep Power
    0

    Default

    Thanks for the push in the right direction. While reading about drawArc I stumbled upon an easier solution.
    Instead of drawing inner and outer circle, I just have to draw one circle with thicker stroke.
    Here's the final code for the OlympicRing class, if anyone is interested.

    Java Code:
    public class OlympicRing 
    {
    	final static BasicStroke myStroke  = new BasicStroke(8);
    	private int xPosition;
    	private int yPosition;
    	private int width;
    	private int height;
    	private Color ringColor;
    	
    	public OlympicRing (int x, int y, Color c)
    	
    	{	
    		xPosition = x;
    		yPosition = y;
    		width = 92;
    		height = 92;
    		ringColor = c;
    		
    	}
    	
    	public void draw (Graphics2D g2)
    	{
    		Ellipse2D.Double ring = new Ellipse2D.Double (xPosition, yPosition, width, height);
    		
    		g2.setStroke(myStroke);	
    		g2.setColor(ringColor);
    		g2.draw(ring);
    		
    		
    	}
    	
    }

Similar Threads

  1. Replies: 13
    Last Post: 07-06-2008, 09:11 AM
  2. Circles (Loops)
    By Zebra in forum New To Java
    Replies: 1
    Last Post: 05-29-2008, 06:38 AM
  3. Replies: 9
    Last Post: 05-26-2008, 04:30 AM
  4. beginner to Java
    By notwist in forum New To Java
    Replies: 15
    Last Post: 04-18-2008, 09:41 AM
  5. Problem in printing java graphics
    By Mahendra in forum Java 2D
    Replies: 0
    Last Post: 01-23-2008, 12:45 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
  •