Advertisement
Guest User

Untitled

a guest
Apr 30th, 2014
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. public class Main extends Canvas {
  2.  
  3. //Static Variables
  4. public static Main main;
  5.  
  6. public static String name = "Game";
  7. public static double version = 1.0;
  8.  
  9. public static int FPS;
  10.  
  11. //Object Variables
  12. private JFrame frame;
  13.  
  14. private boolean running;
  15.  
  16. private int screenX;
  17. private int screenY;
  18.  
  19. private int x = 0;
  20.  
  21. //Constructor
  22. public Main() {
  23. setSize(new Dimension(500, 500));
  24. }
  25.  
  26. //Main Method
  27. public static void main(String[] args) {
  28. main = new Main();
  29.  
  30. main.init();
  31. }
  32.  
  33. //Object Methods
  34. private void init() {
  35. frame = new JFrame(name);
  36. frame.setSize(500, 500);
  37. frame.setResizable(false);
  38. frame.setVisible(true);
  39. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  40.  
  41. frame.add(main);
  42.  
  43. loop();
  44. }
  45.  
  46. public void loop() {
  47. running = true;
  48.  
  49. int fps = 0;
  50.  
  51. long timer = System.currentTimeMillis();
  52. long lastTime = System.nanoTime();
  53.  
  54. final double ns = 1000000000.0 / 60;
  55. double delta = 0;
  56.  
  57. while (running) {
  58.  
  59. long now = System.nanoTime();
  60. delta += (now - lastTime) / ns;
  61. lastTime = now;
  62.  
  63. while (delta >= 1) {
  64.  
  65. if (fps <= 60) {
  66. fps++;
  67.  
  68. update();
  69. frame.repaint();
  70.  
  71. delta--;
  72. }
  73. }
  74.  
  75. if (System.currentTimeMillis() - timer > 1000) {
  76. timer += 1000;
  77.  
  78. log("Running at " + fps + " FPS and UPS");
  79.  
  80. FPS = fps;
  81.  
  82. fps = 0;
  83.  
  84. }
  85. }
  86.  
  87. }
  88.  
  89. public void update() {
  90. screenX = frame.getWidth();
  91. screenY = frame.getHeight();
  92.  
  93. x++;
  94.  
  95. if (x >= 500) x = 0;
  96.  
  97. log("update");
  98.  
  99. //update gametstate
  100. }
  101.  
  102. public void log(String string) {
  103. System.out.println("[" + name + "] [" + version + "] " + string);
  104. }
  105.  
  106. public void log() {
  107. System.out.println("[" + name + "] [" + version + "]");
  108. }
  109.  
  110. @Override
  111. public void paint(Graphics graphics) {
  112. graphics.setColor(Color.WHITE);
  113. graphics.fillRect(0, 0, screenX, screenY);
  114.  
  115. //update gamestate
  116.  
  117. graphics.setColor(Color.BLUE);
  118. graphics.fillRect(0, 200, x, 300);
  119. log("rendered");
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement