Biblioteca Java - Blame information for rev 27
Subversion Repositories:
Rev | Author | Line No. | Line |
---|---|---|---|
27 | mihai | 1 | package ex2_balls; |
2 | import java.awt.*; | ||
3 | |||
4 | public class Ball extends Thread { | ||
5 | |||
6 | BallWorld world; | ||
7 | |||
8 | private int xpos, ypos, xinc, yinc; | ||
9 | |||
10 | private final Color col; | ||
11 | |||
12 | private final static int ballw = 10; | ||
13 | private final static int ballh = 10; | ||
14 | |||
15 | public Ball(BallWorld world, int xpos, int ypos, | ||
16 | int xinc, int yinc, Color col) { | ||
17 | |||
18 | this.world = world; | ||
19 | this.xpos = xpos; this.ypos = ypos; | ||
20 | this.xinc = xinc; this.yinc = yinc; | ||
21 | this.col = col; | ||
22 | |||
23 | world.addBall(this); | ||
24 | } | ||
25 | |||
26 | |||
27 | public void move() { | ||
28 | if (xpos >= world.getWidth() - ballw || xpos <= 0 ) xinc = -xinc; | ||
29 | |||
30 | if (ypos >= world.getHeight() - ballh || ypos <= 0 ) yinc = -yinc; | ||
31 | |||
32 | try { | ||
33 | Thread.sleep(30); | ||
34 | xpos += xinc; | ||
35 | ypos += yinc; | ||
36 | } | ||
37 | catch (InterruptedException e) { | ||
38 | System.err.println(e.getMessage()); | ||
39 | System.exit(1); | ||
40 | } | ||
41 | } | ||
42 | |||
43 | public void run() { | ||
44 | while (true) move(); | ||
45 | } | ||
46 | |||
47 | public void draw(Graphics g) { | ||
48 | g.setColor(col); | ||
49 | g.fillOval(xpos,ypos,ballw,ballh); | ||
50 | } | ||
51 | |||
52 | } |