Yeah, dual monitor support in Windows I’ve found to be very quirky whether its Java, my software DVD player, or playing Doom3 without first disabling the second monitor.
For what it’s worth though, it’s not too difficult to get things moving again when dragging your JFrame from one monitor to another. In my code I have:
public class GameParentFrame extends JFrame implements ComponentListener
{
private ArrayList deviceSpace = new ArrayList();
public void initView()
{
addComponentListener(this);
initGraphicsEnvironment();
}
public void initGraphicsEnvironment()
{
GraphicsDevice[] deviceList = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
for (int x=0;x<deviceList.length;x++)
{
GraphicsDevice gd = deviceList[x];
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle bounds = gc.getBounds();
if (bounds != null)
{
deviceSpace.add(bounds);
}
}
}
public Rectangle getDeviceLocation(GraphicsConfiguration gConfig)
{
System.out.println("Device origin= " + gConfig.getBounds().getX() + ", " + gConfig.getBounds().getY()); //$NON-NLS-1$ //$NON-NLS-2$
return gConfig.getBounds();
}
public void componentMoved(ComponentEvent e)
{
//check for spatial location and reset the tickers if necessary to prevent them from "freezing" on a device context change
Rectangle currentBounds = getBounds();
for (int x=0;x<deviceSpace.size();x++)
{
Rectangle deviceBounds = (Rectangle)deviceSpace.get(x);
if (deviceBounds.intersects(currentBounds))
{
if (deviceLocation != x)
{
deviceLocation = x;
view.notifyDeviceContextChanged();
}
}
}
}
}
and then in my view code where I extend Canvas and have a BufferStrategy setup I have:
public void deviceContextChanged()
{
trackComponent.resetDrawing();
}
public void resetDrawing()
{
recreateBufferStrategy();
}
public void recreateBufferStrategy()
{
if ((running == true) && (isVisible() == true))
{
if ((getHeight() > 0) && (getWidth() > 0))
{
createBufferStrategy(3);
strategy = getBufferStrategy();
}
}
}
So as the frame is dragged from window to window, it will recreate the buffer strategy and get the animation rolling again. It flickers a little while it’s in the middle of the two monitors while its being dragged but seems to work OK. Of course it would be better if it recreated the BufferStrategy only after the drag is complete, but I could never figure out how to get Java to notify me when the dragging of a JFrame is finished.