Biblioteca Java - Rev 27
Subversion Repositories:
package ex2_balls;
import java.awt.*;
public class Ball extends Thread {
BallWorld world;
private int xpos, ypos, xinc, yinc;
private final Color col;
private final static int ballw = 10;
private final static int ballh = 10;
public Ball(BallWorld world, int xpos, int ypos,
int xinc, int yinc, Color col) {
this.world = world;
this.xpos = xpos; this.ypos = ypos;
this.xinc = xinc; this.yinc = yinc;
this.col = col;
world.addBall(this);
}
public void move() {
if (xpos >= world.getWidth() - ballw || xpos <= 0 ) xinc = -xinc;
if (ypos >= world.getHeight() - ballh || ypos <= 0 ) yinc = -yinc;
try {
Thread.sleep(30);
xpos += xinc;
ypos += yinc;
}
catch (InterruptedException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
public void run() {
while (true) move();
}
public void draw(Graphics g) {
g.setColor(col);
g.fillOval(xpos,ypos,ballw,ballh);
}
}
import java.awt.*;
public class Ball extends Thread {
BallWorld world;
private int xpos, ypos, xinc, yinc;
private final Color col;
private final static int ballw = 10;
private final static int ballh = 10;
public Ball(BallWorld world, int xpos, int ypos,
int xinc, int yinc, Color col) {
this.world = world;
this.xpos = xpos; this.ypos = ypos;
this.xinc = xinc; this.yinc = yinc;
this.col = col;
world.addBall(this);
}
public void move() {
if (xpos >= world.getWidth() - ballw || xpos <= 0 ) xinc = -xinc;
if (ypos >= world.getHeight() - ballh || ypos <= 0 ) yinc = -yinc;
try {
Thread.sleep(30);
xpos += xinc;
ypos += yinc;
}
catch (InterruptedException e) {
System.err.println(e.getMessage());
System.exit(1);
}
}
public void run() {
while (true) move();
}
public void draw(Graphics g) {
g.setColor(col);
g.fillOval(xpos,ypos,ballw,ballh);
}
}