50
50
51
51
public class MainWindow {
52
52
53
- private static final Dimension PREFERRED_SIZE = new Dimension (640 , 480 );
53
+ public static final int BUFFER_WIDTH = 640 ;
54
+ public static final int BUFFER_HEIGHT = 480 ;
54
55
55
56
private static final BufferedImage PAUSE_BUTTON = Images .loadImage ("gui/pause" );
56
57
private static final BufferedImage BACKGROUND = Images .loadImage ("gui/back_game" );
@@ -79,7 +80,7 @@ public MainWindow() {
79
80
+ Stepfish .GAME_NAME + " " + Stepfish .GAME_VERSION + ")" );
80
81
81
82
theFrame .setContentPane (contentPane = new CustomContentPane ());
82
- contentPane .setPreferredSize (PREFERRED_SIZE );
83
+ contentPane .setPreferredSize (new Dimension ( BUFFER_WIDTH , BUFFER_HEIGHT ) );
83
84
theFrame .addWindowListener (new WindowAdapter () {
84
85
@ Override
85
86
public void windowClosing (WindowEvent e ) {
@@ -90,22 +91,25 @@ public void windowClosing(WindowEvent e) {
90
91
91
92
@ Override
92
93
public void mousePressed (MouseEvent e ) {
94
+ Point mouseLocation = getMouseLocation ();
93
95
if (openGui == null ) {
94
96
if (e .getButton () == MouseEvent .BUTTON1 ) {
95
- if (e . getX () >= 2 && e . getY () >= 2 && e . getX () < 2 + PAUSE_BUTTON . getWidth ()
96
- && e . getY () < 2 + PAUSE_BUTTON .getHeight ()) {
97
+ if (mouseLocation . x >= 2 && mouseLocation . y >= 2
98
+ && mouseLocation . x < 2 + PAUSE_BUTTON . getWidth () && mouseLocation . y < 2 + PAUSE_BUTTON .getHeight ()) {
97
99
openGui (new GuiPauseMenu ());
98
100
}
99
101
}
100
102
} else {
101
- openGui .mousePressed (e . getX (), e . getY () , e .getButton ());
103
+ openGui .mousePressed (mouseLocation . x , mouseLocation . y , e .getButton ());
102
104
}
103
105
}
104
106
105
107
@ Override
106
108
public void mouseReleased (MouseEvent e ) {
107
- if (openGui != null )
108
- openGui .mouseReleased (e .getX (), e .getY (), e .getButton ());
109
+ if (openGui != null ) {
110
+ Point mouseLocation = getMouseLocation ();
111
+ openGui .mouseReleased (mouseLocation .x , mouseLocation .y , e .getButton ());
112
+ }
109
113
}
110
114
111
115
});
@@ -130,7 +134,6 @@ public void focusLost(FocusEvent e) {
130
134
}
131
135
});
132
136
contentPane .requestFocusInWindow ();
133
- theFrame .setResizable (false );
134
137
theFrame .pack ();
135
138
theFrame .setLocationRelativeTo (null );
136
139
contentPane .requestFocus ();
@@ -292,6 +295,29 @@ public void redraw() {
292
295
theFrame .repaint ();
293
296
}
294
297
298
+ private void draw (Graphics g ) {
299
+ if (openGui == null || openGui .shouldDrawLevelBackground ()) {
300
+ g .drawImage (BACKGROUND , 0 , 0 , BUFFER_WIDTH , BUFFER_HEIGHT , null );
301
+
302
+ synchronized (objects ) {
303
+ for (GameObject object : objects ) {
304
+ object .draw (g );
305
+ }
306
+ }
307
+ }
308
+
309
+ if (openGui == null ) {
310
+ g .drawImage (PAUSE_BUTTON , 2 , 2 , null );
311
+ Point mousePos = getMouseLocation ();
312
+ if (mousePos .x >= 2 && mousePos .y >= 2 && mousePos .x < 34 && mousePos .y < 34 ) {
313
+ g .setColor (Color .WHITE );
314
+ g .drawRect (2 , 2 , 32 , 32 );
315
+ }
316
+ } else {
317
+ openGui .drawScreen (g );
318
+ }
319
+ }
320
+
295
321
public void updateTick () {
296
322
musicCooldown --;
297
323
if (musicCooldown == 0 ) {
@@ -455,7 +481,7 @@ private void openGuiDangerously(Gui gui) {
455
481
this .paused = false ;
456
482
} else {
457
483
this .paused = gui .pausesGame ();
458
- gui .validate (contentPane . getWidth (), contentPane . getHeight () );
484
+ gui .validate (BUFFER_WIDTH , BUFFER_HEIGHT );
459
485
}
460
486
}
461
487
@@ -468,39 +494,53 @@ public Point getMouseLocation() {
468
494
Point compLocation = contentPane .getLocationOnScreen ();
469
495
mouseLocation .x -= compLocation .x ;
470
496
mouseLocation .y -= compLocation .y ;
497
+ if (contentPane .isWidthControlling ()) {
498
+ int height = BUFFER_HEIGHT * contentPane .getWidth () / BUFFER_WIDTH ;
499
+ mouseLocation .y -= contentPane .getHeight () / 2 - height / 2 ;
500
+ mouseLocation .x = mouseLocation .x * BUFFER_WIDTH / contentPane .getWidth ();
501
+ mouseLocation .y = mouseLocation .y * BUFFER_HEIGHT / height ;
502
+ } else {
503
+ int width = BUFFER_WIDTH * contentPane .getHeight () / BUFFER_HEIGHT ;
504
+ mouseLocation .x -= contentPane .getWidth () / 2 - width / 2 ;
505
+ mouseLocation .x = mouseLocation .x * BUFFER_WIDTH / width ;
506
+ mouseLocation .y = mouseLocation .y * BUFFER_HEIGHT / contentPane .getHeight ();
507
+ }
471
508
return mouseLocation ;
472
509
}
473
510
474
511
private class CustomContentPane extends JPanel {
475
512
476
513
private static final long serialVersionUID = -5888940429070142635L ;
477
514
515
+ private final BufferedImage buffer = new BufferedImage (BUFFER_WIDTH , BUFFER_HEIGHT , BufferedImage .TYPE_INT_RGB );
516
+
478
517
@ Override
479
518
public void paintComponent (Graphics g ) {
480
519
super .paintComponent (g );
481
520
482
- if ( openGui == null || openGui . shouldDrawLevelBackground ()) {
483
- g . drawImage ( BACKGROUND , 0 , 0 , getWidth (), getHeight (), null );
521
+ g . setColor ( Color . BLACK );
522
+ g . fillRect ( 0 , 0 , getWidth (), getHeight ());
484
523
485
- synchronized (objects ) {
486
- for (GameObject object : objects ) {
487
- object .draw (g );
488
- }
489
- }
490
- }
524
+ Graphics bufferGraphics = buffer .createGraphics ();
525
+ draw (bufferGraphics );
526
+ bufferGraphics .dispose ();
491
527
492
- if (openGui == null ) {
493
- g .drawImage (PAUSE_BUTTON , 2 , 2 , null );
494
- Point mousePos = getMouseLocation ();
495
- if (mousePos .x >= 2 && mousePos .y >= 2 && mousePos .x < 34 && mousePos .y < 34 ) {
496
- g .setColor (Color .WHITE );
497
- g .drawRect (2 , 2 , 32 , 32 );
498
- }
528
+ if (isWidthControlling ()) {
529
+ int height = BUFFER_HEIGHT * getWidth () / BUFFER_WIDTH ;
530
+ g .drawImage (buffer , 0 , getHeight () / 2 - height / 2 , getWidth (), height , null );
499
531
} else {
500
- openGui .drawScreen (g );
532
+ int width = BUFFER_WIDTH * getHeight () / BUFFER_HEIGHT ;
533
+ g .drawImage (buffer , getWidth () / 2 - width / 2 , 0 , width , getHeight (), null );
501
534
}
502
535
}
503
536
537
+ /**
538
+ * Returns whether the screen is narrow, and therefore the width is the controlling scale factor
539
+ */
540
+ public boolean isWidthControlling () {
541
+ return BUFFER_WIDTH * getHeight () > BUFFER_HEIGHT * getWidth ();
542
+ }
543
+
504
544
}
505
545
506
546
}
0 commit comments