3

如何将组件(如 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)
    ...
4

2 回答 2

2

示例说明了panel.paint(g2d)如何在. 然后将图像缩放并显示在面板下方以供参考。JPanelJLabelBufferedImage

图片

于 2013-02-22T19:47:59.247 回答
1

你可以假设我正在绘制一个 BufferedImage,事实上我有时会这样做。

请参阅Screen Image,它也可以创建组件图像。

于 2013-02-23T00:44:04.680 回答