Frank Palmasani
Frank Palmasani

Reputation: 185

paintComponent() is being invoked 200 times

I am having an odd issue when using paintComponent() and repaint(). As you see below, I have a paintComponent() class as an inner class as the main JPanel of my GUI.

    // add another panel to centerInner
    tableBottom = new JPanel() {
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (!paintImages.isEmpty()) {
                for (PaintImages temp : paintImages) {
                    g.drawImage(temp.getImage(), temp.getX(), temp.getY(), this);
                }
            }
            if (!extraCards.isEmpty()) {
                for (PaintImages temp1 : extraCards) {
                    g.drawImage(temp1.getImage(), temp1.getX(), temp1.getY(), this);
                }
            }
        }
    };

This is a black jack game with 4 players a dealer.

repaint() is called by 4 functions:

The constructor for the initial draw. An update method that creates an ArrayList of objects to print for the initial deal. An another update method that creates an ArrayList for each card drawn. And the reset which clears all ArrayLists and repaints the new initial deal.

I won't go into the backend, but every one of those four methods only run the desired number of times. just once for every time its called.

My problem is that when paintComponent is invoked by repaint(), paintComponent() runs almost 200 times, not including the for loops that run around 10 times a piece on average per game.

My question is:

1) Is this common behavior for a paintComponent method? Does paintComponent call itself repeatedly over and over again until all painting necessary has been completed?

OR

2) Does this have to do with the JPanel tableBottom? at this point nothing is actually being added to the JPanel because it is top most JPanel. But maybe paintComponent is ran repeatedly for every JPanel, JFrame, ContentPane, Label, etc.,

OR

3) Did I do something wrong in my code below? Again through testing using increments and print statements I found the update methods are called the appropriate amount of times and doing their jobs correctly.

Thanks for any help.

Upvotes: 2

Views: 253

Answers (1)

camickr
camickr

Reputation: 324137

being a inner class I call repaint like tableBottom.paintComponent()

Never invoke the paintComponent() method directly. To repaint the panel you do:

tableBottom.repaint();

The request will be passed to the RepaintManager which will then combine repaint() requests for all components and then paint the components as necessary. This will make painting more efficient.

g.drawImage(temp1.getImage(), temp1.getX(), temp1.getY(), this);

The "this" means that images can be repainted as they are being read. That is sometimes the painting method is invoked before the image I/O has completed. So in this case when the I/O is finished another paint request will be made so the image is painted completely. If you are reading the images at the start of you class and storing them in some data structure then you can probably just use "null" instead of "this".

Did I do something wrong in my code below?

The code provide looks reasonable, but we can't see the context of how/when you invoke the painting code. I already mentioned one problem.

Post a proper SSCCE that demonstrates the problem.

Upvotes: 3

Related Questions