The following code prototypes screen flicker. The only flicker (noticeable to me at least) is while the Frame is being re-sized.
Is there a technique to eliminate flicker during Frame resizing? Given how the paint(Graphics g) method works (at least how I think it works), I don't think there is...? When I start using light-weight components, I wonder if the flickering while re-sizing will disappear? Will that be something to look for when testing JComponent instead of Canvas? As this is my first serious attempt at AWT, any general comments about how the code is written is welcomed.
public class Main {
private static final Frame frame = new Frame("Alpha GUI v1.3");
static {
frame.setSize(800, 600);
frame.addWindowListener(new MyWindowListener());
}
public static void main(String[] args) {
Four four = new Four();
frame.add(four);
frame.setVisible(true);
new Thread(four).start();
}
// I don't like the looks of anonymous classes yet. too confusing to debug right now.
static class MyWindowListener extends WindowAdapter implements WindowListener {
@Override
public void windowClosing(WindowEvent ev) { System.exit(1); }
}
}
public class Four extends Canvas implements Runnable {
private final Canvas canvas;
private final StrokeJP[] strokes = new StrokeJP[4];
Four() {
strokes[0] = new Stroke1();
strokes[1] = new Stroke2();
strokes[2] = new Stroke3();
strokes[3] = new Stroke4();
canvas = this;
}
@Override
public void run() {
for(int i = 0; i < 500; i++) {
try { Thread.currentThread().sleep(10); } catch(InterruptedException err) { }
EventQueue.invokeLater(strokes[0]);
EventQueue.invokeLater(strokes[1]);
EventQueue.invokeLater(strokes[2]);
EventQueue.invokeLater(strokes[3]);
}
}
@Override
public void paint(Graphics g) {
Image img = createImage(getSize().width, getSize().height);
Graphics og = img.getGraphics();
strokes[0].paintFull(og);
strokes[1].paintFull(og);
strokes[2].paintFull(og);
strokes[3].paintFull(og);
g.drawImage(img, 0, 0, null);
}
// Still designing a shared parent class for all strokes
private class Stroke1 extends StrokeJP {
private BasicStroke stroke = new BasicStroke(10, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
private final float x1 = 10;
private final float y1 = 10;
private float x2 = x1; private float x3 = x1;
private float y2 = y1; private float y3 = y1;
@Override
public void run() {
paintIncrimental(canvas.getGraphics());
x2++;
}
public void paintIncrimental(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(stroke);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.draw(new Line2D.Float(x3, y3, x2, y2));
x3 = x2;
}
public void paintFull(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(stroke);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.draw(new Line2D.Float(x1, y1, x2, y2));
}
}
// Still designing a shared parent class for all strokes
private class Stroke2 extends StrokeJP {
private BasicStroke stroke = new BasicStroke(10, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
private final float x1 = 10;
private final float y1 = 10;
private float x2 = x1; private float x3 = x1;
private float y2 = y1; private float y3 = y1;
@Override
public void run() {
paintIncrimental(canvas.getGraphics());
x2++;
y2++;
}
public void paintIncrimental(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(stroke);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.draw(new Line2D.Float(x3, y3, x2, y2));
x3 = x2;
y3 = y2;
}
public void paintFull(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(stroke);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.draw(new Line2D.Float(x1, y1, x2, y2));
}
}
// ...................
}
// Working on this class, but would like closure on all flickering issues before proceeding.
public abstract class StrokeJP implements Runnable {
abstract void paintIncrimental(Graphics g);
abstract void paintFull(Graphics g);
}
I paint incrimentally when possible, and also double buffer. Are there other techniques?
Edited by: rerf on Aug 8, 2010 1:49 AM
Edited by: rerf on Aug 8, 2010 7:18 AM