Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package programmingproject5;
- /**
- *
- * @author Jarod Takemoto
- */
- import java.util.Random;
- import java.awt.Color;
- import stddraw.StdDraw;
- public class ProgrammingProject5 {
- public static final int CANVAS_SIZE = 400;
- public static void main(String[] args) {
- StdDraw.setCanvasSize(CANVAS_SIZE, CANVAS_SIZE);
- StdDraw.setXscale(0, CANVAS_SIZE);
- StdDraw.setYscale(0, CANVAS_SIZE);
- Random rand = new Random();
- MovingRectangle[] rectangles = new MovingRectangle[5];
- for (int i = 0; i < 5; i++) {
- int xC = rand.nextInt(CANVAS_SIZE);
- int yC = rand.nextInt(CANVAS_SIZE);
- int w = rand.nextInt((20) + 1) + 20;
- int h = rand.nextInt((20) + 1) + 20;
- int xV = rand.nextInt(10) - 5;
- int yV = rand.nextInt(10) - 5;
- int canvas = CANVAS_SIZE;
- rectangles[i] = new MovingRectangle(xC, yC, w, h, xV, yV, canvas);
- }
- int count = 0;
- String text = "YOU WIN";
- while (true) {
- StdDraw.clear();
- for (int i = 0; i < 5; i++) {
- rectangles[i].move();
- rectangles[i].draw();
- if (StdDraw.mousePressed() && rectangles[i].containsPoint(StdDraw.mouseX(), StdDraw.mouseY())) {
- if (rectangles[i].isFrozen() == false) {
- rectangles[i].setColor(StdDraw.RED);
- rectangles[i].setFrozen(true);
- count++;
- }
- }
- //MovingRectangle test = rectangles.isInteresting(r.xCoord);
- for (int j = 0; j < 5; j++) {
- if (j != i) {
- double xDistance = Math.abs(rectangles[i].getX() - rectangles[j].getX());
- double yDistance = Math.abs(rectangles[i].getY() - rectangles[j].getY());
- if (xDistance <= ((rectangles[i].getWidth() + rectangles[j].getWidth()) / 2) && yDistance <= ((rectangles[i].getHeight() + rectangles[j].getHeight()) / 2)){
- if (rectangles[i].isFrozen() == true && rectangles[j].isFrozen() == false) {
- rectangles[i].setFrozen(false);
- rectangles[j].setFrozen(false);
- count--;
- }
- }
- }
- }
- }
- if (count >= 5) {
- StdDraw.setPenColor();
- StdDraw.text(CANVAS_SIZE / 2, CANVAS_SIZE / 2, text);
- }
- StdDraw.show(20);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement