Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Transparent Swing component as overlay to GLCanvas problem

843807Feb 8 2010 — edited Feb 9 2010
Hi

I'm currently playing with JOGL, and so forth it's been good to me. But I'd like to have a transparent overlay (a Swing component) and I'd like to avoid having to draw it with JOGL. Here's what I've been trying:

The Swing component that should behave like a transparent overlay:
public class ViewportControlOverlay extends JPanel {

    public ViewportControlOverlay(...) {
        //Left out a bunch of stuff here
        ...        
        this.setOpaque(false);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;

        // I had hoped this would work
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g2d.fillRoundRect(0, 0, this.getWidth(), 16, 10, 10);
        g2d.setFont(new Font(Font.DIALOG, Font.BOLD, 10));
    }
And here's the viewport (a layered pane) that holds the GLCanvas and the overlay
public class Viewport extends JLayeredPane implements KeyListener {
    protected GLCanvas canvas;
    protected ViewportControlOverlay controlOverlay;


    public Viewport(...) {
        canvas = new GLCanvas(...);
        controlOverlay = new ViewportControlOverlay(...);
        this.add(controlOverlay, new Integer(3));
        this.add(canvas, new Integer(0));
    }
Now, everything basically works, and I can draw stuff on the canvas. But the problem is that the ViewportControlOverlay is not transparent at all on the openGL canvas. I figure that it's because the stuff on the openGL canvas is not visible to Swing. I've tried using the overlay on a viewport created with Swing and Graphics2D, and the overlay worked like a charm.

So I see where the problem is, but I'm hoping that one of you could provide me with a solution to this, without me having to draw the overlay panel with JOGL. I left out alot of code, but hopefully I kept enough to convey my question.

Best regards

P.S. I hope this is the correct place to post, my apologies if it's not
P.P.S. Is it just my bad programming skills or is Graphics2D really, really sluggish when drawing many objects?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 9 2010
Added on Feb 8 2010
2 comments
618 views