Skip to main content

BufferStrategy not working on Windows?

5 replies [Last post]
afishionado
Offline
Joined: 2004-05-26
Points: 0

Hello,

Stupid question time. :-)

Why does the following code (correctly) display a black screen with white numbers under Linux, but just grey under Windows XP? Tested on Java 1.4 and 5 under Windows, Java 1.4, 5, and 6 under Linux (it even works in Kaffe!).

I know I'm missing something obvious. :-P

Thanks!

William

[pre]import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

public class BufferTest {
public static void main(String args[]) {
JFrame frame;
Canvas canvas;
GraphicsDevice device;
BufferStrategy strategy;
DisplayMode mode;

canvas = new Canvas();
frame = new JFrame();
frame.setUndecorated(true);

canvas.setIgnoreRepaint(true);

device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
mode = device.getDisplayMode();

frame.setSize(mode.getWidth(), mode.getHeight());
frame.getContentPane().add(canvas);
device.setFullScreenWindow(frame);
frame.show();

try {
canvas.createBufferStrategy(2);
strategy = canvas.getBufferStrategy();

for (int counter = 100; counter >= 0; --counter) {
Graphics g = strategy.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, mode.getWidth(), mode.getHeight());
g.setColor(Color.WHITE);
g.drawString(Integer.toString(counter), mode.getWidth() / 2, mode.getHeight() / 2);
g.dispose();
strategy.show();
Toolkit.getDefaultToolkit().beep();
Thread.sleep(100);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
device.setFullScreenWindow(null);
System.exit(0);
}
}
}
[/pre]

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Dmitri Trembovetski

Hi there,

> Stupid question time. :-)

Not so stupid, actually. This is because of our bug, which I happy
to report was fixed in mustang.

To work around it, don't use Canvas, just create the BufferStrategy
off your frame.

Also, you might want to use Frame instead of JFrame - you don't want
to bring Swing in if you don't use it..

Thanks,
Dmitri
Java2D Team

On Tue, Jul 25, 2006 at 02:37:40PM -0700, java2d@javadesktop.org wrote:
> Hello,
>
> Stupid question time. :-)
>
> Why does the following code (correctly) display a black screen with white numbers under Linux, but just grey under Windows XP? Tested on Java 1.4 and 5 under Windows, Java 1.4, 5, and 6 under Linux (it even works in Kaffe!).
>
> I know I'm missing something obvious. :-P
>
> Thanks!
>
> William
>
> [pre]import java.awt.*;
> import java.awt.image.*;
> import javax.swing.*;
>
> public class BufferTest {
> public static void main(String args[]) {
> JFrame frame;
> Canvas canvas;
> GraphicsDevice device;
> BufferStrategy strategy;
> DisplayMode mode;
>
> canvas = new Canvas();
> frame = new JFrame();
> frame.setUndecorated(true);
>
> canvas.setIgnoreRepaint(true);
>
> device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
> mode = device.getDisplayMode();
>
> frame.setSize(mode.getWidth(), mode.getHeight());
> frame.getContentPane().add(canvas);
> device.setFullScreenWindow(frame);
> frame.show();
>
> try {
> canvas.createBufferStrategy(2);
> strategy = canvas.getBufferStrategy();
>
> for (int counter = 100; counter >= 0; --counter) {
> Graphics g = strategy.getDrawGraphics();
> g.setColor(Color.BLACK);
> g.fillRect(0, 0, mode.getWidth(), mode.getHeight());
> g.setColor(Color.WHITE);
> g.drawString(Integer.toString(counter), mode.getWidth() / 2, mode.getHeight() / 2);
> g.dispose();
> strategy.show();
> Toolkit.getDefaultToolkit().beep();
> Thread.sleep(100);
> }
> } catch (Exception e) {
> e.printStackTrace();
> } finally {
> device.setFullScreenWindow(null);
> System.exit(0);
> }
> }
> }
> [/pre]
> [Message sent by forum member 'afishionado' (afishionado)]
>
> http://forums.java.net/jive/thread.jspa?messageID=136491
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

fred34
Offline
Joined: 2004-06-19
Points: 0

Damn it, I was just about to chime in with the exact same fix!

Yeh ditch the canvas thing completely. Just use the original frame.

Dmitri Trembovetski

Well, if he were to just change JFrame to Frame (no
lightweight/heavyweight issues), it still wouldn't work because of
the (fullscreen/BufferStrategy-related) bug. But anyway, the
workaround is indeed to ditch Canvas.

Thanks,
Dmitri
Java2D Team

On Tue, Jul 25, 2006 at 04:43:47PM -0700, java2d@javadesktop.org wrote:
> Damn it, I was just about to chime in with the exact same fix!
>
> Yeh ditch the canvas thing completely. Just use the original frame.
> [Message sent by forum member 'fred34' (fred34)]
>
> http://forums.java.net/jive/thread.jspa?messageID=136539
>
> ===========================================================================
> To unsubscribe, send email to listserv@java.sun.com and include in the body
> of the message "signoff JAVA2D-INTEREST". For general help, send email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

afishionado
Offline
Joined: 2004-05-26
Points: 0

Shoot, I figured it out about an hour after I posted, and couldn't get online until now.

As a footnote, on Windows GraphicsDevice.getDefaultConfiguration().getBufferCapabilities().isFullScreenRequred() returns true. That was what finally clued me in to the problem.

afishionado
Offline
Joined: 2004-05-26
Points: 0

AS a footnote to this, the reason I was using a frame was because I couldn't figure out how to get keyboard focus otherwise.

The solution I found was to add the key listener to the parent frame, rather than the window that is drawing.

William