NekoLLX
NekoLLX

Reputation: 219

Dynamically change Background Image of a JPanel

Trying ti change the BG image of a jpanel but i cant call poaint on any normal method, it works great when i'm building the constructor but i don't want to rebuild the constructor.

....

Sort of found a solution by putting a label in my center frame and calling setIcon but i need to be able to pull the relelevant info so i need to find a way to store a value into my Jtoggle button (the id of the Race or class so i can fetch it's picture and change the icon)

thoughts? Everything compiles outside the iff statement which is my sticking point

RaceButtons_lft[i] =  new JToggleButton();
RaceButtons_lft[i].addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JToggleButton cb = (JToggleButton)ae.getSource();
for (int j=0; j<MyRaceArray.size(); j++)
{
if (MyRaceArray.get(j).getraceID() == combo_contents.getIndex()){//here is my sticking point, i need to find a way to match MyRaceArray's getRaceID to some value saved withthe Toggle button
final ImageIcon BGCSMs = ScaledImageIcon("Fantasy_Landscape_01.jpg", "Profile Pic", (468-(60*2)), 285);
picLabel.setIcon(BGCSMs);
}//if
}//for
}//action performed;
});//button add action listener

Upvotes: 0

Views: 3558

Answers (4)

NekoLLX
NekoLLX

Reputation: 219

so ultimately tried a few thing and just got lazy, added a label in the center and called "SetIcon, does what I need it to do, thanks for the ideas though.

Upvotes: 0

gavlaaaaaaaa
gavlaaaaaaaa

Reputation: 652

Call repaint() on the component after you have finished your changes to the background

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347184

You could be suffering from a number of problems, which we can't see because we don't have enough context...

  • You could have a reference issue, instead of trying to repaint the component on the screen, you've inadvertently gotten the wrong reference...
  • You could be shadowing your variables...
  • You could be painting to a opaque component...

Assuming that the code you have posted is linear (ie, it appears in you code in this exact order or close enough to it), I can see one possible problem...

ImageIcon RCicon = createImageIcon(temp_race.getActiveHeadshot(), temp_race.getRaceNameString(race.getraceID()));
Image RCimg = RCicon.getImage();
RCimg = RCimg.getScaledInstance((468-(60*2)), 285, java.awt.Image.SCALE_SMOOTH);

portraitCenterOptions.setBackground(Color.White){
    protected void paintComponent(Graphics h)
    {
        //...//
        // There is no way that this reference can be valid...
        // The image created above will only have a local reference unto itself
        // suggestion that you're shadowing your variables...
        final ImageIcon bodypicSM = new ImageIcon(RCimg);
        //...//
    }
};

But without a working example, it's impossible to know...

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.text.DateFormat;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ChangeBackground {

    public static void main(String[] args) {
        new ChangeBackground();
    }

    public ChangeBackground() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final PaintPane pane = new PaintPane();
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(pane);

                JButton change = new JButton("Change");
                change.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        pane.changeBackground();
                        pane.repaint();
                    }
                });

                frame.add(change, BorderLayout.SOUTH);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class PaintPane extends JPanel {

        private BufferedImage bg;
        private int changes = 0;

        public PaintPane() {
            changeBackground();
        }

        public void changeBackground() {

            bg = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = bg.createGraphics();
            FontMetrics fm = g.getFontMetrics();
            g.setColor(getForeground());
            String[] text = {
                "I've been changed " + changes + " times", 
                "Last changed at " + DateFormat.getDateTimeInstance().format(new Date())};
            int y = (200 - (fm.getHeight() * 2)) / 2;
            for (String value : text) {
                int x = (200 - fm.stringWidth(value)) / 2;
                g.drawString(value, x, y + fm.getAscent());
                y += fm.getHeight();
            }
            g.dispose();
            changes++;

        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g); 
            int x = (getWidth() - bg.getWidth()) / 2;
            int y = (getHeight() - bg.getHeight()) / 2;
            g.drawImage(bg, x, y, this);
        }

    }

}

Upvotes: 1

Jens Birger Hahn
Jens Birger Hahn

Reputation: 917

Calling

super.paintComponent(..)

will probably - depending on the super class - fill the component with the background color.

public void paintComponent(Graphics g) {
  // Let UI Delegate paint first, which 
  // includes background filling since 
  // this component is opaque.

  super.paintComponent(g);       
  g.drawString("This is my custom Panel!",10,20);
  redSquare.paintSquare(g);
}

(see A Closer Look at the Paint Mechanism). You don't need repaint(..) in this case.

Upvotes: 1

Related Questions