Crenox

Moving Star Java Method #1

Apr 28th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. package com.zetcode.main;
  2.  
  3. import java.awt.EventQueue;
  4. import javax.swing.JFrame;
  5.  
  6. @SuppressWarnings("serial")
  7. public class Zetcode extends JFrame
  8. {
  9.  
  10. public Zetcode()
  11. {
  12.  
  13. add(new Board());
  14.  
  15. setTitle("Star");
  16.  
  17. setResizable(false);
  18. pack();
  19. setLocationRelativeTo(null);
  20. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21. }
  22.  
  23. public static void main(String[] args)
  24. {
  25. EventQueue.invokeLater(new Runnable()
  26. {
  27.  
  28. @Override
  29. public void run()
  30. {
  31. JFrame ex = new Zetcode();
  32. ex.setVisible(true);
  33. }
  34. });
  35. }
  36. }
  37. -------------------------------------------------------------------------------------------------------------------------------------
  38. /* In the Board class we animate a star that will move from the
  39. * upper left corner to the bottom right corner.
  40. */
  41. package com.zetcode.main;
  42.  
  43. import java.awt.Color;
  44. import java.awt.Dimension;
  45. import java.awt.Graphics;
  46. import java.awt.Graphics2D;
  47. import java.awt.Image;
  48. import java.awt.Toolkit;
  49. import java.awt.event.ActionEvent;
  50. import java.awt.event.ActionListener;
  51. import javax.swing.ImageIcon;
  52. import javax.swing.JPanel;
  53. import javax.swing.Timer;
  54.  
  55. @SuppressWarnings("serial")
  56. public class Board extends JPanel implements ActionListener
  57. {
  58.  
  59. /* Four constants are defined. The first two constants are the board width and height.
  60. * The third and fourth are the initial coordinates of the star.
  61. * The last one determines the speed of the animation.
  62. */
  63. private final int B_WIDTH = 350;
  64. private final int B_HEIGHT = 350;
  65. private final int INITIAL_X = -40;
  66. private final int INITIAL_Y = -40;
  67. private final int DELAY = 25;
  68.  
  69. private Image star;
  70. private Timer timer;
  71. private int x, y;
  72.  
  73. public Board()
  74. {
  75.  
  76. loadImage();
  77. initBoard();
  78. }
  79.  
  80. /* In the loadImage() method we create an instance of the
  81. * ImageIcon class. The image is located in the project
  82. * directory. The getImage() method will return the the Image
  83. * object from this class. This object will be drawn on the
  84. * board.
  85. */
  86. private void loadImage()
  87. {
  88.  
  89. ImageIcon ii = new ImageIcon("star.png");
  90. star = ii.getImage();
  91. }
  92.  
  93. private void initBoard()
  94. {
  95.  
  96. setBackground(Color.BLACK);
  97. setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
  98.  
  99. /* Our JPanel component will use a buffer to paint. This
  100. * means that all drawing will be done in memory first.
  101. * Later the off-screen buffer will be copied to the
  102. * screen. In this simple example, we might not notice
  103. * any differences.
  104. */
  105. setDoubleBuffered(true);
  106.  
  107. x = INITIAL_X;
  108. y = INITIAL_Y;
  109.  
  110. /* Here we create a Swing Timer class and call its start()
  111. * method. Every DELAY ms the timer will call the
  112. * actionPerformed() method. In order to use the
  113. * actionPerformed() method, we must implement the
  114. * ActionListener interface.
  115. */
  116. timer = new Timer(DELAY, this);
  117. timer.start();
  118. }
  119.  
  120. /* Custom painting is done in the paintComponent() method in
  121. * Swing. Note that we also call the paintComponent() method
  122. * of the parent (JPanel). The actual painting is delegated
  123. * to the drawStar() method.
  124. */
  125. @Override
  126. public void paintComponent(Graphics g)
  127. {
  128. super.paintComponent(g);
  129.  
  130. drawStar(g);
  131. }
  132.  
  133. /* In the drawStar() method, we draw the image on the window
  134. * with the usage of the drawImage() method.
  135. */
  136. private void drawStar(Graphics g)
  137. {
  138. Graphics2D g2d = (Graphics2D) g;
  139. g2d.drawImage(star, x, y, this);
  140. /* We must synchronize the painting on Linux systems.
  141. * Otherwise, the animation would not be smooth.
  142. */
  143. Toolkit.getDefaultToolkit().sync();
  144. g.dispose();
  145. }
  146.  
  147. /* In the actionPerformed() method we increase the x, y values
  148. * of the star object. Then we call the repaint() method,
  149. * which will cause the paintComponent() to be called. This
  150. * way we regularly repaint the Board thus making the
  151. * animation.
  152. */
  153. @Override
  154. public void actionPerformed(ActionEvent e)
  155. {
  156.  
  157. x += 1;
  158. y += 1;
  159.  
  160. if (y > B_HEIGHT) {
  161.  
  162. y = INITIAL_Y;
  163. x = INITIAL_X;
  164. }
  165.  
  166. repaint();
  167. }
  168. }
Advertisement
Add Comment
Please, Sign In to add comment