Crenox

Moving Star Java Method #2

May 2nd, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 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 this example, the timer will regularly call the run() method of the ScheduleTask class.
  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 java.util.Timer;
  47. import java.util.TimerTask;
  48. import javax.swing.ImageIcon;
  49. import javax.swing.JPanel;
  50.  
  51.  
  52. @SuppressWarnings("serial")
  53. public class Board extends JPanel
  54. {
  55. private final int B_WIDTH = 350;
  56. private final int B_HEIGHT = 350;
  57. private final int INITIAL_X = -40;
  58. private final int INITIAL_Y = -40;
  59. private final int INITIAL_DELAY = 100;
  60. private final int PERIOD_INTERVAL = 25;
  61.  
  62. private Image star;
  63. private Timer timer;
  64. private int x, y;
  65.  
  66. public Board()
  67. {
  68.  
  69. loadImage();
  70. initBoard();
  71. }
  72.  
  73. private void loadImage()
  74. {
  75. ImageIcon ii = new ImageIcon("star.png");
  76. star = ii.getImage();
  77. }
  78.  
  79. private void initBoard()
  80. {
  81. setBackground(Color.BLACK);
  82. setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
  83.  
  84. setDoubleBuffered(true);
  85.  
  86. x = INITIAL_X;
  87. y = INITIAL_Y;
  88.  
  89. // Here we create a timer and schedule a task with a specific interval. There is an initial delay.
  90. timer = new Timer();
  91. timer.scheduleAtFixedRate(new ScheduleTask(),
  92. INITIAL_DELAY, PERIOD_INTERVAL);
  93. }
  94.  
  95.  
  96. @Override
  97. public void paintComponent(Graphics g)
  98. {
  99. super.paintComponent(g);
  100.  
  101. drawStar(g);
  102. }
  103.  
  104. private void drawStar(Graphics g)
  105. {
  106. Graphics2D g2d = (Graphics2D)g;
  107. g2d.drawImage(star, x, y, this);
  108. Toolkit.getDefaultToolkit().sync();
  109. g.dispose();
  110. }
  111.  
  112.  
  113. private class ScheduleTask extends TimerTask {
  114.  
  115. // Each 10ms the timer will call this run() method.
  116. @Override
  117. public void run()
  118. {
  119. x += 1;
  120. y += 1;
  121.  
  122. if (y > B_HEIGHT)
  123. {
  124. y = INITIAL_Y;
  125. x = INITIAL_X;
  126. }
  127.  
  128. repaint();
  129. }
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment