I have been using youtube to learn how to use the JPanel, as the prof has not given a lesson regarding that…

The project was a game, which I have finished, but for bonus I can do some sweet sweet graphics.

Anyhow the game saves the data in arraylists that chance as the game goes on…

I have figured out how to do most of what I want to do, but I need to a box for each item in the arraylist (which changes as u play the game, so I need the boxes to change in the panel.)

I set up a for loop (int I = 0: i<data1.size();i++) then setup an int x starting at 0 and after drawing a box, it adds 50 to x… and the x coordinate in the drawRect is set to the variable x…
however the x counter just sits at 0 so the boxes just stack…

So in short is there a way to draw some boxes in a line…. That as the arraylist length changes the boxes change as u play..?

Any help is greatly appreciated!

8 Years
Discussion Span
Last Post by Ezzaral

What you describe sounds like it should work just fine. Perhaps you have a bug in adding to your x position or you are re-declaring x to be zero each iteration? Hard to say without code.


Yeah, as you posted that I solved my problem...
Hit the nail on the head there...

However that was more so out of the frying pan and into the fire...
How to I insert this code into my game...
I set up a void method with a test arraylist...

public void paintComponent(Graphics g){

    …. Drawing code…..


If all I need to do is call this by sending the arraylist with the data.

How can I call it sending the arguments or am I going about this the wrong way?

Edited by Nick Evan: Fixed formatting


You don't call that method directly. That is called by the UI painting methods. If you call repaint() to update the screen, that method will automatically be called.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.