next up previous
Next: 5.2 UnivParticle.java Up: 5 Particleシミュレーション Previous: 5 Particleシミュレーション

5.1 UnivApplet.java

/*
  <APPLET code="UnivApplet.class"
     width=600 height=600>
  </APPLET>
*/
import java.awt.*;
import java.applet.*;
import java.util.*;

public class UnivApplet
    extends Applet implements Runnable {
    private Thread trig = null;
    int NumP = 6;
    Particle[] ps = new Particle[NumP];
    int size = 70;
    static int height = 600, width = 600;

    class Particle {
        String name;
        int pos[] = new int[2];
        int dot[] = new int[2];
        int acc[] = new int[2];

        Particle(String n, int px, int py,
                 int vx, int vy) {
            name = n;
            pos[0] = px; pos[1] = py;
            dot[0] = vx; dot[1] = vy;
        }
        Particle(String n, int px, int py) {
            this(n,px,py,0,0);
        }
        double dist(Particle p) {
            return(
                Math.sqrt(
                    (double)
                    ((p.pos[0]-pos[0])*
                     (p.pos[0]-pos[0]) +
                     (p.pos[1]-pos[1])*
                     (p.pos[1]-pos[1]))));
        }
        void step() {
            pos[0] += dot[0]; pos[1] += dot[1];
            dot[0] += acc[0]; dot[1] += acc[1];
            if (pos[0] <= 0) {
                pos[0]=0; dot[0] = - dot[0];}
            if (pos[1] <= 0) {
                pos[1]=0; dot[1] = - dot[1];}
            if (pos[0] >= width-size) {
                pos[0]=width-size;
                dot[0] = - dot[0];}
            if (pos[1] >= height-size) {
                pos[1]=height-size;
                dot[1] = - dot[1];}
        }
        public void draw(Graphics g) {
            g.setColor(Color.black);
            g.fillOval(pos[0],pos[1],size,size);
        }
        public void crashdraw(Graphics g) {
            g.setColor(Color.yellow);
            g.fillOval(pos[0],pos[1],size,size);                
        }
        public boolean
        iscrashed(Particle p, double thd) {
            return (dist(p) < thd);
        }
    }
    
    class Rocket extends Particle {
        int motor[] = new int[2];
    
        public Rocket(String n, int x0, int y0,
                      int vx, int vy, int mx, int my) {
            super(n, x0, y0, vx, vy);
            acc[0] = motor[0] = mx;
            acc[1] = motor[1] = my;
        }
        public void draw(Graphics g) {
            g.setColor(Color.black);
            g.fillRect(pos[0],pos[1],size,size);
        }
        public void crashdraw(Graphics g) {
            g.setColor(Color.red);
            g.fillRect(pos[0],pos[1],size,size);
        }
    }
    public void init() {
        ps[0] = new Particle("p1",300,100,0,2);
        ps[1] = new Particle("p2",100,100,2,0);
        ps[2] = new Particle("p3",200,100,2,2);

        ps[3] = new Rocket("r1", 50,50,2,0, 1,0);
        ps[4] = new Rocket("r2",150,50,0,2, 0,1);
        ps[5] = new Rocket("r3",250,50,2,2, 1,1);

        trig = new Thread(this);
        trig.start();
    }
    public void paint(Graphics g) {
        boolean crashed=false;
        for (int i=0; i<NumP; i++) ps[i].step();
        
        for (int i=0; i<NumP; i++) {
            crashed = false;
            for (int j=0; j<NumP; j++) {
                if (i!=j) {
                    if (ps[i].
                        iscrashed(ps[j],
                                  (double)size))
                        crashed = true;
                }
            }
            if (crashed) ps[i].crashdraw(g);
            else ps[i].draw(g);
        }
    }
    public void run() {
        while (trig != null) {
            repaint();
            try {
                Thread.sleep(100);
            }
            catch (Exception e) {
                showStatus("Error: " + e);
            }
        }
    }
}


generated through LaTeX2HTML. M.Inaba 平成18年5月7日