Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. package balls;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5.  
  6. public class Ball {
  7.  
  8. private double x, y; // position
  9. private final double radius;
  10.  
  11. private Color color;
  12.  
  13. private double velocityX, velocityY;
  14.  
  15. public Ball(final double x, final double y, final double radius,
  16. final Color color, final double velocityX, final double velocityY) { //added velx and vely... i'll remove if wrong
  17. this.x = x;
  18. this.y = y;
  19. this.radius = radius;
  20. this.color = color;
  21.  
  22. // this.velocityX = this.velocityY = 0;
  23. this.velocityX = 10;
  24. this.velocityY = 10;
  25. } //Ball object
  26.  
  27. public void draw(final Graphics g) {
  28. final double xAdjust = x - radius;
  29. final double yAdjust = y - radius;
  30. final double diameter = 2 * radius;
  31.  
  32. g.setColor(color);
  33. g
  34. .fillOval((int) xAdjust, (int) yAdjust, (int) diameter,
  35. (int) diameter);
  36. } //draw()
  37.  
  38. public double getX() {
  39. return x;
  40. } //getX()
  41.  
  42. public double getY() {
  43. return y;
  44. } //getY()
  45.  
  46. public double getRadius() {
  47. return radius;
  48. } //getX()
  49.  
  50. public Color getColor() {
  51. return color;
  52. } //getColor()
  53.  
  54. public double getVelocityX() {
  55. return velocityX;
  56. } //getVelocityX()
  57.  
  58. public double getVelocityY() {
  59. return velocityY;
  60. } //getVelocityY()
  61.  
  62. public void setX(double x) {
  63. this.x = x;
  64. } //setX()
  65.  
  66. public void setY(double y) {
  67. this.y = y;
  68. } //setY()
  69.  
  70. public void setColor(Color color) {
  71. this.color = color;
  72. } //setColor()
  73.  
  74. public void setVelocityX(double velocityX) {
  75. this.velocityX = velocityX;
  76. } //setVelocityX()
  77.  
  78. public void setVelocityY(double velocityY) {
  79. this.velocityY = velocityY;
  80. } //setVelocityY()
  81.  
  82. public boolean intersectsBall(Ball b) {
  83. double x1 = b.getX();
  84. double y1 = b.getY();
  85. double rad1 = b.getRadius();
  86. double x2 = x;
  87. double y2 = y;
  88. double rad2= radius;
  89.  
  90. double distance = Math.sqrt(((y2-y1)*(y2-y1))-(((x2-x1)*x2-x1)));
  91. if (distance == ( rad1+(rad2)) )
  92. return true;
  93. else return false;
  94. } //intersectsBall()
  95.  
  96. public void setCollided() {
  97. this.setColor(Color.red);
  98.  
  99. } //setCollided()
  100.  
  101. } //Ball class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement