I have to create a special TextFieldUI that draws an image as the background. That image contains some alpha components. However, whenever a character is written in that text field, first it redraws the background and then draws the character. This is fine when the background contains no alpha components, but after a few characters have been typed, the alpha areas sum up to become black.
我必须创建一个特殊的TextFieldUI,将图像绘制为背景。该图像包含一些alpha分量。但是,只要在该文本字段中写入字符,首先它会重绘背景然后绘制字符。当背景不包含alpha分量时,这很好,但是在输入几个字符后,alpha区域总和变为黑色。
The only way I can see around this is in the paintBackground method of TextfieldUI (which I'm overriding), I have to first sample the color of the background at that location, paint the entire graphics component that color, and then paint my background.
我可以看到的唯一方法是在TextfieldUI的paintBackground方法中(我要覆盖),我必须首先在该位置采样背景的颜色,绘制整个图形组件的颜色,然后绘制我的背景。
-
Does anyone know how to sample the color of a pixel when all I have access to is the Graphics object?
当我有权访问的是Graphics对象时,有没有人知道如何对像素的颜色进行采样?
-
Is there a better way to draw a custom image as the textfield background other than overriding paintBackground in TextfieldUI?
除了覆盖TextfieldUI中的paintBackground之外,还有更好的方法将自定义图像绘制为文本字段背景吗?
Thanks
3 个解决方案
#1
I haven't tried it before, but Swing is built on top of AWT, and the Robot class had a way of sampling specific pixels in the AWT
我之前没有尝试过,但Swing是建立在AWT之上的,而Robot类有一种方法可以在AWT中对特定像素进行采样
#2
Well, I don't know what your custom code looks like in the paintBackground method, but I would make sure you fill in the text field background before you draw the image.
好吧,我不知道你的自定义代码在paintBackground方法中是什么样的,但我会确保你在绘制图像之前填写文本字段背景。
I'll let you decide if its "better" or not, but you can use the Background Panel which allows you to add an image to a panel. Then you add the text field to the panel (the text field is automatically made non-opaque so the image shows through). Then you add the panel to the GUI.
我会让你决定它是否“更好”,但你可以使用背景面板,它允许你向面板添加图像。然后将文本字段添加到面板(文本字段自动变为非不透明,以便显示图像)。然后将面板添加到GUI。
If that doesn't work then it would be nice to have a demo of your code so we can see whats actually happening.
如果这不起作用那么最好有一个代码演示,这样我们就可以看到实际发生了什么。
#3
When you override paintBackground, you're calling the superclass version first, right? It already lays down a background-color rectangle that would give your image a fresh-start.
当你覆盖paintBackground时,你首先调用超类版本,对吧?它已经奠定了一个背景颜色矩形,可以让你的图像重新开始。
Rather than 'sampling' the background color, it's probably already correct (the superclass paintBackground code gets it from the parent component if not locally set). If that default is not correct, set it in initial interface construction. (Your field isn't being overlaid on other complicated arbitrary interface of unknown solid colors, is it?)
它不是“采样”背景颜色,而是可能已经正确(超类paintBackground代码从父组件获取它,如果没有本地设置)。如果该默认值不正确,请在初始界面构造中进行设置。 (你的领域没有被覆盖在未知纯色的其他复杂任意界面上,是吗?)
#1
I haven't tried it before, but Swing is built on top of AWT, and the Robot class had a way of sampling specific pixels in the AWT
我之前没有尝试过,但Swing是建立在AWT之上的,而Robot类有一种方法可以在AWT中对特定像素进行采样
#2
Well, I don't know what your custom code looks like in the paintBackground method, but I would make sure you fill in the text field background before you draw the image.
好吧,我不知道你的自定义代码在paintBackground方法中是什么样的,但我会确保你在绘制图像之前填写文本字段背景。
I'll let you decide if its "better" or not, but you can use the Background Panel which allows you to add an image to a panel. Then you add the text field to the panel (the text field is automatically made non-opaque so the image shows through). Then you add the panel to the GUI.
我会让你决定它是否“更好”,但你可以使用背景面板,它允许你向面板添加图像。然后将文本字段添加到面板(文本字段自动变为非不透明,以便显示图像)。然后将面板添加到GUI。
If that doesn't work then it would be nice to have a demo of your code so we can see whats actually happening.
如果这不起作用那么最好有一个代码演示,这样我们就可以看到实际发生了什么。
#3
When you override paintBackground, you're calling the superclass version first, right? It already lays down a background-color rectangle that would give your image a fresh-start.
当你覆盖paintBackground时,你首先调用超类版本,对吧?它已经奠定了一个背景颜色矩形,可以让你的图像重新开始。
Rather than 'sampling' the background color, it's probably already correct (the superclass paintBackground code gets it from the parent component if not locally set). If that default is not correct, set it in initial interface construction. (Your field isn't being overlaid on other complicated arbitrary interface of unknown solid colors, is it?)
它不是“采样”背景颜色,而是可能已经正确(超类paintBackground代码从父组件获取它,如果没有本地设置)。如果该默认值不正确,请在初始界面构造中进行设置。 (你的领域没有被覆盖在未知纯色的其他复杂任意界面上,是吗?)