Hey,
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!
:)

Recommended Answers

All 3 Replies

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){
        super.paintComponent(g);
        this.setBackground(Color.WHITE);

    …. 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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.