Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Can you tell me how it worked?
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


PrinceRaj
Can you tell me how it worked?
0 Answer(s)      9 months ago
Posted in : Java Beginners


public class Ball {

private static final int DIAMETER = 30;
int x = 0;
int y = 0;
int xa = 1;
int ya = 1;
private Game game;

public Ball(Game game) {
    this.game= game;
}

void move() {
    if (x + xa < 0)
        xa = 1;
    if (x + xa > game.getWidth() - DIAMETER)
        xa = -1;
    if (y + ya < 0)
        ya = 1;
    if (y + ya > game.getHeight() - DIAMETER)
        game.gameOver();
    if (collision()){
        ya = -1;
        y = game.racquet.getTopY() - DIAMETER;
    }
    x = x + xa;
    y = y + ya;
}

private boolean collision() {
    return game.racquet.getBounds().intersects(getBounds());
}

public void paint(Graphics2D g) {
    g.fillOval(x, y, DIAMETER, DIAMETER);
}

public Rectangle getBounds() {
    return new Rectangle(x, y, DIAMETER, DIAMETER);
}

}

How is the 'getBounds()' method called into another method 'collision()' without first creating the object of this class - 'Ball'? I mean, it should be like ball.getBounds()


Advertisement
View Answers

Related Tutorials/Questions & Answers:
Advertisements
Advertisements
 

 

 

DMCA.com