DrawingPanel中刷新图片扩展了JPanel

发布于 2024-11-27 12:04:57 字数 1341 浏览 1 评论 0 原文

我必须在软件底部加载一个小图标。只是为了有一个加载/确定/错误图标。正如“http://www.article.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 的另一个扩展。任何关于为什么我无法更改图片?

谢谢, 斯特凡诺

I have to load a small icon on the botton of my software. just to have a loading/ok/error icon. as sudgested on "http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter06/images.html" i create a dwowing panel extending JPanel.

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

}

I initialize the component in the following way:

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

all works well but at runtime i want to be able to change the icon within the pannel. i tryed all the fo;llowing but none managed to view the new picture:

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();

(i tryed this because the class in whic i am writing the code is another extension of JPanel that contains IconPanel. Any idea about why i do not manage to change the picture?

Thanks,
Stefano

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生来就爱笑 2024-12-04 12:04:57

首先不要以小名称开头。将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

}

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

First thing don't start class name with small name. Rename drawingPanel to DrawingPanel.

I have tried making a simple demo based on your description and it works fine. The image in panel is changing perfectly.

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

}

The changes I have made is add a setter method of img in DrawingPanel class. So instead of creating new DrawingPanel you just have to call setImg() with new Image and then call reapint to paint the new image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文