Skip to content

Commit 6a7add0

Browse files
committed
resized the window and made responsive.
1 parent 790221a commit 6a7add0

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ All notable changes to this project will be documented in this file.
1111
- Counter for each tile type. (Shows the number of tiles of each type)
1212
- Timer 1 - Time took for the user to create mage.
1313
- Timer 2 - Time took for the AI to solve the Mage solution (duration for AI to **graphically** reach the goal).
14+
- Window can now be resized.
1415

1516
### Changed
1617

1718
- "No Path Found" message now centrally aligned on screen.
1819
- Optimized initial rendering speed for the ant image.
1920
- Button placement from right column to top row.
2021
- Adjusted ant's speed consistency across different tile type.
22+
- Remove the word "Location" from the Start and Goal buttons.
2123

2224
### Fixed
2325

src/App.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,37 @@
44
Description: Main class to run the game
55
*/
66

7+
import java.awt.BorderLayout;
8+
import java.awt.Dimension;
9+
710
import javax.swing.JFrame;
811
import javax.swing.UIManager;
9-
1012
import com.formdev.flatlaf.FlatLightLaf;
1113

1214
public class App {
1315
public static void main(String[] args) throws Exception {
1416

17+
int height = 800;
18+
int width = 1000;
19+
1520
try {
1621
UIManager.setLookAndFeel(new FlatLightLaf());
1722
} catch (Exception ex) {
1823
System.err.println("Failed to initialize theme. Using fallback.");
24+
ex.printStackTrace();
1925
}
2026

2127
JFrame frame = new JFrame("Ant Path Finding with A* Algorithm");
22-
frame.setSize(1400, 800);
28+
frame.setSize(width, height);
29+
frame.setMinimumSize(new Dimension(width, height));
2330

2431
Game game = new Game(frame);
2532

2633
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27-
frame.add(game);
34+
frame.setLayout(new BorderLayout());
35+
36+
frame.add(game, BorderLayout.CENTER);
37+
frame.setLocationRelativeTo(null); // Center the window on the screen
2838

2939
frame.setVisible(true);
3040
}

src/Game.java

+30-24
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ The robot character (an ant) should find its way from the start point (home) to
3131
import java.awt.Image;
3232
import java.awt.event.ActionEvent;
3333
import java.awt.event.ActionListener;
34+
import java.awt.event.ComponentAdapter;
35+
import java.awt.event.ComponentEvent;
3436
import java.awt.event.MouseEvent;
3537
import java.awt.event.MouseListener;
3638
import java.awt.event.MouseMotionListener;
@@ -113,7 +115,7 @@ public class Game extends JPanel implements MouseListener, MouseMotionListener {
113115
public Game(JFrame frame) {
114116

115117
// change the color if needed
116-
118+
117119
bgColor = new Color(234, 242, 255,(int) (0.7 * 255)); // 0.7 is the opacity
118120
openTerrainColor = new Color(255, 255, 255, (int) (0.5 * 255));
119121
openTerrainBoarderColor = new Color(0, 0, 0, (int) (0.5 * 255));
@@ -134,13 +136,6 @@ public Game(JFrame frame) {
134136
buttonPanel = new JPanel();
135137
buttonPanel.setLayout(new GridLayout(1, buttonCount, 10, 10));
136138

137-
// set tile offset
138-
int xOffset = (int) ((frame.getWidth() - TILE_SIZE * NUM_COLS) / 2);
139-
int yOffset = (int) ((frame.getHeight() - TILE_SIZE * NUM_ROWS) / 1.8);
140-
141-
Tile.setxOffset(xOffset);
142-
Tile.setyOffset(yOffset);
143-
144139
//load all images
145140
loadImg();
146141

@@ -167,32 +162,43 @@ public Game(JFrame frame) {
167162
iconSize = 20; // Define the size of your icons
168163
iconTextSpacing = 5; // Spacing between icon and text
169164
wordSpacing = 25; // Spacing between each icon-count pair
170-
171-
terrainCountX = (int)(frame.getWidth()/2.4);
172-
terrainCountY = frame.getHeight()/15;
173165
iconScaling = 2.5;
174166

175-
startTimer = false;
176-
startClicked = false;
177-
startTile = null;
178-
goalTile = null;
179-
startMovingAnt = false;
180-
noPath = false;
181167
tobeDrawn = new LinkedList<Tile>();
182-
obstacleCount = 0;
183-
swamplandCount = 0;
184-
grasslandCount = 0;
185-
openTerrainCount = 0;
186168

187169
// set timer
188170
elapsedTimeStringBeforeSearch = "0:00:000";
189171
elapsedTimeStringAfterAnimation = "0:00:000";
190172
timerX = 100;
191173
timerY = frame.getHeight() - 100;
192-
193174

194175
setTimerMageCreation();
195176
setTimerSolving();
177+
178+
// resizeble windows
179+
resizeWindow();
180+
181+
}
182+
183+
private void resizeWindow() {
184+
this.addComponentListener(new ComponentAdapter() {
185+
@Override
186+
public void componentResized(ComponentEvent e) {
187+
188+
// the top bar location
189+
terrainCountX = (int)(frame.getWidth()/2.5);
190+
terrainCountY = frame.getHeight()/15;
191+
192+
// set the grid offset/start point
193+
int xOffset = (int) ((frame.getWidth() - TILE_SIZE * NUM_COLS) / 2);
194+
int yOffset = (int) ((frame.getHeight() - TILE_SIZE * NUM_ROWS) / 1.8);
195+
196+
Tile.setxOffset(xOffset);
197+
Tile.setyOffset(yOffset);
198+
199+
repaint();
200+
}
201+
});
196202
}
197203

198204
private void loadImg() {
@@ -337,7 +343,7 @@ private void resetCameFrom(Tile[][] tiles) {
337343
}
338344

339345
private void selectGoalLocation() {
340-
JButton result = new JButton("Goal Location");
346+
JButton result = new JButton("Goal");
341347
result.addActionListener(new ActionListener() {
342348
@Override
343349
public void actionPerformed(ActionEvent e) {
@@ -353,7 +359,7 @@ public void actionPerformed(ActionEvent e) {
353359
}
354360

355361
public void selectStartLocation() {
356-
JButton result = new JButton("Start Location");
362+
JButton result = new JButton("Start");
357363
result.addActionListener(new ActionListener() {
358364
@Override
359365
public void actionPerformed(ActionEvent e) {

0 commit comments

Comments
 (0)