ace

frame3

ace
Mar 10th, 2010
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 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. private int ground;
  29. private int ceiling;
  30. private int rightWall;
  31. private int leftWall;
  32.  
  33. /**
  34. * Create a BallDemo object. Creates a fresh canvas and makes it visible.
  35. */
  36. public BoxBounce(int width, int height)
  37. {
  38. myCanvas = new Canvas("Box Bounce", width, height);
  39. myCanvas.setVisible(true);
  40. this.width = width;
  41. this.height = height;
  42. randomXPos = new Random();
  43. randomYPos = new Random();
  44. }
  45.  
  46. /**
  47. * Demonstrate some of the drawing operations that are
  48. * available on a Canvas object.
  49. */
  50. public void drawDemo()
  51. {
  52. myCanvas.setFont(new Font("helvetica", Font.BOLD, 14));
  53. myCanvas.setForegroundColor(Color.red);
  54.  
  55. myCanvas.drawString("We can draw text, ...", 20, 30);
  56. myCanvas.wait(1000);
  57.  
  58. myCanvas.setForegroundColor(Color.black);
  59. myCanvas.drawString("...draw lines...", 60, 60);
  60. myCanvas.wait(500);
  61. myCanvas.setForegroundColor(Color.gray);
  62. myCanvas.drawLine(200, 20, 300, 50);
  63. myCanvas.wait(500);
  64. myCanvas.setForegroundColor(Color.blue);
  65. myCanvas.drawLine(220, 100, 370, 40);
  66. myCanvas.wait(500);
  67. myCanvas.setForegroundColor(Color.green);
  68. myCanvas.drawLine(290, 10, 320, 120);
  69. myCanvas.wait(1000);
  70.  
  71. myCanvas.setForegroundColor(Color.gray);
  72. myCanvas.drawString("...and shapes!", 110, 90);
  73.  
  74. myCanvas.setForegroundColor(Color.red);
  75.  
  76. // the shape to draw and move
  77. int xPos = 10;
  78. Rectangle rect = new Rectangle(xPos, 150, 30, 20);
  79.  
  80. // move the rectangle across the screen
  81. for(int i = 0; i < 200; i ++) {
  82. myCanvas.fill(rect);
  83. myCanvas.wait(10);
  84. myCanvas.erase(rect);
  85. xPos++;
  86. rect.setLocation(xPos, 150);
  87. }
  88. // at the end of the move, draw once more so that it remains visible
  89. myCanvas.fill(rect);
  90. }
  91.  
  92. public void drawBox()
  93.  
  94. {
  95. int cHeight = new Double(myCanvas.getHeight()).intValue();
  96. int cWidth = new Double(myCanvas.getWidth()).intValue();
  97. int boxHeight = (cHeight - cHeight/2);
  98. int boxWidth = (cWidth -cWidth/2);
  99. Rectangle frame = new Rectangle(20, 20, boxWidth, boxHeight);
  100. myCanvas.fill(frame);
  101. myCanvas.wait(200);
  102. myCanvas.eraseRectangle (40, 40, boxWidth-40, boxHeight-40);
  103. this.boxWidth = boxWidth;
  104. this.boxHeight = boxHeight;
  105. this.ground = boxHeight-40;
  106. this.ceiling = 40;
  107. this.rightWall = boxWidth-40;
  108. this.leftWall = 40;
  109. }
  110.  
  111.  
  112.  
  113. /* Simulate two bouncing balls
  114. */
  115.  
  116. public int setballXPos()
  117. {
  118. System.out.println("box width " + boxWidth);
  119. int ballXPos = randomXPos.nextInt (boxWidth-40) + 20;
  120. //if (ballXPos > 40 && ballXPos < boxWidth - 40)
  121. {
  122. //System.out.println("Win! " + ballXPos);
  123. return ballXPos;
  124. }
  125.  
  126. /*else
  127. {
  128. System.out.println("Fail! " + ballXPos);
  129. ballXPos = setballXPos();
  130. return ballXPos;
  131.  
  132. }*/
  133. //System.out.println("Weird!" + ballXPos);
  134. //return ballXPos;
  135. }
  136.  
  137.  
  138. public int setballYPos ()
  139. {
  140. int ballYPos = randomYPos.nextInt(boxHeight-40) + 20;
  141. System.out.println("ball ypos " + ballYPos);
  142. return ballYPos;
  143. }
  144.  
  145. public int setGround()
  146. {
  147. int ground = boxHeight-25;
  148. return ground;
  149. }
  150.  
  151. public int setCeiling()
  152. {
  153. int ceiling = 40;
  154. return ceiling;
  155. }
  156.  
  157. public int setRightWall()
  158. {
  159. int rightWall = boxWidth - 25;
  160. return rightWall;
  161. }
  162.  
  163. public int setLeftWall()
  164. {
  165. int leftWall = 40;
  166. return leftWall;
  167. }
  168.  
  169. public void bounce()
  170. {
  171. ground = setGround(); // position of the ground line
  172. ceiling = setCeiling(); // positon the ceiling line
  173. leftWall = setLeftWall(); // position the left wall
  174. rightWall = setRightWall(); // position the right wall
  175. myCanvas.setVisible(true);
  176.  
  177. // draw the ground
  178. //myCanvas.drawLine(20, ground, boxWidth, ground);
  179.  
  180. BouncingBall ball = new BouncingBall(setballXPos(), setballYPos(), 16, Color.blue, ground, myCanvas, ceiling, leftWall, rightWall);
  181. ball.draw();
  182. BouncingBall ball2 = new BouncingBall(setballXPos(), setballYPos(), 16, Color.blue, ground, myCanvas, ceiling, leftWall, rightWall);
  183. ball2.draw();
  184.  
  185.  
  186. // make them bounce
  187. boolean finished = false;
  188. while(!finished) {
  189. myCanvas.wait(50); // small delay
  190. ball.move();
  191. ball2.move();
  192. // stop once ball has travelled a certain distance on x axis
  193. /*if(ball.getXPosition() >= boxWidth-60 && ball2.getXPosition() >= boxWidth-60)
  194. {
  195. finished = true;
  196. }*/
  197. }
  198. ball.erase();
  199. ball2.erase();
  200. System.out.println ("finished");
  201.  
  202.  
  203.  
  204.  
  205. }
  206.  
  207. }
Add Comment
Please, Sign In to add comment