共享绘制在比屏幕本身更大的图形上的图像

Share image painted on a larger graphics than the screen itself

下面的代码将在图像图形上绘制组件 - 但它会平移它 - 而不是使其适应图形的大小。

int ratio = 2;
Image screenshotImage = Image.createImage(getWidth()* ratio,getHeight()* ratio);
c.revalidate();
c.setVisible(true);
c.paintComponent(screenshotImage.getGraphics(), true);

我不能使用图像只能缩放它,因为一些内容会被截断。 组件能否在指定尺寸的图像图形上绘制自己

非常感谢

使用:

c.setX(0);
c.setY(0);
c.setWidth(img.getWidth());
c.setHeight(img.getHeight());
if(c instanceof Container) {
   c.setShouldLayout(true);
   c.layoutContainer();
}
c.remove();
c.paintComponent(myImage.getGraphics(), true);
// add c back to it's parent container and revalidate

如果该组件当前在一个表单上,您可能希望随后通过在父表单上调用 revalidate 来撤消所有这些操作。