将图像绘制为按钮的背景或使用 Java2D 绘制它更好/更快

发布于 2024-09-16 23:43:55 字数 74 浏览 6 评论 0原文

假设我有一个按钮,想要将背景按钮更改为渐变,使用 Java2D 绘制渐变更好/更快,还是只获取具有该渐变的图像并将其放在按钮的背景中?

Say I have a button and want to change the background button to a gradient, is it better/faster to draw a gradient using Java2D or just get an image with that gradient and put it in the background of the button?

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

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

发布评论

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

评论(3

绝情姑娘 2024-09-23 23:43:55

判断某事物是否更快的唯一方法是测量它。由于更改按钮的背景颜色因外观和颜色而异。感觉,我喜欢实现 图标界面,用于装饰按钮,如本示例中所建议的。

The only way to tell if something is faster is to measure it. As changing a button's background color varies by look & feel, I like to implement the Icon interface in order to decorate a button, as suggested in this example.

笑叹一世浮沉 2024-09-23 23:43:55

最佳答案: 测量一下看看。

现实的答案:并不重要。屏幕刷新率通常是更重要的因素。

最佳解决方案:使用某种形式的双缓冲使问题变得无关紧要。

Best answer: Measure it and see.

Realistic answer: Doesn't really matter. Screen refresh rate is usually the more important factor.

Best solution: Use some form of double buffering to make the question irrelevant.

烟燃烟灭 2024-09-23 23:43:55

我同意衡量并观察它很重要。根据我的经验,绘制渐变可能比绘制图像慢。但是,如果您只是谈论一个小按钮,则可能没有足够的担心。有一些加速梯度的技巧。例如,您可以将其绘制到 BufferedImage 上,并且每当您需要绘制渐变时,请使用 g.drawImage(Image img, int x, int y, ImageObserverobserver) 而不是创建新的渐变。更重要的是,如果您的渐变是垂直向上和向下的,您可以将渐变存储在 1 像素宽度的 BufferedImage 中。然后,您可以使用 g.drawImage(Image img, int x, int y, int width, int height, ImageObserverobserver) 在背景的整个宽度上绘制渐变。

然而,如果一个需要 1 毫秒而另一个需要 0.1 毫秒(当然,除非您尝试每秒调用它 1000 次),那么担心哪个更快通常是没有意义的。欲了解更多信息,我推荐《Filthy Rich Clients》一书,尤其是第 4、5 和 7 章。

I agree that it is important to measure it and see. From my experience, painting a gradient is likely going to be slower than painting an image. However, if you are only talking about a small button, it may not make enough difference to worry about. There are some tricks to speed up gradients. For instance, you can paint it to a BufferedImage and whenever you need to paint your gradient use g.drawImage(Image img, int x, int y, ImageObserver observer) instead of creating a new gradient. Even more, if your gradient is straight up and down, you can store the gradient in a BufferedImage of 1 pixel width. Then you can paint the gradient across the entire width of your background using g.drawImage(Image img, int x, int y, int width, int height, ImageObserver observer).

However, worrying about which is faster is generally pointless though if one takes 1 millisecond and the other takes 0.1 millisecond (unless you are trying to call it 1000 times a second of course). For more information I would recommend the book Filthy Rich Clients, especially chapters 4, 5, and 7.

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