Skip to content

Commit abdddae

Browse files
committed
resolves #5 #6
1 parent bd6a4a9 commit abdddae

File tree

4 files changed

+105
-77
lines changed

4 files changed

+105
-77
lines changed

assets/images/ant.png

-82.4 KB
Loading

src/AStarSearch.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ public LinkedList<Tile> search() {
2929
// add start to openSet
3030
openSet.add(start);
3131

32-
33-
3432
boolean foundGoal = false;
3533
boolean noPath = false;
3634

@@ -115,8 +113,6 @@ public LinkedList<Tile> search() {
115113

116114
} // end main while loop
117115

118-
119-
120116
// the list of all paths for drawing purposes
121117
ant.setAllPath2D(allPath2D);
122118
return output;

src/Game.java

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,26 @@ The robot character (an ant) should find its way from the start point (home) to
2525
import java.awt.Color;
2626
import java.awt.Dimension;
2727
import java.awt.Font;
28+
import java.awt.FontMetrics;
2829
import java.awt.Graphics;
2930
import java.awt.GridLayout;
30-
31+
import java.awt.Image;
3132
import java.awt.event.ActionEvent;
3233
import java.awt.event.ActionListener;
3334
import java.awt.event.MouseEvent;
3435
import java.awt.event.MouseListener;
3536
import java.awt.event.MouseMotionListener;
36-
37+
import java.io.IOException;
3738
import java.util.LinkedList;
3839
import javax.swing.Timer;
39-
40+
import javax.imageio.ImageIO;
4041
import javax.swing.JButton;
4142
import javax.swing.JFrame;
4243
import javax.swing.JPanel;
4344

4445
public class Game extends JPanel implements MouseListener, MouseMotionListener {
4546

47+
private JFrame frame;
4648
private final static int TILE_SIZE = 40;
4749
private final int NUM_ROWS = 16;
4850
private final int NUM_COLS = 16;
@@ -51,7 +53,6 @@ public class Game extends JPanel implements MouseListener, MouseMotionListener {
5153
private int grasslandCount;
5254
private int openTerrainCount;
5355

54-
5556
private Ant ant;
5657
private Tile[][] tiles;
5758
private LinkedList<Tile> tobeDrawn;
@@ -88,8 +89,15 @@ public class Game extends JPanel implements MouseListener, MouseMotionListener {
8889
private boolean antReached;
8990
protected boolean startTimer;
9091

91-
public Game(JFrame frame) {
92+
// images
93+
private static Image antImage;
94+
private static Image foodImg;
95+
private static Image grasslandImg;
96+
private static Image swamplandImg;
97+
private static Image obstacleImg;
9298

99+
public Game(JFrame frame) {
100+
this.frame = frame;
93101
buttonCount = 8; // number of buttons
94102
// Set the layout manager to a BorderLayout
95103
setLayout(new BorderLayout());
@@ -105,6 +113,10 @@ public Game(JFrame frame) {
105113
Tile.setxOffset(xOffset);
106114
Tile.setyOffset(yOffset);
107115

116+
//load all images
117+
loadImg();
118+
119+
// create board
108120
createTiles();
109121
addMouseListener(this);
110122
addMouseMotionListener(this);
@@ -152,6 +164,18 @@ public Game(JFrame frame) {
152164
setTimerSolving();
153165
}
154166

167+
private void loadImg() {
168+
try {
169+
obstacleImg = ImageIO.read(getClass().getResource("/assets/images/obstacle.png"));
170+
swamplandImg = ImageIO.read(getClass().getResource("/assets/images/swampland.png"));
171+
grasslandImg = ImageIO.read(getClass().getResource("/assets/images/grassland.png"));
172+
foodImg = ImageIO.read(getClass().getResource("/assets/images/food.png"));
173+
antImage = ImageIO.read(getClass().getResource("/assets/images/ant.png"));
174+
} catch (IOException e) {
175+
e.printStackTrace();
176+
}
177+
}
178+
155179
private void resetButton() {
156180
JButton result = new JButton("Reset");
157181
result.addActionListener(new ActionListener() {
@@ -617,13 +641,9 @@ protected void paintComponent(Graphics g) {
617641
// draw path
618642
drawPath(g, TILE_SIZE, tobeDrawn);
619643

620-
if(noPath){
621-
// write no path found on the screen at the center with increased font size
622-
g.setFont(g.getFont().deriveFont(50f));
623-
g.setColor(Color.RED);
624-
g.drawString("No Path Found", 300, 300);
625-
}
644+
printIfNoPath(g);
626645

646+
627647
// Draw the elapsed time
628648
g.setColor(Color.RED); // Sets the color to red.
629649
g.setFont(new Font("Courier New", Font.PLAIN, 20));
@@ -641,6 +661,27 @@ protected void paintComponent(Graphics g) {
641661

642662
} // end paintComponent
643663

664+
private void printIfNoPath(Graphics g) {
665+
if(noPath){
666+
// write no path found on the screen at the center with increased font size
667+
Font font = g.getFont().deriveFont(50f);
668+
g.setFont(font);
669+
g.setColor(Color.RED);
670+
671+
// Calculate the size of the string
672+
FontMetrics metrics = g.getFontMetrics(font);
673+
int stringWidth = metrics.stringWidth("No Path Found");
674+
int stringHeight = metrics.getAscent() - metrics.getDescent();
675+
676+
// Calculate x and y for the string to be centered
677+
int x = (frame.getWidth() - stringWidth) / 2;
678+
int y = (frame.getHeight() - 75 + stringHeight) / 2;
679+
680+
g.drawString("No Path Found", x, y);
681+
}
682+
683+
}
684+
644685
private void printBoardAndCount(Graphics g) {
645686
// draw tiles and add counter for each terrain
646687

@@ -683,7 +724,7 @@ public void actionPerformed(ActionEvent e) {
683724
elapsedTimeStringBeforeSearch = String.format("%d:%02d:%03d", minutes, seconds, milliseconds);
684725
repaint();
685726

686-
if(startClicked){
727+
if(startClicked || noPath){
687728
((Timer) e.getSource()).stop();
688729
}
689730
}
@@ -708,7 +749,7 @@ public void actionPerformed(ActionEvent e) {
708749
repaint();
709750
}
710751

711-
if(antReached){
752+
if(antReached || noPath){
712753
((Timer) e.getSource()).stop();
713754
}
714755
}
@@ -742,4 +783,26 @@ public static int getTileSize() {
742783
return TILE_SIZE;
743784
}
744785

786+
public static Image getObstacleImg() {
787+
return obstacleImg;
788+
}
789+
790+
public static Image getSwamplandImg() {
791+
return swamplandImg;
792+
}
793+
794+
public static Image getGrasslandImg() {
795+
return grasslandImg;
796+
}
797+
798+
public static Image getFoodImg() {
799+
return foodImg;
800+
}
801+
802+
public static Image getAntImg() {
803+
return antImage;
804+
}
805+
806+
807+
745808
}// end class

src/Tile.java

Lines changed: 29 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -34,33 +34,32 @@ public class Tile implements Comparable<Tile> {
3434
private Tile cameFrom;
3535
private boolean removeAntImg;
3636
private static Color boardDefaultColor = Color.WHITE;
37-
private Image antImage;
38-
private Image foodImg;
39-
private Image grasslandImg;
40-
private Image swamplandImg;
41-
private Image obstacleImg;
4237

4338
public Tile(int x, int y) {
4439
this.x = x;
4540
this.y = y;
46-
41+
4742
this.cost = COST_OPEN_TERRAIN;
4843
isOpenTerrain = true;
4944
this.cameFrom = null;
5045
}
5146

5247
public void draw(Graphics g) {
48+
49+
g.setColor(boardDefaultColor);
50+
g.fillRect(getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize()); // draw the tile background
51+
5352
if (isObstacle) {
5453
drawObstacleImage(g);
5554
} else if (isStart) {
5655
if (removeAntImg) {
5756
g.setColor(Color.LIGHT_GRAY);
5857
g.fillRect(getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize());
5958
} else {
60-
drawAntImage(g);
59+
drawAntImg(g);
6160
}
6261
} else if (isGoal) {
63-
drawFoodImage(g);
62+
drawFoodImg(g);
6463
} else if (isGrassland) {
6564
drawGrassland(g);
6665
} else if (isSwampland) {
@@ -71,78 +70,48 @@ public void draw(Graphics g) {
7170
g.fillRect(getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize());
7271
}
7372

74-
// if((!isStart || removeAntImg) && !isGoal){
75-
76-
// }
77-
7873
g.setColor(Color.BLACK);
7974
g.drawRect(getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize());
8075

8176
}
8277

8378
private void drawObstacleImage(Graphics g) {
84-
try {
85-
if(obstacleImg == null){
86-
obstacleImg = ImageIO.read(getClass().getResource("/assets/images/obstacle.png"));
87-
}
88-
g.setColor(boardDefaultColor);
89-
g.fillRect(getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize());
90-
g.drawImage(obstacleImg, getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize(), null);
91-
} catch (IOException e) {
92-
e.printStackTrace();
79+
if (Game.getObstacleImg() != null) {
80+
g.drawImage(Game.getObstacleImg(), getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize(), null);
81+
} else {
82+
System.out.println("Obstacle image is null");
9383
}
9484
}
9585

9686
private void drawSwampland(Graphics g) {
97-
try {
98-
if(swamplandImg == null){
99-
swamplandImg = ImageIO.read(getClass().getResource("/assets/images/swampland.png"));
100-
}
101-
g.setColor(boardDefaultColor);
102-
g.fillRect(getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize());
103-
g.drawImage(swamplandImg, getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize(), null);
104-
} catch (IOException e) {
105-
e.printStackTrace();
87+
if (Game.getSwamplandImg() != null) {
88+
g.drawImage(Game.getSwamplandImg(), getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize(), null);
89+
} else {
90+
System.out.println("Swampland image is null");
10691
}
10792
}
10893

10994
private void drawGrassland(Graphics g) {
110-
try {
111-
if(grasslandImg == null){
112-
grasslandImg = ImageIO.read(getClass().getResource("/assets/images/grassland.png"));
113-
}
114-
g.setColor(boardDefaultColor);
115-
g.fillRect(getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize());
116-
g.drawImage(grasslandImg, getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize(), null);
117-
} catch (IOException e) {
118-
e.printStackTrace();
95+
if (Game.getGrasslandImg() != null) {
96+
g.drawImage(Game.getGrasslandImg(), getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize(), null);
97+
} else {
98+
System.out.println("Grassland image is null");
11999
}
120-
121100
}
122101

123-
private void drawFoodImage(Graphics g) {
124-
try {
125-
if(foodImg == null){
126-
foodImg = ImageIO.read(getClass().getResource("/assets/images/food.png"));
127-
}
128-
g.setColor(boardDefaultColor);
129-
g.fillRect(getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize());
130-
g.drawImage(foodImg, getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize(), null);
131-
} catch (IOException e) {
132-
e.printStackTrace();
102+
private void drawFoodImg(Graphics g) {
103+
if (Game.getFoodImg() != null) {
104+
g.drawImage(Game.getFoodImg(), getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize(), null);
105+
} else {
106+
System.out.println("Food image is null");
133107
}
134108
}
135109

136-
private void drawAntImage(Graphics g) {
137-
try {
138-
if(antImage == null){
139-
antImage = ImageIO.read(getClass().getResource("/assets/images/ant.png"));
140-
}
141-
g.setColor(boardDefaultColor);
142-
g.fillRect(getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize());
143-
g.drawImage(antImage, getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize(), null);
144-
} catch (IOException e) {
145-
e.printStackTrace();
110+
private void drawAntImg(Graphics g) {
111+
if (Game.getAntImg() != null) {
112+
g.drawImage(Game.getAntImg(), getXpixel(), getYpixel(), Game.getTileSize(), Game.getTileSize(), null);
113+
} else {
114+
System.out.println("Ant image is null");
146115
}
147116
}
148117

0 commit comments

Comments
 (0)