1

I'm trying to add an overlay to an Applet, displayed in a JFrame.

The Applet is on the content pane, while the overlay is on the JLayeredPane of the JFrame.

Here's what it looks like visually

The only thing happening that isn't shown, is that the addApplet() method is called once the Applet has been loaded and is ready to play.

I tried to disable all drawing in the JLayeredPane, yet it still draws the grey background that is visible. I don't know where that background is coming from.

Note that both of the colors are visible, so each PaintComponent must be transparent.

How do I get rid of the grey background?

public class RuneFrame extends JFrame {

    private JLabel loadingLabel;
    private PaintComponent paintLayer1;
    private PaintComponent paintLayer2;

    public RuneFrame() {
        super("Rune");
        this.setSize(781, 542);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initUI();
    }

    private void initUI() {
        loadingLabel = new JLabel("Loading...");
        loadingLabel.setHorizontalAlignment(SwingConstants.CENTER);
        add(loadingLabel);

        setLocationRelativeTo(null);
    }

    /**
     * Adds an Applet to the frame. Do this when the applet has loaded.
     * @param applet    Game shown in this frame.
     */
    public void addApplet(Applet applet) {
        remove(loadingLabel);

        paintLayer1 = new PaintComponent(Color.yellow, new Color(0,0,0,0));
        this.getLayeredPane().add(paintLayer1, new Integer(1000));

        paintLayer2 = new PaintComponent(new Color(0,0,0,0), Color.red);
        this.getLayeredPane().add(paintLayer2, new Integer(1000));

        this.getLayeredPane().setOpaque(false);
        this.getLayeredPane().setBackground(new Color(0,0,0,0));

        add(applet);
        pack();
    }
}
public class PaintComponent extends JComponent {

    private Color c1;
    private Color c2;

    public PaintComponent(Color c1, Color c2) {
        super();
        this.c1 = c1;
        this.c2 = c2;

        this.setOpaque(false);
        this.setSize(300, 300);
        this.setBackground(new Color(0, 0, 0, 0));
    }

    @Override
    public void paint(Graphics g) {
        //super.paintComponent(g);

        Graphics2D g2 = (Graphics2D) g.create();

        int w = 200;
        int h = 200;
        g2.setComposite(AlphaComposite.getInstance(
                AlphaComposite.SRC_OVER, .5f));
        g2.setPaint(new GradientPaint(0, 0, c1, 0, h, c2));
        g2.fillRect(0, 0, w, h);

        g2.dispose();
    }
}
9
  • AWT components (like Applet) aren't capable of been transparent/translucent. The paint method of your PaintComponent should be calling super.paint, assuming you're expecting it to paint it's children Commented Feb 9, 2016 at 23:16
  • I'd also have a look at How to use RootPanes to see how the JLayeredPane is used by the JFrame Commented Feb 9, 2016 at 23:19
  • The Applet wont be transparent. It's in the back. The idea is that the Applet is drawn first - then the PaintComponent on top. That also explains why I'm not calling super.paint: I'm not interested in anything else than the gradient. EDIT: I read the documentation on JRootPane before posting. It says "You can choose to put components in the root pane's layered pane".
    – MadsPH
    Commented Feb 9, 2016 at 23:37
  • Okay, AWT components also don't have a concept of z-ordering, so you're likely to have issues with that to Commented Feb 9, 2016 at 23:39
  • 1
    Thank you for your help. :) I ended up using reflection to subclass the Applet, and paint my overlay using the applet's own paint().
    – MadsPH
    Commented Feb 10, 2016 at 17:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.