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

时间:2013-12-31 13:46:19

标签: java swing graphics paintcomponent java-2d

我声明并构建了一个包含两个Balls实例的数组'm_ball [0] = new Ball(400,400,400,180);' &安培; 'm_ball [1] =新球(400,400,400,0);'具有不同的参数值(180和0)但由于某种原因,当Timer开始运行并且两个球放置在同一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;
    }

}

2 个答案:

答案 0 :(得分:0)

在您的Ball构造函数中,offsetdeg都未设置。尝试:

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

答案 1 :(得分:0)

Ball课程中,m_xm_y是静态的,因此Ball的所有实例都会共享它们。从声明中删除单词static应该会有所帮助。