Advertisement
mainerimiina

GamePanel

Oct 5th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. package com.maineri.mainepix;
  2.  
  3. import java.awt.*;
  4. import java.awt.image.BufferStrategy;
  5. import java.awt.image.BufferedImage;
  6. import java.awt.event.*;
  7. import java.io.File;
  8.  
  9. import javax.imageio.ImageIO;
  10. import javax.swing.JLabel;
  11. import javax.swing.JPanel;
  12.  
  13. public class GamePanel extends Canvas
  14. implements Runnable, KeyListener{
  15. private static final long serialVersionUID = -2342164197569319147L;
  16. // game thread
  17. private Thread thread;
  18. private boolean running;
  19. private int FPS = 60;
  20. private long targetTime = 1000 / FPS;
  21.  
  22. // image
  23. private BufferedImage image;
  24. private Graphics2D g;
  25.  
  26. // game state manager
  27. private GameStateManager gsm;
  28.  
  29. public GamePanel() {
  30. super();
  31. setFocusable(true);
  32. requestFocus();
  33. }
  34.  
  35. public void addNotify() {
  36. super.addNotify();
  37. if(thread == null) {
  38. thread = new Thread(this, "Display");
  39. addKeyListener(this);
  40. thread.start();
  41. }
  42. }
  43.  
  44. public static class HandlerClass implements MouseListener, MouseMotionListener {
  45.  
  46. public void mouseDragged(MouseEvent me) {
  47.  
  48. }
  49.  
  50. public void mouseMoved(MouseEvent me) {
  51.  
  52. }
  53.  
  54. public void mouseClicked(MouseEvent me) {
  55. System.exit(0);
  56. }
  57.  
  58. public void mouseEntered(MouseEvent me) {
  59.  
  60. }
  61.  
  62. public void mouseExited(MouseEvent me) {
  63. System.exit(0);
  64. }
  65.  
  66. public void mousePressed(MouseEvent me) {
  67. System.exit(0);
  68. }
  69.  
  70. public void mouseReleased(MouseEvent me) {
  71. System.exit(0);
  72. }
  73.  
  74. }
  75.  
  76. static BufferedImage logBox;
  77.  
  78. private void init() {
  79.  
  80. JPanel jp = new JPanel();
  81. JLabel jl = new JLabel();
  82. HandlerClass handler = new HandlerClass();
  83. jl.addMouseListener(handler);
  84. jl.addMouseMotionListener(handler);
  85. jp.addMouseListener(handler);
  86. jp.addMouseMotionListener(handler);
  87.  
  88. Game gam = new Game();
  89. gam.frame.addMouseListener(handler);
  90. gam.frame.addMouseMotionListener(handler);
  91.  
  92. try {
  93. logBox = ImageIO.read(new File(getClass().getResource("LogginBox.png").toURI()));
  94. }catch(Exception E) {
  95. E.printStackTrace();
  96. }
  97.  
  98. running = true;
  99.  
  100. gsm = new GameStateManager();
  101.  
  102. }
  103.  
  104. public void run() {
  105.  
  106. init();
  107.  
  108. long start;
  109. long elapsed;
  110. long wait;
  111.  
  112. // game loop
  113. while(running) {
  114.  
  115. start = System.nanoTime();
  116.  
  117. update();
  118. draw();
  119.  
  120. elapsed = System.nanoTime() - start;
  121.  
  122. wait = targetTime - elapsed / 1000000;
  123. if(wait < 0) wait = 5;
  124.  
  125. try {
  126. Thread.sleep(wait);
  127. }
  128. catch(Exception e) {
  129. e.printStackTrace();
  130. }
  131.  
  132. System.out.println(FPS);
  133.  
  134. }
  135.  
  136. }
  137.  
  138. private void update() {
  139. gsm.update();
  140. }
  141.  
  142. private void draw() {
  143. BufferStrategy bs = getBufferStrategy();
  144. if(bs == null) {
  145. createBufferStrategy(3);
  146. return;
  147. }
  148.  
  149. Font font1 = new Font("Serif", Font.BOLD, 90);
  150. Font font2 = new Font("Serif", Font.BOLD, 12);
  151.  
  152. Graphics g2 = (Graphics2D) bs.getDrawGraphics();
  153.  
  154. if(gsm.currentState == 1) {
  155. //GAMESTATE
  156. }
  157. if(gsm.currentState == 0) {
  158. //MENUSTATE
  159. g2.setColor(Color.green);
  160. g2.fillRect(0, 0, 600, 400);
  161. g2.setColor(Color.gray);
  162. g2.setFont(font2);
  163. g2.drawString("Copyright Maineri ©", 2, 10);
  164.  
  165. }
  166. /*if(MenuState == true) {
  167.  
  168. }
  169. if(MenuState == true) {
  170.  
  171. }
  172. if(MenuState == true) {
  173.  
  174. }
  175. */
  176.  
  177. g2.dispose();
  178. bs.show();
  179. }
  180.  
  181. public void keyTyped(KeyEvent key) {}
  182. public void keyPressed(KeyEvent key) {
  183. gsm.keyPressed(key.getKeyCode());
  184. }
  185. public void keyReleased(KeyEvent key) {
  186. gsm.keyReleased(key.getKeyCode());
  187. }
  188.  
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement