Reputation: 1765
I've heard that swing is double buffered by default. I'm not trying to have swing double buffered. I am using double buffering and I want to add some swing objects(right now just a JButton added to a JPanel that is added to the JFrame).
The problem is calling frame's paint, repaint or paintComponents methods after I render other stuff erases the visuals of the other stuff. Calling the methods before rendering other stuff causes other stuff to be in front of the swing objects(right now just a JButton). Doing the same directly to JPanel seems to have no effect.
I believe I need a way to paint Jframe or the JPanel added to it without its default background(gray) which causes the window to become blank when set to Color(0,0,0,0).
public void paint() {
// uses double buffering system.
do {
do {
Graphics2D g2d = (Graphics2D) bufferStrategy.getDrawGraphics();
g2d.fillRect(0, 0, frame.getWidth(), frame.getHeight());
try {// frame.paintComponents(g2d); // calling it here draws buttons to behind of the object
rendering(g2d); // I draw other objects in this method
// frame.paintComponents(g2d);// calling it here makes other objects disappear
} catch (NullPointerException e) {
e.printStackTrace();
}
g2d.dispose();
} while (bufferStrategy.contentsRestored());
bufferStrategy.show();
} while (bufferStrategy.contentsLost());
}// method end
This is how I set up my button :
private void setUpGUI() {
panel = new JPanel();
LayoutManager layout = new FlowLayout();
panel.setLayout(layout);
panel.setOpaque(false);//this does not seems to have any effect
panel.setBackground(Color.yellow);//this does not seems to have any effect. this is just for testing
JButton but1 = new JButton("but1");
panel.add("but1", but1);
frame.add(panel);
}
edit: This is the workaround/fix:
new paint method:
public void paint() {
// uses double buffering system.
do {
do {
Graphics2D g2d = (Graphics2D) bufferStrategy.getDrawGraphics();
g2d.fillRect(0, 0, frame.getWidth(), frame.getHeight());
try {
frame.paint(g2d);
} catch (NullPointerException e) {
e.printStackTrace();
}
g2d.dispose();
} while (bufferStrategy.contentsRestored());
bufferStrategy.show();
} while (bufferStrategy.contentsLost());
}// method end
I've overridden panel's paint method:
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Main.main().rendering(g2d);
super.paint(g);
g.dispose();
g2d.dispose();
}
}
Upvotes: 0
Views: 1556
Reputation: 324157
There is no need to create your own BufferStategy!!!
If you want to do custom painting the override the paintComponent() method of a JPanel and then add the panel to the frame. Read the section from the Swing tutorial on Custom Painting for more information and examples.
You can then still add components to this panel the same way you do for any other panel. The layout manager of the panel will position the components.
panel.setOpaque(false);
panel.setBackground(Color.yellow);
The setBackground(...) does nothing because you said the panel is not opaque, which means the background of the parent component will be painted.
panel.add("but1", but1);
That is not how you add a component to the panel. The proper method to use is:
panel.add(but1);
since your panel is using a FlowLayout there is no need to specify a constraint.
Upvotes: 3