问题出在标题中。例如,如何将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)。
我会非常感谢任何可以告诉我如何使用Codename来做到这一点的人。如果不可能,那就知道了!
干杯,
答案 0 :(得分:2)
你可以做的是创建一个Image作为缓冲区,从图像中获取图形对象并对其进行所有绘图操作。然后将整个图像绘制到显示器并将其另存为文件:
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);
注意,我还没有测试过,但它应该是那样的。
答案 1 :(得分:1)
图形只是表面的代理,它不知道或访问它所绘制的底层表面,原因很简单。它可以吸引硬件加速"表面"没有底层图像的地方。
在iOS和Android上都是这种情况,其中"屏幕"是原生绘制的,没有缓冲区。