Crenox

Moving Star Java Method #3

May 2nd, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. package com.java.main;
  2.  
  3. import java.awt.EventQueue;
  4. import javax.swing.JFrame;
  5.  
  6. @SuppressWarnings("serial")
  7. public class Main extends JFrame
  8. {
  9. public Main()
  10. {
  11. add(new Board());
  12.  
  13. setTitle("Star");
  14.  
  15. setResizable(false);
  16. pack();
  17. setLocationRelativeTo(null);
  18. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19. }
  20.  
  21. public static void main(String[] args)
  22. {
  23. EventQueue.invokeLater(new Runnable()
  24. {
  25.  
  26. @Override
  27. public void run()
  28. {
  29. JFrame ex = new Main();
  30. ex.setVisible(true);
  31. }
  32. });
  33. }
  34. }
  35. -----------------------------------------------------------------------------------------------------------------------------------
  36. // In the previous examples, we executed a task at specific intervals. In this example, the animation will take place inside a thread. The run() method is called only once. This is why why we have a while loop in the method. From this method, we call the cycle() and the repaint() methods.
  37.  
  38. package com.java.main;
  39.  
  40. import java.awt.Color;
  41. import java.awt.Dimension;
  42. import java.awt.Graphics;
  43. import java.awt.Graphics2D;
  44. import java.awt.Image;
  45. import java.awt.Toolkit;
  46. import javax.swing.ImageIcon;
  47. import javax.swing.JPanel;
  48.  
  49. @SuppressWarnings("serial")
  50. public class Board extends JPanel implements Runnable {
  51.  
  52. private final int B_WIDTH = 350;
  53. private final int B_HEIGHT = 350;
  54. private final int INITIAL_X = -40;
  55. private final int INITIAL_Y = -40;
  56. private final int DELAY = 25;
  57.  
  58. private Image star;
  59. private Thread animator;
  60. private int x, y;
  61.  
  62. public Board()
  63. {
  64. loadImage();
  65. initBoard();
  66. }
  67.  
  68. private void loadImage()
  69. {
  70. ImageIcon ii = new ImageIcon("star.png");
  71. star = ii.getImage();
  72. }
  73.  
  74. private void initBoard()
  75. {
  76. setBackground(Color.BLACK);
  77. setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
  78. setDoubleBuffered(true);
  79.  
  80. x = INITIAL_X;
  81. y = INITIAL_Y;
  82. }
  83.  
  84. // The addNotify() method is called after our JPanel has been added to the JFrame component. This method is often used for various initialization tasks.
  85. // We want our game run smoothly, at constant speed. Therefore we compute the system time.
  86. @Override
  87. public void addNotify()
  88. {
  89. super.addNotify();
  90.  
  91. animator = new Thread(this);
  92. animator.start();
  93. }
  94.  
  95. @Override
  96. public void paintComponent(Graphics g)
  97. {
  98. super.paintComponent(g);
  99.  
  100. drawStar(g);
  101. }
  102.  
  103. private void drawStar(Graphics g)
  104. {
  105. Graphics2D g2d = (Graphics2D) g;
  106. g2d.drawImage(star, x, y, this);
  107. Toolkit.getDefaultToolkit().sync();
  108. g.dispose();
  109. }
  110.  
  111. private void cycle()
  112. {
  113. x += 1;
  114. y += 1;
  115.  
  116. if (y > B_HEIGHT)
  117. {
  118. y = INITIAL_Y;
  119. x = INITIAL_X;
  120. }
  121. }
  122.  
  123. @Override
  124. public void run()
  125. {
  126. long beforeTime, timeDiff, sleep;
  127.  
  128. beforeTime = System.currentTimeMillis();
  129.  
  130. while (true)
  131. {
  132. cycle();
  133. repaint();
  134.  
  135. // The cycle() and the repaint() methods might take different time at various while cycles. We calculate the time both methods run and subtract it from the DELAY constant. This way we want to ensure that each while cycle runs a constant time. In our case, it is DELAYms each cycle.
  136. timeDiff = System.currentTimeMillis() - beforeTime;
  137. sleep = DELAY - timeDiff;
  138.  
  139. if (sleep < 0)
  140. {
  141. sleep = 2;
  142. }
  143.  
  144. try
  145. {
  146. Thread.sleep(sleep);
  147. }
  148. catch (InterruptedException e)
  149. {
  150. System.out.println("Interrupted: " + e.getMessage());
  151. }
  152.  
  153. beforeTime = System.currentTimeMillis();
  154. }
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment