绘图时 Java Swing NullPointerException
我正在使用自定义 JLayeredPane。 我有几个需要在 JLayeredPane 中的不同图层上绘制的形状。
为了测试这一点,我创建了一个 JPanel 并询问它的图形。然后我在 JPanel 上绘制一个测试矩形(准备图形),并在 JLayeredPane 的 PaintComponent 方法中最终绘制了所有内容。但这失败了(NullPointerException)。
public class MyCustomPanel extends JLayeredPane {
// test
JPanel testpane;
Graphics g2;
// test
// constructor
public MyCustomPanel() {
testpane = new JPanel();
this.add(testpane, new Integer(14));
g2 = testpane.getGraphics();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g2.drawRect(10, 10, 300, 300);
}
为什么
// run:
//Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
// at view.MyCustomPanel.paintComponent(MyCustomPanel.java:65)
我不能从 JLayeredPane 中绘制这样的 JPanel?我可以从 PaintComponent 方法中直接在 JLayeredPane 上绘图,但那是在 JLayeredPane 的默认面板上。我需要在 JLayeredPane 中添加的多个图层上创建和绘制。
我做错了什么? :s
I'm using a custom JLayeredPane.
I have several Shapes which needed to be drawn on different layers in the JLayeredPane.
To test this I create a JPanel and ask its graphics. Then I draw a test rectangle on that JPanel (preparing the graphics) and in my paintComponent method from the JLayeredPane I finally draw everything. But this fails (NullPointerException).
public class MyCustomPanel extends JLayeredPane {
// test
JPanel testpane;
Graphics g2;
// test
// constructor
public MyCustomPanel() {
testpane = new JPanel();
this.add(testpane, new Integer(14));
g2 = testpane.getGraphics();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g2.drawRect(10, 10, 300, 300);
}
}
// run:
//Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
// at view.MyCustomPanel.paintComponent(MyCustomPanel.java:65)
Why can't I draw on such a JPanel from within my JLayeredPane? I can draw directly on my JLayeredPane from within my paintComponent method but that's on the default Panel from the JLayeredPane. I need to create and draw on several layers which are added in my JLayeredPane.
What am I doing wrong? :s
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用
g2
转换传递给您的Graphics
:为什么不尝试解耦事物呢?
这更有意义。
也因为您正在尝试做一些与 Swing 行为不符的事情。
Swing 会自行对必须显示的内容调用适当的
paint
方法,并且要遵循此协议,您应该告诉Graphics
对象在 Swing 询问时要绘制什么到您的对象(调用paint
)方法,而不是当您想要这样做时。通过这种方式,每当 Swing 想要绘制您的
JLayeredPane
时,您只需在其他事物的Graphic
对象上绘制事物,而无需考虑 Swing 会在正确的时间调用它们适当的方法。这样做。结论:您无法在需要时在
Graphic
对象上绘制某些内容。您可以在 Swing 调用的方法中执行此操作,因为否则这些对象的Graphics
没有任何意义You should use
g2
casting theGraphics
that is passed to you:Why don't you try decoupling things?
this makes more sense..
Also because you are trying to do something that doesn't agree with Swing behaviour.
Swing will care by itself to call the appropriate
paint
methods over things that must be displayed, and to go with this protocol you should tellGraphics
objects what to draw when Swing asks it to your objects (calling thepaint
) method, not when you want to do it.In this way whenever Swing wants to draw your
JLayeredPane
you just draw things on aGraphic
object of other things without considering that Swing will call their appropriate methods when it's the right time to do so.In conclusion: you can't draw something on a
Graphic
object when you want it. You can do it just inside methods invoked by Swing, because otherwiseGraphics
of these objects doesn't mean anything变量 g2 可能为 null,因为您在构造函数中设置它,而不是在绘图时设置。相反,请使用传入的“g”。
您只能从当前正在绘制的组件中获取合法的 Graphics。否则,它是无效的。在您请求它时,未显示 MyCustomPanel(),也未显示 testpane。
The variable g2 is probably null because you set it in the constructor, not when drawing. Instead, use the "g" that was passed in.
You can only get a legitimate Graphics from a component that is currently being painted. Otherwise, it's not valid. At the point you're requesting it, the MyCustomPanel() isn't being displayed, and neither is testpane.