aspose file tools*
The moose likes Game Development and the fly likes Generate endless Circle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "Generate endless Circle" Watch "Generate endless Circle" New topic
Author

Generate endless Circle

Giada Sebben
Greenhorn

Joined: Jul 17, 2014
Posts: 10
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
Ranch Hand

Joined: Mar 08, 2009
Posts: 547
    
  11
hi Nina,

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

Greetz,
Piet
Giada Sebben
Greenhorn

Joined: Jul 17, 2014
Posts: 10
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]

[catcher.jpg]

Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 547
    
  11
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

Joined: Jul 17, 2014
Posts: 10
Hello

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

Greets
Nina
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 547
    
  11
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

Joined: Jul 17, 2014
Posts: 10
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

Joined: Oct 02, 2003
Posts: 11313
    
  16

How do you do anything over and over and over in a program?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Piet Souris
Ranch Hand

Joined: Mar 08, 2009
Posts: 547
    
  11
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

Joined: Jul 17, 2014
Posts: 10
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
Ranch Hand

Joined: Mar 08, 2009
Posts: 547
    
  11
hi Nina,

you're welcome, and well done!

Greetz,
Piet
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generate endless Circle