Drawing 有没有一种方法可以在不截图的情况下将图形对象保存为代号1?

Drawing 有没有一种方法可以在不截图的情况下将图形对象保存为代号1?,drawing,codenameone,Drawing,Codenameone,问题在标题中。例如,如何在下面的代码段中将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)

问题在标题中。例如,如何在下面的代码段中将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)

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


干杯,

图形只是曲面的代理,它不知道或无法访问它正在绘制的底层曲面,原因很简单。它可以绘制到一个硬件加速的“表面”,在那里物理上没有底层图像


在iOS和Android上都是这种情况,“屏幕”是本机绘制的,没有缓冲区。

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

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);

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

谢谢@Shai,因此如果没有缓冲区,就无法将图形保存到文件中。因此,截图是我唯一的出路!谢谢,这是预期的工作!!!输出图像文件的尺寸与原始图像相同!但为什么我使用的方法不起作用呢?与您的唯一区别是,您使用中间图像作为缓冲区,而我直接在图形对象g上绘制。如果您或@Shai可以添加一些解释,请提前感谢。