有没有办法在不截图的情况下保存代号一中的图形object?

Is there a way to save a Graphics object in Codename One without without taking a screenshot?

问题在标题里。例如,如何将 g 保存在以下代码片段中的文件中?

public void paints(Graphics g, Image background, Image watermark, int width, int height) {

g.drawImage(background, 0, 0);
g.drawImage(watermark, 0, 0);
g.setColor(0xFF0000);

// Upper left corner
g.fillRect(0, 0, 10, 10);

// Lower right corner
g.setColor(0x00FF00);
g.fillRect(width - 10, height - 10, 10, 10);

g.setColor(0xFF0000);
Font f = Font.createTrueTypeFont("Geometos", "Geometos.ttf").derive(220, Font.STYLE_BOLD);
g.setFont(f);
// Draw a string right below the M from Mercedes on the car windscreen (measured in Gimp)
g.drawString("HelloWorld", 
        (int) (848 ),
        (int) (610)
        );

// NOW how can I save g in a file ?

}

我不想截图的原因是因为我想保持 g 的完整分辨率(例如:2000 x 1500)。

如果有人能告诉我如何用代号一做到这一点,我将不胜感激。如果不可能,那么知道它已经很好了!

干杯,

图形只是表面的代理,它不知道或无法访问它正在绘制的底层表面,原因很简单。它可以绘制到硬件加速 "surface",其中物理上没有底层图像。

iOS 和 Android 都是这种情况,其中 "screen" 是本地绘制的并且没有缓冲区。

您可以做的是创建一个图像作为缓冲区,从图像中获取图形对象并对其执行所有绘图操作。然后将整个图像绘制到显示器上并保存为文件:

int height = 2000;
int width = 1500;
float saveQuality = 0.7f;

// Create image as buffer
Image imageBuffer = Image.createImage(width, height, 0xffffff);
// Create graphics out of image object
Graphics imageGraphics  = imageBuffer.getGraphics();

// Do your drawing operations on the graphics from the image
imageGraphics.drawWhaterver(...);

// Draw the complete image on your Graphics object g (the screen I guess) 
g.drawImage(imageBuffer, w, h);

// Save the image with the ImageIO class
OutputStream os = Storage.getInstance().createOutputStream("storagefilename.png");
ImageIO.getImageIO().save(imageBuffer, os, ImageIO.FORMAT_PNG, saveQuality);

请注意,我还没有测试过它,但它应该是这样工作的。