Results 1 to 8 of 8
  1. #1
    jazzo is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Drawing concentric circles

    hi all, I need to draw a series (10) concentric circles starting fromt he middle of the panel. This is what I have done but the circles are not concentric at all...what am I getting wrong in the coordinates? Ah, I am very new to java and I haven't done arrays as yet, just control statements and ideally I should use that for this program.

    Java Code:
    //Ovals.java
    
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    public class Ovals extends JPanel{
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		//calculates the centre of the frame
    		//int widthMiddle = ((getWidth()) / 2);
    		//int heightMiddle = ((getHeight()) / 2);
    		
    		int width = getWidth();
    		int height = getHeight();		
    		
    		
    		//calculates the centre of the panel
    		
    		int xPoint = width / 2;
    		int yPoint = height / 2; 
    		
    		//draw the rectangles
    		for(int i = 0; i < 10; i++){
    			g.drawOval(xPoint - (i * 10), yPoint - (i * 10), (i * 10), (i * 10));
    			System.out.printf("Width is %d \t and height is %d\n", xPoint, yPoint );
    		}
    	}
    }
    Java Code:
    //OvalsTest.java
    //This program draws concentric circles starting fromt the middle of the frame
    import javax.swing.JFrame;
    public class OvalsTest{
    	public static void main( String[] args){
    		Ovals ovalsPanel = new Ovals();
    		JFrame newFrame = new JFrame();
    		
    		newFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    		newFrame.add( ovalsPanel );
    		newFrame.setSize( 550, 550 );
    		newFrame.setVisible( true );
    	}
    }
    thanks

  2. #2
    jazzo is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Re: Drawing concentric circles

    ah! I am such a donkey, sorry here's why the x and y coordinates should increase only half of the size of the circle!
    Java Code:
    //Ovals.java
    
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    public class Ovals extends JPanel{
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		//calculates the centre of the frame
    		//int widthMiddle = ((getWidth()) / 2);
    		//int heightMiddle = ((getHeight()) / 2);
    		
    		int width = getWidth();
    		int height = getHeight();		
    		System.out.printf("Width is %d \t and height is %d\n", width, height );
    		
    		//calculates the centre of the panel
    		
    		int xPoint = width / 2;
    		int yPoint = height / 2;
    	//	System.out.printf("Width is %d \t and height is %d\n", xPoint, yPoint );
    		//draw the rectangles
    		//g.drawOval(xPoint - 75, yPoint -75, ( 150), ( 150));		
    		//g.drawRect(xPoint - 75, yPoint -75, ( 150), ( 150));
    		
    		for(int i = 0; i < 10; i++){
    			g.drawOval(xPoint - (i * 5), yPoint - (i * 5), (i * 10), (i * 10));
    			//System.out.printf("Width is %d \t and height is %d\n", xPoint, yPoint );
    			
    		}
    		
    	}
    }
    So that is resolved, in that I now do have circles, but they don't resize when I resize the window and I have only 10 circles whereas I am thinking I should draw as many as the frame can take: so
    1) how do I get my circles to resize when I resize the window
    2)how do I get as many circles as I can fit in the frame?
    thanks

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default Re: Drawing concentric circles

    1) I believe the paintComponent() is automatically called when the Window is resized.

    2) You have a for loop limiting yourself to 10 circles. Try changing this to produce as many circles that would fit inside the component (Hint: You know the height/width of the component...)

  4. #4
    jazzo is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Re: Drawing concentric circles

    I see, perhaps this will do then:
    Java Code:
    //Ovals.java
    
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    public class Ovals extends JPanel{
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		//calculates the centre of the frame
    		//int widthMiddle = ((getWidth()) / 2);
    		//int heightMiddle = ((getHeight()) / 2);
    		
    		int width = getWidth();
    		int height = getHeight();		
    		System.out.printf("Width is %d \t and height is %d\n", width, height );
    		
    		//calculates the centre of the panel
    		
    		int xPoint = width / 2;
    		int yPoint = height / 2;
    	//	System.out.printf("Width is %d \t and height is %d\n", xPoint, yPoint );
    		//draw the rectangles
    		//g.drawOval(xPoint - 75, yPoint -75, ( 150), ( 150));		
    		//g.drawRect(xPoint - 75, yPoint -75, ( 150), ( 150));
    		
    		for(int i = 0; i < xPoint / 10; i++){
    			g.drawOval(xPoint - (i * 5), yPoint - (i * 5), (i * 10), (i * 10));
    			//System.out.printf("Width is %d \t and height is %d\n", xPoint, yPoint );
    			
    		}
    		
    	}
    }
    I am using xPoint / 10 because each time the circles are 10px bigger and because xPoint changes when I resize the window it will change also the number of circles drawn. correct?
    thanks

  5. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default Re: Drawing concentric circles

    Well what does this give you? Does it give you want you want?

    You have xPoint which is half the width of the JPanel... and 10 which is the factor by with the diameter of the Circle is being increased... Does this fill up the entire JPanel with circles out to the limits?

  6. #6
    jazzo is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Re: Drawing concentric circles

    Pretty much yes it works the way I wanted, unless you have a better way of course : - ), open to suggestions!
    thanks

  7. #7
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default Re: Drawing concentric circles

    I hate just giving out answers/code but try the following...
    Java Code:
         for(int i = 0; i < xPoint / 5; i++){
    Does that look better? It should fill up the entire width of the panel with circles. I'm not sure if you wanted to limit it so that it only goes to the height either.

    You should really understand what that for loop is doing... why 5 is preferable to 10 and everything.

  8. #8
    jazzo is offline Member
    Join Date
    Feb 2013
    Posts
    18
    Rep Power
    0

    Default Re: Drawing concentric circles

    Thanks StormyWaters, I actually did try that too before and it draws more circles , a bit too many actually, that's why I went for /10, but I was kind og looking for perhaps a logic way to do it...I assume this is logic enough then : - )

Similar Threads

  1. finding the overlap of two circles
    By aortell24 in forum Java 2D
    Replies: 6
    Last Post: 06-10-2012, 05:03 AM
  2. newbie drawing two circles simultaneously
    By nadeemshafi9 in forum Threads and Synchronization
    Replies: 7
    Last Post: 01-09-2011, 01:53 PM
  3. Draw circles, select circles
    By cselic in forum Java 2D
    Replies: 2
    Last Post: 05-17-2010, 02:02 PM
  4. Replies: 4
    Last Post: 09-13-2008, 11:07 AM
  5. Circles (Loops)
    By Zebra in forum New To Java
    Replies: 1
    Last Post: 05-29-2008, 06:38 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
  •