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();
}
}
}
}