Java透明度渲染错误 [英] Java Transparency rendering error

查看:171
本文介绍了Java透明度渲染错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个自助服务终端风格的Java程序,用于显示天气,时间,预测等。以下是程序应该的样子(在更新时间之前拍摄的截图)





各个部分创建为JPanels,并覆盖了 paintComponent()方法。它们绘制渐变标题和半透明背景。但是,当我更改其中一个面板的内容时,之前的内容会被遗忘,而不是被删除。示例如下所示:



如您所见,当时间从11:51变为11:52时,数字重叠,背景变浅(因为重新划分半透明背景)



图形错误





我该怎么做才能防止这种重叠?最终,当单个面板更新其内容时,我宁愿不必重绘整个屏幕。我宁愿只重绘那个特定的部分。重新绘制背景,然后是其中的所有组件将很容易。



更新



有几个答案涉及清除要绘制的区域。无论我使用 g.clear()还是 AlphaComposite.CLEAR ,它都会删除背景图片。这可以在下图中看到。也许如果我可以在清除区域之前复制背景,我可以重新绘制背景的那一部分,然后适当地对其他图像进行分层。想法?





更新2



提交的答案显示了透明面板和背景上的计时器的完整工作示例。这非常有用,但是,我不是手动绘制我的子组件。时间,天气图标和温度都是JLabel布局在半透明面板上。我宁愿不必手动绘制单个组件。

解决方案

类似问题



http://img641.imageshack.us/img641/8873/translucentfail.png 之前的图片

此处

图片http://i46.tinypic.com/6gj1gk.png



另请注意,Swing程序应覆盖 paintComponent()而不是覆盖 paint().\"- 在AWT和Swing中绘画:绘画方法


I am currently developing a kiosk style Java program to display weather, time, forecast etc. Here is a shot of what the program should look like (screenshot taken before the time updated)

The various sections are created as JPanels with the paintComponent() method overridden. They draw in a gradient header and a semitransparent background. However, when I change the content of one of these panels, the previous content is left behind, rather than being removed. An example is shown below:

As you can see, when the time changed from 11:51 to 11:52, the numbers overlapped, and the background became lighter (because the semitransparent background was redrawn over it)

The Graphical Error

What should I do to prevent this overlap? Ultimately, I would prefer to not have to redraw the entire screen when an individual panel updates it's contents. I would prefer to only redraw that particular section. It would be easy to repaint the background, and then all the components within.

Update

Several answers involve clearing the area to be drawn. Whether I use g.clear() or AlphaComposite.CLEAR it erases the background image. this can be seen in the following image. Perhaps if I could copy the background before I clear the area, I could repaint that section of the background, then layer the other images appropriately. Thoughts?

Update 2

An answer was submitted which shows a complete working example of a timer laid over a transparent panel and a background. This is very useful, however, I am not painting my sub-componenets manually. The time, weather icons and temperatures are all JLabels laid out over the semi-transparent panel. I would prefer to not have to paint the individual components manually.

解决方案

A similar problem

image before http://img641.imageshack.us/img641/8873/translucentfail.png

is addressed here

image after http://i46.tinypic.com/6gj1gk.png

Note also, "Swing programs should override paintComponent() instead of overriding paint()."—Painting in AWT and Swing: The Paint Methods.

这篇关于Java透明度渲染错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆