Skip to content

Commit a7deaba

Browse files
Fixed several issues in view
1 parent 9b8e05b commit a7deaba

File tree

24 files changed

+145
-21
lines changed

24 files changed

+145
-21
lines changed

bin/images/free.png

213 Bytes
Loading

bin/images/full_resolution/gold.png

16.1 KB
Loading

bin/images/full_resolution/player.png

27.9 KB
Loading

bin/images/full_resolution/wall.png

2.04 KB
Loading

bin/images/gold.png

2.4 KB
Loading

bin/images/player.png

2.82 KB
Loading

bin/images/wall.png

496 Bytes
Loading

src/be/dylanvanassche/maze/controller/Controller.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static void main(String[] args) throws UnknownMovementDirection, WeHaveAW
4949

5050
public Controller() {
5151
// Player name screen can be added in the future as extension
52-
this.setMaze(new Maze("Hermanneke"));
52+
this.setMaze(new Maze());
5353
this.setMainFrame(new MainFrame(this));
5454
}
5555

@@ -70,8 +70,7 @@ public void exitApplication() {
7070
}
7171

7272
public void newGame() {
73-
this.setMaze(new Maze("Joske"));
74-
System.out.println(this.getMaze().getPlayer().getName());
73+
this.setMaze(new Maze());
7574
this.getMainFrame().newGame();
7675
}
7776

@@ -87,4 +86,8 @@ public void movePlayer(MovementType movement) throws UnknownMovementDirection, B
8786
throw new WeHaveAWinner(exception.getMessage());
8887
}
8988
}
89+
90+
public void setPlayerName(String name) {
91+
this.getMaze().getPlayer().setName(name);
92+
}
9093
}

src/be/dylanvanassche/maze/model/Maze.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void setTiles(Tile[][] tiles) {
5353
/*
5454
* @brief: constructs a new random Maze
5555
*/
56-
public Maze(String playerName) {
56+
public Maze() {
5757
// n^2 tiles
5858
for(int i=0; i<mazeSize*2; i++) {
5959
for(int j=0; j<mazeSize*2; j++) {
@@ -62,7 +62,7 @@ public Maze(String playerName) {
6262
}
6363

6464
// create new player and enable gold and void collision
65-
this.setPlayer(new Player(playerName));
65+
this.setPlayer(new Player());
6666
this.getTiles()[(int)(Math.random()*2*mazeSize)][(int)(Math.random()*2*mazeSize)].enableGold();
6767
Tile target = this.getTiles()[(int)(Math.random()*2*mazeSize)][(int)(Math.random()*2*mazeSize)];
6868
while(target.getMiddleSquare().getContent() == SquareType.GOLD) { // It's never possible that the content is WALL
@@ -227,7 +227,7 @@ else if(newSquare.isGold() == true)
227227
this.getPlayer().setPosition(new Position(newSquare, newTile));
228228
oldSquare.setContent(SquareType.FREE); // Player can only be on FREE Squares
229229
newSquare.setContent(SquareType.PLAYER);
230-
throw new WeHaveAWinner("You won!\nCongratulations!");
230+
throw new WeHaveAWinner("You won!\nCongratulations " + this.getPlayer().getName() + "!");
231231
}
232232
else
233233
{

src/be/dylanvanassche/maze/model/Player.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public void setPosition(Position position) {
2222
this.position = position;
2323
}
2424

25-
public Player(String name) {
26-
this.setName(name);
25+
public Player() {
2726
}
2827
}

src/be/dylanvanassche/maze/model/Square.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public String toString() {
2222
return "$";
2323
}
2424
else if(this.isWall()) {
25-
return "";
25+
return "#";
2626
}
2727
else if(this.isPlayer()) {
2828
return "X";

src/be/dylanvanassche/maze/view/MainFrame.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Dylan Van Assche - 3 ABA EI
22
package be.dylanvanassche.maze.view;
33

4+
import java.awt.BorderLayout;
5+
46
import javax.swing.*;
57
import be.dylanvanassche.maze.controller.*;
68

@@ -35,20 +37,26 @@ public void setNavigationView(NavigationView navigationView) {
3537

3638
public MainFrame(final Controller c) {
3739
this.setController(c);
38-
this.getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
40+
this.getContentPane().setLayout(new BorderLayout());
3941
this.setNavigationView(new NavigationView(this.getController()));
4042
this.newGame();
4143
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
4244
this.setJMenuBar(new MenuBar(this.getController()));
4345
this.pack();
46+
this.setResizable(false);
4447
this.setVisible(true);
4548
}
4649

4750
public void newGame() {
51+
String name = null;
52+
while(name == null || name.length() == 0) {
53+
name = JOptionPane.showInputDialog(this, "What's your name?");
54+
}
55+
this.getController().setPlayerName(name);
4856
this.getContentPane().removeAll(); // clean it up
4957
this.setMazeView(new MazeView(this.getController()));
50-
this.getContentPane().add(this.getMazeView());
51-
this.getContentPane().add(this.getNavigationView());
58+
this.getContentPane().add(this.getMazeView(), BorderLayout.CENTER);
59+
this.getContentPane().add(this.getNavigationView(), BorderLayout.SOUTH);
5260
// Revalidates the component hierarchy, when adding/removing stuff at runtime you need to reload the UI,
5361
// this is NOT repaint since we add/remove the components completely without modifying their properties!
5462
// If you modify their properties only, a repaint() is sufficient!

src/be/dylanvanassche/maze/view/MazeView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void setController(Controller controller) {
2222

2323
public MazeView(final Controller c) {
2424
this.setController(c);
25-
this.setLayout(new GridLayout(0,4));
25+
this.setLayout(new GridLayout(0,4,0,0)); // gap = 0
2626
this.newGame();
2727
this.setVisible(true);
2828
}

src/be/dylanvanassche/maze/view/NavigationView.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ public void actionPerformed(ActionEvent event) {
128128
}
129129
}
130130
});
131+
this.getUpButton().setMnemonic(KeyEvent.VK_UP); // ALT required
132+
this.getDownButton().setMnemonic(KeyEvent.VK_DOWN);
133+
this.getLeftButton().setMnemonic(KeyEvent.VK_LEFT);
134+
this.getRightButton().setMnemonic(KeyEvent.VK_RIGHT);
131135
this.setVisible(true);
132136
}
133137

src/be/dylanvanassche/maze/view/SquareView.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,27 @@
22
package be.dylanvanassche.maze.view;
33

44
import java.awt.*;
5+
import java.awt.image.*;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import javax.imageio.ImageIO;
59
import javax.swing.*;
6-
import be.dylanvanassche.maze.controller.*;
7-
import be.dylanvanassche.maze.model.Square;
10+
import be.dylanvanassche.maze.controller.Controller;
11+
import be.dylanvanassche.maze.model.*;
812
import be.dylanvanassche.maze.view.*;
913

1014
public class SquareView extends JPanel {
11-
private Controller controller;
12-
private Square square;
13-
15+
private Controller controller;
16+
private Square square;
17+
1418
public Controller getController() {
1519
return controller;
1620
}
1721

1822
public void setController(Controller controller) {
1923
this.controller = controller;
2024
}
21-
25+
2226
public Square getSquare() {
2327
return square;
2428
}
@@ -31,7 +35,7 @@ public SquareView(final Controller c, Square s) {
3135
this.setController(c);
3236
this.setSquare(s);
3337
this.setLayout(new FlowLayout());
34-
this.add(new JLabel(this.getSquare().toString()));
38+
this.add(new SquareViewElement(this.getSquare().getContent()));
3539
this.setVisible(true);
3640
}
3741
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package be.dylanvanassche.maze.view;
2+
3+
import java.awt.Dimension;
4+
import java.awt.Graphics;
5+
import java.awt.image.BufferedImage;
6+
import java.io.File;
7+
import java.io.IOException;
8+
9+
import javax.imageio.ImageIO;
10+
import javax.swing.*;
11+
import be.dylanvanassche.maze.model.SquareType;
12+
13+
14+
public class SquareViewElement extends JComponent {
15+
private SquareType content;
16+
private static final String WALL_PATH = "src/images/wall.png";
17+
private static final String PLAYER_PATH = "src/images/player.png";
18+
private static final String GOLD_PATH = "src/images/gold.png";
19+
private static final String FREE_PATH = "src/images/free.png";
20+
private static final int IMG_SIZE = 64;
21+
private BufferedImage wallImg;
22+
private BufferedImage playerImg;
23+
private BufferedImage goldImg;
24+
private BufferedImage freeImg;
25+
26+
public SquareType getContent() {
27+
return content;
28+
}
29+
30+
public void setContent(SquareType content) {
31+
this.content = content;
32+
}
33+
34+
public BufferedImage getWallImg() {
35+
return wallImg;
36+
}
37+
38+
public void setWallImg(BufferedImage wallImg) {
39+
this.wallImg = wallImg;
40+
}
41+
42+
public BufferedImage getPlayerImg() {
43+
return playerImg;
44+
}
45+
46+
public void setPlayerImg(BufferedImage playerImg) {
47+
this.playerImg = playerImg;
48+
}
49+
50+
public BufferedImage getGoldImg() {
51+
return goldImg;
52+
}
53+
54+
public void setGoldImg(BufferedImage goldImg) {
55+
this.goldImg = goldImg;
56+
}
57+
58+
public BufferedImage getFreeImg() {
59+
return freeImg;
60+
}
61+
62+
public void setFreeImg(BufferedImage freeImg) {
63+
this.freeImg = freeImg;
64+
}
65+
66+
public SquareViewElement(SquareType content) {
67+
this.setContent(content);
68+
try {
69+
this.setWallImg(ImageIO.read(new File(WALL_PATH)));
70+
this.setPlayerImg(ImageIO.read(new File(PLAYER_PATH)));
71+
this.setGoldImg(ImageIO.read(new File(GOLD_PATH)));
72+
this.setFreeImg(ImageIO.read(new File(FREE_PATH)));
73+
}
74+
catch (IOException e) {
75+
76+
e.printStackTrace();
77+
}
78+
}
79+
80+
@Override
81+
public Dimension getPreferredSize() {
82+
return new Dimension(IMG_SIZE,IMG_SIZE);
83+
}
84+
85+
@Override
86+
protected void paintComponent(Graphics graphics) {
87+
//super.paintComponent(graphics);
88+
switch(this.getContent())
89+
{
90+
case WALL:
91+
graphics.drawImage(this.getWallImg(), 0, 0, IMG_SIZE, IMG_SIZE, null);
92+
break;
93+
case PLAYER:
94+
graphics.drawImage(this.getPlayerImg(), 0, 0, IMG_SIZE, IMG_SIZE, this);
95+
break;
96+
case GOLD:
97+
graphics.drawImage(this.getGoldImg(), 0, 0, IMG_SIZE, IMG_SIZE, this);
98+
break;
99+
default:
100+
case FREE:
101+
graphics.drawImage(this.getFreeImg(), 0, 0, IMG_SIZE, IMG_SIZE, this);
102+
break;
103+
}
104+
105+
}
106+
}

src/be/dylanvanassche/maze/view/TileView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void setTile(Tile tile) {
2929
public TileView(final Controller c, Tile tile) {
3030
this.setController(c);
3131
this.setTile(tile);
32-
this.setLayout(new GridLayout(0,3));
32+
this.setLayout(new GridLayout(0,3,0,0)); // gap = 0
3333
for(int i = 0; i < 9; i++) {
3434
this.add(new SquareView(this.getController(), this.getTile().nextSquare()));
3535
}

src/images/free.png

213 Bytes
Loading

src/images/full_resolution/gold.png

16.1 KB
Loading

src/images/full_resolution/player.png

27.9 KB
Loading

src/images/full_resolution/wall.png

2.04 KB
Loading

src/images/gold.png

2.4 KB
Loading

src/images/player.png

2.82 KB
Loading

src/images/wall.png

496 Bytes
Loading

0 commit comments

Comments
 (0)