シミュレーション

戻る
::::::::::::::
Attractor.java
::::::::::::::
/**
$Id: MySimulation.html,v 1.1 2009/06/22 16:11:50 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class Attractor {

    private int x, y;

    public Attractor ( int x, int y ) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }

}
::::::::::::::
GlobalPainter.java
::::::::::::::
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
<pre>
様々な図形などの描画をコントロールするクラス
</pre>
$Id: MySimulation.html,v 1.1 2009/06/22 16:11:50 kishi Exp kishi $
@author KISHI Yasuhiro
*/

class GlobalPainter extends JComponent implements Runnable {
    private int count = 0;
    private int x = 0;
    private Color color = Color.blue;
    private Dimension dim = getSize();

    /** 矩形を別スレッドで描画するクラス */
    private RectFiller rf1;
    private RectFiller rf2;
    private Attractor attractor = new Attractor( 200, 200 );

    private SphereManager sm = new SphereManager( 45, attractor );

    public GlobalPainter() {
        ( new Thread( this ) ).start();

        rf1 = new RectFiller( new Color( 0x33, 0x00, 0x99 ), 30, 40 );
        rf1.start();

        rf2 = new RectFiller( Color.yellow, 50, 60 );
        rf2.start();

        // sphere1 = new Sphere(Color.red, 200,200,10,10);

    }

    public void run() {

        while ( true ) {
            x += 3;
            if ( x > 100 ) {
                x = 0;
            }

            sm.add();

            /************* 【重要】 ****************/
            SwingUtilities.invokeLater(
                new Runnable() {
                    public void run() {
                        repaint();
                    }
                }
            );

            try {
                Thread.sleep( 10 );
            } catch ( Exception exception ) {}
        }

    }

    public void paintComponent( Graphics g ) {

        g.setColor( Color.black );
        g.fillRect( 0, 0, getWidth(), getHeight() );

        g.setColor( color );
        g.fillRect( 10 , 10 , 10 + x , 20 );

        /* このオブジェクトは別スレッドで動いている */
        rf1.paint( g );
        rf2.paint( g );

        /* Sphereクラスのオブジェクトたちを動かす */
        sm.paint( g );
    }

}

::::::::::::::
MySimulation.java
::::::::::::::
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
<pre>
Swingでは勝手にダブルバッファリングしてくれるので便利!
</pre>
$Id: MySimulation.html,v 1.1 2009/06/22 16:11:50 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class MySimulation extends JApplet {

    public void init() {

        setBackground( Color.black );

        Container contentPane = getContentPane();
        contentPane.setLayout( new FlowLayout() );

        GlobalPainter gp = new GlobalPainter();

        gp.setPreferredSize( new Dimension( 600 , 400 ) );

        contentPane.add( gp );

    }

}

::::::::::::::
RectFiller.java
::::::::::::::
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
$Id: MySimulation.html,v 1.1 2009/06/22 16:11:50 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class RectFiller extends Thread {
    private int x = 0;
    private Color color;
    private int sx;
    private int sy;

    public RectFiller( Color color, int sx, int sy ) {
        this.color = color;
        this.sx = sx;
        this.sy = sy;
    }

    public void paint( Graphics g ) {
        g.setColor( color );
        g.fillRect( sx , sy , 10 + x , 20 );
    }

    public void run() {
        while ( true ) {
            if ( x++ > 100 ) {
                x = 0;
            }
            try {
                sleep( 100 );
            } catch ( Exception exception ) {}

            // System.out.println( "x=" + x );
        }
    }
}
::::::::::::::
Sphere.java
::::::::::::::
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/**
$Id: MySimulation.html,v 1.1 2009/06/22 16:11:50 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class Sphere {
    static int count = 0;
    private int cx, cy = 0;
    private int rx, ry = 0;
    private Color color;
    private double radian = 0.0;
    private Attractor attractor;
    private double angularSpeed = 6.0;
    private double radius = 5.0;

    public Sphere( Color color, int rx, int ry, Attractor attractor ) {

        count++;

        this.color = color;

        /** 適当な乱れを与える */
        angularSpeed += count * 2;
        radius += count * 4;

        this.rx = rx;
        this.ry = ry;

        this.attractor = attractor;

    }

    public void paint( Graphics g ) {
        g.setColor( color );
        g.fillOval( cx , cy , rx, ry );
    }

    // 角速度に従い回転する
    public void move() {

        radian += angularSpeed * Math.PI / 180.0; // 12度ずつ回転する

        cx = attractor.getX() + ( int ) ( Math.cos( radian ) * radius );
        cy = attractor.getY() + ( int ) ( Math.sin( radian ) * radius );

    }
}
::::::::::::::
SphereManager.java
::::::::::::::
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/**
$Id: MySimulation.html,v 1.1 2009/06/22 16:11:50 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class SphereManager {
    private int max = 0;
    java.util.List list;
    Attractor attractor;

    /**
    @param max 惑星の数
    @param attractor アトラクタ
    */
    public SphereManager( int max, Attractor attractor ) {
        this.max = max;
        this.attractor = attractor;

        list = new java.util.LinkedList();
    }

    /** 惑星を追加する */
    public synchronized void add
        ( ) {

        if ( list.size() < max ) {
            Sphere sphere = new Sphere( Color.red, 10, 10, attractor );
            list.add( sphere );

            System.out.println( "list.size()=" + list.size() );
        }

    }

    /** 惑星を描画する
    @param g 描画オブジェクト
    */
    public synchronized void paint( Graphics g ) {
        Iterator iterator = list.iterator();
        while ( iterator.hasNext() ) {
            Sphere sphere = ( Sphere ) iterator.next();
            sphere.move( );
            sphere.paint( g );
        }
    }
}
戻る inserted by FC2 system