将图形保存为图像?

发布于 2024-11-16 03:19:41 字数 213 浏览 2 评论 0原文

我正在使用 Eclipse 为 BlackBerry 开发一个应用程序,想知道是否有任何方法可以保存使用 Graphics 对象绘制的图片...该应用程序涉及使用 Graphics 绘制饼图。我第一次使用时效果很好打开屏幕显示图表,但下次我尝试通过调用屏幕创建图表时,它会变得混乱。所以我想知道是否可以将图表保存为图像......这样我就不必继续绘制每次我打开屏幕时一遍又一遍......拜托帮助...提前致谢!

I'm developing an app for BlackBerry with Eclipse and want to know if there is any way to save a picture that is drawn with a Graphics object...The app involves drawing a pie chart using Graphics.It works fine the first time I open the screen to display the chart but the next time I try to create the chart by calling the screen, it goes haywire.So I was wondering if I can save the chart as an image...that way I wont have to keep drawing over and over everytime I open the screen...Please help...Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回梦里 2024-11-23 03:19:41

我知道您正在使用 java.awt.Graphics 类,对吗?
确保您仅在 AWT 事件线程上绘图。
用于

SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    // YOUR CODE        
  }
});

在事件线程上运行保存代码。如果您尝试从另一个线程保存,如果未完全绘制图像,则图像可能会变形。

I understood you are using the java.awt.Graphics class, right?
Ensure you are drawing only on the AWT event thread.
Use

SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    // YOUR CODE        
  }
});

to run your saving code on the event thread. If you try to save from another thread, the image could get distorted if it is not drawn completely.

走过海棠暮 2024-11-23 03:19:41

我不知道黑莓 API,但你可以采取其他方法来解决你的问题。您可以进行离屏渲染。创建一个 BufferedImage,获取对其 Graphics 对象的引用,进行渲染,然后将 BufferedImage 保存为 png 格式。

例如:通过

int width = 200, height = 200;

// TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
// into integer pixels
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

Graphics2D ig2 = bi.createGraphics();

// Draw your chart

ImageIO.write(bi, "PNG", new File("yourImageName.PNG"));

这种方式,您可以一次完成所有绘图,将其保存为文件或仅保存在内存中(取决于您的需要),然后您只需要从文件加载图像或在中执行 g.drawImage()你的屏幕。

但正如我之前所说,我不知道这是否适用于 Blackberry API,它肯定适用于桌面中的 JDK/JRE。

I don't know the blackberry API but you could take other aproach to your problem. You could do offscreen rendering. Create a BufferedImage, get a reference to its Graphics object, do the rendering and then save the BufferedImage as a png for example.

For example:

int width = 200, height = 200;

// TYPE_INT_ARGB specifies the image format: 8-bit RGBA packed
// into integer pixels
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

Graphics2D ig2 = bi.createGraphics();

// Draw your chart

ImageIO.write(bi, "PNG", new File("yourImageName.PNG"));

This way you can do all your drawing once, saving it as a file or just in memory (depends on what you need) and then you just need either to load the image from the file or do g.drawImage() in your screen.

But as i said before i don't know if this applies to the Blackberry API it works for sure with the JDK/JRE in a desktop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文