0

Why my while loop is looping just once ? It should loops 99 times. Could Thread.sleep method break this loop or repaint ? Can you help me resolve this ? It doesn't show any errors. And i am using netbeans.

Licznik.java:

    import javax.swing.JApplet;

public class Licznik extends JApplet {
    @Override
    public void init() {
        setSize(900,900);
        PanelLicznik pl=new PanelLicznik();
    add(pl);
    } 
}

PanelLicznik.java:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.geom.*;
import java.awt.geom.Line2D;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class PanelLicznik extends JPanel implements Runnable
{
    int srodek = 450;
    Image bg;
    int a = 400, t = 0, v = 0, i = 0;
    double x = 152, y = 756;
    public PanelLicznik() {  
       Thread watek1 = new Thread(this);
       watek1.start();
    }

    @Override
    public Dimension getPreferredSize() { 
        return new Dimension(900, 900); 
    }

    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g; 
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        bg = new ImageIcon(this.getClass().getResource("s.gif")).getImage();
        g2.drawImage(bg,0,0,null); 
        Line2D wsk = new Line2D.Double(srodek,srodek,x,y);
        g2.setColor(new Color(255,255,255));
        g2.setStroke(new BasicStroke(15.0f,BasicStroke.CAP_ROUND,BasicStroke.JOIN_MITER));
        g2.draw(wsk);
    }

    @Override
    public void run() {
      t = 3;
      v = 100; 
      i = 0
      while(i < v){
         try{  
            Thread.sleep(100);
         } 
         catch(Exception ek) {}
         double stopien = 231.4 - ((360*v)/280);
         double radSek = Math.toRadians(stopien);
         x = srodek + (a * Math.cos(radSek));   
         y = srodek - (a * Math.sin(radSek));
         repaint();
         i++;
      }
    }
 }

repaint

public void repaint() {
    repaint(0, 0, 0, width, height);
}

/**
 * Repaints the component.  If this component is a lightweight
 * component, this results in a call to <code>paint</code>
 * within <code>tm</code> milliseconds.
 * <p>
 * <b>Note</b>: For more information on the paint mechanisms utilitized
 * by AWT and Swing, including information on how to write the most
 * efficient painting code, see
 * <a href="http://www.oracle.com/technetwork/java/painting-140037.html">Painting in AWT and Swing</a>.
 *
 * @param tm maximum time in milliseconds before update
 * @see #paint
 * @see #update(Graphics)
 * @since JDK1.0
 */
public void repaint(long tm) {
    repaint(tm, 0, 0, width, height);
}

/**
 * Repaints the specified rectangle of this component.
 * <p>
 * If this component is a lightweight component, this method
 * causes a call to this component's <code>paint</code> method
 * as soon as possible.  Otherwise, this method causes a call to
 * this component's <code>update</code> method as soon as possible.
 * <p>
 * <b>Note</b>: For more information on the paint mechanisms utilitized
 * by AWT and Swing, including information on how to write the most
 * efficient painting code, see
 * <a href="http://www.oracle.com/technetwork/java/painting-140037.html">Painting in AWT and Swing</a>.
 *
 * @param     x   the <i>x</i> coordinate
 * @param     y   the <i>y</i> coordinate
 * @param     width   the width
 * @param     height  the height
 * @see       #update(Graphics)
 * @since     JDK1.0
 */
public void repaint(int x, int y, int width, int height) {
    repaint(0, x, y, width, height);
}

/**
 * Repaints the specified rectangle of this component within
 * <code>tm</code> milliseconds.
 * <p>
 * If this component is a lightweight component, this method causes
 * a call to this component's <code>paint</code> method.
 * Otherwise, this method causes a call to this component's
 * <code>update</code> method.
 * <p>
 * <b>Note</b>: For more information on the paint mechanisms utilitized
 * by AWT and Swing, including information on how to write the most
 * efficient painting code, see
 * <a href="http://www.oracle.com/technetwork/java/painting-140037.html">Painting in AWT and Swing</a>.
 *
 * @param     tm   maximum time in milliseconds before update
 * @param     x    the <i>x</i> coordinate
 * @param     y    the <i>y</i> coordinate
 * @param     width    the width
 * @param     height   the height
 * @see       #update(Graphics)
 * @since     JDK1.0
 */
public void repaint(long tm, int x, int y, int width, int height) {
    if (this.peer instanceof LightweightPeer) {
        // Needs to be translated to parent coordinates since
        // a parent native container provides the actual repaint
        // services.  Additionally, the request is restricted to
        // the bounds of the component.
        if (parent != null) {
            if (x < 0) {
                width += x;
                x = 0;
            }
            if (y < 0) {
                height += y;
                y = 0;
            }

            int pwidth = (width > this.width) ? this.width : width;
            int pheight = (height > this.height) ? this.height : height;

            if (pwidth <= 0 || pheight <= 0) {
                return;
            }

            int px = this.x + x;
            int py = this.y + y;
            parent.repaint(tm, px, py, pwidth, pheight);
        }
    } else {
        if (isVisible() && (this.peer != null) &&
            (width > 0) && (height > 0)) {
            PaintEvent e = new PaintEvent(this, PaintEvent.UPDATE,
                                          new Rectangle(x, y, width, height));
            SunToolkit.postEvent(SunToolkit.targetToAppContext(this), e);
        }
    }
}
sTreeTu
  • 1
  • 1

1 Answers1

5

Reset i before the while

v = 100;
i = 1; // if you want 99 times
while (i<v) { ...

Note: try to stay away from using global values like this because you have to keep track of the value of the variable all the time. Use local variables if possible.

Local variables:

  int ax = 100; 
  int bx = 1;
  while(bx < ax){
     try{  
        Thread.sleep(100);
     } 
     catch(Exception ek) {}
     double stopien = 231.4 - ((360*v)/280);
     double radSek = Math.toRadians(stopien);
     x = srodek + (a * Math.cos(radSek));   
     y = srodek - (a * Math.sin(radSek));
     repaint();
     bx++;
  }
Code Whisperer
  • 1,041
  • 8
  • 16
  • Or even better, make `i` a local variable of the method, too. – John Bollinger Mar 18 '15 at 17:21
  • I add it and it is still looping once :( – sTreeTu Mar 18 '15 at 17:58
  • Show us the method `repaint()`. My suspicion is that you do something in `repaint()` that modifies `i`. Otherwise replace `v = 100; i = 1;` with `int ax = 100; int bx = 1;` and replace `v` and `i` appropriately in the loop and see if it still loops only once. – Code Whisperer Mar 18 '15 at 18:00
  • i change it to ax and bx and it didn't help. I don't understand what u mean by showing repaint. I didn't do anything with it – sTreeTu Mar 18 '15 at 18:14
  • You sure repaint() is not changed? I am trying to see where else the loop would break and can't see any other issue so far. – Code Whisperer Mar 18 '15 at 18:19
  • I can place it sources or something, i just write down repaint() and it was working. Sorry for being noob but i started learning jave few days ago. – sTreeTu Mar 18 '15 at 18:23
  • Check this looks like similar issue with Thread.sleep(): [sleep](http://stackoverflow.com/questions/4917383/repainting-a-jpanel-in-a-loop-with-a-delay) – Code Whisperer Mar 18 '15 at 18:24