I'm trying to change the image being painted by sending the new image directory to my paint panel and then changing the Image variable to that image. I've tested the code to make sure that the directories are getting through, but the images never change. They just seem to ignore the new image and keep painting the previous one. I apologize for the messy code but I've been tweaking this for hours trying to get the image to change and the frustration is getting to me.

public class painting extends JPanel {
    private String BGDir;
    private String Dir;
    private int Width;
    private int Height;
    private Image image1;
    private Image image2;

    public painting(int h, int w, String BG, String Char) {
        BGDir = BG;
        Dir = Char;
        Height = h;
        Width = w;
        System.out.println(BGDir);
        System.out.println(Dir);
        try {
            image1 = (new ImageIcon(Char)).getImage();
            image2 = (new ImageIcon(BG)).getImage();
        } catch (Exception e) {
            System.out.println("no such file");
        }
        repaint();
    }

    public void update(Graphics g) {
        paintComponent(g);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        System.out.println(BGDir);
        System.out.println(Dir);
        // PAINTS THE BACKGROUND
        g2.drawImage(image2, 0, 0, getWidth(), (int) (getWidth() * .533), null);
        // PAINTS THE SCALED CHARACTER
        g2.drawImage(image1, (int) (Width / 6.22), (int) (Height * .246),
                (int) (Height * .754 * .2376), (int) (Height * .754), null);
        repaint();

    }
}
有帮助吗?

解决方案

I use BufferedImage, but this is a sample from working code I have.

public class PaintPanel extends JPanel {

    private static final long serialVersionUID = 1L;

    BufferedImage icon;

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(getBufferedIcon(), 12, 12, null);
    }

    public void setBufferedIcon(BufferedImage icon) {
        this.icon = icon;
    }

    private BufferedImage getBufferedIcon() {
        return icon;
    }
}

Ignore the offset of 12, that's just because I ripped this from a project of mine. All I do in another class is create the PaintPanel and set the BufferedIcon.

Every time I wish to update the icon, I call PaintPanel.setBufferedIcon(icon); and then PaintPanel.repaint();

Remember to name the PaintPanel, I'm just calling it that so you can read the code easier :)

其他提示

You can not change the images through the constructor parameters after this panel has been instantiated once. You can only create a new instance of this panel (using the new URLs) and afterwards replace the old instance with the new one. You should probably show the code where you create the first instance of this pane, AND the code that shows how you try to update the images.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top