We would like to know how to redraw JFrame lag/pop-in.
import java.awt.Color; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.util.Random; /*ww w. j a v a 2 s .c o m*/ import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.Timer; public class Main extends JPanel { static Color[] COLORS = { Color.blue, Color.red, Color.yellow, Color.orange, Color.green, Color.cyan, Color.lightGray, Color.magenta, Color.white, Color.black }; static Icon[] ICONS = new Icon[COLORS.length]; static int ROWS = 20; static int COLS = 30; static int BI_WIDTH = 20; static int BI_HEIGHT = BI_WIDTH; static int TIMER_DELAY = 15; static int NUMBER_TO_SWAP = 15; JLabel[][] grid = new JLabel[ROWS][COLS]; Random random = new Random(); static { for (int i = 0; i < ICONS.length; i++) { BufferedImage img = new BufferedImage(BI_WIDTH, BI_HEIGHT, BufferedImage.TYPE_INT_ARGB); Graphics g = img.getGraphics(); g.setColor(COLORS[i]); g.fillRect(0, 0, BI_WIDTH, BI_HEIGHT); g.dispose(); ICONS[i] = new ImageIcon(img); } } public Main() { setLayout(new GridLayout(ROWS, COLS)); for (int row = 0; row < grid.length; row++) { for (int col = 0; col < grid[row].length; col++) { JLabel label = new JLabel(); int index = random.nextInt(COLORS.length); label.setIcon(ICONS[index]); add(label); grid[row][col] = label; } } new Timer(TIMER_DELAY, new ActionListener() { @Override public void actionPerformed(ActionEvent evt) { for (int i = 0; i < NUMBER_TO_SWAP; i++) { int row = random.nextInt(ROWS); int col = random.nextInt(COLS); int iconIndex = random.nextInt(ICONS.length); grid[row][col].setIcon(ICONS[iconIndex]); } } }).start(); } public static void main(String[] args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new Main()); frame.pack(); frame.setVisible(true); } }