Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy both ovals located at the same XY?
    text
    copied!<p>I declared and built an array of two Balls instances ' m_ball[0]=new Ball(400,400,400,180);' &amp; ' m_ball[1]=new Ball(400,400,400,0);' with different parameter values (180 and 0) but for some reason it seems that this different values get reset to 0 when the Timer start running and than both balls place on the same XY location. Can someone see why? I'm new in programming, Many Thanks... .</p> <pre><code>//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-&gt;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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload