Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Coloring The Nested Ellipses/Circles

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Coloring The Nested Ellipses/Circles

    Hey all,

    I am learning Java Swing for 2 days and trying to do some simple codes for better learning.

    I have three(can be more) nested elllipse. I want to fill the gaps between the ellipses with different colors. However, have little coordinate problem. That piece of code can properly draws 3 circles (or whatever number you want). I just want to fill the gaps between those nested circles with different colors. How to use fill method and how to give it correct coordinate parameters to do that job?
    Thanks in advance.

    	protected void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		Graphics2D obj = (Graphics2D)g;
    		obj.setRenderingHint(
    				RenderingHints.KEY_ANTIALIASING,
    				RenderingHints.VALUE_ANTIALIAS_ON);
     
    		int maxRadius = Math.min(getWidth(), getHeight());
    		//int alpha = 255;
    		//int range = 255 - 32; ignore those lines.
     
    		obj.setStroke(new BasicStroke(0, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
     
    		for(int i = 1; i< 4; i++){ // That code draws nested 3 circles.
     
    			float progress = (float) i / (float)4; //  it increases the radiance of the ellipses step by step.
    			//alpha = 255 - Math.round(range * progress);
    			//Color color = new Color(0, 0, 0, alpha);
     
     
    			int radius = Math.round(maxRadius * progress);
     
     
    			int x = (getWidth() - radius) / 2;
    			int y = (getHeight() - radius) / 2;
    			obj.setColor(Color.BLACK);	
    			obj.draw(new Ellipse2D.Float(x, y , radius, radius));
    			//obj.fill(new Ellipse2D.Float(x, y, radius,radius ));
     
     
    		}
     
    		obj.dispose();
    	}


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Coloring The Nested Ellipses/Circles

    Use obj.fill, as you've commented out and draw the largest circle first. Set a different color for each circle.

Similar Threads

  1. coloring the checked row in jtable
    By harshilshah in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 18th, 2013, 02:06 PM
  2. coloring the checked row in jtable using checkbox
    By harshilshah in forum AWT / Java Swing
    Replies: 1
    Last Post: April 18th, 2013, 12:53 PM
  3. sudoku using graph coloring algorithm
    By priyank in forum Algorithms & Recursion
    Replies: 3
    Last Post: April 21st, 2011, 02:22 PM
  4. Help with creating random circles
    By cool48 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 5th, 2011, 09:18 AM
  5. [SOLVED] Coloring Pixels
    By Javajava in forum Java SE APIs
    Replies: 2
    Last Post: July 7th, 2010, 04:02 PM

Tags for this Thread