gpt4 book ai didi

java - 在 JFrame 上绘画有效,但无论如何它都会抛出 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 08:28:06 27 4
gpt4 key购买 nike

窗口出现并按预期工作,但出于某种原因它仍然给我一个 NullPointerException。事实上两个。这是代码:

public class SwagFrame extends JFrame {

public SwagFrame() {super("Swag");}

public void paint(Graphics g) {
Image bg = null;

try {
bg = ImageIO.read(new File("swag.png"));
} catch (IOException e) {
System.out.println("Swag not turned on");
System.exit(-1);
}

g.drawImage(bg, 0, 0, null); // exception here
g.setColor(Color.GREEN);
g.fillOval(250, 250, 100, 100);
}

public static void main(String[] args) {
SwagFrame frame = new SwagFrame();

frame.setVisible(true);
frame.setSize(525, 525);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);

frame.paint(null); // exception here
}
}

如果 paint() 在其参数中需要一个对象,为什么它仍然可以工作但在事后抛出异常?

最佳答案

我做了两件事。我将 SwagFrame 设为 JPanel 而不是 JFrame,因此我可以使用 paintComponent 方法,我删除了 frame.paint(null) 并更改了 null drawImagethis。该代码现在可以正常工作。

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

public class SwagFrame extends JPanel {
public SwagFrame() {

}

public void paintComponent(Graphics g) {
super.paintComponent(g);
BufferedImage bg = null;

try {
bg = ImageIO.read(new File("icons/stacko/stackoverflow1.png"));
} catch (IOException e) {
System.out.println("Swag not turned on");
//System.exit(-1);
}


g.drawImage(bg, 0, 0, this); // exception here
g.setColor(Color.GREEN);
g.fillOval(250, 250, 100, 100);
}

public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new SwagFrame());
frame.setSize(525, 525);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

//frame.paint(g); // exception here
}
}

此外,如果你想设置图像的大小,你可以将它作为参数传递给 .drawImage() 方法

g.drawImage(bg, 0, 0, width, height, this);

还有一件事,确保图像位于正确的文件位置。

正如@MadProgrammer 在他的评论中所说:

" Generally, top level containers aren't double buffered, where as Swing components (extending from JComponent) are. Top level containers have a number of layers placed ontop of them (layered pane, content pane and glass pane), making it very complex surface to safely paint to. Also, generally, when a child component is painted, the parent container doesn't need to be this can lead to dirty paints occurring. Also, generally, extending from JFrame is discouraged as you are never actually adding any additional functionality to it

The reason the OPs code didn't work (as you seem to have duduced) was they were passing a null value to the paint method, when means when they tried to access "g", it threw a NPE, however, the the Repaint Manager schedule it's paint request, the paint method was passed a valid Graphics refernce, meaning it was able to work properly...also, the OP isn't call super.paint, bad on them" - @MadProgrammer

关于java - 在 JFrame 上绘画有效,但无论如何它都会抛出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20390218/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com