Java swing 更改 JPanel 图像
我在网上环顾四周,发现如何将图像添加到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来对面板的静态引用永远不会被设置:
如果没有看到所有实际代码,很难确定,但看起来 netbeans 正在这里创建对不同面板的引用:
尝试:
It looks like the static reference to your panel is never getting set:
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:
try:
只需定义此 JPanel 变量:
lp2_2
然后按以下方式将图像设置到此 JPanel:
Just define this JPanel variable:
lp2_2
And then set the image to this JPanel in the following manner: