Advertisement
ChairmanMeow1

ProgrammingProject5 Class 1.2

Nov 17th, 2015
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package programmingproject5;
  7.  
  8. /**
  9. *
  10. * @author Jarod Takemoto
  11. */
  12. import java.util.Random;
  13. import java.awt.Color;
  14. import stddraw.StdDraw;
  15.  
  16. public class ProgrammingProject5 {
  17.  
  18. public static final int CANVAS_SIZE = 400;
  19.  
  20. public static void main(String[] args) {
  21. StdDraw.setCanvasSize(CANVAS_SIZE, CANVAS_SIZE);
  22. StdDraw.setXscale(0, CANVAS_SIZE);
  23. StdDraw.setYscale(0, CANVAS_SIZE);
  24. Random rand = new Random();
  25. MovingRectangle[] rectangles = new MovingRectangle[5];
  26.  
  27. for (int i = 0; i < 5; i++) {
  28. int xC = rand.nextInt(CANVAS_SIZE);
  29. int yC = rand.nextInt(CANVAS_SIZE);
  30. int w = rand.nextInt((20) + 1) + 20;
  31. int h = rand.nextInt((20) + 1) + 20;
  32. int xV = rand.nextInt(10) - 5;
  33. int yV = rand.nextInt(10) - 5;
  34. int canvas = CANVAS_SIZE;
  35. rectangles[i] = new MovingRectangle(xC, yC, w, h, xV, yV, canvas);
  36.  
  37. }
  38. int count = 0;
  39. String text = "YOU WIN";
  40. while (true) {
  41. StdDraw.clear();
  42. for (int i = 0; i < 5; i++) {
  43. rectangles[i].move();
  44. rectangles[i].draw();
  45. if (StdDraw.mousePressed() && rectangles[i].containsPoint(StdDraw.mouseX(), StdDraw.mouseY())) {
  46. if (rectangles[i].isFrozen() == false) {
  47. rectangles[i].setColor(StdDraw.RED);
  48. rectangles[i].setFrozen(true);
  49. count++;
  50. }
  51.  
  52. }
  53. //MovingRectangle test = rectangles.isInteresting(r.xCoord);
  54. for (int j = 0; j < 5; j++) {
  55. if (j != i) {
  56. double xDistance = Math.abs(rectangles[i].getX() - rectangles[j].getX());
  57. double yDistance = Math.abs(rectangles[i].getY() - rectangles[j].getY());
  58. if (xDistance <= ((rectangles[i].getWidth() + rectangles[j].getWidth()) / 2) && yDistance <= ((rectangles[i].getHeight() + rectangles[j].getHeight()) / 2)){
  59. if (rectangles[i].isFrozen() == true && rectangles[j].isFrozen() == false) {
  60. rectangles[i].setFrozen(false);
  61. rectangles[j].setFrozen(false);
  62. count--;
  63. }
  64. }
  65. }
  66.  
  67. }
  68. }
  69. if (count >= 5) {
  70. StdDraw.setPenColor();
  71. StdDraw.text(CANVAS_SIZE / 2, CANVAS_SIZE / 2, text);
  72. }
  73. StdDraw.show(20);
  74.  
  75. }
  76. }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement