Advertisement
ssun098

simpanimstar

Sep 17th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3.  
  4. import javax.swing.*;
  5.  
  6. /**
  7. * This file can be used to create very simple animations. Just fill in
  8. * the definition of drawFrame with the code to draw one frame of the
  9. * animation, and possibly change a few of the values in the rest of
  10. * the program as noted below. Note that if you change the name of the
  11. * class, you must also change the name in the main() routine!
  12. */
  13. public class SimpleAnimationStarter extends JPanel implements ActionListener {
  14.  
  15. /**
  16. * Draws one frame of an animation. This subroutine is called re
  17. * second and is responsible for redrawing the entire drawing area. The
  18. * parameter g is used for drawing. The frameNumber starts at zero and
  19. * increases by 1 each time this subroutine is called. The parameters width
  20. * and height give the size of the drawing area, in pixels.
  21. * The sizes and positions of the rectangles that are drawn depend
  22. * on the frame number, giving the illusion of motion.
  23. */
  24. public void drawFrame(Graphics g, int frameNumber, int width, int height) {
  25.  
  26. /* NOTE: To get a different animation, just erase the contents of this
  27. * subroutine and substitute your own. If you don't fill the picture
  28. * with some other color, the background color will be white. The sample
  29. * code here just shows the frame number.
  30. */
  31.  
  32. int row; // Row number, from 0 to 7
  33. int col; // Column number, from 0 to 7
  34. int x,y; // Top-left corner of square
  35. int everyFifty = ((frameNumber/ 50) % 2);
  36. for ( row = 0; row < 8; row++ ) {
  37. for ( col = 0; col < 8; col++) {
  38. x = col * 20;
  39. y = row * 20;
  40. if (everyFifty == 0) {
  41. if ( (row % 2) == (col % 2) )
  42. g.setColor(Color.red);
  43. else
  44. g.setColor(Color.black);
  45. }
  46. else {
  47. if ( (row % 2) == (col % 2) )
  48. g.setColor(Color.black);
  49. else
  50. g.setColor(Color.red);
  51. }
  52. g.fillRect(x, y, 20, 20);
  53. }
  54. }
  55.  
  56. }
  57.  
  58. //------ Implementation details: DO NOT EXPECT TO UNDERSTAND THIS ------
  59.  
  60.  
  61. public static void main(String[] args) {
  62.  
  63. /* NOTE: The string in the following statement goes in the title bar
  64. * of the window.
  65. */
  66. JFrame window = new JFrame("Simple Animation");
  67.  
  68. /*
  69. * NOTE: If you change the name of this class, you must change
  70. * the name of the class in the next line to match!
  71. */
  72. SimpleAnimationStarter drawingArea = new SimpleAnimationStarter();
  73.  
  74. drawingArea.setBackground(Color.WHITE);
  75. window.setContentPane(drawingArea);
  76.  
  77. /* NOTE: In the next line, the numbers 600 and 450 give the
  78. * initial width and height of the drawing array. You can change
  79. * these numbers to get a different size.
  80. */
  81. drawingArea.setPreferredSize(new Dimension(600,450));
  82.  
  83. window.pack();
  84. window.setLocation(100,50);
  85. window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  86.  
  87.  
  88. /*
  89. * Note: In the following line, you can change true to
  90. * false. This will prevent the user from resizing the window,
  91. * so you can be sure that the size of the drawing area will
  92. * not change. It can be easier to draw the frames if you know
  93. * the size.
  94. */
  95. window.setResizable(true);
  96.  
  97. /* NOTE: In the next line, the number 20 gives the time between
  98. * calls to drawFrame(). The time is given in milliseconds, where
  99. * one second equals 1000 milliseconds. You can increase this number
  100. * to get a slower animation. You can decrease it somewhat to get a
  101. * faster animation, but the speed is limited by the time it takes
  102. * for the computer to draw each frame.
  103. */
  104. Timer frameTimer = new Timer(20,drawingArea);
  105.  
  106. window.setVisible(true);
  107. frameTimer.start();
  108.  
  109. } // end main
  110.  
  111. private int frameNum;
  112.  
  113. public void actionPerformed(ActionEvent evt) {
  114. frameNum++;
  115. repaint();
  116. }
  117.  
  118. protected void paintComponent(Graphics g) {
  119. super.paintComponent(g);
  120. drawFrame(g, frameNum, getWidth(), getHeight());
  121. }
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement