PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mack/main/RPGFrame.java

https://github.com/MackValentine/LTDC
Java | 222 lines | 169 code | 49 blank | 4 comment | 21 complexity | a821fde5c4fb8350a3d6675b93128531 MD5 | raw file
  1. package mack.main;
  2. import java.awt.Color;
  3. import java.awt.FontFormatException;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import javax.imageio.ImageIO;
  9. import javax.swing.JFrame;
  10. import javax.xml.parsers.ParserConfigurationException;
  11. import mack.scene.Scene_Title;
  12. import org.xml.sax.SAXException;
  13. public class RPGFrame extends JFrame {
  14. /**
  15. *
  16. */
  17. private static final long serialVersionUID = 7244025052949037200L;
  18. private static final long TIME_INT = 1000000L;
  19. private static final long TIME_LONG = 1000000000L;
  20. private Graphics backContext;
  21. private Image backBuffer;
  22. private RPGGame frame;
  23. private int old_SizeScreen;
  24. private int fps;
  25. private long wait;
  26. private long frameDelay;
  27. private long milli;
  28. private int nano;
  29. public RPGFrame() throws IOException, ParserConfigurationException,
  30. SAXException, FontFormatException {
  31. this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  32. InputStream is = getClass().getResourceAsStream(
  33. "/pics/Icons/icon16.png");
  34. this.setIconImage(ImageIO.read(is));
  35. this.setTitle("Loading");
  36. frame = new RPGGame(false);
  37. this.setSize(Option.WIDTH * RPGPanel.option.SizeScreen + 6,
  38. Option.HEIGHT * RPGPanel.option.SizeScreen + 29);
  39. this.setLocationRelativeTo(null);
  40. this.setVisible(true);
  41. this.setResizable(false);
  42. this.setContentPane(frame.panel);
  43. this.setTitle("");
  44. this.addKeyListener(RPGGame.keyListener);
  45. backBuffer = this.createImage(160, 160);
  46. backContext = backBuffer.getGraphics();
  47. Color c = new Color(0, 0, 0);
  48. getGraphics().setColor(c);
  49. backContext.setColor(c);
  50. if (getDesiredRate() == 0) {
  51. this.frameDelay = 0;
  52. } else {
  53. this.frameDelay = TIME_LONG / getDesiredRate();
  54. }
  55. try {
  56. Graphics g = getGraphics();
  57. g.setColor(Color.black);
  58. int i = (int) RPGPanel.option.SizeScreen * 160;
  59. int j = (int) RPGPanel.option.SizeScreen * 160;
  60. int x = 0;
  61. int y = 0;
  62. backContext.fillRect(x, y, i, j);
  63. paint(g);
  64. Thread.sleep(200);
  65. } catch (InterruptedException e) {
  66. // TODO Auto-generated catch block
  67. e.printStackTrace();
  68. }
  69. go();
  70. }
  71. public void paint(Graphics g) {
  72. int i = 160;
  73. int j = 160;
  74. if (RPGPanel.option != null) {
  75. if (old_SizeScreen != RPGPanel.option.SizeScreen) {
  76. if (RPGPanel.option.SizeScreen > 1) {
  77. setSize((Option.WIDTH) * RPGPanel.option.SizeScreen + 6,
  78. (Option.HEIGHT) * RPGPanel.option.SizeScreen + 29);
  79. } else {
  80. setSize(Option.WIDTH + 6, Option.HEIGHT + 29);
  81. }
  82. if (old_SizeScreen > RPGPanel.option.SizeScreen) {
  83. this.setLocation(this.getLocation().x + Option.WIDTH / 1,
  84. this.getLocation().y + Option.HEIGHT / 1);
  85. } else {
  86. this.setLocation(this.getLocation().x - Option.WIDTH / 2,
  87. this.getLocation().y - Option.HEIGHT / 2);
  88. }
  89. old_SizeScreen = RPGPanel.option.SizeScreen;
  90. }
  91. i = (int) RPGPanel.option.SizeScreen * 160;
  92. j = (int) RPGPanel.option.SizeScreen * 160;
  93. }
  94. int x = 3;
  95. int y = 26;
  96. if (g != null)
  97. g.drawImage(backBuffer, x, y, i, j, null);
  98. }
  99. private void go() throws IOException, ParserConfigurationException,
  100. SAXException {
  101. long n = 0L;
  102. fps = 0;
  103. int f = 0;
  104. do {
  105. do {
  106. long old_time = System.currentTimeMillis();
  107. long lastTime = System.nanoTime();
  108. update(backContext);
  109. Graphics g = getGraphics();
  110. if (g != null)
  111. paint(g);
  112. this.sync(System.nanoTime() - lastTime);
  113. long new_time = System.currentTimeMillis();
  114. n += new_time - old_time;
  115. f++;
  116. } while (n <= 1000L);
  117. n = 0L;
  118. fps = f;
  119. f = 0;
  120. setTitle((new StringBuilder("Crepuscule")).append(" : ")
  121. .append(fps).toString());
  122. } while (true);
  123. }
  124. private void sync(long time) {
  125. if (getDesiredRate() != 0) {
  126. try {
  127. this.wait = this.frameDelay - time;
  128. if (this.wait > 0) {
  129. this.milli = this.wait / TIME_INT;
  130. this.nano = (int) (this.wait % TIME_INT);
  131. Thread.sleep(this.milli, this.nano);
  132. }
  133. } catch (InterruptedException e) {
  134. }
  135. }
  136. }
  137. private int getDesiredRate() {
  138. return 60;
  139. }
  140. public void update(Graphics g) {
  141. super.update(g);
  142. frame.panel.paint(g);
  143. if (Option.triggerPower()) {
  144. try {
  145. frame.call_scene(new Scene_Title(frame));
  146. } catch (IOException e) {
  147. e.printStackTrace();
  148. } catch (FontFormatException e) {
  149. e.printStackTrace();
  150. } catch (ParserConfigurationException e) {
  151. e.printStackTrace();
  152. } catch (SAXException e) {
  153. e.printStackTrace();
  154. }
  155. }
  156. try {
  157. frame.panel.update();
  158. } catch (ParserConfigurationException e) {
  159. e.printStackTrace();
  160. } catch (SAXException e) {
  161. e.printStackTrace();
  162. } catch (IOException e) {
  163. e.printStackTrace();
  164. } catch (FontFormatException e) {
  165. e.printStackTrace();
  166. }
  167. }
  168. public static void main(String[] args) throws IOException,
  169. ParserConfigurationException, SAXException, FontFormatException {
  170. @SuppressWarnings("unused")
  171. RPGFrame fenetre = new RPGFrame();
  172. }
  173. }