gpt4 book ai didi

java - 为什么两个椭圆位于同一个 XY 位置?

转载 作者:太空宇宙 更新时间:2023-11-04 15:15:53 24 4
gpt4 key购买 nike

我声明并构建了两个 Balls 实例的数组“m_ball[0]=new Ball(400,400,400,180);” & ' m_ball[1]=新球(400,400,400,0);'具有不同的参数值(180 和 0),但由于某种原因,当计时器开始运行并且两个球放置在相同的 XY 位置时,这些不同的值似乎会重置为 0。有人能看出为什么吗?我是编程新手,非常感谢....

//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 构造函数中,offsetdeg 均未设置。尝试:

public Ball(int x, int y, int offset, int deg) {
m_x = x;
m_y = y;
this.offset = offset;
this.deg = deg;
}

关于java - 为什么两个椭圆位于同一个 XY 位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20858356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com