AWT Canvas flickering on manual resize?

The key I believe is to use double buffering, and one way to possibly solve this is to use Swing which double buffers by default.

The key I believe is to use double buffering, and one way to possibly solve this is to use Swing which double buffers by default: import java.awt. *; import java.util. Random; import javax.swing.

*; public class FlickerSwing extends JPanel { public static void main(String args) { java.awt.EventQueue. InvokeLater(new Runnable() { public void run() { createAndShowGui(); } }); } private static void createAndShowGui() { JFrame f = new JFrame(str); f. SetDefaultCloseOperation(JFrame.

EXIT_ON_CLOSE); f. Add(new FlickerSwing()); f. SetLocationRelativeTo(null); f.pack(); f.

SetVisible(true); } private Color bgColor; private Color contentColor; Font f = new Font("Georgia", Font. BOLD, 16); static String str = "Swing Resize Flickering"; public FlickerSwing() { Random r = new Random(); bgColor = new Color(r. NextInt(256), r.

NextInt(256), r. NextInt(256)); contentColor = new Color(r. NextInt(256), r.

NextInt(256), r. NextInt(256)); setBackground(bgColor); } public Dimension getPreferredSize() { FontMetrics fm = getFontMetrics(f); return new Dimension(fm. StringWidth(str) + 20, fm.getHeight() + 10); } @Override protected void paintComponent(Graphics g) { super.

PaintComponent(g); g. SetColor(contentColor); g. SetFont(f); FontMetrics fm = g.

GetFontMetrics(f); int dx = getWidth() / 2 - (fm. StringWidth(str) / 2); int dy = getHeight() / 2 + (fm.getHeight() / 2); g. DrawString(str, dx, dy); } }.

1 @HFOE: +1 I sometimes make it explicit the super(true) constructor. – trashgod Mar 27 at 21:45.

You can add this to the beginning of your main method to avoid the background flicker: System. SetProperty("sun.awt. Noerasebackground", "true").

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions