public void configure() {
Frame frame = ctx.getFrame();
frame.setTitle("AstroCycles | By: Carlos Aviles");
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setFocusable(true);
frame.setSize(WIDTH, HEIGHT);
frame.addKeyListener(ctx.getKeyEventDispatcher());
frame.addWindowListener(ctx.getWindowEventDispatcher());
frame.addMouseListener(ctx.getMouseEventDispatcher());
frame.setVisible(true);
frame.createBufferStrategy(3);
frame.getBufferStrategy().getDrawGraphics().setColor(Color.RED);
frame.getBufferStrategy().getDrawGraphics().fillRect(0, 0, 75, 75);
frame.getBufferStrategy().getDrawGraphics().dispose();
frame.getBufferStrategy().show();
}
The frame is showing, but the rectangle (red) with the requested coordinates and size are not showing on the frame. I have no idea why it's not drawing. The console is not throwing any errors. The BufferStrategy is also not null.
So, two things jump out, one, getBufferStrategy
will return the next buffer to be used, so you are changing properties on different buffers.
Two, there is a race condition between you drawing on the buffer and the window been painted again by the native peer, overwriting your buffer's contents.
This example basically sets up a infinite update loop which repaints the buffer, you can slow down the delay and you can see it change from one state to another
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Test {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setTitle("AstroCycles | By: Carlos Aviles");
frame.setLocationRelativeTo(null);
frame.setFocusable(true);
frame.setSize(100, 100);
frame.setBackground(Color.BLUE);
frame.setVisible(true);
frame.createBufferStrategy(3);
do {
BufferStrategy bs = frame.getBufferStrategy();
while (bs == null) {
System.out.println("buffer");
bs = frame.getBufferStrategy();
}
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
System.out.println("draw");
Graphics graphics = bs.getDrawGraphics();
// Render to graphics
// ...
graphics.setColor(Color.RED);
graphics.fillRect(0, 0, 100, 100);
// Dispose the graphics
graphics.dispose();
// Repeat the rendering if the drawing buffer contents
// were restored
} while (bs.contentsRestored());
System.out.println("show");
// Display the buffer
bs.show();
// Repeat the rendering if the drawing buffer was lost
} while (bs.contentsLost());
System.out.println("done");
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
}
} while (true);
}
}
A better solution would be to use a Canvas
and add this to the frame and use it's BufferStrategy
, this will prevent you from painting under the frame's borders and provide you with a better idea of the actual viewable space you can paint
来源:https://stackoverflow.com/questions/34689176/bufferstrategy-not-showing-on-java-awt-frame