Guest User

Untitled

a guest
Apr 27th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4.  
  5. public class RollB extends JPanel implements ActionListener{
  6.  
  7. int width = 700; int height = 250; //height, width of panel/window
  8. Timer clock = new Timer(20,this); //timer object with delay 20
  9. // wall values
  10. int NWall = 0; int WWall = 0; int EWall = width; int SWall = height;
  11. // initial ball directions: h increasing by 3, v by 2
  12. int h,v, balls = 20;
  13. int oldH, oldV, theSpeed;
  14. // initial position of ball
  15. int x = 125; int y = 21;
  16.  
  17. Ball[] ball = new Ball[20];
  18. int count = 0;
  19.  
  20. JMenu gameControls = new JMenu("Game Controls");
  21. JMenuItem quit = new JMenuItem("Quit");
  22. JMenuItem stop = new JMenuItem("Stop");
  23. JMenuItem start = new JMenuItem("Start");
  24. JMenuItem speed = new JMenuItem("Speed");
  25. JMenuItem newStart = new JMenuItem("New Start");
  26. JMenuItem ballCount = new JMenuItem("Ball Count");
  27. JTextField input;
  28.  
  29.  
  30. public RollB(JMenuBar b){
  31. b.add(gameControls);
  32. gameControls.add(quit);
  33. quit.addActionListener(this);
  34. gameControls.add(stop);
  35. stop.addActionListener(this);
  36. gameControls.add(start);
  37. start.addActionListener(this);
  38. gameControls.add(speed);
  39. speed.addActionListener(this);
  40. gameControls.add(newStart);
  41. newStart.addActionListener(this);
  42. gameControls.add(ballCount);
  43. ballCount.addActionListener(this);
  44.  
  45. setPreferredSize(new Dimension(width,height));
  46. clock.start();
  47. }
  48. public int randGen() {
  49. if (Math.random()<.5) {
  50. double temp = 1000*Math.random();
  51. return (int)temp%6;
  52. } else {
  53. double temp = -1000*Math.random();
  54. return (int)temp%6;
  55. }
  56.  
  57. }
  58. public void paintComponent(Graphics g){
  59. super.paintComponent(g);
  60. for(int i = 0; i <balls; i++) {
  61. h = randGen();
  62. v = randGen();
  63. ball[count] = new Ball(x,y,50,h,v);
  64. drawBalls(g);
  65. count++;
  66. }
  67. }
  68.  
  69. public void drawBalls(Graphics g){
  70. g.fillOval(ball[count].getX(),ball[count].getY(),ball[count].getR(),ball[count].getR());
  71. }
  72.  
  73. public void reflectBalls(){
  74. // when ball reaches (crosses) a wall, it flips (bounces)
  75. if (ball[count].getY() <= NWall) ball[count].flipVertical();
  76. else if (ball[count].getY() >= (SWall-50)) ball[count].flipVertical();
  77. else if (ball[count].getX() <= WWall) ball[count].flipHorizontal();
  78. else if (ball[count].getX() >= EWall-50) ball[count].flipHorizontal();
  79. }
  80.  
  81. public void advanceBalls(){
  82. // ball moves ahead h,v units
  83. ball[count].advance();
  84. }
  85.  
  86. public void actionPerformed(ActionEvent e){
  87. if (e.getSource() == clock) {
  88. advanceBalls();
  89. reflectBalls();
  90. }
  91. else if(e.getSource() == quit) {
  92. System.exit(0);
  93. }
  94. else if(e.getSource() == stop) {
  95. oldH = ball[count].getH();
  96. oldV = ball[count].getV();
  97. ball[count].setH(0);
  98. ball[count].setV(0);
  99. }
  100. else if(e.getSource() == start) {
  101. ball[count].setH(oldH);
  102. ball[count].setV(oldV);
  103. }
  104. else if(e.getSource() == speed) {
  105. theSpeed = Integer.parseInt(JOptionPane.showInputDialog("Enter a speed. (20 is default. Smaller = faster)"));
  106. clock.setDelay(theSpeed);
  107. }
  108. else if(e.getSource() == ballCount) {
  109. balls = Integer.parseInt(JOptionPane.showInputDialog("Enter a speed. (20 is default. Smaller = faster)"));
  110. repaint();
  111. }
  112.  
  113. repaint();
  114. }
  115. }
Add Comment
Please, Sign In to add comment