Skip to content

Commit 48b13b7

Browse files
Merge pull request #25 from mkhattat/game
Game
2 parents 28864df + be08416 commit 48b13b7

17 files changed

Lines changed: 780 additions & 21 deletions

src/main/java/nl/tudelft/pooralien/Controller/Board.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
* The Board class.
1010
*/
1111
public class Board {
12-
private static final int WIDTH = 10;
13-
private static final int HEIGHT = 10;
12+
public static final int WIDTH = 10;
13+
public static final int HEIGHT = 10;
1414
private static final int MIN_REQUIRED_ITEMS = 3;
1515

1616
private Item[][] items = new Item[WIDTH][HEIGHT];
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package nl.tudelft.pooralien.Controller;
2+
3+
/**
4+
* class for controlling the flow of the game.
5+
*/
6+
public final class Game {
7+
private static Game game;
8+
private Board board;
9+
10+
/**
11+
* Initialise the singleton Game object.
12+
*/
13+
private Game() {
14+
board = new Board();
15+
}
16+
17+
/**
18+
* Returns the single Game object.
19+
* @return the Game object
20+
*/
21+
public static synchronized Game getGame() {
22+
if (game == null) {
23+
game = new Game();
24+
}
25+
return game;
26+
}
27+
28+
/**
29+
* Returns the board.
30+
* @return the board being used.
31+
*/
32+
public Board getBoard() {
33+
return board;
34+
}
35+
}
Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
11
package nl.tudelft.pooralien;
22

3+
4+
import nl.tudelft.pooralien.ui.MainScreen;
5+
6+
import javax.swing.JFrame;
7+
38
/**
4-
* Created by mostafa on 7-9-17.
9+
* The Launcher of the game.
510
*/
6-
final class Launcher {
11+
public class Launcher {
12+
13+
private MainScreen mainScreen;
714

815
/**
9-
* To prevent checkstyle error.
16+
* Launch the game GUI.
1017
*/
11-
private Launcher() {
18+
public void launch() {
19+
JFrame mainWindow = new JFrame("Poor Alien");
20+
MainScreen mainScreen = new MainScreen();
21+
mainWindow.setSize(0, 0);
22+
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
23+
mainWindow.getContentPane().add(mainScreen);
1224

25+
new MouseEventHandler(mainScreen);
26+
mainWindow.pack();
27+
mainWindow.setVisible(true);
1328
}
1429

1530
/**
1631
* Entry point of the game.
1732
* @param args The program arguments.
1833
*/
19-
public static void main(final String[] args) {
20-
21-
System.out.println("Hi");
22-
34+
public static void main(String[] args) {
35+
//StartupScreen startupScreen = new StartupScreen();
36+
//startupScreen.show();
37+
new Launcher().launch();
2338
}
2439
}

src/main/java/nl/tudelft/pooralien/MouseEventHandler.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package nl.tudelft.pooralien;
22

3+
import nl.tudelft.pooralien.ui.Animation;
4+
import nl.tudelft.pooralien.ui.MainScreen;
5+
import nl.tudelft.pooralien.ui.RTLDragAnimation;
6+
import nl.tudelft.pooralien.ui.TTBDragAnimation;
7+
38
import java.awt.event.MouseEvent;
49
import java.awt.event.MouseListener;
510
import java.awt.event.MouseMotionListener;
6-
import javax.swing.JFrame;
711
import java.awt.Point;
812

913
/**
@@ -14,15 +18,18 @@
1418
public class MouseEventHandler implements MouseListener, MouseMotionListener {
1519

1620
private MouseEventHandler.MouseAction mouseAction;
21+
private MainScreen mainScreen;
22+
private Animation dragAnimation;
1723

1824
/**
1925
* Initiate event mouse listeners on a given JFrame.
2026
*
21-
* @param window the JFrame where where to listen for events
27+
* @param mainScreen the JFrame where where to listen for events
2228
*/
23-
public MouseEventHandler(JFrame window) {
24-
window.addMouseListener(this);
25-
window.addMouseMotionListener(this);
29+
public MouseEventHandler(MainScreen mainScreen) {
30+
mainScreen.addMouseListener(this);
31+
mainScreen.addMouseMotionListener(this);
32+
this.mainScreen = mainScreen;
2633
}
2734

2835
/**
@@ -32,7 +39,6 @@ public MouseEventHandler(JFrame window) {
3239
* @param e the mouse event that was captured
3340
*/
3441
public void mousePressed(MouseEvent e) {
35-
System.out.println("Create MouseAction Object");
3642
Point mousePosition = new Point(e.getX(), e.getY());
3743
mouseAction = new MouseEventHandler.MouseAction(mousePosition);
3844
}
@@ -51,7 +57,11 @@ public void mouseReleased(MouseEvent e) {
5157
if (mouseAction.getMouseActionType() == MouseAction.CLICK_ACTION) {
5258
System.out.println("Click Action on x:" + xPos + ", y:" + yPos);
5359
} else {
54-
System.out.println("End Drag Animation");
60+
if (dragAnimation == null) {
61+
return;
62+
}
63+
dragAnimation.end();
64+
dragAnimation = null;
5565
}
5666
}
5767

@@ -111,10 +121,17 @@ public void mouseDragged(MouseEvent e) {
111121
mouseAction.setPosition(mousePosition);
112122

113123
if (mouseAction.getMouseActionType() != MouseAction.CLICK_ACTION) {
124+
Point p = e.getPoint();
125+
if (dragAnimation != null) {
126+
dragAnimation.update(p);
127+
return;
128+
}
114129
if (mouseAction.getMouseActionType() == MouseAction.HORIZONTAL_DRAG_ACTION) {
115-
System.out.println("Animate DRAG Horizontal x:" + e.getX() + ", y:" + e.getY());
130+
dragAnimation = new RTLDragAnimation(mainScreen);
131+
dragAnimation.start(p);
116132
} else if (mouseAction.getMouseActionType() == MouseAction.VERTICAL_DRAG_ACTION) {
117-
System.out.println("Animate DRAG Vertical x:" + e.getX() + ", y:" + e.getY());
133+
dragAnimation = new TTBDragAnimation(mainScreen);
134+
dragAnimation.start(p);
118135
}
119136
}
120137
}
@@ -133,8 +150,6 @@ public class MouseAction {
133150
private Point startPoint;
134151
private Point currentPoint;
135152

136-
//private int xPosStart, yPosStart;
137-
//private int xPosCurrent, yPosCurrent;
138153
private int mouseActionType;
139154

140155

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package nl.tudelft.pooralien.ui;
2+
3+
import java.awt.Point;
4+
5+
/**
6+
* Interface for the Animation class.
7+
*/
8+
public interface Animation {
9+
/**
10+
* Initializing the drag move.
11+
* @param p is the point where mouse is pressed.
12+
*/
13+
14+
void start(Point p);
15+
16+
/**
17+
* updating of the screen when mouse is dragged.
18+
* @param p the mouse coordinate of the new point.
19+
*/
20+
21+
void update(Point p);
22+
23+
/**
24+
* End the animation.
25+
*/
26+
void end();
27+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package nl.tudelft.pooralien.ui;
2+
3+
import javax.swing.JLabel;
4+
import javax.swing.JPanel;
5+
import java.awt.Component;
6+
import java.awt.Point;
7+
8+
/**
9+
* JPanelTile holds image icons on the GridBoard on the screen.
10+
*/
11+
public class JPanelTile extends JPanel {
12+
private JLabel imageIcon = null;
13+
private Point gridPosition;
14+
15+
/**
16+
* Constructor of JPanelTile.
17+
* @param gridPosition is the position of this tile on the grid board.
18+
*/
19+
public JPanelTile(Point gridPosition) {
20+
super();
21+
this.gridPosition = gridPosition;
22+
}
23+
24+
@Override
25+
public Component add(Component comp) {
26+
imageIcon = (JLabel) comp;
27+
return super.add(comp);
28+
}
29+
30+
@Override
31+
public void remove(Component comp) {
32+
if (comp.equals(imageIcon)) {
33+
imageIcon = null;
34+
}
35+
super.remove(comp);
36+
}
37+
38+
@Override
39+
public void removeAll() {
40+
imageIcon = null;
41+
super.removeAll();
42+
}
43+
44+
/**
45+
* Get the ImageIcon which is placed on a JLabel.
46+
* @return JLabel containing the Icon of this object.
47+
*/
48+
public JLabel getImageIcon() {
49+
return imageIcon;
50+
}
51+
52+
/**
53+
* Get the position of this item on the GridBoard.
54+
* @return the position of tile on the grid board.
55+
*/
56+
public Point getGridPosition() {
57+
return gridPosition;
58+
}
59+
}

0 commit comments

Comments
 (0)