Flickering during resizing.
When I attempt to rescale a JFrame, which has no listeners, and has two JPanels in it, the layout takes a bit to resize itself. It's not slowness due to long calculations, since there's nothing that would take that long. I've stripped out everything that has to do with the underlying simulation it's supposed to display (and replaced it with plain colors), and it still has that small lag. EDIT: Kinda forgot the main point. How can this be stopped?
Main window:
Code:
package graphics;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
/**
* Represents the top-level window for the GUI of the logic toolkit
*
* @author Henry Swanson
* @version 1.0 Apr 3, 2011
*/
public class MainWindow extends JFrame {
/** Serial ID. What is this? */
private static final long serialVersionUID = -3880026026104218593L;
/**
* Creates a {@link MainWindow} and the {@link WorkspacePanel} and
* {@link ActionPanel} within.
*/
public MainWindow() {
super("Logic Toolkit");
WorkspacePanel w = new WorkspacePanel();
ActionPanel a = new ActionPanel();
add(w, BorderLayout.CENTER);
add(a, BorderLayout.LINE_END);
setMinimumSize(new Dimension(400, 300));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
}
Workspace panel:
Code:
package graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
/**
* The panel where all the gates are shown.
*
* @author Henry Swanson
* @version 1.0 Apr 3, 2011
*/
public class WorkspacePanel extends JPanel {
/** Serial ID. What is this? */
private static final long serialVersionUID = 6634247456369380795L;
/**
* Creates a {@link WorkspacePanel}.
*/
public WorkspacePanel() {
super(null);
setPreferredSize(new Dimension(400, 300));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.GREEN);
g2d.fillRect(0, 0, 1024, 1024);
}
}
Action panel:
Code:
package graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
/**
* The panel where buttons for actions such as adding and removing gates are.
*
* @author Henry Swanson
* @version 1.0 Apr 3, 2011
*/
public class ActionPanel extends JPanel {
/** Serial ID. what is this? */
private static final long serialVersionUID = 9139567862742794039L;
/**
* Creates a {@link ActionPanel}.
*/
public ActionPanel() {
super(null);
setPreferredSize(new Dimension(100, 300));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.CYAN);
g2d.fillRect(0, 0, 1024, 1024);
}
}
Re: Flickering during resizing.
If you mean the delay before repainting when the frame is resized rapidly, I see what you mean - paintComponent seems to be called more often than the graphics painting is actually done - the painting seems to be deferred while resizing is in progress. The frame border resizes smoothly, but that's handled in the frame peer, which isn't accessible.
I tried just setting the background colour of the panels rather than hand-painting the panel colours, which works, but gives exactly the same effect.
I can't think of a simple way to fix this, it seems to be handled by the Graphics library (btw, you don't need Graphics2D for fillRect), which presumably calls the frame peer to update itself.
You have to honor failure, because failure is just the negative space around success...
R. Nelson
Re: Flickering during resizing.
Alright. I suppose I can just ignore the problem, and hope the user won't be bothered by it. :P