//import java.applet.Applet;
import java.awt.*;
import javax.swing.*;public class StarSim extends JFrame    implements Runnable
{    int STARS;
    int x[];
    int y[];
    int xv[];
    int xi[];
    Color c[];
    Thread runner;
    Image DB_Image;
    Graphics DB_Graphics;
    Dimension DB_Dimension;    public StarSim()
    {
     super("Demo"); 
     STARS = 200;
    
        if(STARS == 0 || STARS > 200 || STARS < 1)
        {
            STARS = 200;
        }
        x = new int[STARS];
        y = new int[STARS];
        xv = new int[STARS];
        xi = new int[STARS];
        c = new Color[STARS];
        start();
            }    public void paint(Graphics g)
    {
        DB_Dimension = size();
        if(DB_Graphics == null)
        {
            DB_Image = createImage(DB_Dimension.width, DB_Dimension.height);
            DB_Graphics = DB_Image.getGraphics();
        }
        setBackground(Color.black);
        for(int i = 0; i < STARS; i++)
        {
            x[i] = (int)(Math.random() * (double)DB_Dimension.width);
            y[i] = (int)(Math.random() * (double)DB_Dimension.height);
            xi[i] = x[i];
            int j;
            while((j = (int)(Math.random() * 30D)) < 0 || j > 2) ;
            switch(j)
            {
            case 0: // '\0'
                xv[i] = 2;
                c[i] = Color.gray;
                break;            case 1: // '\001'
                xv[i] = 4;
                c[i] = Color.lightGray;
                break;            case 2: // '\002'
                xv[i] = 6;
                c[i] = Color.white;
                break;
            }
        
        DB_Graphics.setColor(Color.black);
        DB_Graphics.fillRect(0, 0, DB_Dimension.width, DB_Dimension.height);
        for( i = 0; i < STARS; i++)
        {
            DB_Graphics.setColor(c[i]);
            DB_Graphics.drawLine(x[i], y[i], x[i], y[i]);
            DB_Graphics.setColor(c[++i]);
            DB_Graphics.drawLine(x[i], y[i], x[i], y[i]);
            DB_Graphics.setColor(c[++i]);
            DB_Graphics.drawLine(x[i], y[i], x[i], y[i]);
            DB_Graphics.setColor(c[++i]);
            DB_Graphics.drawLine(x[i], y[i], x[i], y[i]);
        }        g.drawImage(DB_Image, 0, 0, this);
     }
   }
    public void run()
    {
        do
        {
            for(int i = 0; i < STARS; i++)
            {
                if((x[i] += xv[i]) >= DB_Dimension.width)
                {
                    x[i] = 0;
                    int j;
                    for(j = 4; (j = (int)(Math.random() * 30D)) < 0 || j > 2;) { }
                    switch(j)
                    {
                    case 0: // '\0'
                        xv[i] = 2;
                        c[i] = Color.gray;
                        break;                    case 1: // '\001'
                        xv[i] = 4;
                        c[i] = Color.lightGray;
                        break;                    case 2: // '\002'
                        xv[i] = 6;
                        c[i] = Color.white;
                        break;
                    }
                }
            }            try
            {
                Thread.sleep(50L);
            }
            catch(InterruptedException _ex)
            {
                stop();
            }
            repaint();
        } while(true);
    }    public void start()
    {
        runner = new Thread(this);
        runner.start();
    }    public void stop()
    {
        if(runner != null)
        {
            runner.stop();
            runner = null;
        }
    }
    
    public static void main(String[] args)
    {
     StarSim ss=new StarSim();
     ss.setSize(200,200);
     ss.show();
    }
}

解决方案 »

  1.   

    import java.applet.Applet;
    import java.awt.*;public class AppletKeyExample extends Applet
        implements Runnable
    {    int STARS;
        int x[];
        int y[];
        int xv[];
        int xi[];
        Color c[];
        Thread runner;
        Image DB_Image;
        Graphics DB_Graphics;
        Dimension DB_Dimension;    public void init()
        {
    //        String s = getParameter("stars");
            String s =null;
            if(s == null)
            {
                s = "200";
            }
            STARS = Integer.valueOf(s).intValue();
            if(STARS == 0 || STARS > 200 || STARS < 1)
            {
                STARS = 200;
            }
            x = new int[STARS];
            y = new int[STARS];
            xv = new int[STARS];
            xi = new int[STARS];
            c = new Color[STARS];
            DB_Dimension = size();
            if(DB_Graphics == null)
            {
                DB_Image = createImage(DB_Dimension.width, DB_Dimension.height);
                DB_Graphics = DB_Image.getGraphics();
            }
            setBackground(Color.black);
            for(int i = 0; i < STARS; i++)
            {
                x[i] = (int)(Math.random() * (double)DB_Dimension.width);
                y[i] = (int)(Math.random() * (double)DB_Dimension.height);
                xi[i] = x[i];
                int j;
                while((j = (int)(Math.random() * 30D)) < 0 || j > 2) ;
                switch(j)
                {
                case 0: // '\0'
                    xv[i] = 2;
                    c[i] = Color.gray;
                    break;            case 1: // '\001'
                    xv[i] = 4;
                    c[i] = Color.lightGray;
                    break;            case 2: // '\002'
                    xv[i] = 6;
                    c[i] = Color.white;
                    break;
                }
            }    }    public void paint(Graphics g)
        {
            update(g);
        }    public void update(Graphics g)
        {
            DB_Graphics.setColor(Color.black);
            DB_Graphics.fillRect(0, 0, DB_Dimension.width, DB_Dimension.height);
            for(int i = 0; i < STARS; i++)
            {
                DB_Graphics.setColor(c[i]);
                DB_Graphics.drawLine(x[i], y[i], x[i], y[i]);
                DB_Graphics.setColor(c[++i]);
                DB_Graphics.drawLine(x[i], y[i], x[i], y[i]);
                DB_Graphics.setColor(c[++i]);
                DB_Graphics.drawLine(x[i], y[i], x[i], y[i]);
                DB_Graphics.setColor(c[++i]);
                DB_Graphics.drawLine(x[i], y[i], x[i], y[i]);
            }        g.drawImage(DB_Image, 0, 0, this);
        }    public void run()
        {
            do
            {
                for(int i = 0; i < STARS; i++)
                {
                    if((x[i] += xv[i]) >= DB_Dimension.width)
                    {
                        x[i] = 0;
                        int j;
                        for(j = 4; (j = (int)(Math.random() * 30D)) < 0 || j > 2;) { }
                        switch(j)
                        {
                        case 0: // '\0'
                            xv[i] = 2;
                            c[i] = Color.gray;
                            break;                    case 1: // '\001'
                            xv[i] = 4;
                            c[i] = Color.lightGray;
                            break;                    case 2: // '\002'
                            xv[i] = 6;
                            c[i] = Color.white;
                            break;
                        }
                    }
                }            try
                {
                    Thread.sleep(50L);
                }
                catch(InterruptedException _ex)
                {
                    stop();
                }
                repaint();
            } while(true);
        }    public void start()
        {
            runner = new Thread(this);
            runner.start();
        }    public void stop()
        {
            if(runner != null)
            {
                runner.stop();
                runner = null;
            }
        }    public AppletKeyExample()
        {
            STARS = 200;
        }
        public static void main(String args[])
        {
        Frame f = new Frame();
        f.setSize(400,300);
        AppletKeyExample obj = new AppletKeyExample();
        f.add(obj);
        f.setVisible(true);
        f.show();
        obj.init();
        obj.start();
        }
    }