Drawing multiple instances of one class with an array

I have been tryng to draw two objects from the same class on a JFrame for about 5 hours now:confused:, but the last object created overrides the others.

For example I want to paint two objects from the class ball on one JFrame. I need to do this in an array, I have tried this with arrayLists and normal arrays from example codes but nothing seems to work. could anyone write me a simple piece of code or give me some tips please?

I have been tryng to draw two objects from the same class on a JFrame for about 5 hours now:confused:, but the last object created overrides the others.

It's hard to know just what you're doing wrong based on description without code.

For example I want to paint two objects from the class ball on one JFrame. I need to do this in an array, I have tried this with arrayLists and normal arrays from example codes but nothing seems to work. could anyone write me a simple piece of code or give me some tips please?

There are many examples of doing this sort of thing on the forum, but before anyone invests a bunch of time re-creating code that's already out there, why not better define just what is tripping you up. I suggest that you show us a small program that's compilable, that tries to do what you are attempting here, but in a very small program that doesn't do anything else, and this way we can know exactly what the current problems are and what you need work on.

Suerte amigo.

edit: oh, if you do post code, please don't forget to use code tags. They'll allow your code to maintain its format and thus (hopefully) be readable. The more readable your code, the greater the chance someone here will take the time to read it. Just place the tags:

The code I gave above works, the only thing is he only displays r2 and i want to display r1 and r2 at the same time using an array or arraylist. I allready created an empty arraylist and tried tons of things to get it working. nothing works though. Could anyone help me on this?

1) Create an ArrayList<Rect> in your JComponent, give it an addRect(Rect rect) method that will allow you to be able to add Rect objects to the JComponent, and in this method place the Rect into the list.

2) In your animation routine (which should be a Swing Timer, by the way), iterate through the list, changing the x and y position of the Rect objects in the list.

3) Then call repaint() on the JComponent when done.

4) In the paintComponent method, iterate through the list, painting the individual Rect objects in the loop.

Also, you should understand why static is messing you up. When you deal with static variables, you need to know that the variable is associated with the class not with an object of the class. For instance, your Rect class has all static variables, and so since there is only one Rect class, no matter how many Rect objects you create, there will only be one copy of those static class fields, and so you can only draw one Rect image.

Your code adds a constant value (5) to y, but keeps adding this value regardless of where the rectangle is on the screen, so that the rectangle is allowed to continue happily off the screen and on to oblivion.

My code allows the rectangles to bounce. The first if block checks to see if the rectangle's y location plus the height is equal to or greater than the height of the JComponent that is displaying them. If so, it changes the value of the number added to the rectangle's y field to be a negative number so that the rectangle will move up (will have a value subtracted from its y field). Conversely, if the rectangle is at or less than 0, the top border of the screen, the deltaY, the number added to y, will be made positive so that the rectangle will move down.

2. I'd really like to use a thread and not a timer, is that doable?

Yes, it's doable, but it's easier to use Timers since you know that all code contained will be called on the Swing main thread, the EDT.

3. Is there an easier way to move the rects seppartly?

Yes, but you'll need to make your movement code more complex. Alternatively, you could give the rectangles some intelligence and let each one handle its own movement.