I am having difficulty getting Canvas to work in windowed mode. Everytime I run I only get the gray background of the canvas instead of what should be rendered. I know the render code is running because I put a some output statements just before it was run. No exceptions appear to be thrown as I am catching Exception (in several locations) and printing the stacktrace.
First: I have gotten exclusive mode to work perfectly, however for that I am creating the buffer strategy directly from the Frame and not from a Canvas. In exclusive mode I don’t even use a Canvas. I would prefer not to use a Canvas but i have found that when in windowed mode (and you have frame decorations on) the graphics context is NOT offset due to the decorations. IE: 0,0 on the graphics context is actually UNDERNEATH the decorations (very bad). This is why I need to use Canvas to create the buffer strategy. If there is a way to fix it so the graphics context is adjusted for the frame decorations please let me know.
The only reason I am doing a windowed mode implementation is 1) completness 2) So I can debug rendering and data issues and still see the console.
Sorry I can’t post all the code but with all the added features it’s getting large.
So here is how I am initializing the windowed frame and rendering to it.
_win is my Frame.
_can = new Canvas();
_can.setIgnoreRepaint(true);
_can.setFocusTraversalKeysEnabled(false);
_can.setFocusable(false);
_can.setSize(WIDTH, HEIGHT);
_can.setVisible(true);
_win.add(_can);
_win.pack();
// center on screen
_win.setLocationRelativeTo(null);
_win.setVisible(true);
_can.createBufferStrategy(2);
After this is created I can begin rendering which goes as follows:
private void render() {
if (!_stop) {
Graphics2D g2d = null;
// check to see if the buffers still exist.
if (_bufferStrategy.contentsLost()) {
// recreate the buffers.
_bufferStrategy = createBufferStrategy();
}
g2d = (Graphics2D) _bufferStrategy.getDrawGraphics();
if (g2d != null) {
// clear the surface for the next frame
Rectangle bounds = _gContainer.getBounds();
g2d.clearRect(bounds.x, bounds.y, bounds.width, bounds.height);
// set the rendering options.
g2d.setRenderingHints(_renderingHints);
// class that contains the render code NOT the canvas.
_gContainer.render(g2d);
try {
_bufferStrategy.show();
}
catch (Exception e) {
e.printStackTrace();
}
g2d.dispose();
}
}
}
Thanks for any help.