public interface Ball {
int DEFAULT_RADIUS = 50;
int radius();
Point center();
void update();
}
public class BouncingBall extends BallImpl {
public static final int MOVEMENT_SPEED = 12;
static final int DOWN = 1;
static final int UP = -1;
private int direction;
BouncingBall(int x, int y, int direction) {
super(x, y);
this.direction = direction;
}
@Override
public void update() {
direction = reverseDirectionIfNecessary();
y = move();
}
private int reverseDirectionIfNecessary() {
if (movingTooHigh() || movingTooLow()) {
return switchDirection();
}
return this.direction;
}
private boolean movingTooLow() {
return y + radius >= BallWorld.BOX_HEIGHT && movingDown();
}
private boolean movingTooHigh() {
return y - radius
Подробнее здесь: [url]https://stackoverflow.com/questions/35149228/how-can-i-make-a-ball-behave-like-a-bouncing-and-elastic-ball[/url]
У меня есть два класса BouncingBall и еще один, ElasticBall. Оба класса расширяют BallImpl, который реализует интерфейс Ball. [code]public interface Ball { int DEFAULT_RADIUS = 50;
int radius(); Point center(); void update(); }
public class BouncingBall extends BallImpl { public static final int MOVEMENT_SPEED = 12;
static final int DOWN = 1; static final int UP = -1;
private int direction;
BouncingBall(int x, int y, int direction) { super(x, y); this.direction = direction; }
@Override public void update() { direction = reverseDirectionIfNecessary(); y = move(); }
private int reverseDirectionIfNecessary() { if (movingTooHigh() || movingTooLow()) { return switchDirection(); }