📄 billiardball.java
字号:
//Hao Chen
//0247604
//com132
//lab8
//dangerchen@hotmail.com
import java.awt.*;
import java.applet.Applet;
public class BilliardBall{
private BilliardTable t;//declar table t
private Color bc;//declar ball's color
private int bw;//declar ball's width
private int x, y;//declar position
private int xChange = 1, yChange = 1;//declar and initial the change
public BilliardBall (BilliardTable t, Color bc, int bw){//constructor
this.t=t;//sign data to object t
this.bc=bc;//sign color data to instance vairable bc
this.bw=bw;//sign ball's width data to instance variable bw
}
public static void pause (int n) // wait n milliseconds
{
try { Thread.sleep (n); } catch (InterruptedException e) {}
}
public void roll (Graphics g)
{ int leftx=t.getTablex();//name tablex of object t
int rightx=t.getTablex()+t.getTablew()-bw;
int topy=t.getTabley();
int boty=t.getTabley()+t.getTableh()-bw;
x = t.getTablex() + (int) (Math.random ()*(t.getTablew()-bw)) ;
//make random initial position
y = t.getTabley() + (int) (Math.random ()*(t.getTableh()-bw)) ;
for(int n=1; n<10000; n++){//create changing ball
Color tac = t.getTableColor();
g.setColor(tac);
//set the table color to fill in the blank after the runing ball
g.fillOval(x,y,bw,bw);
if(x<=leftx||x>=rightx)//if the ball hit the left or right wall
xChange=-xChange;////ball will return
if(y<=topy||y>=boty)
yChange=-yChange;
x=x+xChange;//correct the xchange
y=y+yChange;
g.setColor(bc);//fill the ball's color in new position
g.fillOval(x,y,bw,bw);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -