Вопрос

How Can I repaint a JPanel that is inside another JPanel? I have tried some answers that I found in the internet but I couldn't make it work... There is a piece of my code:

My Main Panel:

    contentPane = new JPanel() {
        protected void paintComponent(java.awt.Graphics g) {
            super.paintComponents(g);
            try {
                g.drawImage(ImageIO.read(JanelaJogo.class
                        .getResource("/imagens/fundo/fundo4.jpg")), 0, 0,
                        this);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    };
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

This is the panel Inside:

bonusPanel = new JPanel() {
        @Override
        public void paintComponents(Graphics g) {
            // TODO Auto-generated method stub
             super.paintComponent(g);                

        }
        @Override
        public Dimension getPreferredSize()
        {
            Dimension dim = contentPane.getSize();
            int largura = dim.width;
            dim = new Dimension(largura, 150);
           return new Dimension(dim);
        }

    };
bonusPanel.setBackground(new Color(0, 0, 0, 0));
contentPane.add(bonusPanel, BorderLayout.SOUTH);

My bonusPanel have 2 labels that are image icons, and I want that image refresh when some user do a combination in the game, the problem is that it isn't refreshing... I have tried repaint(), revalidade(), validade() in contentPane and in bonusPanel but none work... Thank you in advance for the help!

Update: There is an image and I will explain better what I want... enter image description here

I put in java console, the outputs of the program, so, When there is a match of "bombs", the power (the bomb on the button) should change the image... and im printing to console the current image, as you can see, it changed to image "bonus_bomba_1.png" and "bonus_bomba_2.png", but the problem is refreshing the GUI... I updated the code that I posted before for my current one... I just didn't change the drawImage because I didn't figure out what should I do yet and since it's working, I didn't changed it yet...

This is what I'm doing in the "power" for update the image of the label:

private void iconBomb(String s) {

    try {
        bombPic = ImageIO.read(this.getClass().getResource(s));
        powerBomb.setIcon(new ImageIcon(bombPic));
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

Thank you in advance for the help!

Это было полезно?

Решение

1) Don't invoke super.paintChildren(). Swing will do that for you. All you do is invoke super.paintComponent();

2) Don't read a File in the painting method. The painting methods are for painting only.

3) You are adding the bonus panel to the SOUTH of the frame, so the panel is displayed at its preferred size. You need to override the getPreferredSize() method of your panel to return an reasonable size:

@Override
public Dimension getPreferredSize()
{
    return new Dimension(...);
}

Read the section from the Swing tutorial on Custom Painting for more information and working examples.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top