Skip to content

Commit 2816ad1

Browse files
author
suncloudsmoon
committed
Added music player, automatic frame initialization, and more!
1 parent 236ed69 commit 2816ad1

14 files changed

+628
-97
lines changed
-183 Bytes
Binary file not shown.
-714 Bytes
Binary file not shown.
-2.52 KB
Binary file not shown.

tilegame2d/src/tilegame2d/MainCharacter.java

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package tilegame2d;
2+
3+
import javax.swing.ImageIcon;
4+
import javax.swing.JFrame;
5+
6+
/**
7+
* Contains methods that automatically creates and sets values for JFrame.
8+
*
9+
* @author Ganesha Ajjampura
10+
*
11+
*/
12+
public class SimpleFrame {
13+
14+
public JFrame frame;
15+
16+
/**
17+
* Initializes the frame with default values.
18+
*
19+
* @param title A String value specifying the title of the JFrame object.
20+
* @param icon A ImageIcon object containing the image for the JFrame icon.
21+
* @param width An integer value specifying the width of the JFrame object.
22+
* @param height An integer value specifying the height of the JFrame object.
23+
*/
24+
public void initializeFrame(String title, ImageIcon icon, int width, int height) {
25+
frame = new JFrame();
26+
frame.setTitle(title);
27+
frame.setSize(width, height);
28+
frame.setIconImage(icon.getImage());
29+
frame.setResizable(false);
30+
frame.setVisible(true);
31+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
32+
}
33+
34+
/**
35+
* Initializes the frame with default values and the animation class added.
36+
*
37+
* @param sg A object containing the child class of SimpleGraphics.
38+
* @param title A String value specifying the title of the JFrame object.
39+
* @param icon A ImageIcon object containing the image for the JFrame icon.
40+
* @param width An integer value specifying the width of the JFrame object.
41+
* @param height An integer value specifying the height of the JFrame object.
42+
*/
43+
public void initializeFrameForAnimation(SimpleGraphics sg, String title, ImageIcon icon, int width, int height) {
44+
frame = new JFrame();
45+
sg.screen();
46+
frame.add(sg);
47+
frame.setTitle(title);
48+
frame.setSize(width, height);
49+
frame.setLocationRelativeTo(null);
50+
frame.setIconImage(icon.getImage());
51+
frame.setResizable(false);
52+
frame.setVisible(true);
53+
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
54+
}
55+
}

tilegame2d/src/tilegame2d/SimpleGame.java

Lines changed: 0 additions & 89 deletions
This file was deleted.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package tilegame2d;
2+
3+
import java.awt.event.ActionEvent;
4+
5+
import javax.swing.AbstractAction;
6+
import javax.swing.Action;
7+
import javax.swing.JComponent;
8+
import javax.swing.JFrame;
9+
import javax.swing.JRootPane;
10+
import javax.swing.KeyStroke;
11+
12+
/**
13+
* Contains methods that simply the process of key bindings.
14+
*
15+
* @author Ganesha Ajjampura
16+
*
17+
*/
18+
public abstract class SimpleGameKeys {
19+
20+
private Action goUp;
21+
private Action goDown;
22+
private Action goLeft;
23+
private Action goRight;
24+
25+
public abstract void moveUp();
26+
27+
public abstract void moveDown();
28+
29+
public abstract void moveLeft();
30+
31+
public abstract void moveRight();
32+
33+
/**
34+
* Configures W A S D keys to an input and action map with key bindings.
35+
*
36+
* @param frame A JFrame object specifying the main frame used by the game.
37+
*/
38+
public void configureWasdKeyBindings(JFrame frame) {
39+
goUp = new goUp();
40+
goDown = new goDown();
41+
goLeft = new goLeft();
42+
goRight = new goRight();
43+
44+
JRootPane rootOfAll = frame.getRootPane();
45+
46+
// Move Up
47+
rootOfAll.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "goUp");
48+
rootOfAll.getActionMap().put("goUp", goUp);
49+
50+
// Move Down
51+
rootOfAll.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("S"), "goDown");
52+
rootOfAll.getActionMap().put("goDown", goDown);
53+
54+
// Move Left
55+
rootOfAll.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("A"), "goLeft");
56+
rootOfAll.getActionMap().put("goLeft", goLeft);
57+
58+
// Move Right
59+
rootOfAll.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("D"), "goRight");
60+
rootOfAll.getActionMap().put("goRight", goRight);
61+
}
62+
63+
private class goUp extends AbstractAction {
64+
65+
private static final long serialVersionUID = -5327395479500958279L;
66+
67+
@Override
68+
public void actionPerformed(ActionEvent e) {
69+
moveUp();
70+
71+
}
72+
73+
}
74+
75+
private class goDown extends AbstractAction {
76+
77+
private static final long serialVersionUID = -2867139883452440832L;
78+
79+
@Override
80+
public void actionPerformed(ActionEvent e) {
81+
moveDown();
82+
83+
}
84+
85+
}
86+
87+
private class goLeft extends AbstractAction {
88+
89+
private static final long serialVersionUID = -3638859233582503618L;
90+
91+
@Override
92+
public void actionPerformed(ActionEvent e) {
93+
moveLeft();
94+
95+
}
96+
97+
}
98+
99+
private class goRight extends AbstractAction {
100+
101+
private static final long serialVersionUID = 7041504566770698300L;
102+
103+
@Override
104+
public void actionPerformed(ActionEvent e) {
105+
moveRight();
106+
107+
}
108+
109+
}
110+
}

0 commit comments

Comments
 (0)