1
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.LayerUI;
public class MyJLayer extends JFrame {
    public static void main(String[] args) {
        MyJLayer jlayer = new MyJLayer();
        jlayer.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        JButton button = new JButton("Debug Only.");
        panel.add(button);

        UI ui = new UI();
        JLayer<JPanel> jLayer = new JLayer<JPanel>(panel, ui);

        jlayer.add(jLayer);
        jlayer.setSize(100, 100);
        jlayer.setVisible(true);
    }
}
class UI extends LayerUI<JPanel>{
    public void paint(Graphics g, JPanel c){
        super.paint(g, c);
        Graphics2D g2d = (Graphics2D)g.create();
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .8f));
        g2d.setColor(Color.BLUE);
        g2d.fillRect(0, 0, c.getWidth(), c.getHeight());
        g2d.dispose();
    }
}

the panel doesn't display BLUE color at all, but I don't know why. Could anyone help me out? I just couldn't find out. http://docs.oracle.com/javase/tutorial/uiswing/misc/jlayer.html

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
David Wong
  • 39
  • 1
  • 8

1 Answers1

4

Your paint method doesn't override a superclass method, so it's not being called. Change the signature to:

public void paint(Graphics g, JComponent c)

... and add the @Override annotation so that in future, the compiler can find the problem for you...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • thanks! I found it! Yes, change the paint(Graphics g, JPanel c)->paint(Graphics g, JComponent c), then done. – David Wong Nov 03 '14 at 23:49