绘图时 Java Swing NullPointerException

发布于 2024-08-28 04:36:46 字数 943 浏览 11 评论 0原文

我正在使用自定义 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 技术交流群。

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

发布评论

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

评论(2

一场信仰旅途 2024-09-04 04:36:46

您应该使用 g2 转换传递给您的 Graphics

Graphics2D g2 = (Graphics2D)g;

为什么不尝试解耦事物呢?

class InnerPanel extends JPanel
{
  public void paint(Graphics g)
  {
     Graphics2D g2 = (Graphics2D)g;
     g2.drawRect(....);
  }
}

class MyLayered extends JLayeredPane()
{
  MyLayered()
  {
    this.add(new InnerPanel(), 14);
  }
}

这更有意义。

也因为您正在尝试做一些与 Swing 行为不符的事情。
Swing 会自行对必须显示的内容调用适当的 paint 方法,并且要遵循此协议,您应该告诉 Graphics 对象在 Swing 询问时要绘制什么到您的对象(调用paint)方法,而不是当您想要这样做时。

通过这种方式,每当 Swing 想要绘制您的 JLayeredPane 时,您只需在其他事物的 Graphic 对象上绘制事物,而无需考虑 Swing 会在正确的时间调用它们适当的方法。这样做。

结论:您无法在需要时在 Graphic 对象上绘制某些内容。您可以在 Swing 调用的方法中执行此操作,因为否则这些对象的Graphics 没有任何意义

You should use g2 casting the Graphics that is passed to you:

Graphics2D g2 = (Graphics2D)g;

Why don't you try decoupling things?

class InnerPanel extends JPanel
{
  public void paint(Graphics g)
  {
     Graphics2D g2 = (Graphics2D)g;
     g2.drawRect(....);
  }
}

class MyLayered extends JLayeredPane()
{
  MyLayered()
  {
    this.add(new InnerPanel(), 14);
  }
}

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 tell Graphics objects what to draw when Swing asks it to your objects (calling the paint) method, not when you want to do it.

In this way whenever Swing wants to draw your JLayeredPane you just draw things on a Graphic 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 otherwise Graphics of these objects doesn't mean anything

娇妻 2024-09-04 04:36:46

变量 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.

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