I've been struggling to get this to work. The only solution (but terrible solution) I found was re creating a new window every frame which is horrible for performance and other obvious reasons. Here is what I have but I'm unsure about how I can go about redrawing every frame. Or if there's another approach I can take to achieve the same functionality, how can I go about doing it?
public static Window w = null;
@SuppressWarnings("serial")
private static void initializeWindow(final Dimension d) {
int windowHeight = d.height - 1;
w = new Window(null) {
@Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
//painting goes here (but it only seems to draw for one frame)
//for some repainting doesn't seem to work
}
@Override
public void update(Graphics g) {
paint(g);
System.out.println(true);
}
};
w.setAlwaysOnTop(true);
w.setBounds(0, 0, d.width, windowHeight);
w.setBackground(new Color(0, true));
w.setVisible(true);
}
I tried the code block attached and I expected to be able to update the window every frame with newly painted stuff.
You problem is pretty basic - you need some way to update the UI on a regular bases.
Your problem is further complicated by the fact that Swing is single thread and NOT thread safe. Lucky for you, the Swing team fore saw this issue and provided the Swing
Timerclass.Start by seeing Concurrency in Swing and How to Use Swing Timers for more details.