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 13 of 13

Thread: Help with Object Looping

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with Object Looping

    Hey everyone, first post from me.

    I'm currently doing a programming course and although so far I've been able to get my head around my assignments this one has me stumped. Here is the code which when run draws a circle at the top left of the frame.

    The assignment is to use a loop that would redraw the circle multiple times, diagonally across the frame finishing at the bottom right corner.


    I'm not looking for someone to simply solve it for me, but point me in the right direction as so far I just can't get my head around it.

    Cheers in advance!


    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.Ellipse2D;
     
    public class DrawShapes extends JFrame
    {
    	public void paint(Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D)g;
        	super.paint(g2);
    		int w = getWidth();
    		int h = getHeight();
    		g.setColor(Color.GREEN);
     
    		Ellipse2D.Double myEllipse;
     
    		myEllipse = new Ellipse2D.Double(50, 50, 25, 25);
    		g2.fill(myEllipse);
     
     
    	}
            public static void main(String[] args)
    	{
    		DrawShapes p = new DrawShapes();
    		p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		p.setSize(500,500);
    		p.setVisible(true);
    	}
    }
    Last edited by helloworld922; November 23rd, 2010 at 03:47 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with Object Looping


  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Object Looping

    I just don't know where to start with this code.

    In practice it makes sense, create a for loop that redraws the circle and increments it by say 50 pixels down and to the right each time. But so far I haven't been able to get a coherent code down.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with Object Looping

    Well then post what you've done, no matter how incoherent, and we'll go from there. Don't forget the code (not quote) tags.

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Object Looping

    This is the direction I was going in but I'm pretty sure I'm way off the mark.
    I'm thinking it's something along these lines but I haven't been able to get anywhere with it.



    import javax.swing.*;
    import java.awt.*;
    import java.awt.geom.Ellipse2D;
     
    public class DrawShapes extends JFrame
    {
    	public void paint(Graphics g)
    	{
    		Graphics2D g2 = (Graphics2D)g;
        	super.paint(g2);
    		int w = getWidth();
    		int h = getHeight();
    		g.setColor(Color.GREEN);
     
    		Ellipse2D.Double myEllipse;
     
    		myEllipse = new Ellipse2D.Double(50, 50, 25, 25);
    		g2.fill(myEllipse);
     
    		for (; w<500; w++)
    		{
    			for (; h<500; h++)
    			{
    				myEllipse = new Ellipse2D.Double(50, 50, 25, 25);
    		        g2.fill(myEllipse);
    	}
    }
     
     
     
    	}
            public static void main(String[] args)
    	{
    		DrawShapes p = new DrawShapes();
    		p.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		p.setSize(500,500);
    		p.setVisible(true);
    	}
    }

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with Object Looping

    Do you know how many circles you want to draw? You should only need one for loop that loops once for each circle. You just need to update where you're drawing each circle each iteration.

    I'd suggest drawing out a few example cases by hand and figuring out where you want the circles to be drawn. Do you start noticing a pattern?

  7. #7
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Object Looping

    Well the pattern needs to be as follows:

    _o
    ___o
    _____o
    _______o
    _________o
    ___________o

    So basically as many circles as it takes to get from the top left of the frame to the bottom right. So I would need a loop that increments the x and y position of the circle by roughly its size each time. Am I headed in the right direction?

    Thanks for taking the time to help me by the way, it is much appreciated.

    EDIT: pattern got messed up, ignore the underscores
    Last edited by a swan ting; November 23rd, 2010 at 11:32 AM.

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with Object Looping

    That makes sense. I guess the question is: if you resize the window (make it bigger), do you want to draw the same number of circles, only bigger, or do you want to draw more circles the same size? And do you care about the case where the window isn't a perfect square? Should the circles always go from the top left corner to the bottom right corner of the window, or should they just be drawn in a line heading away from the top left corner without actually caring about where the bottom right corner is?

  9. #9
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Object Looping

    Here it would be a case of drawing enough circles to fill the frame regardless of its size. The assignment is a very simple in that all it wants is for me to understand how a loop can be used to draw an object multiple times. Things like resizing the frame and the number of circles would come into play later, for now I just need to understand how to apply a for loop to an object and have it redraw itself in a pattern.

    Once I get the basic code down I intend to play around with it and try different scenarios, but I just don't see it.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with Object Looping

    Well let's say I want to draw three circles in a row. All I'd have to do is update the x value as I draw them, right?
    Here's some pseudocode:
    int x = 20;
    for(int i = 0; i < 3; i++){
       g.drawCircle(x, 100);
       x += 100;
    }

    I could also do something like this:
    for(int i = 0; i < 3; i++){
       int x = 20 + i*100;
       g.drawCircle(x, 100);
    }

  11. #11
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with Object Looping

    Right just had to run some errands, back to work.

    I've made progress in the sense that I can see the loop works but I cannot get it to increment the ball each time it draws it. At this point I'm trying to have it draw itself 5 times but it seems to do it all on the same spot.

    How can I tell it to move each time it draws it? I tried to implement some of the code you suggested but it seems to just move the circle altogether.

    This is one example of how I tried to do it

    for (int i=0; i<5; i++)
    		{
                 Graphics2D g2 = (Graphics2D)g;
        	     super.paint(g2);
    		     int w = 50;
    		     int h = 50;
    		     g.setColor(Color.GREEN);
     
    		     Ellipse2D.Double myEllipse;
     
    		     myEllipse = new Ellipse2D.Double(w, h, 25, 25);
    		     g2.fill(myEllipse);
    		     w+=50;
    		     h+=50;
     
    		 }

    I tried different combinations but so far haven't cracked it, no matter what I do it draws one ellipse at a time

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with Object Looping

    You are initializing w and h each time, inside the loop. That means they'll start at the same value each iteration. Look at how I initialized the variable (x in my first example) outside the loop, so that it retains its incremented value each time.

  13. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with Object Looping

    And actually, you don't want to call super.paint each time through the loop. You only want to do that once at the very beginning of the method. And you should really be in a paintComponent method, not paint.

Similar Threads

  1. Looping through Tokens
    By Viking N7 in forum Loops & Control Statements
    Replies: 2
    Last Post: September 12th, 2010, 12:06 PM
  2. Looping object creation
    By Charlie in forum Java Theory & Questions
    Replies: 5
    Last Post: June 19th, 2010, 11:12 AM
  3. 2D Object makes my object smaller, Why?
    By MassiveResponse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2010, 02:33 PM
  4. Not Looping? (do - while) bad execution!
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 23rd, 2009, 08:51 PM
  5. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM