11package com .gustavolr .engine .window ;
22
3- import java .awt .Canvas ;
4- import java .awt .Graphics ;
53import java .awt .Dimension ;
4+ import java .awt .Frame ;
65import java .awt .image .BufferedImage ;
76import java .awt .image .BufferStrategy ;
7+ import java .awt .event .WindowEvent ;
8+ import java .awt .event .WindowListener ;
9+ import java .awt .Graphics ;
810
9- import javax .swing .JFrame ;
10-
11- public final class GameWindow extends Canvas {
12-
13- private static final long serialVersionUID = 1L ;
14-
15- private static JFrame windowFrame ;
11+ public final class GameWindow extends Frame implements WindowListener {
1612
1713 private static short width ;
1814 private static short height ;
1915 private static byte scale ;
2016
2117 public static BufferedImage bufferLayer ;
2218
19+ public boolean playerPressXtoQuit ;
20+
2321 public GameWindow (String frameName ) {
2422 this (GameWindowConstants .DEFAULT_WINDOW_WIDTH ,
2523 GameWindowConstants .DEFAULT_WINDOW_HEIGHT ,
@@ -28,10 +26,13 @@ public GameWindow(String frameName) {
2826 }
2927
3028 public GameWindow (int width , int height , int scale , String frameName ) {
29+ super (frameName );
3130 GameWindow .width = (short )width ;
3231 GameWindow .height = (short )height ;
3332 GameWindow .scale = (byte )scale ;
3433
34+ playerPressXtoQuit = false ;
35+
3536 this .initWindowFrame (frameName );
3637 }
3738
@@ -61,18 +62,15 @@ public BufferedImage getBufferedImage() {
6162
6263 public void initWindowFrame (String frameName ) {
6364
64- windowFrame = new JFrame (frameName );
65-
66- windowFrame .setResizable (false );
65+ setResizable (false );
6766
68- windowFrame .setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
69-
70- this .setPreferredSize (new Dimension (width *scale ,height *scale ));
71- windowFrame .add (this );
72- windowFrame .pack (); // The pack method sizes the frame so that all its contents are at or above their preferred sizes
67+ setPreferredSize (new Dimension (width *scale ,height *scale ));
7368
69+ pack (); // The pack method sizes the frame so that all its contents are at or above their preferred sizes
70+ addWindowListener (this );
7471 // Centralize the window to the center of the user screen
75- windowFrame .setLocationRelativeTo (null );
72+ setLocationRelativeTo (null );
73+ setLayout (null );
7674
7775 bufferLayer = new BufferedImage ((int )width ,(int )height , BufferedImage .TYPE_INT_RGB );
7876
@@ -82,7 +80,7 @@ public void initWindowFrame(String frameName) {
8280 }
8381
8482 public void toggleFrameVisibility (boolean visibility ) {
85- windowFrame .setVisible (visibility );
83+ this .setVisible (visibility );
8684 }
8785
8886 public Graphics getWindowLayer () {
@@ -106,11 +104,58 @@ public void drawFinalScene(Graphics g) {
106104 BufferStrategy bs = this .getBufferStrategy ();
107105
108106 g .dispose ();
109- g = bs .getDrawGraphics ();
110107
111- g .drawImage (bufferLayer , 0 , 0 , width * scale , height * scale , null );
108+ try {
109+ g = bs .getDrawGraphics ();
110+
111+ g .drawImage (bufferLayer , 5 , 29 , width * scale , height * scale , null );
112+
113+ bs .show ();
114+ } catch (Exception e ) {
115+ e .printStackTrace ();
116+ }
117+ }
118+
119+ @ Override
120+ public void windowActivated (WindowEvent arg0 ) {
121+ // TODO Auto-generated method stub
122+
123+ }
124+
125+ @ Override
126+ public void windowClosed (WindowEvent arg0 ) {
127+ // TODO Auto-generated method stub
128+
129+ }
130+
131+ @ Override
132+ public void windowClosing (WindowEvent arg0 ) {
133+ setVisible (false );
134+ dispose ();
135+ System .exit (0 );
136+ }
137+
138+ @ Override
139+ public void windowDeactivated (WindowEvent arg0 ) {
140+ // TODO Auto-generated method stub
141+
142+ }
143+
144+ @ Override
145+ public void windowDeiconified (WindowEvent arg0 ) {
146+ // TODO Auto-generated method stub
147+
148+ }
149+
150+ @ Override
151+ public void windowIconified (WindowEvent arg0 ) {
152+ // TODO Auto-generated method stub
153+
154+ }
155+
156+ @ Override
157+ public void windowOpened (WindowEvent arg0 ) {
158+ // TODO Auto-generated method stub
112159
113- bs .show ();
114160 }
115-
116161}
0 commit comments