Re: Help with Object Looping
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.
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.
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.
Code :
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);
}
}
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?
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
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?
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.
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:
Code :
int x = 20;
for(int i = 0; i < 3; i++){
g.drawCircle(x, 100);
x += 100;
}
I could also do something like this:
Code :
for(int i = 0; i < 3; i++){
int x = 20 + i*100;
g.drawCircle(x, 100);
}
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
Code :
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 :confused:
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.
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.