0

I am building an application with some swing drawings, but after the screen sleeps, it can take up to 30 seconds after waking to begin showing changes. Even a JLabel on the screen stays frozen.

I took all my repaint()'s out of the code to check to see if that was the issue - no help with the JLabel.

I am calling repaint() from an external threaded timer using the Timer class and in the main drawing routines.

If I move the window, it starts repainting instantly.

Any suggestions on how to 'wake' the repainting quicker in code would be greatly appreciated.

Even this simple code locks up the painting on wake from screen sleep, what's wrong with it? Thanks.

package waketest;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;

public class Wake extends JFrame implements Runnable  {

    private JPanel contentPane;
    private JLabel label;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Wake frame = new Wake();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                
        }});
    }

    public Wake() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        
        label = new JLabel("New label");
        label.setFont(new Font("Tahoma", Font.PLAIN, 20));
        contentPane.add(label, BorderLayout.CENTER);
        
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void run() {
        int x=0;
        while(true) {
            x++;
            label.setText(String.valueOf(x));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }
    }   
}
MartinH
  • 33
  • 3
  • 1
    You need to include more specifics, since there's no way we can figure out what's going wrong. Swing does not have these weird behaviour after a sleep screen. – Just another Java programmer Apr 23 '22 at 16:29
  • Common causes of such rendering artifact are outlined [here](https://stackoverflow.com/a/11389042/230513). For specific guidance, please [edit] your question to include a [mre] that exhibits the problem you describe. – trashgod Apr 23 '22 at 16:52
  • I tested a very simple sample which has the same results - painting freezes after screen wake. I don't know why. It's been added to my initial. – MartinH Apr 23 '22 at 23:18
  • Does this answer your question? [Why does my custom Swing component repaint faster when I move the mouse? (Java)](https://stackoverflow.com/questions/57948299/why-does-my-custom-swing-component-repaint-faster-when-i-move-the-mouse-java) – Progman Apr 23 '22 at 23:22

0 Answers0