Results 1 to 4 of 4

Thread: Problem with printing circles

  1. #1
    Join Date
    Dec 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with printing circles

    Code:
    	int width = getWidth();
    	int height = getHeight();
    
    	int counter = 0;
    	int counter2 = 0;
    
    
    	
    
    		while (counter < 7)
    		{
    		g.drawOval(width*counter/8   ,0,   width*(counter+1)/8  ,height/8);
    		
    		counter++;
    		}
    I'm trying to print a row if 8 cirlces the same size along the top of a jpane, but it doesn't work.

    the first circle prints
    the second circles starts at the right place but streches out to double where it should be


    the third also starts at the right place but stretches out weird again.

  2. #2
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    can you show a screenshot?
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You're forgetting your basic arithmetic Division is applied before subtraction, so you're doing width * (counter / 8) which is always zero (counter being an int). You want (width * counter) / 8. Also, here you have an initialiser, a test, and an incrementor, which is what for loops are for, although to optimise we can put the incrementation and the addition steps together:
    Code:
    int width = getWidth(), height = getHeight();
    
    for(int i = 0; i < 8; )
      g.drawOval((width * i) / 8, 0, (width * (++i)) / 8, height / 8);
    Also note that because you started from zero, if you want to execute the loop eight times you must loop until i < 8 or i <= 7. i < 7 will only execute seven times.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Jul 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default java

    Hi, i am new to this forum.
    This topic is also new to me.
    This kind of forums are really helpful for those who are working on java.
    It is a very informative site.

    ===============================
    Jessy

    Wide Circles

Bookmarks

Posting Permissions

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