public class GameFrame extends JFrame implements ExitController, RectangleComponent{
private static final long serialVersionUID = 1L;
private int width = 600;
private int height = 600;
public static enum ConstructorPar{
DECORATED,
UNDECORATED
}
// drawing context
private JPanel jContentPane = null;
public CompositePainter painters = new CompositePainter();
/**
* This is the default constructor
*/
public GameFrame() {
super();
initializeWindowed();
}
public GameFrame(ConstructorPar par) {
super();
if(par==ConstructorPar.UNDECORATED){
initializeWindowed();
}
if(par == ConstructorPar.DECORATED){
initializeWindowedDecorated();
}
}
/**
* This initilalizes the component in windowed mode
*
* @return void
*/
private void initializeWindowed() {
this.setSize(this.width, this.height);
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
this.setContentPane(getJContentPane());
this.setUndecorated(true);
this.setVisible(true);
this.setLocation(new java.awt.Point(0, 0));
this.setIgnoreRepaint(true);
this.setResizable(false);
this.createBufferStrategy(2);
}
/**
* This initilalizes the component in windowed mode
*
* @return void
*/
private void initializeWindowedDecorated() {
this.setSize(this.width, this.height);
this.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
this.setContentPane(getJContentPane());
this.setUndecorated(false);
this.setVisible(true);
this.setLocation(new java.awt.Point(0, 0));
this.setIgnoreRepaint(true);
this.setResizable(false);
this.createBufferStrategy(2);
}
/**
* Sets the GameFrame fullscreen with the specified width,height,depth and refreshrate
* @param width is the width of the fullscreen gameframe
* @param height is the width of the fullscreen gameframe
* @param depth is the color value you want to have in fullscreen mode
* @param refreshRate is the amount of times the screen is refreshed in one second
*/
public void setFullScreen(int width,int height,int depth,int refreshRate) {
setFullScreen(new DisplayMode(width,height,depth,refreshRate));
}
/**
* Accepts a DisplayMode and sets the gameFrame to the specified DisplayMode
* Note that this method presumes the frame is undecorated and not resizable
* this is being taken care of in the method initializeWindowed at the creation
* of this GameFrame
* @param displayMode is the DisplayMode we want to use
*/
public void setFullScreen(DisplayMode displayMode) {
try {
this.getGraphics2D().setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
getGraphicsDevice().setFullScreenWindow(this);
getGraphicsDevice().setDisplayMode(displayMode);
this.width=displayMode.getWidth();
this.height=displayMode.getHeight();
} catch (Exception e) {
//TODO : Message sturen dat FullScreen mislukt is, Daan slaagt me niet dood voor de aanpassing :-)
setWindowed();
}
}
/**
* Returns the used graphicsDevice
* @return the GraphicsDevice used
*/
private GraphicsDevice getGraphicsDevice() {
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
return environment.getDefaultScreenDevice();
}
/**
* Sets the frame back to it's previous condition
*/
public void setWindowed() {
getGraphicsDevice().setFullScreenWindow(null);
}
public DisplayMode[] getAllModes(){
return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayModes();
}
public boolean isValidDisplayMode(DisplayMode mode){
if (mode == null){
return false;
}
for(DisplayMode modey: this.getAllModes()){
if(modey.equals(mode)){
return true;
}
}
return false;
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(new BorderLayout());
}
return jContentPane;
}
/**
* Returns the graphic context
*
* @return de drawing context van het frame
*/
public Graphics2D getGraphics2D() {
return (Graphics2D) this.getBufferStrategy().getDrawGraphics();
}
/**
* This makes the page-flipping happen. Should be called every game-frame
*
*/
private void update() {
BufferStrategy strategy = this.getBufferStrategy();
if (!strategy.contentsLost()) {
strategy.show();
}
}
public void update(long elapsedTime) {
if(this.height!= super.getHeight()||this.width!= super.getWidth()){
this.width = super.getWidth();
this.height = super.getHeight();
GameThread.getInstance().getMessageController().sendMessage(this, new java.awt.Dimension(this.width,this.height), "frame");
}
Graphics2D g = this.getGraphics2D();
painters.paint(g);
this.update();
}
/**
* utility method for drawing images
*
* @param g
* @param image
* @param x
* @param y
*/
public void drawImage(Graphics2D g, Image image, int x, int y) {
g.drawImage(image, x, y, null);
}
public void drawImage(Graphics2D g, Image image, int x, int y, int width, int height, double direction){
//get a rotation
AffineTransform at = AffineTransform.getRotateInstance(direction, x + width / 2, y+ height / 2);
// add a translation so the image stays on it's position
at.translate(x, y);
g.drawImage(image, at, null);
}
/**
* Return the centre of the drawing component
*/
public Point getMidpoint() {
return new Point(this.width / 2, this.height / 2);
}
public void cleanUp() {
this.setWindowed();
}
public static DisplayMode getCurrentDisplayMode(){
return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode();
}
}