Reputation: 3349
i am developing a java swing application that doesn't fit the normal application profile. It contains a tabbed pane (and some other panels) and each tab contains a Canvas with active rendering (only one thread paints at a time).
The problem is that when i resize the main application window the canvas content doesn't show (it shows up when i stop dragging the mouse) and i cant figure why. I've also noticed that some tutorials render the content to a buffered image:
http://jamesgames.org/resources/double_buffer/double_buffering_and_active_rendering.html
Could that be the reason the Canvas is flickering (the way i understand the tutorial buffered image was only use to remove the title bar offset)?
The GUI is structured like this:
JFrame
- TabbedPane
- ScrollPane
- Canvas 1
- ScrollPane
- Canvas 2
- JPanel
- labels,buttons,..
The code:
class Renderer extends Canvas implements Runnable
{
Thread hThread = null;
BufferStrategy strategy = null;
Renderer()
{
setIgnoreRepaint(true);
}
public void run()
{
while(active)
{
g = strategy.getDrawGraphics();
draw(g); // renderer elements
strategy.show();
g.dispose();
}
}
// Called when switched to in Tabbed pane
public void start()
{
createBufferStrategy(2);
strategy = getBufferStrategy();
hThread = new Thread(this);
hThread.start();
}
// Called when switched off in Tabbed pane
public void stop(){
hThread.stop();
}
}
Upvotes: 1
Views: 1845
Reputation: 3349
fixed my problem. The solution was to remove the JScrollPane and replace it with a JPanel. I then add the canvas to the JPanel and handle scrolling on my own (with JScrollBar). The canvas isnt flickering anymore and also resizing performs better.
Upvotes: 1
Reputation: 3684
Double buffering techniques are for reducing or eliminating flickering in fast painting code, which is what you should be accomplishing via the BufferStrategy (internally using a BufferedImage).
Looks like this will get you headed in the right direction: AWT custom rendering - capture smooth resizes and eliminate resize flicker (EDIT: this link actually does not solve the Resize problem)
Upvotes: 1