Camtech075

FEJava source: GameFrame.java

Oct 19th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. package graphics.main;
  2.  
  3. import javax.swing.JFrame;
  4. import java.awt.Container;
  5. import java.awt.event.KeyListener;
  6. import java.awt.event.ActionListener;
  7. import javax.swing.JPanel;
  8. import javax.swing.BoxLayout;
  9. import javax.swing.JLabel;
  10. import javax.swing.WindowConstants;
  11. import java.awt.Dimension;
  12. import java.awt.Graphics;
  13. import java.awt.Color;
  14. import java.awt.FlowLayout;
  15. import engine.FireEmblemEngine;
  16. import engine.system.*;
  17. import engine.system.internal.*;
  18. import engine.chapterdata.*;
  19. import graphics.img.fireemblem.*;
  20.  
  21. /**
  22. * This class displays the basic game frame.
  23. */
  24.  
  25. public class GameFrame extends JPanel implements GameConstants,FEImages,InternalCommands {
  26.  
  27. protected static final int GAME_WIDTH = 640;
  28. protected static final int GAME_HEIGHT = 480;
  29.  
  30. protected FireEmblemEngine gameEngine;
  31.  
  32. //JFrame gFrame;
  33. protected static JFrame gFrame;
  34.  
  35. private static final Dimension preferredSize = new Dimension(GAME_WIDTH,GAME_HEIGHT);
  36. private static int mapPosX = 0;
  37. private static int mapPosY = 0;
  38.  
  39. //Finds the size of the frame.
  40.  
  41. public Dimension getPreferredSize() {
  42.  
  43. return preferredSize;
  44.  
  45. }
  46.  
  47. public static JFrame getCurrentFrame() {
  48.  
  49. return gFrame;
  50.  
  51. }
  52.  
  53. //Whee, constructor.
  54.  
  55. public GameFrame(JFrame j) {
  56.  
  57. gameEngine = new FireEmblemEngine(this);
  58. j.addKeyListener(gameEngine);
  59.  
  60. }
  61.  
  62. public JFrame getFrame() {
  63.  
  64. if (gFrame != null)
  65. {
  66.  
  67. return gFrame;
  68.  
  69. }
  70. else
  71. {
  72.  
  73. return null;
  74.  
  75. }
  76.  
  77. }
  78.  
  79. public void addPanelToFrame(Container c) {
  80.  
  81. c.add(this);
  82.  
  83. }
  84.  
  85. public void paintComponent(Graphics g) {
  86.  
  87. super.paintComponent(g);
  88.  
  89. //Fill the entire screen with white, dummy at the moment
  90. g.setColor(Color.WHITE);
  91. g.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);
  92.  
  93. //g.drawImage(gameEngine.getCurrentMap().getImage());
  94.  
  95. switch(gameEngine.getCurrentMode())
  96. {
  97.  
  98. case MAP_MODE:
  99. int mapHeight = gameEngine.getCurrentMap().getHeight();
  100. int mapWidth = gameEngine.getCurrentMap().getWidth();
  101.  
  102. switch(gameEngine.scrollMap())
  103. {
  104.  
  105. case NONE:
  106. break;
  107.  
  108. case UP:
  109. mapPosY = (((mapPosY == 0) ? mapPosY:(mapPosY + TILE_INCR)));
  110. break;
  111.  
  112. case DOWN:
  113. mapPosY = (((mapPosY == GAME_HEIGHT - mapHeight) ? mapPosY:(mapPosY - TILE_INCR)));
  114. break;
  115.  
  116. case LEFT:
  117. mapPosX = (((mapPosX == 0) ? mapPosX:(mapPosX + TILE_INCR)));
  118. break;
  119.  
  120. case RIGHT:
  121. mapPosX = (((mapPosX == GAME_WIDTH - mapWidth) ? mapPosX:(mapPosX - TILE_INCR)));
  122. break;
  123.  
  124. }
  125.  
  126. g.drawImage(gameEngine.getCurrentMap().getImage(),mapPosX,mapPosY,this);
  127. g.drawImage(Cursor,gameEngine.getCursorX(),gameEngine.getCursorY(),this);
  128.  
  129. }
  130.  
  131. }
  132.  
  133. //Actually display the frame
  134. public static void main(String[] args) {
  135.  
  136. gFrame = new JFrame("Fire Emblem!");
  137.  
  138. GameFrame game = new GameFrame(gFrame);
  139.  
  140. Container contentPane = gFrame.getContentPane();
  141.  
  142. game.addPanelToFrame(contentPane);
  143. contentPane.setLayout(new FlowLayout());
  144.  
  145. //label.setIcon(FELogo);
  146.  
  147. //JLabel label = new JLabel(FELogo);
  148. //contentPane.add(label);
  149.  
  150. gFrame.pack();
  151. //gFrame.setSize(preferredSize);
  152. gFrame.setResizable(false);
  153. gFrame.setVisible(true);
  154. gFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  155.  
  156. }
  157.  
  158. }
  159.  
  160.  
  161. package graphics.main;
  162.  
  163. import javax.swing.JFrame;
  164. import java.awt.Container;
  165. import java.awt.event.KeyListener;
  166. import java.awt.event.ActionListener;
  167. import javax.swing.JPanel;
  168. import javax.swing.BoxLayout;
  169. import javax.swing.JLabel;
  170. import javax.swing.WindowConstants;
  171. import java.awt.Dimension;
  172. import java.awt.Graphics;
  173. import java.awt.Color;
  174. import java.awt.FlowLayout;
  175. import engine.FireEmblemEngine;
  176. import engine.system.*;
  177. import engine.system.internal.*;
  178. import engine.chapterdata.*;
  179. import graphics.img.fireemblem.*;
  180.  
  181. /**
  182. * This class displays the basic game frame.
  183. */
  184.  
  185. public class GameFrame extends JPanel implements GameConstants,FEImages,InternalCommands {
  186.  
  187. protected static final int GAME_WIDTH = 640;
  188. protected static final int GAME_HEIGHT = 480;
  189.  
  190. protected FireEmblemEngine gameEngine;
  191.  
  192. //JFrame gFrame;
  193. protected static JFrame gFrame;
  194.  
  195. private static final Dimension preferredSize = new Dimension(GAME_WIDTH,GAME_HEIGHT);
  196. private static int mapPosX = 0;
  197. private static int mapPosY = 0;
  198.  
  199. //Finds the size of the frame.
  200.  
  201. public Dimension getPreferredSize() {
  202.  
  203. return preferredSize;
  204.  
  205. }
  206.  
  207. public static JFrame getCurrentFrame() {
  208.  
  209. return gFrame;
  210.  
  211. }
  212.  
  213. //Whee, constructor.
  214.  
  215. public GameFrame(JFrame j) {
  216.  
  217. gameEngine = new FireEmblemEngine(this);
  218. j.addKeyListener(gameEngine);
  219.  
  220. }
  221.  
  222. public JFrame getFrame() {
  223.  
  224. if (gFrame != null)
  225. {
  226.  
  227. return gFrame;
  228.  
  229. }
  230. else
  231. {
  232.  
  233. return null;
  234.  
  235. }
  236.  
  237. }
  238.  
  239. public void addPanelToFrame(Container c) {
  240.  
  241. c.add(this);
  242.  
  243. }
  244.  
  245. public void paintComponent(Graphics g) {
  246.  
  247. super.paintComponent(g);
  248.  
  249. //Fill the entire screen with white, dummy at the moment
  250. g.setColor(Color.WHITE);
  251. g.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);
  252.  
  253. //g.drawImage(gameEngine.getCurrentMap().getImage());
  254.  
  255. switch(gameEngine.getCurrentMode())
  256. {
  257.  
  258. case MAP_MODE:
  259. int mapHeight = gameEngine.getCurrentMap().getHeight();
  260. int mapWidth = gameEngine.getCurrentMap().getWidth();
  261.  
  262. switch(gameEngine.scrollMap())
  263. {
  264.  
  265. case NONE:
  266. break;
  267.  
  268. case UP:
  269. mapPosY = (((mapPosY == 0) ? mapPosY:(mapPosY + TILE_INCR)));
  270. break;
  271.  
  272. case DOWN:
  273. mapPosY = (((mapPosY == GAME_HEIGHT - mapHeight) ? mapPosY:(mapPosY - TILE_INCR)));
  274. break;
  275.  
  276. case LEFT:
  277. mapPosX = (((mapPosX == 0) ? mapPosX:(mapPosX + TILE_INCR)));
  278. break;
  279.  
  280. case RIGHT:
  281. mapPosX = (((mapPosX == GAME_WIDTH - mapWidth) ? mapPosX:(mapPosX - TILE_INCR)));
  282. break;
  283.  
  284. }
  285.  
  286. g.drawImage(gameEngine.getCurrentMap().getImage(),mapPosX,mapPosY,this);
  287. g.drawImage(Cursor,gameEngine.getCursorX(),gameEngine.getCursorY(),this);
  288.  
  289. }
  290.  
  291. }
  292.  
  293. //Actually display the frame
  294. public static void main(String[] args) {
  295.  
  296. gFrame = new JFrame("Fire Emblem!");
  297.  
  298. GameFrame game = new GameFrame(gFrame);
  299.  
  300. Container contentPane = gFrame.getContentPane();
  301.  
  302. game.addPanelToFrame(contentPane);
  303. contentPane.setLayout(new FlowLayout());
  304.  
  305. //label.setIcon(FELogo);
  306.  
  307. //JLabel label = new JLabel(FELogo);
  308. //contentPane.add(label);
  309.  
  310. gFrame.pack();
  311. //gFrame.setSize(preferredSize);
  312. gFrame.setResizable(false);
  313. gFrame.setVisible(true);
  314. gFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  315.  
  316. }
  317.  
  318. }
  319.  
Advertisement
Add Comment
Please, Sign In to add comment