Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. Physics class:
  2.  
  3.  
  4. package balls;
  5.  
  6. import java.awt.Color;
  7. import java.awt.Component;
  8. import java.awt.Rectangle;
  9. import java.util.ArrayList;
  10.  
  11. public class Physics extends Thread {
  12.  
  13. private ArrayList<Ball> balls;
  14. private Component parent;
  15. private Component boundary;
  16. private long lastTime;
  17. private long sleepTime;
  18. private boolean done;
  19.  
  20. public Physics(Component parent, Component boundary, int framesPerSecond) {
  21. this.parent = parent;
  22. this.boundary = boundary;
  23. sleepTime = 1000 / framesPerSecond;
  24. lastTime = System.currentTimeMillis();
  25. balls = new ArrayList<Ball>();
  26. done = false;
  27. start();
  28. }
  29.  
  30. public synchronized void stopIt() {
  31. done = true;
  32. }
  33.  
  34. private synchronized boolean getDone() {
  35. return done;
  36. }
  37.  
  38. public synchronized void addBall(Ball ball) {
  39. balls.add(ball);
  40. }
  41.  
  42. private synchronized void handleBalls() {
  43. long currentTime;
  44. currentTime = System.currentTimeMillis();
  45. for (int i = 0; i < balls.size(); i++)
  46. setBallPosition(currentTime - lastTime, balls.get(i));
  47. for (int i = 0; i < balls.size(); i++)
  48. for (int j = i + 1; j < balls.size(); j++)
  49. if (balls.get(i).intersectsBall(balls.get(j))) {
  50. balls.get(i).setCollided();
  51. balls.get(j).setCollided();
  52. }
  53. lastTime = currentTime;
  54. }
  55.  
  56. private void setBallPosition(long timeDiff, Ball ball) {
  57. double deltaTime = timeDiff / 1000.0;
  58. double oldX = ball.getX();
  59. double oldY = ball.getY();
  60. double radius = ball.getRadius();
  61. double velocityX = ball.getVelocityX();
  62. double velocityY = ball.getVelocityY();
  63. double newX = oldX + velocityX * deltaTime;
  64. double newY = oldY + velocityY * deltaTime;
  65. Rectangle bounds = boundary.getBounds();
  66. ball.setX(newX);
  67. ball.setY(newY);
  68. if (newX - radius <= 0) {
  69. newX = 2 * radius - newX;
  70. ball.setVelocityX(Math.abs(velocityX));
  71. }
  72. if (newX + radius >= bounds.width) {
  73. newX = 2 * (bounds.width - radius) - newX;
  74. ball.setVelocityX(-Math.abs(velocityX));
  75. }
  76. if (newY - radius <= 0) {
  77. newY = 2 * radius - newY;
  78. ball.setVelocityY(Math.abs(velocityY));
  79. }
  80. if (newY + radius >= bounds.height) {
  81. newY = 2 * (bounds.height - radius) - newY;
  82. ball.setVelocityY(-Math.abs(velocityY));
  83. }
  84. }
  85.  
  86. public void run() {
  87. while (!getDone()) {
  88. handleBalls();
  89. parent.repaint();
  90. try {
  91. sleep(sleepTime);
  92. } catch (InterruptedException e) {
  93. }
  94. }
  95. }
  96.  
  97.  
  98.  
  99.  
  100. }
  101.  
  102.  
  103.  
  104. Ball class:
  105.  
  106. package balls;
  107.  
  108. import java.awt.Color;
  109. import java.awt.Graphics;
  110.  
  111. public class Ball {
  112.  
  113. private double x, y; // position
  114. private final double radius;
  115.  
  116. private Color color;
  117.  
  118. private double velocityX, velocityY;
  119.  
  120. public Ball(final double x, final double y, final double radius,
  121. final Color color, final double velocityX, final double velocityY) { //added velx and vely... i'll remove if wrong
  122. this.x = x;
  123. this.y = y;
  124. this.radius = radius;
  125. this.color = color;
  126.  
  127. // this.velocityX = this.velocityY = 0;
  128. this.velocityX = 10;
  129. this.velocityY = 10;
  130. } //Ball object
  131.  
  132. public void draw(final Graphics g) {
  133. final double xAdjust = x - radius;
  134. final double yAdjust = y - radius;
  135. final double diameter = 2 * radius;
  136.  
  137. g.setColor(color);
  138. g
  139. .fillOval((int) xAdjust, (int) yAdjust, (int) diameter,
  140. (int) diameter);
  141. } //draw()
  142.  
  143. public double getX() {
  144. return x;
  145. } //getX()
  146.  
  147. public double getY() {
  148. return y;
  149. } //getY()
  150.  
  151. public double getRadius() {
  152. return radius;
  153. } //getX()
  154.  
  155. public Color getColor() {
  156. return color;
  157. } //getColor()
  158.  
  159. public double getVelocityX() {
  160. return velocityX;
  161. } //getVelocityX()
  162.  
  163. public double getVelocityY() {
  164. return velocityY;
  165. } //getVelocityY()
  166.  
  167. public void setX(double x) {
  168. this.x = x;
  169. } //setX()
  170.  
  171. public void setY(double y) {
  172. this.y = y;
  173. } //setY()
  174.  
  175. public void setColor(Color color) {
  176. this.color = color;
  177. } //setColor()
  178.  
  179. public void setVelocityX(double velocityX) {
  180. this.velocityX = velocityX;
  181. } //setVelocityX()
  182.  
  183. public void setVelocityY(double velocityY) {
  184. this.velocityY = velocityY;
  185. } //setVelocityY()
  186.  
  187. public boolean intersectsBall(Ball b) {
  188. double x1 = b.getX();
  189. double y1 = b.getY();
  190. double rad1 = b.getRadius();
  191. double x2 = x;
  192. double y2 = y;
  193. double rad2= radius;
  194.  
  195. double distance = Math.sqrt(((y2-y1)*(y2-y1))-(((x2-x1)*x2-x1)));
  196. if (distance == ( rad1+(rad2)) )
  197. return true;
  198. else return false;
  199. } //intersectsBall()
  200.  
  201. public void setCollided() {
  202. this.setColor(Color.red);
  203.  
  204. } //setCollided()
  205.  
  206. } //Ball class
  207.  
  208.  
  209. BallsApplet class:
  210.  
  211. // Brock Haun
  212. // HELP! Nothing is working for part 3. I did everything, but no actual balls are produced on the applet.
  213.  
  214. package balls;
  215.  
  216. import java.awt.Color;
  217. import java.awt.Graphics;
  218. import java.awt.Rectangle;
  219. import java.util.Random;
  220.  
  221. import javax.swing.JApplet;
  222.  
  223. public class BallsApplet extends JApplet {
  224.  
  225. private int numBalls = 20; //just for private usage. makes code more readable.
  226. private Ball balls[] = new Ball[numBalls];
  227.  
  228. private boolean stop = false; //private usage
  229. private Physics physics = new Physics(this,this,10);
  230.  
  231. @Override
  232. public void init() {
  233. setSize(400, 500);
  234. //ball = new Ball(30, 30, 10, Color.GREEN);
  235.  
  236.  
  237.  
  238. for (int i=0;i<numBalls;i++) {
  239. balls[i]= makeRandomBall();
  240. physics.addBall(balls[i]);
  241. } //for
  242.  
  243.  
  244.  
  245.  
  246.  
  247. //physics.addBall(ball);
  248. } //init()
  249.  
  250. @Override
  251. public void paint(Graphics g) {
  252.  
  253. Rectangle bounds = getBounds();
  254. g.setColor(Color.lightGray);
  255. g.fillRect(0, 0, bounds.width, bounds.height);
  256.  
  257. int redBalls = 0;
  258. int greenBalls = 0;
  259.  
  260. for (int i=0;i<numBalls;i++) {
  261. balls[i].draw(g);
  262. if (balls[i].getColor().equals(Color.red))
  263. redBalls++;
  264. else greenBalls++;
  265. } //for
  266.  
  267.  
  268. //string output
  269. String greenCount = "Green balls: " + greenBalls;
  270. String redCount = "Red balls: " + redBalls;
  271.  
  272. g.drawString(greenCount, 20, 20);
  273. g.drawString(redCount, 20, 30);
  274. g.drawString("Brock Haun" , 300, 10);
  275.  
  276. if (greenBalls == 0)
  277. physics.stopIt();
  278.  
  279. } //paint()
  280.  
  281. private int randomRange(int low, int high) {
  282. Random r = new Random();
  283. int randomInt = r.nextInt(high)+ low;
  284.  
  285. return randomInt;
  286. } //randomRange()
  287.  
  288. private Ball makeRandomBall() {
  289. Random r = new Random();
  290.  
  291.  
  292. int randX = r.nextInt(180)+21; //instructions say between 20 and 200, not inclusive so ceiling=179,floor=21. rand starts at 0.
  293. int randY = r.nextInt(180)+21;
  294. int randRad = r.nextInt(7)+3; //same as before, but BETWEEN 2 and 10, so 3-9
  295.  
  296. //making random values for x and y is a 2 step process since we can't declare a negative range.
  297. //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,
  298. //the sign won't change (stay positive). if it is 1, the sign will become negative.
  299.  
  300. int sign = r.nextInt(2);
  301.  
  302. double randVelX = (Math.pow(-1, sign)*(r.nextInt(41)));
  303. double randVelY = (Math.pow(-1, sign)*(r.nextInt(41)));
  304.  
  305. Ball randomBall = new Ball(randX,randY,randRad,Color.green, randVelX, randVelY);
  306.  
  307. return randomBall;
  308. } //makeRandomBall()
  309.  
  310. } //BallsApplet class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement