Guest User

Untitled

a guest
Apr 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import java.awt.Dimension;
  2. import java.awt.Graphics;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import javax.swing.JPanel;
  6. import javax.swing.Timer;
  7.  
  8. public class RollA extends JPanel
  9. implements ActionListener
  10. {
  11.  
  12. public RollA()
  13. {
  14. width = 700;
  15. height = 250;
  16. clock = new Timer(20, this);
  17. NWall = 0;
  18. WWall = 0;
  19. EWall = width;
  20. SWall = height;
  21. h = 3;
  22. v = 2;
  23. x = 125;
  24. y = 21;
  25. ball = new Ball(x, y, 50, h, v);
  26. setPreferredSize(new Dimension(width, height));
  27. clock.start();
  28. }
  29.  
  30. public void paintComponent(Graphics g)
  31. {
  32. super.paintComponent(g);
  33. drawBalls(g);
  34. }
  35.  
  36. public void drawBalls(Graphics g)
  37. {
  38. g.fillOval(ball.getX(), ball.getY(), ball.getR(), ball.getR());
  39. }
  40.  
  41. public void reflectBalls()
  42. {
  43. if(ball.getY() <= NWall)
  44. ball.flipVertical();
  45. else
  46. if(ball.getY() >= SWall - 50)
  47. ball.flipVertical();
  48. else
  49. if(ball.getX() <= WWall)
  50. ball.flipHorizontal();
  51. else
  52. if(ball.getX() >= EWall - 50)
  53. ball.flipHorizontal();
  54. }
  55.  
  56. public void advanceBalls()
  57. {
  58. ball.advance();
  59. }
  60.  
  61. public void actionPerformed(ActionEvent e)
  62. {
  63. if(e.getSource() == clock)
  64. {
  65. advanceBalls();
  66. reflectBalls();
  67. }
  68. repaint();
  69. }
  70.  
  71. int width;
  72. int height;
  73. Timer clock;
  74. int NWall;
  75. int WWall;
  76. int EWall;
  77. int SWall;
  78. int h;
  79. int v;
  80. int x;
  81. int y;
  82. Ball ball;
  83. }
Add Comment
Please, Sign In to add comment