Guest User

Untitled

a guest
Apr 25th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 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. JMenuBar b;
  8. JMenu menu = new JMenu("Game Controls");
  9. JMenuItem quit = new JMenuItem("Quit");
  10. JMenuItem stop = new JMenuItem("Stop");
  11. JMenuItem start = new JMenuItem("Start");
  12. JMenuItem speed = new JMenuItem("Speed");
  13. JMenuItem newstart = new JMenuItem("New Start");
  14. JMenuItem ballcount = new JMenuItem("Ball Count");
  15. private int tempH;
  16. private int tempV;
  17. private int tempR;
  18. private int ballCount = 20;
  19. private String newspeed;
  20. int width = 700;
  21. int height = 250;
  22. Timer clock = new Timer(20,this);
  23. int NWall = 0; int WWall = 0; int EWall = width; int SWall = height;
  24. int h = ((int)(Math.random() * 10) - 5);
  25. int v = ((int)(Math.random() * 10) - 5);
  26. int r = 50;
  27. int x = (r + ((int)(Math.random() * (width - r)) - r));
  28. int y = (r + ((int)(Math.random() * (height - r)) - r));
  29. Ball ball = new Ball(x,y,r,h,v);
  30.  
  31. public RollB(JMenuBar bar){
  32. setPreferredSize(new Dimension(width,height));
  33. clock.start();
  34. this.b = bar;
  35. b.add(menu);
  36. menu.add(quit);
  37. menu.add(stop);
  38. menu.add(start);
  39. menu.add(speed);
  40. menu.add(newstart);
  41. menu.add(ballcount);
  42. quit.addActionListener(this);
  43. stop.addActionListener(this);
  44. start.addActionListener(this);
  45. speed.addActionListener(this);
  46. newstart.addActionListener(this);
  47. ballcount.addActionListener(this);
  48. Ball[] ballarray = new Ball[20]
  49. }
  50.  
  51. public void paintComponent(Graphics g){
  52. super.paintComponent(g);
  53. drawBalls(g);
  54. }
  55.  
  56. public void drawBalls(Graphics g){
  57. g.fillOval(ball.getX(),ball.getY(),ball.getR(),ball.getR());
  58. }
  59.  
  60. public void reflectBalls(){
  61. // when ball reaches (crosses) a wall, it flips (bounces)
  62. if (ball.getY() <= NWall) ball.flipVertical();
  63. else if (ball.getY() >= (SWall-50)) ball.flipVertical();
  64. else if (ball.getX() <= WWall) ball.flipHorizontal();
  65. else if (ball.getX() >= EWall-50) ball.flipHorizontal();
  66. }
  67.  
  68. public void advanceBalls(){
  69. // ball moves ahead h,v units
  70. ball.advance();
  71. }
  72.  
  73. public void actionPerformed(ActionEvent e){
  74. if (e.getSource() == quit){
  75. System.exit(0);
  76. }
  77. if (e.getSource() == stop){
  78. tempH = ball.getH();
  79. tempV = ball.getV();
  80. ball.setH(0);
  81. ball.setV(0);
  82. }
  83. if (e.getSource() == start){
  84. ball.setH(tempH);
  85. ball.setV(tempV);
  86. }
  87. if (e.getSource() == speed){
  88. newspeed = JOptionPane.showInputDialog("enter delay - smaller = faster");
  89. clock.setDelay(Integer.parseInt(newspeed));
  90. }
  91. if (e.getSource() == newstart){
  92. ball.setH((int)(Math.random() * 10) - 5);
  93. ball.setV((int)(Math.random() * 10) - 5);
  94. ball.setX(r + ((int)(Math.random() * (width - r)) - r));
  95. ball.setY(r + ((int)(Math.random() * (height - r)) - r));
  96. }
  97.  
  98. if (e.getSource() == clock) {
  99. advanceBalls();
  100. reflectBalls();
  101. }
  102. repaint();
  103. }
  104. }
Add Comment
Please, Sign In to add comment