• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generate endless Circle

 
Giada Sebben
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Is there an easy way to generate endless Circles from the top (y-axis) on different coordinate without a overlay between the Circles?

Greets
 
Piet Souris
Rancher
Posts: 1295
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Nina,

I don't understand the question. Can you elaborate somewhat?

Greetz,
Piet
 
Giada Sebben
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

As you see on my image it should generate random circles from above during the program is running. My question is how can I realize this without
the circles are colliding?

Greets
Nina
endless-fallen-circles.png
[Thumbnail for endless-fallen-circles.png]
catcher.jpg
[Thumbnail for catcher.jpg]
 
Piet Souris
Rancher
Posts: 1295
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see.

Well, that is not so very simple. But then again, it is not that difficult too.
The following is only one way of doing, so if you see other ways, that's
fine as well.

For a start, I would make a dedicated subclass of Rectangle.
Apart from storing the coordinates, you could add an 'update' method,
that updates the coordinates, at regular intervals.
It should contain a method 'draw(Graphics g)' that draws a circle within
this rectangle.

Then a class that manages all your rectangles. It should have a List
of Rectangles (ArrayList for instance). At regular intervals it would
have all its rectangles update their coordinates. It also should be able
to create new rectangles. Such a new rectangle would have
coordinates that put that rectangle completely above the panel
in which all the circles are drawn.
For the non overlapping part, a Rectangle has some nice methods
for that. See the Rectangle API.
It can also remove rectangles that were caught or otherwise dropped
below the panel.

All this woud be driven by a Timer, in the actionPerformed method
you would call the container to perform the necessary actions.
It wold then call 'pnel.repaint()' and in your panel.paintComponent
you can let your container draw all the relevant rectangles.

Instead of rectangles in which you draw your circles, you could
of course use icons, or BufferedImages, like in the second image
of your reply.

Well, easier said then done. So, enjoy!

Greetz,
Piet
 
Giada Sebben
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Ok. And how would you generate those object on different coordinates so they don't overlay?

Greets
Nina
 
Piet Souris
Rancher
Posts: 1295
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming the coordinates of your panel are say (0,0) - (width, height) (top left to bottom right),
and say the siide of a rectangle is R, then some initial y coordinate would be
-R - some random number. You know then that the rectangle will be completely above
the panel. While the coordinates get updated, it will appear sooner or later in your panel.

Now, I would also have some variable that indicates whether the rectangle should appear
somewhat on the left or somewhat on the right, sort of semi random x coordinate.

Then you would have to check if the rectangle is not overlapping another rectangle.
If you look at the API of the Rectangle class, you will find a method
'boolean intersects(Rectangle r)'. Now, you have a complete list of the exisitng
rectangles, so check these. And should you find an overlapping rectangle,
then you must adjust the coordinates of the new rectangle somewhat. How?
Well, if that part gives you problems, then let us know. But try it first.

Greetz,
Piet
 
Giada Sebben
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I did it. But I still don't know how to create automatically new Recantgles during the runtime of the program...

Greets
Nina
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you do anything over and over and over in a program?
 
Piet Souris
Rancher
Posts: 1295
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed.

Well, in my earlier reply I mentioned that I would use a Timer to drive the animation.
The ActionListener involved would then simply call the rectangles container to do all
necessary updating, including generating new rectangles. How much rectangles, and
whether that should be done every time that Timer fires, well, that involves creating
some strategy for this.

Now, I am assuming quite a lot about how it could be done, but I would like to know
what you have achieved so far. Can you show us some of your code?

Greetz,
Piet
 
Giada Sebben
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:Indeed.

Well, in my earlier reply I mentioned that I would use a Timer to drive the animation.
The ActionListener involved would then simply call the rectangles container to do all
necessary updating, including generating new rectangles. How much rectangles, and
whether that should be done every time that Timer fires, well, that involves creating
some strategy for this.

Now, I am assuming quite a lot about how it could be done, but I would like to know
what you have achieved so far. Can you show us some of your code?

Greetz,
Piet


Hello

It's done =) I really thank you and the other for your hints.

Greets
Nina
 
Piet Souris
Rancher
Posts: 1295
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Nina,

you're welcome, and well done!

Greetz,
Piet
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic