简体   繁体   English

while循环仅循环一次-Java

[英]while loop loops just once - java

Why my while loop is looping just once ? 为什么我的while循环只循环一次? It should loops 99 times. 它应该循环执行99次。 Could Thread.sleep method break this loop or repaint ? Thread.sleep方法可以中断此循环或重新绘制吗? Can you help me resolve this ? 你能帮我解决这个问题吗? It doesn't show any errors. 它没有显示任何错误。 And i am using netbeans. 而且我正在使用netbeans。

Licznik.java: 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: 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);
        }
    }
}

Reset i before the while 重置i的面前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++;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM