0

This a simple application I got from here this answer to How to set a Transparent Background of JPanel that's supposed to explain how setOpaque() works.

public class TwoPanels {
public static void main(String[] args) {

    JPanel p = new JPanel();
    // setting layout to null so we can make panels overlap
    p.setLayout(null);

    CirclePanel topPanel = new CirclePanel();
    // drawing should be in blue
    topPanel.setForeground(Color.blue);
    // background should be black, except it's not opaque, so 
    // background will not be drawn
    topPanel.setBackground(Color.black);
    // set opaque to false - background not drawn
    topPanel.setOpaque(false);
    topPanel.setBounds(50, 50, 100, 100);
    // add topPanel - components paint in order added, 
    // so add topPanel first
     p.add(topPanel);

    CirclePanel bottomPanel = new CirclePanel();
    // drawing in green
    bottomPanel.setForeground(Color.green);
    // background in cyan
    bottomPanel.setBackground(Color.cyan);
    // and it will show this time, because opaque is true
    bottomPanel.setOpaque(true);
    bottomPanel.setBounds(30, 30, 100, 100);
    // add bottomPanel last...
    p.add(bottomPanel);

    // frame handling code...
    JFrame f = new JFrame("Two Panels");
    f.setContentPane(p);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setSize(300, 300);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

// Panel with a circle drawn on it.
private static class CirclePanel extends JPanel {

    // This is Swing, so override paint*Component* - not paint
    protected void paintComponent(Graphics g) {
        // call super.paintComponent to get default Swing 
        // painting behavior (opaque honored, etc.)
        super.paintComponent(g);
        int x = 10;
        int y = 10;
        int width = getWidth() - 20;
        int height = getHeight() - 20;
        g.fillArc(x, y, width, height, 0, 360);
    }
}
}

The thing the I don't get is how come he is adding the opaque layer on top of transparent layer? shouldn't be the other way around?

The way that I picture how it should work is by adding the transparent layer on top of the opaque one, kinda of like how you put a screen protector over a phone(sorry for the dumb example)

Can someone please explain how transparency works in java?

I apologize of my question is a bit naive but this has been bothering me for a while!

Community
  • 1
  • 1
GamefanA
  • 1,555
  • 2
  • 16
  • 23
  • possible duplicate of [JPanel said to be opaque what does that mean?](http://stackoverflow.com/questions/4416454/jpanel-said-to-be-opaque-what-does-that-mean) – DavidPostill Aug 11 '14 at 08:31
  • Do you know about "alpha"? – Nabin Aug 11 '14 at 08:32
  • I know what does the alpha value mean. but what does that have to do with my questions? – GamefanA Aug 11 '14 at 08:35
  • so alpha can be used to control the opacity of any components in java – Nabin Aug 11 '14 at 08:52
  • Ok. I think a better questions could be in what order will the panels be painted? I put a print statement in each panel's paintComponent method and to my surprise the panel added later (the bottom panel) had it's paintComponent method called first. can you explain this please? – GamefanA Aug 11 '14 at 08:58

1 Answers1

0

Yes, the example reliest on the fact that with a null layout, the child components are indeed drawn in reverse order. An implementation dependency. That at least deserves mention. Adding a visible border would make it more evident:

private static class CirclePanel extends JPanel {
    CirclePanel() {
        setBorder(BorderFactory.createLineBorder(Color.RED));
    }
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • even when I set a layout manager the `paintComponent` methods are still called in reverse order. – GamefanA Aug 11 '14 at 09:06
  • The layouts are positioned on _validation_ (invalidate/validate), a method `paintChildren` is called, and that of course uses the same code. – Joop Eggen Aug 11 '14 at 09:13