tags:

views:

286

answers:

3

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.

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.

  1. Does anyone know how to sample the color of a pixel when all I have access to is the Graphics object?

  2. Is there a better way to draw a custom image as the textfield background other than overriding paintBackground in TextfieldUI?

Thanks

+1  A: 

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

Uri
So I tried the Robot class and it seems to work, but I'm having issues getting the correct pixel location. I tried component.getSreenLocation(), and that doesn't give me the right results. Any ideas?
Jon
This worked perfectly when I used component.getLocationOnScreen() and sampled the pixel at x-1, y-1
Jon
I'm not sure, but I think there is an issue of relative coordinates vs. absolute coordinates. It is possible that you were somehow giving it the relative coordinates (e.g., relative to this specific panel) then a screen coordinate.,
Uri
OK, so now I have another issue with this, when the screen is being repainted, the Robot gets the color from the old screen and not the new screen. Essentially I want to force a full redraw then repaint again. Any ideas?
Jon
A: 

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.

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.

If that doesn't work then it would be nice to have a demo of your code so we can see whats actually happening.

camickr
JComponent component=getComponent();Int width=component.getWidth();int height=component.getHeight();BufferedImage scaledImage=PreferencesDialog.scaleToFitWindow(image,width,height);g.drawImage(scaledImage,0,0,component);
Jon
A: 

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.

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?)

gojomo
no, i am using an arbitrary background, which makes this problem much more difficult
Jon