Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package balls;
- import java.awt.Color;
- import java.awt.Graphics;
- public class Ball {
- private double x, y; // position
- private final double radius;
- private Color color;
- private double velocityX, velocityY;
- public Ball(final double x, final double y, final double radius,
- final Color color, final double velocityX, final double velocityY) { //added velx and vely... i'll remove if wrong
- this.x = x;
- this.y = y;
- this.radius = radius;
- this.color = color;
- // this.velocityX = this.velocityY = 0;
- this.velocityX = 10;
- this.velocityY = 10;
- } //Ball object
- public void draw(final Graphics g) {
- final double xAdjust = x - radius;
- final double yAdjust = y - radius;
- final double diameter = 2 * radius;
- g.setColor(color);
- g
- .fillOval((int) xAdjust, (int) yAdjust, (int) diameter,
- (int) diameter);
- } //draw()
- public double getX() {
- return x;
- } //getX()
- public double getY() {
- return y;
- } //getY()
- public double getRadius() {
- return radius;
- } //getX()
- public Color getColor() {
- return color;
- } //getColor()
- public double getVelocityX() {
- return velocityX;
- } //getVelocityX()
- public double getVelocityY() {
- return velocityY;
- } //getVelocityY()
- public void setX(double x) {
- this.x = x;
- } //setX()
- public void setY(double y) {
- this.y = y;
- } //setY()
- public void setColor(Color color) {
- this.color = color;
- } //setColor()
- public void setVelocityX(double velocityX) {
- this.velocityX = velocityX;
- } //setVelocityX()
- public void setVelocityY(double velocityY) {
- this.velocityY = velocityY;
- } //setVelocityY()
- public boolean intersectsBall(Ball b) {
- double x1 = b.getX();
- double y1 = b.getY();
- double rad1 = b.getRadius();
- double x2 = x;
- double y2 = y;
- double rad2= radius;
- double distance = Math.sqrt(((y2-y1)*(y2-y1))-(((x2-x1)*x2-x1)));
- if (distance == ( rad1+(rad2)) )
- return true;
- else return false;
- } //intersectsBall()
- public void setCollided() {
- this.setColor(Color.red);
- } //setCollided()
- } //Ball class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement