Java Swing: paint under JPanel components?

I have a JPanel that has a button in it. The location of the button does not matter. Code paint(Graphics g) :

  @Override
     public void paint (Graphics g) {
         super.paint (g);
         / * drawing code * /
     }

If I wanted to fill the entire panel space with a black rectangle, and also by clicking the button on the panel, the filled rectangle would simply cover everything. Thus, instead of pressing a button and then black around the button, the entire panel is black.

Is there a way to change the panel or drawing procedure so that the components are drawn on top of the custom painting?

I tried putting super.paint(g) at the end of the picture, for example:

  @Override
     public void paint (Graphics g) {
         / * drawing code * /
         super.paint (g);
     }

... thinking that at first it will make a normal picture, and then just place the components above it. However, if everything is done like this, the usual picture disappears altogether, and only the button appears. That is, only the button and the white (default) background, and not the black rectangle.

Any ideas?

Thank you

Edit: I want to clarify that the black rectangle is an example. I know that I could just set the background color, but I try to ultimately make any custom picture that I would like.

+4
source share
2 answers

I think you want to override paintComponent() not paint() .

paint() usually does not paint() , but delegates the values โ€‹โ€‹of paintComponent() , paintBorder() and paintChildren() .

see javax.swing.JComponent.paint() for more information

Example:

  @Override public void paintComponent(Graphics g) { /* your draw code here */ } 

You can call super.paintComponent (g); to draw the component as usual, but you can omit it.

+8
source

You can set the panel background to black and adjust the colors of the interface delegate. You can setBorderPainted(false) and select the button as desired. Here's sscce , with which you can update your question as needed. This AnimationTest shows how paintComponent() can update the back panel components.

Addendum: first put super.paintComponent(g) so that the components appear on top of the (possibly changing) background. Since the background color of the component is controlled by its user interface delegate, you can do any of the following:

  • use the UIManager to change the corresponding properties as shown below.
  • use a custom JButton that overrides paintComponent() as shown here .
  • use the user interface as shown here .

SSCCE:

 import component.Laf; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; /** @see https://stackoverflow.com/a/11075785/230513 */ public class ButtonPanel extends JPanel { public ButtonPanel() { this.setBackground(Color.black); final JButton b = new JButton(new AbstractAction("Button"){ @Override public void actionPerformed(ActionEvent e) { System.out.println("Clicked"); } }); // b.setBorderPainted(false); this.add(b); } private void display() { UIManager.put("Button.foreground", Color.white); UIManager.put("Button.background", Color.black); JFrame f = new JFrame("ButtonPanel"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(this); f.pack(); f.setLocationRelativeTo(null); f.setVisible(true); } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new ButtonPanel().display(); } }); } } 
+6
source