Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. // Brock Haun
  2. // HELP! Nothing is working for part 3. I did everything, but no actual balls are produced on the applet.
  3.  
  4. package balls;
  5.  
  6. import java.awt.Color;
  7. import java.awt.Graphics;
  8. import java.awt.Rectangle;
  9. import java.util.Random;
  10.  
  11. import javax.swing.JApplet;
  12.  
  13. public class BallsApplet extends JApplet {
  14.  
  15. private int numBalls = 20; //just for private usage. makes code more readable.
  16. private Ball balls[] = new Ball[numBalls];
  17.  
  18. private boolean stop = false; //private usage
  19. private Physics physics = new Physics(this,this,10);
  20.  
  21. @Override
  22. public void init() {
  23. setSize(400, 500);
  24. //ball = new Ball(30, 30, 10, Color.GREEN);
  25.  
  26.  
  27.  
  28. for (int i=0;i<numBalls;i++) {
  29. balls[i]= makeRandomBall();
  30. physics.addBall(balls[i]);
  31. } //for
  32.  
  33.  
  34.  
  35.  
  36.  
  37. //physics.addBall(ball);
  38. } //init()
  39.  
  40. @Override
  41. public void paint(Graphics g) {
  42.  
  43. Rectangle bounds = getBounds();
  44. g.setColor(Color.lightGray);
  45. g.fillRect(0, 0, bounds.width, bounds.height);
  46.  
  47. int redBalls = 0;
  48. int greenBalls = 0;
  49.  
  50. for (int i=0;i<numBalls;i++) {
  51. balls[i].draw(g);
  52. if (balls[i].getColor().equals(Color.red))
  53. redBalls++;
  54. else greenBalls++;
  55. } //for
  56.  
  57.  
  58. //string output
  59. g.setColor(Color.blue);
  60. String greenCount = "Green balls: " + greenBalls;
  61. String redCount = "Red balls: " + redBalls;
  62.  
  63. g.drawString(greenCount, 20, 20);
  64. g.drawString(redCount, 20, 30);
  65. g.drawString("Brock Haun" , 300, 10);
  66.  
  67. if (greenBalls == 0)
  68. physics.stopIt();
  69.  
  70. } //paint()
  71.  
  72. private int randomRange(int low, int high) {
  73. Random r = new Random();
  74. int randomInt = r.nextInt(high)+ low;
  75.  
  76. return randomInt;
  77. } //randomRange()
  78.  
  79. private Ball makeRandomBall() {
  80. Random r = new Random();
  81.  
  82.  
  83. int randX = r.nextInt(180)+21; //instructions say between 20 and 200, not inclusive so ceiling=179,floor=21. rand starts at 0.
  84. int randY = r.nextInt(180)+21;
  85. int randRad = r.nextInt(7)+3; //same as before, but BETWEEN 2 and 10, so 3-9
  86.  
  87. //making random values for x and y is a 2 step process since we can't declare a negative range.
  88. //first we make a random int (0 or 1); then we take -1 and square it by the random int (0 or 1). if it is 0,
  89. //the sign won't change (stay positive). if it is 1, the sign will become negative.
  90.  
  91. int sign = r.nextInt(2);
  92.  
  93. double randVelX = (Math.pow(-1, sign)*(r.nextInt(41)));
  94. double randVelY = (Math.pow(-1, sign)*(r.nextInt(41)));
  95.  
  96. Ball randomBall = new Ball(randX,randY,randRad,Color.green, randVelX, randVelY);
  97.  
  98. return randomBall;
  99. } //makeRandomBall()
  100.  
  101. } //BallsApplet class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement