如何将组件(如 JPanel)绘制到我选择的图形中,就好像它是屏幕一样?
在谷歌上没有运气。
我试过的:
JPanel p = new JPanel();
p.setSize(32, 32);
p.setLocation(8, 8);
p.add(new JLabel(...));
p.add(new JLabel(...));
p.add(new ImageIcon(...));
Graphics g = getSomeGraphics();
p.paint(g);
然而,这最后一条指令导致
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.RepaintManager.getVolatileOffscreenBuffer(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
...