ace

boxbounce3 no really!

ace
Mar 3rd, 2010
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.geom.*;
  3. import java.lang.Number;
  4. import java.util.ArrayList;
  5. import java.util.Iterator;
  6. import java.util.Random;
  7.  
  8. /**
  9. * Class BallDemo - provides two short demonstrations showing how to use the
  10. * Canvas class.
  11. *
  12. * @author Michael Kolling and David J. Barnes
  13. * @version 2006.03.30
  14. */
  15.  
  16. public class BoxBounce
  17. {
  18. private Canvas myCanvas;
  19. private ArrayList <BouncingBall> ballResevoir;
  20. private Random resevoirCapacity;
  21. private Random randomXPos;
  22. private Random randomYPos;
  23. private Random randomBallSize;
  24. private int width;
  25. private int height;
  26. private int boxWidth;
  27. private int boxHeight;
  28.  
  29. /**
  30. * Create a BallDemo object. Creates a fresh canvas and makes it visible.
  31. */
  32. public BoxBounce(int width, int height)
  33. {
  34. myCanvas = new Canvas("Box Bounce", width, height);
  35. myCanvas.setVisible(true);
  36. this.width = width;
  37. this.height = height;
  38.  
  39. }
  40.  
  41. /**
  42. * Demonstrate some of the drawing operations that are
  43. * available on a Canvas object.
  44. */
  45. public void drawDemo()
  46. {
  47. myCanvas.setFont(new Font("helvetica", Font.BOLD, 14));
  48. myCanvas.setForegroundColor(Color.red);
  49.  
  50. myCanvas.drawString("We can draw text, ...", 20, 30);
  51. myCanvas.wait(1000);
  52.  
  53. myCanvas.setForegroundColor(Color.black);
  54. myCanvas.drawString("...draw lines...", 60, 60);
  55. myCanvas.wait(500);
  56. myCanvas.setForegroundColor(Color.gray);
  57. myCanvas.drawLine(200, 20, 300, 50);
  58. myCanvas.wait(500);
  59. myCanvas.setForegroundColor(Color.blue);
  60. myCanvas.drawLine(220, 100, 370, 40);
  61. myCanvas.wait(500);
  62. myCanvas.setForegroundColor(Color.green);
  63. myCanvas.drawLine(290, 10, 320, 120);
  64. myCanvas.wait(1000);
  65.  
  66. myCanvas.setForegroundColor(Color.gray);
  67. myCanvas.drawString("...and shapes!", 110, 90);
  68.  
  69. myCanvas.setForegroundColor(Color.red);
  70.  
  71. // the shape to draw and move
  72. int xPos = 10;
  73. Rectangle rect = new Rectangle(xPos, 150, 30, 20);
  74.  
  75. // move the rectangle across the screen
  76. for(int i = 0; i < 200; i ++) {
  77. myCanvas.fill(rect);
  78. myCanvas.wait(10);
  79. myCanvas.erase(rect);
  80. xPos++;
  81. rect.setLocation(xPos, 150);
  82. }
  83. // at the end of the move, draw once more so that it remains visible
  84. myCanvas.fill(rect);
  85. }
  86.  
  87. public void drawBox()
  88.  
  89. {
  90. int cHeight = new Double(myCanvas.getHeight()).intValue();
  91. int cWidth = new Double(myCanvas.getWidth()).intValue();
  92. int boxHeight = (cHeight - cHeight/2);
  93. int boxWidth = (cWidth -cWidth/2);
  94. Rectangle frame = new Rectangle(20, 20, boxWidth, boxHeight);
  95. myCanvas.fill(frame);
  96. myCanvas.wait(200);
  97. myCanvas.eraseRectangle (40, 40, boxWidth-40, boxHeight-40);
  98. this.boxWidth = boxWidth;
  99. this.boxHeight = boxHeight;
  100. }
  101.  
  102.  
  103.  
  104. /* Simulate two bouncing balls
  105. */
  106.  
  107. public int setballXPos()
  108. {
  109. int ballXPos = randomXPos.nextInt(boxWidth-20);
  110. if (ballXPos > 40 & ballXPos < boxWidth - 40)
  111. {
  112. System.out.println("ball xpos " + ballXPos);
  113. return ballXPos;
  114. }
  115.  
  116. else
  117. {
  118. setballXPos();
  119. }
  120. return ballXPos;
  121. }
  122.  
  123. public int setballYPos ()
  124. {
  125. int ballYPos = randomYPos.nextInt(boxHeight-20);
  126. System.out.println("ball ypos " + ballYPos);
  127. return ballYPos;
  128. }
  129.  
  130. public void bounce()
  131. {
  132. int ground = 400; // position of the ground line
  133.  
  134. myCanvas.setVisible(true);
  135.  
  136. // draw the ground
  137. myCanvas.drawLine(50, ground, 550, ground);
  138.  
  139. BouncingBall ball = new BouncingBall(setballXPos(), setballYPos(), 16, Color.blue, ground, myCanvas);
  140. ball.draw();
  141. BouncingBall ball2 = new BouncingBall(70, 80, 20, Color.red, ground, myCanvas);
  142. ball2.draw();
  143.  
  144.  
  145. // make them bounce
  146. boolean finished = false;
  147. while(!finished) {
  148. myCanvas.wait(50); // small delay
  149. ball.move();
  150. ball2.move();
  151. // stop once ball has travelled a certain distance on x axis
  152. if(ball.getXPosition() >= 550 && ball2.getXPosition() >= 550) {
  153. finished = true;
  154. }
  155. }
  156. ball.erase();
  157. ball2.erase();
  158.  
  159.  
  160.  
  161.  
  162. }
  163.  
  164. }
Add Comment
Please, Sign In to add comment