Skip to content

Commit 047d5bd

Browse files
committed
Add ability to resize window
1 parent bd83da1 commit 047d5bd

File tree

2 files changed

+67
-28
lines changed

2 files changed

+67
-28
lines changed

src/net/earthcomputer/stepfish/MainWindow.java

Lines changed: 65 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050

5151
public class MainWindow {
5252

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;
5455

5556
private static final BufferedImage PAUSE_BUTTON = Images.loadImage("gui/pause");
5657
private static final BufferedImage BACKGROUND = Images.loadImage("gui/back_game");
@@ -79,7 +80,7 @@ public MainWindow() {
7980
+ Stepfish.GAME_NAME + " " + Stepfish.GAME_VERSION + ")");
8081

8182
theFrame.setContentPane(contentPane = new CustomContentPane());
82-
contentPane.setPreferredSize(PREFERRED_SIZE);
83+
contentPane.setPreferredSize(new Dimension(BUFFER_WIDTH, BUFFER_HEIGHT));
8384
theFrame.addWindowListener(new WindowAdapter() {
8485
@Override
8586
public void windowClosing(WindowEvent e) {
@@ -90,22 +91,25 @@ public void windowClosing(WindowEvent e) {
9091

9192
@Override
9293
public void mousePressed(MouseEvent e) {
94+
Point mouseLocation = getMouseLocation();
9395
if (openGui == null) {
9496
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()) {
9799
openGui(new GuiPauseMenu());
98100
}
99101
}
100102
} else {
101-
openGui.mousePressed(e.getX(), e.getY(), e.getButton());
103+
openGui.mousePressed(mouseLocation.x, mouseLocation.y, e.getButton());
102104
}
103105
}
104106

105107
@Override
106108
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+
}
109113
}
110114

111115
});
@@ -130,7 +134,6 @@ public void focusLost(FocusEvent e) {
130134
}
131135
});
132136
contentPane.requestFocusInWindow();
133-
theFrame.setResizable(false);
134137
theFrame.pack();
135138
theFrame.setLocationRelativeTo(null);
136139
contentPane.requestFocus();
@@ -292,6 +295,29 @@ public void redraw() {
292295
theFrame.repaint();
293296
}
294297

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+
295321
public void updateTick() {
296322
musicCooldown--;
297323
if (musicCooldown == 0) {
@@ -455,7 +481,7 @@ private void openGuiDangerously(Gui gui) {
455481
this.paused = false;
456482
} else {
457483
this.paused = gui.pausesGame();
458-
gui.validate(contentPane.getWidth(), contentPane.getHeight());
484+
gui.validate(BUFFER_WIDTH, BUFFER_HEIGHT);
459485
}
460486
}
461487

@@ -468,39 +494,53 @@ public Point getMouseLocation() {
468494
Point compLocation = contentPane.getLocationOnScreen();
469495
mouseLocation.x -= compLocation.x;
470496
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+
}
471508
return mouseLocation;
472509
}
473510

474511
private class CustomContentPane extends JPanel {
475512

476513
private static final long serialVersionUID = -5888940429070142635L;
477514

515+
private final BufferedImage buffer = new BufferedImage(BUFFER_WIDTH, BUFFER_HEIGHT, BufferedImage.TYPE_INT_RGB);
516+
478517
@Override
479518
public void paintComponent(Graphics g) {
480519
super.paintComponent(g);
481520

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());
484523

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();
491527

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);
499531
} 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);
501534
}
502535
}
503536

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+
504544
}
505545

506546
}

src/net/earthcomputer/stepfish/gui/GuiPauseMenu.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.awt.Rectangle;
66
import java.awt.image.BufferedImage;
77

8+
import net.earthcomputer.stepfish.MainWindow;
89
import net.earthcomputer.stepfish.util.Images;
910

1011
public class GuiPauseMenu extends Gui
@@ -48,10 +49,8 @@ protected void onPressed()
4849
@Override
4950
protected void drawMiddleLayer(Graphics g)
5051
{
51-
Rectangle clipBounds = g.getClipBounds();
5252
g.setColor(new Color(128, 128, 128, 128));
53-
g.fillRect((int) clipBounds.getX(), (int) clipBounds.getY(), (int) clipBounds.getWidth(),
54-
(int) clipBounds.getHeight());
53+
g.fillRect(0, 0, MainWindow.BUFFER_WIDTH, MainWindow.BUFFER_HEIGHT);
5554
}
5655

5756
@Override

0 commit comments

Comments
 (0)