Reputation: 1028
Inside my paintComponent() method, I have a drawRect() that paints the background of a jpanel. But because the jbutton is drawn on the screen before the paintComponent() method gets called, the jbutton is blocked out by the drawRect. Does anyone know how to fix this? My guess is to add the jbutton before repaint gets called, but I don't know how to do that?
Some code:
public Frame(){
add(new JButton());
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(0,0,screenwidth,screenheight); //paints the background with a color
//but blocks out the jbutton.
}
Upvotes: 0
Views: 1021
Reputation: 29656
Now, firstly, I will tell you what you're doing wrong here -- JFrame
is not a JComponent
, and has no paintComponent
for you to override. Your code will probably never be called. Aside from that, drawRect
merely draws a rectangle -- it does not fill one.
However, I believe there is a proper way to do this.
Since you're using a JFrame
, you should take advantage of the container's layered pane via JFrame.getLayeredPane
.
A layered pane is a container with depth such that overlapping components can appear one on top of the other. General information about layered panes is in How to Use Layered Panes. This section discusses the particulars of how root panes use layered panes.
Root panes are covered in How to Use Root Panes, a part of the Java Tutorials. A layered pane is a child of the root pane, and a JFrame
, as a top-level container, utilizes an underlying JRootPane
.
Anyways, since you're interested in creating a background, see the following diagram for how a layered pane generally looks inside a top-level container:
The table below describes the intended use for each layer and lists the JLayeredPane constant that corresponds to each layer:
Layer Name - Value - Description
FRAME_CONTENT_LAYER
-new Integer(-30000)
- The root pane adds the menu bar and content pane to its layered pane at this depth.
Since we want to specify our background is behind the content, we first add it to the same layer (JLayeredPane.FRAME_CONTENT_LAYER
), as follows:
final JComponent background = new JComponent() {
private final Dimension size = new Dimension(screenwidth, screenheight);
private Dimension determineSize() {
Insets insets = super.getInsets();
return size = new Dimension(screenwidth + insets.left + insets.right,
screenheight + insets.bottom + insets.top);
}
public Dimension getPreferredSize() {
return size == null ? determineSize() : size;
}
public Dimension getMinimumSize() {
return size == null ? determineSize() : size;
}
protected void paintComponent(final Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, screenwidth, screenheight);
}
};
final JLayeredPane layeredPane = frame.getLayeredPane();
layeredPane.add(background, JLayeredPane.FRAME_CONTENT_LAYER);
Now, to make sure we draw our background before the content, we use JLayeredPane.moveToBack
:
layeredPane.moveToBack(background);
Upvotes: 6
Reputation: 175
I encountered this before, although not jframe specifically and not the kind of the scenario that you have. Try this code,
this.getContentPane.repaint();
on your jframe. Im not sure about this, but give it a try.
Upvotes: 1
Reputation: 347334
I did this really quick test. As HovercraftFullOfEels has pointed out. JFrame does not have a paintComponent
, so I used a JPanel
instead.
Which was produced by this code
public class PanelTest extends JPanel {
private JButton button;
public PanelTest() {
setLayout(new GridBagLayout());
button = new JButton("Can you see me ?");
add(button);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Rectangle bounds = button.getBounds();
bounds.x -= 10;
bounds.y -= 10;
bounds.width += 20;
bounds.height += 20;
g.setColor(Color.RED);
((Graphics2D)g).fill(bounds);
}
}
I've I try and replicate the issue by using paintComponents
on the JFrame
, I don't see the rectangle. Even if I overwrite paint
on the JFrame
, the rectangle is still painted under the button (Not that I would ever recommend doing either).
The problem is, you haven't given us enough code to know what's going wrong
ps - drawRect
won't "fill" anything
Upvotes: 5