Transparency in overlay

Hi
I’m trying to get transparent components into the overlay with xith. The following code is how i’m creating my object I’m trying to set it so that the text is non transparent, the first colour block is non transparent, the middle block is semi transparent, and the last block is 100% transparent. All that currently happens is that the colour is scaled by the alpha chanel, so the 100% transparent part comes out black, the 50% comes out dark blue.
I add the panel by calling

        UIWindow window = new UIWindow(component, width, height, true, true);
        uiComponentMap.put(component, window);
        uiWindowManager.addOverlay(window);
        uiWindowManager.setPosition(window, x, y);
        uiWindowManager.setVisible(window, true);

Does anyone know what i’m doing wrong?, or have any required debug info?

Cheers

Endolf

public class TestLabel extends JPanel {
    private Color backGroundColour = new Color(Color.BLUE.getRed(), Color.BLUE.getGreen(), Color.BLUE.getBlue(), 255);
    private Color backGroundColourFaded = new Color(Color.BLUE.getRed(), Color.BLUE.getGreen(), Color.BLUE.getBlue(), 128);
    private Color backGroundColourAlphad = new Color(Color.BLUE.getRed(), Color.BLUE.getGreen(), Color.BLUE.getBlue(), 0);
    private Color clearColour = new Color(Color.white.getRed(), Color.white.getGreen(), Color.white.getBlue(), 0);
    
    /** Creates a new instance of TestLabel */
    public TestLabel() {
        setDoubleBuffered(true);
        setSize(250, 100);
        setLocation(0,0);
    }
    
    public void paint(Graphics graphics) {
        if(graphics == null) {
            System.out.println("Erm, panel.getGraphics() is null");
        } else {
            System.out.println("Graphics is of type: " + graphics.getClass().getName());
        }
        graphics.setPaintMode();
        graphics.setColor(clearColour);
        graphics.fillRect(0,0,249,99);
        graphics.setColor(backGroundColour);
        graphics.fillRect(0,0,90,99);
        graphics.setColor(backGroundColourFaded);
        graphics.fillRect(90,0,90,99);
        graphics.setColor(backGroundColourAlphad);
        //graphics.fillRect(180,0,69,99);
        graphics.setColor(java.awt.Color.WHITE);
        graphics.drawRect(0,0,249,99);
        graphics.setColor(java.awt.Color.WHITE);
        graphics.drawString("Test", 2 ,2 + graphics.getFontMetrics().getAscent() );
        
    }
}

I thought the (true,true) on the end of the UIWindow construction was meant to enable transparency, but I’ve never been able to get it to work.

I just assumed it’s one of them things that’ll get fixed eventually.

Kev

Hi
I wasn’t sure if it’s something in xith on the ‘todo’ list, or if it’s my code, it’s copied from my old j3d hud (the painting bits), but then I had a Graphics2D which you had to call setBackground on and then clearRect the bits you wanted, you could then paste over the rest with a new colour (again with alhpa bits set) and it would work, if you didn’t clear it with a color with alpha first then it wouldn’t be transparent. Graphics doesn’t have a setClearColor method, but it does have the clearRect, so i’m not sure what i’m really supposed to be doing here :slight_smile:

Just hoped someone might have tried and got it working.

Cheers

Endolf

Edit: fixed some typos, again

We use this. Yes you have to clear the alpha channel in the gtaphics context to get this to work.

g.setColor(new Color(1,1,1,0.5));
g.fillrect(0,0,getWidth(), getHeight());

Or somthing to that effect.

Hi
It turns out I was doing it right, I just needed to update my xith3d.jar.

Cheers

Endolf

Further to this, I’ve worked out what I was doing to make transparency not work…

The only way to update a component that you’ve drawn to seems to be to call setRoot() on the UIWindow associated with. This is work around, I believe calling repaint() is meant to work but doesn’t seem to want to.

Calling setRoot() later on undoes the transparency settings and stops the component supporting the alpha channel drawing…

That made no sense, I’ll try and edit it to rephrase in the morning.

Kev

That doesn’t seem right to me. I never call set root more than once. In fact I never call repaint() on a regular component…, just do a label.setText(), etc and it update properly.

If you have a simple test case I can try to debug it for you.

I’ll knock one together over the weekend, on a jolly today I’m afraid.

The difference is that I’m drawing directly to my component. I have an extension of JPanel, called in this case HealthPanel. I override the paint() method to draw some images and a bar to the panel.

Now, in Swing if I call repaint on the panel it redraws the panel, calling my paint method which allows me to update the health bar. However, if I call repaint on HealthPanel when I’ve added it Xith is has no effect. Last time I check it didn’t even result in paint() being recalled (although I’ll check this).

So, the point is I don’t have any sub-components, and hence don’t have anything to call setText() on. Although, come to think of it, Jens mentioned in his GUI tutorial that the only way he could get his JTextField for his FPS counter to update was to call setRoot() again.

Kev

A test case:

http://www.cokeandcode.com/xith/ GUITest.java

It simply(?) creates two components based on JPanels over the top of a single quad scene. There is a count displayed in both panels that should be updated.

To cause an update to the UIWindows I have two different methods.

  1. call repaint() on the JPanel - this doesn’t seem to work at all.
  2. call setRoot() on the UIWindow with the JPanel (as Jens does in the tutorial) - this works but the transparency of the component is reset.

Kev