I’m still a little new to Java, and I’m quite confused with how objects are supposed to be used. I know of
object object = new object();
but these have to be accessed manually.
say I wanted to create a particle effect. small white dots appear in the centere of the screen, and slowly move to an edge of the screen at a random angle. as they approach, they go faster. finally, they are removed if they go out of the view area.
if I wanted to do this with objects, I would need to be able to:
- automatically make objects
- collect data from every object (their X and Y co-ordinates)
so that they can be rendered - have the objects update every frame so that they can increase speed
- destroy the objects when they go out of bounds.
How would I automatically make/destroy objects, and take in data from all of them independent of how many objects are present?
Right now, my games (well, I’ve only ever made 1 in Java) are limited to using an array and a sort of cellular automata, but this won’t work for bigger games.
This might have been asked before or there might be a tutorial on this, but all I managed to find was manually making objects. I’m probably searching for the wrong thing, so sorry if this is the case.
Creating Objects
I’m not sure what you mean by “automatically create objects”, but this is what constructors are for. When you define a constructor, you can add all sorts of implementation and initialization details in there that will create your variable exactly how you want it to be created. See “Providing Constructors for your classes”. So say you have a point…
Collecting Data from Objects
Getting and setting information in these classes would be achieved through getters and setters (also known as accessors and mutators).
To collect the data from all objects in existence, independently of how many there are, simply have them all added to a
Listof some kind. You could even add them to aprivate static ArrayList<T>in your constructor and access theArrayList<T>statically for your information without another part of your program ever having to “know” how many there are.Incremental GUI Changes
There are a couple ways to implement your frame-by-frame changes, but I prefer using a
Timerand anActionListener. This allows you to set your frame rate and, in thepublic void actionPerformed(ActionEvent)method to perform the actions you require. This is somewhat complicated for a beginner, so you will likely have to do a good bit of reading and testing. See “How to Write an Action Listener” and “How to Use Swing Timers” to get you started.Destroying Objects
There are no destructors in Java as there are in languages like C++. Instead, there is a garbage collector which runs periodically (and sporadically) which destroys all objects it deems no longer have a reference pointing to them. Thus, if you want the garbage collector to pick up your objects when they go out of bounds, simply assign all references to them to null. While this won’t destroy the object immediately, it ensures that they will be “tagged” by the garbage collector on its next run through.
As mentioned,
System.gc()is a method to “invoke” the garbage collector. However, you should not assume that calling it will force the garbage collector to run. In the API, it says that this call “suggests” to the System that it should run the garbage collector. Ultimately, it is not reliable and garbage collection is generally outside of your scope of control.From the API:
Also, as a side nit pick, be sure to capitalize the class name when you are creating objects. 🙂
For OP’s specific purposes: If you are concerned about a game environment where you are “drawing everything that exists,” (and so you want to destroy the objects immediately to ensure that the game runs correctly), consider using a
ListorSetand populating that with the things you want to draw, then removing them as they go off the screen.References: