package com.betato.gamedisplay;

import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

public class Frame extends JFrame{
	private static final long serialVersionUID = 1L;
	
	private GameWindow gameWindow;
	private int defaultWidth;
	private int defaultHeight;
	
	public Frame(GameWindow gameWindow, String title, int width, int height, boolean useContentSize, boolean resizable, boolean fullscreen, boolean hideCursor) {
		setup(gameWindow, title, width, height);
		
		if (useContentSize) {
			setContentSize(width, height);
		} else {
			setSize(width, height);
		}
		
		setResizable(resizable);
		if (fullscreen) {
			setUndecorated(true);
			setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
		}
		if (hideCursor) {
			setCursorHidden(true);
		}
	}
	
	public Frame(GameWindow gameWindow, int width, int height, String title) {
		setup(gameWindow, title, width, height);
		
		setSize(width, height);
		setResizable(false);
	}
	
	private void setup(GameWindow gameWindow, String title, int width, int height) {
		defaultWidth = width;
		defaultHeight = height;
		
		this.gameWindow = gameWindow;
		add(gameWindow);
		
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setTitle(title);
	}
	
	public void setFullscreen(boolean fullscreen) {
		if (isUndecorated() == fullscreen) {
			return;
		}
		// Dispose window to decorate
		dispose();
		setUndecorated(fullscreen);
		if (fullscreen) {
			// Set window full screen
			setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
		} else {
			// Set window to defaults
			setSize(defaultHeight, defaultWidth);
		}
		// Show window
		add(gameWindow);
		setVisible(true);
	}
	
	public void setCursorHidden(boolean hideCursor){ 			
		if (hideCursor) { 			
			// Create a new blank cursor
			BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
			Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
					cursorImg, new Point(0, 0), "blank cursor"); 			
			// Set the cursor
			getContentPane().setCursor(blankCursor);
		} else {
			// Set default cursor
			getContentPane().setCursor(Cursor.getDefaultCursor());
		}
	}
	
	public void setContentSize(int width, int height) {
		// Set content size and pack
		getContentPane().setPreferredSize(new Dimension(width, height));
		pack();
	}
}

@@ -323,22 +323,19 @@
 ate int 
-defaul
+las
 tWidth;%0A
@@ -347,22 +347,19 @@
 ate int 
-defaul
+las
 tHeight;
@@ -1125,22 +1125,19 @@
 ht) %7B%0A%09%09
-defaul
+las
 tWidth =
@@ -1146,22 +1146,19 @@
 idth;%0A%09%09
-defaul
+las
 tHeight 
@@ -1403,24 +1403,138 @@
 return;%0A%09%09%7D%0A
+%09%09// Store size before full screen%0A%09%09if (fullscreen)%7B%0A%09%09%09lastWidth = getWidth();%0A%09%09%09lastHeight = getHeight();%0A%09%09%7D%0A
 %09%09// Dispose
@@ -1710,16 +1710,36 @@
 _BOTH);%0A
+%09%09%09add(gameWindow);%0A
 %09%09%7D else
@@ -1777,43 +1777,57 @@
 %0A%09%09%09
-setSize(defaultHeight, defaultWidth
+add(gameWindow);%0A%09%09%09setSize(lastWidth, lastHeight
 );%0A%09
@@ -1846,35 +1846,16 @@
  window%0A
-%09%09add(gameWindow);%0A
 %09%09setVis

Fix window returning to incorrect size after full screen