jeudi 29 septembre 2016

Java can't figure out the this obstacle for ball animation

I have a task in which i have to make a app in JFrame form. Which contains 5 balls, which move randomly around the form, they can't exit the form and have to hit the borders. There's also an rectangle in the middle of the form, and that's the main problem, because i can't figure out how to make the ball's bounce off the rectangle. I already started doing something but the balls just bounce off at some random places in the form.

Task: Create JFrame (Done) Create 5 Balls, which move around and spawn in random positions (Done) Create a Rectangle in the middle of the form (Done) Make the balls bounce off the rectangle. (Need help)

Here is my code.

package lv.jak.viksna;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Main extends JPanel {

Random rn = new Random();
int blueX = rn.nextInt(650) + 1;
int blueY = rn.nextInt(650) + 1;

int redX = rn.nextInt(650) + 1;
int redY = rn.nextInt(650) + 1;

int yellowX = rn.nextInt(650) + 1;
int yellowY = rn.nextInt(650) + 1;

int greenX = rn.nextInt(650) + 1;
int greenY = rn.nextInt(650) + 1;

int magentaX = rn.nextInt(650) + 1;
int magentaY = rn.nextInt(650) + 1;

int blueAngleX = rn.nextInt(10) + 1;
int blueAngleY = rn.nextInt(20) + 1;

int redAngleX = rn.nextInt(10) + 1;
int redAngleY = rn.nextInt(50) + 1;

int yellowAngleX = rn.nextInt(40) + 1;
int yellowAngleY = rn.nextInt(50) + 1;

int greenAngleX = rn.nextInt(30) + 1;
int greenAngleY = rn.nextInt(20) + 1;

int magentaAngleX = rn.nextInt(20) + 1;
int magentaAngleY = rn.nextInt(50) + 1;

int rectX = 325, rectY = 325, rectW = 100, rectH = 100;

int speed = 5;

private void move() {

    rectContact();

    if (blueX + blueAngleX < 0) {
        blueAngleX = speed;
    } else if (blueX + blueAngleX > getWidth() - 30) {
        blueAngleX = -speed;
    } else if (blueY + blueAngleY < 0) {
        blueAngleY = speed;
    } else if (blueY + blueAngleY > getHeight() - 30) {
        blueAngleY = -speed;
    }

    blueX = blueX + blueAngleX;
    blueY = blueY + blueAngleY;

    ///////////////////////////////////////////////////////////////////////////////////////////

    if (redX + redAngleX < 0) {
        redAngleX = speed;
    } else if (redX + redAngleX > getWidth() - 30) {
        redAngleX = -speed;
    } else if (redY + redAngleY < 0) {
        redAngleY = speed;
    } else if (redY + redAngleY > getHeight() - 30) {
        redAngleY = -speed;
    }

    redX = redX + redAngleX;
    redY = redY + redAngleY;

    ///////////////////////////////////////////////////////////////////////////////////////////

    if (yellowX + yellowAngleX < 0) {
        yellowAngleX = speed;
    } else if (yellowX + yellowAngleX > getWidth() - 30) {
        yellowAngleX = -speed;
    } else if (yellowY + yellowAngleY < 0) {
        yellowAngleY = speed;
    } else if (yellowY + yellowAngleY > getHeight() - 30) {
        yellowAngleY = -speed;
    }

    yellowX = yellowX + yellowAngleX;
    yellowY = yellowY + yellowAngleY;

    ///////////////////////////////////////////////////////////////////////////////////////////

    if (greenX + greenAngleX < 0) {
        greenAngleX = speed;
    } else if (greenX + greenAngleX > getWidth() - 30) {
        greenAngleX = -speed;
    } else if (greenY + greenAngleY < 0) {
        greenAngleY = speed;
    } else if (greenY + greenAngleY > getHeight() - 30) {
        greenAngleY = -speed;
    }

    greenX = greenX + greenAngleX;
    greenY = greenY + greenAngleY;

    ///////////////////////////////////////////////////////////////////////////////////////////

    if (magentaX + magentaAngleX < 0) {
        magentaAngleX = speed;
    } else if (magentaX + magentaAngleX > getWidth() - 30) {
        magentaAngleX = -speed;
    } else if (magentaY + magentaAngleY < 0) {
        magentaAngleY = speed;
    } else if (magentaY + magentaAngleY > getHeight() - 30) {
        magentaAngleY = -speed;
    }

    magentaX = magentaX + magentaAngleX;
    magentaY = magentaY + magentaAngleY;

}

public void rectContact() {

    if (blueY + 30 >= rectY && (blueX >= rectX - 25 && blueX <= rectX)
            || (blueY >= rectY + 100 && (blueX >= rectX && blueX <= rectX + 100))) {
        blueAngleX = -speed;
    }
    if (blueX + 10 >= rectX && (blueY >= rectY && blueY <= rectY - 25)
            || (blueX >= rectX && (blueY >= rectY && blueY <= rectY))) {
        blueAngleY = -speed;
    }

}

@Override
public void paint(Graphics g) {
    super.paint(g);
    g.setColor(Color.BLUE);
    g.fillOval(blueX, blueY, 30, 30);

    g.setColor(Color.RED);
    g.fillOval(redX, redY, 30, 30);

    g.setColor(Color.YELLOW);
    g.fillOval(yellowX, yellowY, 30, 30);

    g.setColor(Color.GREEN);
    g.fillOval(greenX, greenY, 30, 30);

    g.setColor(Color.MAGENTA);
    g.fillOval(magentaX, magentaY, 30, 30);

    g.setColor(Color.RED);
    g.fillRect(rectX, rectY, rectW, rectH);
}

public static void main(String[] args) throws InterruptedException {

    JFrame frame = new JFrame("Moving Ball!");
    Main main = new Main();
    frame.add(main);
    frame.setBounds(300, 0, 750, 750);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    while (true) {
        main.move();
        main.repaint();
        Thread.sleep(10);
    }
}

}

Aucun commentaire:

Enregistrer un commentaire