Need some help with double buffer drawing

B

Builder Chad

Hi, I am trying to create a fullscreen app that has some widgets and a
canvas with a double buffer where I will draw a tile based map.

My preliminary code is cobbled together from examples from various
books. The problem is that all the examples only have code where the
whole screen is used in the doublwe buffer update. I need a smaller
area for the updates and am trying to use a canvas but it doesnt seem to
work.

In my update loop I am flood filling the image with red but it never
appears. Any help would be great.

Here is the code:


import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;

public class AWTMixed2 {

AWTMixed2 () {

//Determine if full-screen mode is supported directly
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
GraphicsConfiguration gc = gd.getDefaultConfiguration();
BufferCapabilities bufCap = gc.getBufferCapabilities();

if (gd.isFullScreenSupported()) {
System.out.println("Real FS");
} else {
System.out.println("Simulated FS");
}

//Create a button that leaves full-screen mode
Button btn = new Button("Exit");
TextArea ta = new TextArea("Needs text editing here...");
TextArea tb = new TextArea("Another frame");

ta.setEditable(true);
ta.setIgnoreRepaint(true);
ta.setEnabled(true);
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
// Return to normal windowed mode
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
gd.setFullScreenWindow(null);
System.exit(0);
}
});

//Create a window for full-screen mode; add a button to leave
full-screen mode
Frame frame = new Frame(gc);

frame.setUndecorated(true); // no decorations aka title bar and
buttons....
frame.add(btn, BorderLayout.NORTH);
frame.add(ta, BorderLayout.SOUTH);

try {
//Enter full-screen mode
gd.setFullScreenWindow(frame);

Canvas canv = new Canvas();
canv.setSize(100,100);
frame.add(canv,BorderLayout.CENTER);

int numBuffers = 2;
canv.createBufferStrategy(numBuffers);

BufferStrategy strategy = canv.getBufferStrategy();
bufCap = strategy.getCapabilities();
BufferCapabilities.FlipContents flipContents =
bufCap.getFlipContents();
if
(flipContents.equals(BufferCapabilities.FlipContents.UNDEFINED)) {
// The contents is unknown after a flip
} else if
(flipContents.equals(BufferCapabilities.FlipContents.BACKGROUND)) {
// The contents cleared to the component's background
color after a flip
} else if
(flipContents.equals(BufferCapabilities.FlipContents.PRIOR)) {
// The contents is the contents of the front buffer just
before the flip
} else if
(flipContents.equals(BufferCapabilities.FlipContents.COPIED)) {
// The contents is identical to the contents just pushed
to the
// front buffer after a flip
}

boolean flip = false;
int screenWidth = canv.getWidth();
int screenHeight = canv.getHeight();

// Draw loop
while (true) {
// Get graphics context for drawing to the window
Graphics g = strategy.getDrawGraphics();

if
(!flipContents.equals(BufferCapabilities.FlipContents.BACKGROUND)) {
// Clear background
g.setColor(Color.red);
g.fillRect(0, 0, screenWidth, screenHeight);
}

// Done drawing
g.dispose();

// Flip the back buffer to the screen
strategy.show();
}

} catch (Throwable e) {
// Process exception...
System.out.println("error!!");
} finally {
//Exit full-screen mode
gd.setFullScreenWindow(null);
System.out.println("fullscreen not supported...");
}
}
}


Thanks for any help in advance :)
 
B

Boudewijn Dijkstra

Builder Chad said:
Hi, I am trying to create a fullscreen app that has some widgets and a
canvas with a double buffer where I will draw a tile based map.

My preliminary code is cobbled together from examples from various books.
The problem is that all the examples only have code where the whole screen
is used in the doublwe buffer update. I need a smaller area for the updates
and am trying to use a canvas but it doesnt seem to work.

Canvas is to be used for direct drawing. On some systems, it does not even
allow buffering, IIRC. So ditch the Canvas and use a Component.
 
B

Builder Chad

Boudewijn said:
Canvas is to be used for direct drawing. On some systems, it does not even
allow buffering, IIRC. So ditch the Canvas and use a Component.

Oh I see, thanks, you've saved me many hours of frustration :) I will
look into using a component.

Builder Chad.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top