Java swing 更改 JPanel 图像

发布于 2024-11-08 01:43:59 字数 5282 浏览 0 评论 0原文

我在网上环顾四周,发现如何将图像添加到 JPanel

这工作正常(当在 ImagePanel 构造函数中加载图像时),但我希望能够动态更改图像(例如:按下按钮时)。我尝试实现这一点,但当我单击按钮时,继续在线程“AWT-EventQueue-0”java.lang.NullPointerException 中收到异常。

有人可以帮忙吗?我是 Java 新手。谢谢!

这是我的 JPanel 扩展类:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class ImagePanel extends JPanel{

    private BufferedImage image;

    public ImagePanel() {
        this.loadImage("/home/raph/Images/shortcurt_unity.png");
    }

    public void loadImage(String filename) {
         try {
          this.image = ImageIO.read(new File(filename));
          this.repaint();
       } catch (IOException ex) {
            // handle exception...
       }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
            if (image != null) {
            // Scale it by width
            int scaledWidth = (int)((image.getWidth() * getHeight()/image.getHeight()));
            // If the image is not off the screen horizontally...
            if (scaledWidth < getWidth()) {
                // Center the left and right destination x coordinates.
                int leftOffset = getWidth() / 2 - scaledWidth / 2;
                int rightOffset = getWidth() / 2 + scaledWidth / 2;
                g.drawImage(image, leftOffset, 0, rightOffset, getHeight(), 0, 0, image.getWidth(), image.getHeight(), null);
            }
            // Otherwise, the image width is too much, even scaled
            // So we need to center it the other direction
            else {
                int scaledHeight = (image.getHeight() * getWidth()) / image.getWidth();
                int topOffset = getHeight() / 2 - scaledHeight / 2;
                int bottomOffset = getHeight() / 2 + scaledHeight / 2;
                g.drawImage(image, 0, topOffset, getWidth(), bottomOffset, 0, 0, image.getWidth(), image.getHeight(), null);
            }
        }
    }
}

我在 JFrame 中初始化它:

private static ImagePanel jPanel1;

Netbeans 自动生成了以下内容(jpanel 相关代码的摘录)

javax.swing.JPanel jPanel1 = new ImagePanel();

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 1260, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 124, Short.MAX_VALUE)
    );

:按钮:

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
    jPanel1.loadImage("/home/raph/Images/shortcurt_unity.png");
}

请注意,在调试时,我无法进入 loadImage,我在到达它之前收到异常:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at musicchecker.MusicCheckerGUI.jButton10ActionPerformed(MusicCheckerGUI.java:276)
    at musicchecker.MusicCheckerGUI.access$400(MusicCheckerGUI.java:26)
    at musicchecker.MusicCheckerGUI$5.actionPerformed(MusicCheckerGUI.java:124)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6267)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6032)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4630)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

I have looked around on the net and found out how to add an image to a JPanel.

This works fine (when image is loaded in ImagePanel constructor), but I'd like to be able to change the image dynamically (ex: on a button key press). I have tried to implement this, but keep on getting an Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException when I click on the button.

Can anyone help ? I am new to Java. Thanks!

Here is my JPanel extended class:

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class ImagePanel extends JPanel{

    private BufferedImage image;

    public ImagePanel() {
        this.loadImage("/home/raph/Images/shortcurt_unity.png");
    }

    public void loadImage(String filename) {
         try {
          this.image = ImageIO.read(new File(filename));
          this.repaint();
       } catch (IOException ex) {
            // handle exception...
       }
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
            if (image != null) {
            // Scale it by width
            int scaledWidth = (int)((image.getWidth() * getHeight()/image.getHeight()));
            // If the image is not off the screen horizontally...
            if (scaledWidth < getWidth()) {
                // Center the left and right destination x coordinates.
                int leftOffset = getWidth() / 2 - scaledWidth / 2;
                int rightOffset = getWidth() / 2 + scaledWidth / 2;
                g.drawImage(image, leftOffset, 0, rightOffset, getHeight(), 0, 0, image.getWidth(), image.getHeight(), null);
            }
            // Otherwise, the image width is too much, even scaled
            // So we need to center it the other direction
            else {
                int scaledHeight = (image.getHeight() * getWidth()) / image.getWidth();
                int topOffset = getHeight() / 2 - scaledHeight / 2;
                int bottomOffset = getHeight() / 2 + scaledHeight / 2;
                g.drawImage(image, 0, topOffset, getWidth(), bottomOffset, 0, 0, image.getWidth(), image.getHeight(), null);
            }
        }
    }
}

I init it in my JFrame:

private static ImagePanel jPanel1;

And Netbeans auto-generated the following (extract of jpanel related code):

javax.swing.JPanel jPanel1 = new ImagePanel();

javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 1260, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 124, Short.MAX_VALUE)
    );

The call I am making from a button:

private void jButton10ActionPerformed(java.awt.event.ActionEvent evt) {
    jPanel1.loadImage("/home/raph/Images/shortcurt_unity.png");
}

Note that when in debug, I cannot step into loadImage, I get the exception before reaching it:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at musicchecker.MusicCheckerGUI.jButton10ActionPerformed(MusicCheckerGUI.java:276)
    at musicchecker.MusicCheckerGUI.access$400(MusicCheckerGUI.java:26)
    at musicchecker.MusicCheckerGUI$5.actionPerformed(MusicCheckerGUI.java:124)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6267)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6032)
    at java.awt.Container.processEvent(Container.java:2041)
    at java.awt.Component.dispatchEventImpl(Component.java:4630)
    at java.awt.Container.dispatchEventImpl(Container.java:2099)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4577)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
    at java.awt.Container.dispatchEventImpl(Container.java:2085)
    at java.awt.Window.dispatchEventImpl(Window.java:2478)
    at java.awt.Component.dispatchEvent(Component.java:4460)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

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

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

发布评论

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

评论(2

抠脚大汉 2024-11-15 01:43:59

看起来对面板的静态引用永远不会被设置:

private static ImagePanel jPanel1;

如果没有看到所有实际代码,很难确定,但看起来 netbeans 正在这里创建对不同面板的引用:

javax.swing.JPanel jPanel1 = new ImagePanel();

尝试:

jPanel1 = new ImagePanel();

It looks like the static reference to your panel is never getting set:

private static ImagePanel jPanel1;

It's hard to know for sure without seeing all the actual code but it looks like netbeans is creating a reference to a different panel here:

javax.swing.JPanel jPanel1 = new ImagePanel();

try:

jPanel1 = new ImagePanel();
南风起 2024-11-15 01:43:59

只需定义此 JPanel 变量:lp2_2

然后按以下方式将图像设置到此 JPanel:

lp2_2.setIcon(new ImageIcon((new ImageIcon("tank.jpg")).getImage().getScaledInstance( 600, 600,  java.awt.Image.SCALE_SMOOTH )));   

Just define this JPanel variable: lp2_2

And then set the image to this JPanel in the following manner:

lp2_2.setIcon(new ImageIcon((new ImageIcon("tank.jpg")).getImage().getScaledInstance( 600, 600,  java.awt.Image.SCALE_SMOOTH )));   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文