import javax.swing.*;
import java.awt.*;public class EllipseDemo1 extends JApplet implements Runnable{        int radiu=0;
        int inc=10; public void paint(Graphics g){
super.paint(g);
 int width =getWidth();
int height=getHeight();
System.out.println("hello");
setBackground(Color.blue);
g.setColor(Color.green);
int x =(int)(width/2-radiu/2);
int y =(int)(height/2-radiu/2);
g.fillOval(x,y,radiu,radiu);
}
public void run(){
while(true){
radiu+=inc;
try{
  Thread.sleep(1000);
}catch(InterruptedException e)
{
System.out.println(e.getMessage());
}
                                              //update(getGraphics());
repaint();
      if (radiu==100)
break;
}

}
public void init(){
EllipseDemo1 demo=new EllipseDemo1();
Thread t =new Thread(demo);
t.start();
}

}我想实现画一个圆球让它的半径不断增大,可是不能刷新啊,用update(Graphics g)又不对,这是怎么回事?

解决方案 »

  1.   

    import javax.swing.*;
    import java.awt.*;public class EllipseDemo1 extends JApplet implements Runnable {
    int radiu=0;
            int inc=10;
    public void init(){
    Thread t =new Thread(this);
    t.start();
    }
    public void run(){
    while(true){
    radiu+=inc;
    if (radiu==100)
    break;
    repaint();
    try{
      Thread.sleep(1000);
    }catch(InterruptedException e)
    {
    System.out.println(e.getMessage());
    }
                                                  //update(getGraphics());
    }

    }
    public void paint(Graphics g){
    super.paint(g);
      int width =getWidth();
    int height=getHeight();
    setBackground(Color.blue);
    g.setColor(Color.green);
    int x =(int)(width/2-radiu/2);
    int y =(int)(height/2-radiu/2);
    g.fillOval(x,y,radiu,radiu);
    }

    }
    试试
      

  2.   

    import javax.swing.*;
    import java.awt.*;public class EllipseDemo1 extends JApplet{
    int radiu=0;
            int inc=10;
    public void init(){
    this.getContentPane().add(new Ell());
    this.setVisible(true);
    }
    private class Ell extends JPanel implements Runnable{
    public Ell()
    {
    setBackground(Color.blue);
    Thread t =new Thread(this);
    t.start();
    }
    public void run(){
    while(true){
    radiu+=inc;
    if (radiu==100)
    break;
    repaint();
    try{Thread.sleep(1000);}catch(InterruptedException e){System.out.println(e.getMessage());}
    }
                                                
    }
    public void paint(Graphics g){
    super.paint(g);
    g.setColor(Color.green);
    int x =(int)(getHeight()/2-radiu/2);
    int y =(int)(getWidth()/2-radiu/2);
    g.fillOval(x,y,radiu,radiu);
    }
    }
    }
      

  3.   

    use following method to test:
    moveall();repaint()