我声明并构建了一个包含两个 Balls 实例的数组 ' m_ball[0]=new Ball(400,400,400,180);'
& ' m_ball[1]=new Ball(400,400,400,0);' 具有不同的参数值(180 和 0),但由于某种原因,当计时器开始运行时,这个不同的值似乎被重置为 0,而不是两个球放在同一个 XY 位置。有人能明白为什么吗?我是编程新手,非常感谢... .
//This is main
public class BBDemo extends JApplet{
...........
}
}//endclass BBDemo
public class BBPanel extends JPanel {
BallInBox m_bb; // The moving ball panel ///in order to take the Timer->setAnimation from m_bb
//========================================================== constructor
/** Creates a panel with the controls and moving ball display. */
BBPanel() {
//... Create components
m_bb = new BallInBox(); // The moving ball panel
.........
startButton.addActionListener(new StartAction());
.........
}
}//endclass BBPanel
public class BallInBox extends JPanel {
private Ball m_ball[]= new Ball[2];
//... Instance variables for the animation
private int m_interval = 40; // Milliseconds between updates.
static Timer m_timer; // Timer fires to animate one step.
static int j;
private Color Pigment;
//=================================================== constructor
/** Set panel size and creates timer. */
public BallInBox() {
.........
m_timer = new Timer(m_interval, new TimerAction());
addMouseListener(new PressBall());
m_ball[0]=new Ball(400,400,400,180);
m_ball[1]=new Ball(400,400,400,0);
}
public void setAnimation(boolean turnOnOff) {
if (turnOnOff) {
m_timer.start(); j=1; // start animation by starting the timer.
} else {
m_timer.stop(); j=0; // stop timer
}
}
//======================================================= paintComponent
public void paintComponent(Graphics g) {
super.paintComponent(g); // Paint background, border
........
g.setColor(Pigment);
m_ball[0].draw(g); // Draw the ball.
m_ball[1].draw(g);
}
class TimerAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
m_ball[0].setBounds(getWidth(), getHeight());
m_ball[0].move(); // Move the ball.
m_ball[1].setBounds(getWidth(), getHeight());
m_ball[1].move(); // Move the ball.
repaint(); // Repaint indirectly calls paintComponent.
}
}
}
}//endclass
public class Ball {
//... Constants
final static int DIAMETER = 50;
//... Instance variables
static int m_x; // x and y coordinates upper left
static int m_y;
......
//public int deg=90; // Pixels to move each time move() is called.
public int deg;
public int offset=400;
//======================================================== constructor
public Ball(int x, int y, int offset, int deg) {
m_x = x;
m_y = y;
}
.........
}
//============================================================== move
public void move() {
double degrees=(double) deg;
double radians=Math.toRadians(degrees);
double Sinu=Math.sin(radians);
double Sinu200=Math.sin(radians)*300;
int SinuInt=(int) Sinu200;
m_y=offset+SinuInt;
double Cos=Math.cos(radians);
double Cos200=Math.cos(radians)*300;
int CosInt=(int) Cos200;
m_x=offset+CosInt;
deg++; // j--;
if (deg==360) deg=0;
}
//============================================================== draw
public void draw(Graphics g) {
g.fillOval(m_x, m_y, DIAMETER, DIAMETER);
}
//============================================= getDiameter, getX, getY
public int getDiameter() { return DIAMETER;}
public int getX() { return m_x;}
public int getY() { return m_y;}
//======================================================== setPosition
public void setPosition(int x, int y) {
m_x = x;
m_y = y;
}
}
在 Ball 构造函数中,offset
和 deg
都没有设置。尝试:
public Ball(int x, int y, int offset, int deg) {
m_x = x;
m_y = y;
this.offset = offset;
this.deg = deg;
}
在Ball
类中,m_x
和 m_y
是静态的,因此它们由 Ball
的所有实例共享。 从他们的声明中删除static
这个词应该会有所帮助。