Guest User

Untitled

a guest
May 25th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. public class MovingSquare extends Rectangle2D.Double implements MovingFigure {
  2. private float angle;
  3. public Color color;
  4.  
  5. MovingSquare() {
  6. Random r = new Random();
  7. this.width = this.height = r.nextInt(20) + 5;
  8. this.angle = r.nextFloat() * 360;
  9. this.x = width * 3 + r.nextDouble() * (CANVAS_W - width * 6);
  10. this.y = height * 3 + r.nextDouble() * (CANVAS_H - height * 6);
  11. this.color = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat());
  12. }
  13.  
  14. public void MoveSelf() {
  15.  
  16. x = x + Math.cos(angle * Math.PI / 180) * SPEED;
  17. y = y + Math.sin(angle * Math.PI / 180) * SPEED;
  18.  
  19. if (x <= this.width || x >= CANVAS_W - width * 2) {
  20. angle = 180 - angle;
  21. }
  22.  
  23. if (y <= this.height || y >= CANVAS_H - height * 2) {
  24. angle = 360 - angle;
  25. }
  26.  
  27. }
  28. }
  29.  
  30. public class MovingCircle extends Ellipse2D.Double implements MovingFigure {
  31.  
  32. private float angle;
  33. public Color color;
  34.  
  35. MovingCircle() {
  36. Random r = new Random();
  37. this.width = this.height = r.nextInt(20) + 5;
  38. this.angle = r.nextFloat() * 360;
  39. this.x = width * 3 + r.nextDouble() * (CANVAS_W - width * 6);
  40. this.y = height * 3 + r.nextDouble() * (CANVAS_H - height * 6);
  41. this.color = new Color(r.nextFloat(), r.nextFloat(), r.nextFloat());
  42. }
  43.  
  44. public void MoveSelf() {
  45.  
  46. x = x + Math.cos(angle * Math.PI / 180) * SPEED;
  47. y = y + Math.sin(angle * Math.PI / 180) * SPEED;
  48.  
  49. //TODO radius
  50. if (x <= this.width || x >= CANVAS_W - width * 2) {
  51. angle = 180 - angle;
  52. }
  53.  
  54. if (y <= this.height || y >= CANVAS_H - height * 2) {
  55. angle = 360 - angle;
  56. }
  57.  
  58. }
  59.  
  60. }
Add Comment
Please, Sign In to add comment