I have been searching around for the answer to this problem but have come of with little information on how to solve the problem. What I am looking to do is be able to use Graphics2D to do all the graphics I need, within a window. I am not very lenient on the use of Graphics2D and a BufferStrategy because I have a large amount of existing code that uses these to make a full screen window using the computers GraphicsDevice. This is a test that I made but there is something that I am missing.
public static void main(String[] args) {
//Creates a frame and sets properties
JFrame frame = new JFrame("FrameDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setResizable(true);
frame.setVisible(true);
frame.createBufferStrategy(2);
//Gets Graphics2D from the bufferstrategy
BufferStrategy s = frame.getBufferStrategy();
Graphics2D g = (Graphics2D)s.getDrawGraphics();
//Draws a background and a line for testing
g.setColor(Color.GRAY);
g.drawRect(0, 0, 500, 500);
g.setColor(Color.BLACK);
g.drawLine(50, 50, 200, 50);
//Displays the graphics to the frame
frame.update(g);
g.dispose();
s.show();
}
When run this only creates an empty frame that is set to the correct size and produces no errors but the line and background are not displayed.
My guess is the problem stems from the last three lines of code where the frame is updated. My confusion is how to display the Graphics2D components when using the BufferStategy... Do you still have to update the frame or do you just need to show the BufferStategy? Any help would be much appreciated and thank you in advance.
Using the example on http://docs.oracle.com/javase/7/docs/api/java/awt/image/BufferStrategy.html I made up this frame example.
public class BufferedStrategyTest extends JFrame implements Runnable, WindowListener {
private Thread graphicsThread;
private boolean running = false;
private BufferStrategy strategy;
public BufferedStrategyTest() {
super("FrameDemo");
addWindowListener(this);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setSize(500, 500);
setResizable(true);
setVisible(true);
createBufferStrategy(2);
strategy = getBufferStrategy();
running = true;
graphicsThread = new Thread(this);
graphicsThread.start();
}
public static void main(String[] args) {
new BufferedStrategyTest();
}
public void addNotify() {
super.addNotify();
createBufferStrategy(2);
strategy = getBufferStrategy();
}
@Override
public void run() {
// Main loop
while (running) {
// Prepare for rendering the next frame
// ...
// Render single frame
do {
// The following loop ensures that the contents of the drawing buffer
// are consistent in case the underlying surface was recreated
do {
// Get a new graphics context every time through the loop
// to make sure the strategy is validated
Graphics g = strategy.getDrawGraphics();
g.setColor(Color.GRAY);
g.drawRect(0, 0, 500, 500);
g.setColor(Color.BLACK);
g.drawLine(50, 50, 200, 50);
// Dispose the graphics
g.dispose();
// Repeat the rendering if the drawing buffer contents
// were restored
} while (running && strategy.contentsRestored());
// Display the buffer
strategy.show();
// Repeat the rendering if the drawing buffer was lost
} while (running && strategy.contentsLost());
}
setVisible(false);
dispose();
}
@Override
public void windowActivated(WindowEvent e) {}
@Override
public void windowClosed(WindowEvent e) {}
@Override
public void windowClosing(WindowEvent e) {
running = false;
}
@Override
public void windowDeactivated(WindowEvent e) {}
@Override
public void windowDeiconified(WindowEvent e) {}
@Override
public void windowIconified(WindowEvent e) {}
@Override
public void windowOpened(WindowEvent e) {}
}