Guest User

Untitled

a guest
Apr 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 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 java.util.Random;
  6. import javax.swing.*;
  7.  
  8. public class RollB extends JPanel
  9. implements ActionListener
  10. {
  11.  
  12. public RollB(JMenuBar b)
  13. {
  14. width = 700;
  15. height = 250;
  16. t = new Timer(20, this);
  17. NWall = 0;
  18. WWall = 0;
  19. EWall = width;
  20. SWall = height;
  21. r = new Random();
  22. ballCount = 20;
  23. balls = new Ball[20];
  24. gameControls = new JMenu("Game Controls");
  25. quit = new JMenuItem("Quit");
  26. stop = new JMenuItem("Stop");
  27. start = new JMenuItem("Start");
  28. speed = new JMenuItem("Speed");
  29. newstart = new JMenuItem("New Start");
  30. ballCt = new JMenuItem("Ball Count");
  31. this.b = b;
  32. b.add(gameControls);
  33. gameControls.add(quit);
  34. quit.addActionListener(this);
  35. gameControls.add(stop);
  36. stop.addActionListener(this);
  37. gameControls.add(start);
  38. start.addActionListener(this);
  39. gameControls.add(speed);
  40. speed.addActionListener(this);
  41. gameControls.add(newstart);
  42. ballCt.addActionListener(this);
  43. gameControls.add(ballCt);
  44. newstart.addActionListener(this);
  45. for(int k = 0; k < balls.length; k++)
  46. balls[k] = new Ball(initX(), initY(), 50, initDir(), initDir());
  47.  
  48. setPreferredSize(new Dimension(width, height));
  49. t.start();
  50. }
  51.  
  52. public void paintComponent(Graphics g)
  53. {
  54. super.paintComponent(g);
  55. drawBalls(g);
  56. }
  57.  
  58. public void drawBalls(Graphics g)
  59. {
  60. Ball arr$[] = balls;
  61. int len$ = arr$.length;
  62. for(int i$ = 0; i$ < len$; i$++)
  63. {
  64. Ball b = arr$[i$];
  65. g.fillOval(b.getX(), b.getY(), b.getR(), b.getR());
  66. }
  67.  
  68. }
  69.  
  70. public void reflectBalls()
  71. {
  72. Ball arr$[] = balls;
  73. int len$ = arr$.length;
  74. for(int i$ = 0; i$ < len$; i$++)
  75. {
  76. Ball b = arr$[i$];
  77. if(b.getY() <= NWall)
  78. {
  79. b.flipVertical();
  80. continue;
  81. }
  82. if(b.getY() >= SWall - 50)
  83. {
  84. b.flipVertical();
  85. continue;
  86. }
  87. if(b.getX() <= WWall)
  88. {
  89. b.flipHorizontal();
  90. continue;
  91. }
  92. if(b.getX() >= EWall - 50)
  93. b.flipHorizontal();
  94. }
  95.  
  96. }
  97.  
  98. public void advanceBalls()
  99. {
  100. Ball arr$[] = balls;
  101. int len$ = arr$.length;
  102. for(int i$ = 0; i$ < len$; i$++)
  103. {
  104. Ball b = arr$[i$];
  105. b.advance();
  106. }
  107.  
  108. }
  109.  
  110. public void initBalls()
  111. {
  112. Ball arr$[] = balls;
  113. int len$ = arr$.length;
  114. for(int i$ = 0; i$ < len$; i$++)
  115. {
  116. Ball b = arr$[i$];
  117. b.setX(initX());
  118. b.setY(initY());
  119. b.setH(initDir());
  120. b.setV(initDir());
  121. }
  122.  
  123. }
  124.  
  125. private int initX()
  126. {
  127. return width / 4 + r.nextInt(width / 2);
  128. }
  129.  
  130. private int initY()
  131. {
  132. return height / 4 + r.nextInt(height / 2);
  133. }
  134.  
  135. private int initDir()
  136. {
  137. return -5 + r.nextInt(10);
  138. }
  139.  
  140. public void actionPerformed(ActionEvent e)
  141. {
  142. if(e.getSource() == t)
  143. {
  144. advanceBalls();
  145. reflectBalls();
  146. repaint();
  147. } else
  148. if(e.getSource() == quit)
  149. System.exit(0);
  150. else
  151. if(e.getSource() == stop)
  152. t.stop();
  153. else
  154. if(e.getSource() == start)
  155. t.start();
  156. else
  157. if(e.getSource() == newstart)
  158. {
  159. initBalls();
  160. t.start();
  161. } else
  162. if(e.getSource() == ballCt)
  163. {
  164. t.stop();
  165. String s = JOptionPane.showInputDialog("enter ball count");
  166. int k = Integer.parseInt(s);
  167. balls = new Ball[k];
  168. for(int j = 0; j < balls.length; j++)
  169. balls[j] = new Ball(initX(), initY(), 50, initDir(), initDir());
  170.  
  171. t.start();
  172. } else
  173. if(e.getSource() == speed)
  174. {
  175. t.stop();
  176. String s = JOptionPane.showInputDialog("enter delay - smaller=faster");
  177. int j = Integer.parseInt(s);
  178. t.setDelay(j);
  179. t.start();
  180. }
  181. }
  182.  
  183. int width;
  184. int height;
  185. Timer t;
  186. int NWall;
  187. int WWall;
  188. int EWall;
  189. int SWall;
  190. Random r;
  191. int h;
  192. int v;
  193. int x;
  194. int y;
  195. int ballCount;
  196. Ball balls[];
  197. JMenuBar b;
  198. JMenu gameControls;
  199. JMenuItem quit;
  200. JMenuItem stop;
  201. JMenuItem start;
  202. JMenuItem speed;
  203. JMenuItem newstart;
  204. JMenuItem ballCt;
  205. }
Add Comment
Please, Sign In to add comment