drawingPanel中的刷新图片扩展了JPanel

时间:2011-08-02 12:45:51

标签: swing drawing refresh jpanel

我必须在我软件的底部加载一个小图标。只是为了有一个加载/确定/错误图标。正如“http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter06/images.html”所述,我创建了一个扩展JPanel的dwowing面板。

class drawingPanel extends JPanel
{
    Image img;

    drawingPanel (Image img){
        this.img = img;
    }

    public void paintComponent (Graphics g) {
        super.paintComponent (g);

        // Use the image width & height to find the starting point
        int imgX = getSize ().width/2 - img.getWidth (this);
        int imgY = getSize ().height/2 - img.getHeight (this);

        //Draw image centered in the middle of the panel    
        g.drawImage (img, imgX, imgY, this);
    } // paintComponent

}

我按以下方式初始化组件:

// Grab the image.
Image img = new ImageIcon(iconPath+"ok.png").getImage();
// Create an instance of DrawingPanel
iconPanel = new drawingPanel(img);

一切正常,但在运行时我希望能够更改面板内的图标。我尝试了所有的佛;但是没有人设法查看新图片:

Image img = new ImageIcon(iconPath+"loading.gif").getImage();
// Create a new  instance of DrawingPanel
this.iconPanel = new drawingPanel(img);
this.iconPanel.repaint();
this.iconPanel.revalidate();
this.iconPanel.repaint();
this.repaint();
this.revalidate();

(我试过这个,因为我正在编写代码的类是包含IconPanel的JPanel的另一个扩展。任何关于我为什么不设法改变图片的想法?

谢谢, 斯特凡诺

1 个答案:

答案 0 :(得分:2)

首先,不要使用小名称启动类名。将drawingPanel重命名为DrawingPanel

我尝试根据您的描述制作一个简单的演示,但它运行正常。面板中的图像正在发生变化。

public class Demo {

    public Demo() {
        JFrame frame = new JFrame();
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new BorderLayout());

        // Grab the image.
        Image img = new ImageIcon("1.png").getImage();
        // Create an instance of DrawingPanel
        final DrawingPanel iconPanel = new DrawingPanel(img);

        frame.add(iconPanel, BorderLayout.CENTER);

        JButton button = new JButton("Change image..");
        frame.add(button, BorderLayout.NORTH);

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                iconPanel.setImg(new ImageIcon("2.png").getImage());
                iconPanel.repaint();
            }
        });

        frame.setVisible(true);
    }

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

class DrawingPanel extends JPanel {
    Image img;

    DrawingPanel(Image img) {
        this.img = img;
    }

    public void setImg(Image img) {
        this.img = img;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // Use the image width & height to find the starting point
        int imgX = getSize().width / 2 - img.getWidth(this);
        int imgY = getSize().height / 2 - img.getHeight(this);

        // Draw image centered in the middle of the panel
        g.drawImage(img, imgX, imgY, this);
    } // paintComponent

}

我所做的更改是在img类中添加DrawingPanel的setter方法。因此,您只需使用新的Image调用setImg(),然后调用reapint绘制新图像,而不是创建新的DrawingPanel