Ball.java 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package model.objects;
  2. import java.awt.Color;
  3. import java.awt.Graphics2D;
  4. import java.awt.geom.Ellipse2D;
  5. import main.Window;
  6. public class Ball {
  7. private Color color;
  8. private int size, bounceheight, direction;
  9. private Ellipse2D.Double bal;
  10. double rx, ry; // position
  11. double vx, vy; // velocity
  12. public Ball(int size, int bounceheight, Color color, int x, int y, int direction, double velocity) {
  13. this.color = color;
  14. this.size = size*20;
  15. this.bounceheight = bounceheight;
  16. this.direction = direction;
  17. this.vx = direction*3;
  18. this.vy = velocity;
  19. setDirection(direction);
  20. rx = x;
  21. ry = y;
  22. bal = new Ellipse2D.Double(x, y, this.size, this.size);
  23. }
  24. public boolean hitLine(ShootingLine l){
  25. return bal.intersects(l.getX(), l.getY()-l.getHeight(), l.getWidth(), l.getHeight());
  26. }
  27. public boolean hitPlayer(Player p){
  28. return bal.intersects(p.getX(),p.getY(),p.getWidth(),p.getHeigth());
  29. }
  30. public Ball clone(){
  31. return new Ball(getSize(), getBounceHeight(), getColor(), getX(), getY(), direction, vy);
  32. }
  33. //** Drawing and Calculating **//
  34. public void paint(Graphics2D g2d) {
  35. g2d.setColor(color);
  36. g2d.fill(bal);
  37. g2d.drawLine(0, 600-bounceheight, 500, 600-bounceheight);
  38. }
  39. public void update() {
  40. if (rx >= (Window.WIDTH - size - 10))
  41. vx = -3;
  42. else if (rx <= 10)
  43. vx =3;
  44. vy += 0.5;
  45. if(ry+size < 600-bounceheight && vy < 0)
  46. {
  47. vy +=0.5;
  48. }
  49. else if(ry+size < 600-bounceheight && vy > 0)
  50. {
  51. vy -=0.2;
  52. }
  53. if (ry >= (600-size))
  54. vy *= -1;
  55. rx += vx;
  56. ry += vy;
  57. setX((int) rx);
  58. setY((int) ry);
  59. }
  60. //** Getters and Setters **//
  61. public int getSize() {
  62. return size/20;
  63. }
  64. public int getBounceHeight() {
  65. return bounceheight;
  66. }
  67. public int getX() {
  68. return (int) bal.getX();
  69. }
  70. public int getY() {
  71. return (int) bal.getY();
  72. }
  73. public int getWidth() {
  74. return (int) bal.getWidth();
  75. }
  76. public int getHeight() {
  77. return (int) bal.getHeight();
  78. }
  79. public Color getColor() {
  80. return color;
  81. }
  82. public void setX(int x) {
  83. bal.setFrame(x, getY(), getWidth(), getHeight());
  84. }
  85. public void setY(int y) {
  86. bal.setFrame(getX(), y, getWidth(), getHeight());
  87. }
  88. public void setDirection(int d){
  89. if(d == -1 || d == 1){
  90. direction = d;
  91. vx = d*3;
  92. }
  93. }
  94. public double getYSpeed()
  95. {
  96. return vy;
  97. }
  98. }