How to avoid Java window flicker with border while resizing?

5Y5T3M

New Member
\[code\]import javax.swing.*;public class Main { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { createMainWindow(); } }); } private static void createMainWindow() { JFrame mainFrame = new JFrame("Flicker Teste"); mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainFrame.add(new MainPanel()); mainFrame.pack(); mainFrame.setSize(400, 400); mainFrame.setVisible(true); }}\[/code\]Main.java\[code\]import javax.swing.*;import java.awt.*;public class MainPanel extends JPanel { public MainPanel() { setBorder(BorderFactory.createLineBorder(Color.red)); //If I comment this line the diagonal is streteched on the border. setBackground(Color.green); } public void paintComponent(Graphics g) { super.paintComponent(g); g.drawLine(0, 0, 5000, 5000); }}\[/code\]MainPanel.javaThese two file are the code to reproduce the resize flicker bug.When you do a fast resize the border is repeated resulting in a horrible flicker.And if there is no border then the last pixel of the diagonal line which touch the edge of the window isrepeated, as if the "blank border" consists of repeating the pixels at the edge.Someone know how to stop this horrible flickering?I already searched for solutions, but I have found no one.
 
Back
Top