Swing Applet

戻る

::::::::::::::
ForestApplet.bat
::::::::::::::
appletviewer Forest.html

<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="generator" content=
"HTML Tidy for Cygwin (vers 1st September 2004), see www.w3.org" />
<title>FOREST SIMULATION</title>
</head>
<body>
<applet code="ForestApplet" archive="Forest.jar"
 width="800" height="600"></applet>
</body>
</html>

::::::::::::::
ForestJFrame.bat
::::::::::::::
java -cp .;Forest.jar ForestJFrame

::::::::::::::
MakeForest.sh
::::::::::::::
#!/bin/sh
# $Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $

javac -cp . *.java 	\
&& astyle -a -j -P *.java 	\
&& rm -f *.orig *.bak		\
&& vu.sh *.java 		\
&& jar cvf ../Forest.jar *.class	\
&& rm -f *.class

::::::::::::::
MakeJavaDoc.sh
::::::::::::::
#!/bin/sh 

javadoc -classpath ../Forest.jar  -windowtitle "Javadoc of Package Forest" -use -author -version -d ../javadoc -private *.java
::::::::::::::
Branch.java
::::::::::::::
import java.util.*;

/**
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class Branch extends Thread {

    private Position start;
    private Position end;
    private Double angle;

    private List children = null;

    public Branch( Position start, double angle ) {}

    public void run() {}
}
::::::::::::::
CommonView.java
::::::::::::::
import java.awt.*;
import java.util.*;

/**
* <pre>
* Listで記述された座標を描画する汎用的なクラス 
* $Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
* @author KISHI Yasuhiro
* </pre>
*/

public class CommonView {

    static public void paint( Graphics g, java.util.List list, Color color ) {
        for ( int i = 1;i < list.size();i++ ) {

            g.setColor( color );
            double[] from = ( double[] ) list.get( i - 1 );
            double[] to = ( double[] ) list.get( i );
            g.drawLine( ( int ) from[ 0 ], ( int ) from[ 1 ], ( int ) to[ 0 ], ( int ) to[ 1 ] );
        }
    }
}
::::::::::::::
DrawingPanel.java
::::::::::::::
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

/**
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class DrawingPanel extends JPanel implements Runnable {

    private int i = 0;
    private int mx = 0;
    private int my = 10;
    private String message = "ツリー構造は全ての構造の基本です。。。";

    private WormView wormView1;
    private WormView wormView2;
    private WormModel wormModel1;
    private WormModel wormModel2;
    double[] attractor;

    private WaveModel waveModel1, waveModel2, waveModel3, waveModel4;
    private WaveView waveView1, waveView2, waveView3;

    public DrawingPanel() {

        System.out.println( isDoubleBuffered() );
        setDoubleBuffered( true );
        System.out.println( isDoubleBuffered() );

        attractor = new double[ 2 ];
        attractor[ 0 ] = 0;
        attractor[ 1 ] = 0;

        wormModel1 = new WormModel( 200, 200, -20, 10 );
        wormView1 = new WormView();
        wormView1.setModel( wormModel1, new Color( 0xff, 0xff, 0x00 ) );

        wormModel2 = new WormModel( 100, 200, 20, 10 );
        wormView2 = new WormView();
        wormView2.setModel( wormModel2, new Color( 0xcc, 0xff, 0xcc ) );

        // 波#1
        waveModel1 = new WaveModel( 0.0, 200.0 );
        waveModel1.setParticleCount( 50, 10 );
        waveModel1.setIntialAngle( 20.0 );
        waveModel1.setAnglularSpeed( 15.0 );
        waveModel1.setMaxHeight( 100.0 );
        waveModel1.setPhasePitch( 15.0 );
        waveModel1.init();

        waveView1 = new WaveView();
        waveView1.setModel( waveModel1, Color.green );

        // 波#2
        waveModel2 = new WaveModel( 0.0, 200.0 );
        waveModel2.setParticleCount( 50, 10 );
        waveModel2.setIntialAngle( 20.0 );
        waveModel2.setAnglularSpeed( 20.0 );
        waveModel2.setMaxHeight( 100.0 );
        waveModel2.setPhasePitch( 10.0 );
        waveModel2.init();

        waveView2 = new WaveView();
        waveView2.setModel( waveModel2, Color.white );

        // 波#3
        waveModel3 = new WaveModel( 400.0, 300.0 );
        waveModel3.setParticleCount( 20, -8 );
        waveModel3.setIntialAngle( 20.0 );
        waveModel3.setAnglularSpeed( 25.0 );
        waveModel3.setMaxHeight( 50.0 );
        waveModel3.setPhasePitch( 12.0 );
        waveModel3.init();

        waveView3 = new WaveView();
        waveView3.setModel( waveModel3, new Color( 0xff, 0x33, 0xcc ) );

        // 波#4 → CommonViewで描画
        waveModel4 = new WaveModel( 350.0, 400.0 );
        waveModel4.setParticleCount( 20, 8 );
        waveModel4.setIntialAngle( 20.0 );
        waveModel4.setAnglularSpeed( 20.0 );
        waveModel4.setMaxHeight( 56.0 );
        waveModel4.setPhasePitch( 12.0 );
        waveModel4.init();

        /***************************************************************************************/
        /** ThreadクラスはRunnableインタフェースで実装されているクラスをrunさせることができる **/
        /***************************************************************************************/
        Thread thread = new Thread( this );
        thread.start();
    }

    public void run() {
        while ( true ) {
            i++;

            SwingUtilities.invokeLater(
                new Runnable() {
                    public void run() {
                        repaint();
                    }
                }
            );

            try {
                Thread.sleep( 100 );
            } catch ( Exception e ) {
                e.printStackTrace();
            }
        }
    }

    public void paintComponent( Graphics g ) {

        int width = getWidth();
        int height = getHeight();

        // 画面クリア
        g.setColor( Color.black );
        // このコンポーネントのサイズを取得する
        g.fillRect( 0 , 0 , width , height );

        // アトラクタを設定する
        attractor[ 0 ] = ( double ) ( 109 * Math.sin( Math.PI * 0.11 * i ) ) + width / 2 ;
        attractor[ 1 ] = ( double ) ( 239 * Math.cos( Math.PI * 0.07 * i ) ) + height / 2 ;

        g.setColor( Color.red );
        g.fillRect( ( int ) attractor[ 0 ] , ( int ) attractor[ 1 ] , 40 , 60 );

        g.setColor( Color.white );
        g.drawString( message, mx, my );
        my += 1; // 下に移動させる
        if ( my > height + 20 ) {
            my = -20;
        }

        wormModel1.move( attractor, 0.3 );
        wormView1.paint( g );

        wormModel2.move( wormModel1.getPosition( 0 ), 0.5 );
        wormView2.paint( g );

        // 波#1
        waveModel1.move( );
        waveView1.paint( g );

        // 波#2
        waveModel2.move( );
        waveView2.paint( g );

        // 波#3
        waveModel3.move( );
        waveView3.paint( g );

        // 波#4
        waveModel4.move( );
        CommonView.paint( g, waveModel4.getList(), Color.blue );
    }

}
::::::::::::::
ForestApplet.java
::::::::::::::
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

/**
* <pre>
* 森をシミュレーションするApplet
* </pre>
* $Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
* @author KISHI Yasuhiro
*/

public class ForestApplet extends JApplet {

    public void init() {

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

        DrawingPanel dp = new DrawingPanel();
        dp.setPreferredSize( new Dimension( 800 , 600 ) );

        contentPane.add( dp );

    }

}


::::::::::::::
ForestJFrame.java
::::::::::::::
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

/**
<pre>
森をシミュレーションするSWINGアプリケーション
</pre>
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class ForestJFrame extends JFrame {

    private NavigatorPanel naviPanel = new NavigatorPanel();
    private UpperPanel upperPanel = new UpperPanel();
    private LowerPanel lowerPanel = new LowerPanel();
    private DrawingPanel dp = new DrawingPanel();
    private TickerPanel tickerPanel = new TickerPanel();

    public static void main( String args[] ) {
        new ForestJFrame().setVisible( true );
    }

    public ForestJFrame() {

        // タイトル
        setTitle( "ティッカーのテスト" );
        setDefaultCloseOperation( EXIT_ON_CLOSE );

        // レイアウトマネージャ
        GridBagLayout layout = new GridBagLayout();
        getContentPane().setLayout( layout );

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets( 5, 5, 5, 5 );

        //----------------------------------------------------
        // ナビゲータ用のパネルを左側に追加
        //----------------------------------------------------
        constraints.gridy = 0;
        constraints.gridx = 0;
        constraints.gridheight = 3;

        layout.setConstraints( naviPanel, constraints );
        this.add( naviPanel );

        //----------------------------------------------------
        // 上部パネルを追加
        //----------------------------------------------------
        constraints.gridx = 1;
        constraints.gridheight = 1;

        layout.setConstraints( upperPanel, constraints );
        this.add( upperPanel );

        ++ constraints.gridy;

        //----------------------------------------------------
        // 描画用パネルを追加
        //----------------------------------------------------
        dp.setPreferredSize( new Dimension( 320 , 400 ) );
        layout.setConstraints( dp, constraints );
        this.add( dp );

        ++ constraints.gridy;

        //----------------------------------------------------
        // ティッカー用パネルを追加
        //----------------------------------------------------
        layout.setConstraints( tickerPanel, constraints );
        this.add( tickerPanel );

        ++ constraints.gridy;

        //----------------------------------------------------
        // 上部パネルを追加
        //----------------------------------------------------
        constraints.gridx = 1;
        constraints.gridheight = 1;

        layout.setConstraints( lowerPanel, constraints );
        this.add( lowerPanel );

        ++ constraints.gridy;

        // サイズを決定
        this.setBounds( 0, 0, 640, 400 );
        this.pack();
    }
}

::::::::::::::
ForestUtil.java
::::::::::::::
import java.util.*;

/**
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class ForestUtil {

    static double getDistance( double[] from , double[] to ) {
        return Math.sqrt(
                   ( from[ 0 ] - to[ 0 ] ) * ( from[ 0 ] - to[ 0 ] )
                   + ( from[ 1 ] - to[ 1 ] ) * ( from[ 1 ] - to[ 1 ] )
               );
    }

    static double getAngle( double[] from , double[] to ) {
        double xDiff = to[ 0 ] - from[ 0 ];
        double yDiff = to[ 1 ] - from[ 1 ];

        // 傾きを得る
        double gradient = yDiff / xDiff;

        // DEGREE(度)で返す
        return Math.atan( gradient ) * 180.0 / Math.PI;
    }

    static public void main( String[] args ) {
        double[] from = {0.0, 0.0};
        double[] to = new double[ 2 ];

        if ( args.length != 2 ) {
            System.out.println( "Usage: java -cp . ForeUtil [pointX] [pointY]" );
            System.exit( -1 );
        }

        to[ 0 ] = new Double( args[ 0 ] ).doubleValue();
        to[ 1 ] = new Double( args[ 1 ] ).doubleValue();

        System.out.println( "二点間の距離は、" + getDistance( from, to ) );
        System.out.println( "仰角は、" + getAngle( from, to ) );
    }
}
::::::::::::::
Growable.java
::::::::::::::
/**
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
@author KISHI Yasuhiro
*/
public interface Growable {
    /** 成長する */
    public void grow();

    /** 子供をもうける */
    public Object beget();
}
::::::::::::::
LowerPanel.java
::::::::::::::
import java.util.*;

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

/**
<pre>
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
</pre>
@author KISHI Yasuhiro 
*/

public class LowerPanel extends JPanel {

    JButton dummy = new JButton( "LOWER" );

    public LowerPanel() {

        this.add( dummy );

    }
}
::::::::::::::
NavigatorPanel.java
::::::::::::::
import java.util.*;

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

/**
<pre>
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
</pre>
@author KISHI Yasuhiro 
*/

public class NavigatorPanel extends JPanel {

    JButton dummy = new JButton( "Navagator" );

    public NavigatorPanel() {

        this.add( dummy );

    }
}
::::::::::::::
Paintable.java
::::::::::::::
import java.awt.*;

/**
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
@author KISHI Yasuhiro
*/
public interface Paintable {

    public void paint( Graphics g );
}
::::::::::::::
Position.java
::::::::::::::
/**
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class Position {
    private int x;
    private int y;

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



}
::::::::::::::
TickerModel.java
::::::::::::::
/**
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class TickerModel {

    private String message;
    private String margin;
    private int viewLength;
    private int from;
    private int to;

    private String substr = "";

    public TickerModel( String message, int viewLength ) {
        this.message = "        " + message + "        ";
        this.viewLength = viewLength;
        from = 0;
    }

    public void shift() {

        if ( from >= message.length() ) {
            from = 0;

            doSleep( 2000 );
        }

        int to = from + viewLength;
        if ( to >= message.length() ) {
            to = message.length();
        }
        substr = message.substring( from , to );

        // System.out.println( "[" + subset + "] " + from + "-" + to );

        from++;

    }

    public String getSubstr() {
        return substr;
    }

    private void doSleep( long msec ) {

        try {
            Thread.sleep( msec );
        } catch ( Exception e ) {
            e.printStackTrace();
        }
    }

}
::::::::::::::
TickerPanel.java
::::::::::::::
import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

/**
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class TickerPanel extends JPanel implements Runnable {

    private TickerModel tm ;
    private JTextField textField = new JTextField( 45 );

    public TickerPanel() {

        this.add( textField );

        tm = new TickerModel( "インデックスは0から始まり、半角英数字も日本語も一文字は一文字として数えられます。例えば、部分文字列を取得するメソッド、 substring(3,6) では、「四文字目から7文字目まで」を指定したことになります。但し、このとき3文字目は部分文字列に含まれ、7文字目は含まれません。", 45 );

        Thread thread = new Thread( this );
        thread.start();

    }

    public void run() {

        while ( true ) {
            tm.shift();

            textField.setText( tm.getSubstr() );
            try {
                Thread.sleep( 500 );
            } catch ( Exception e ) {}
        }
    }
}
::::::::::::::
UpperPanel.java
::::::::::::::
import java.util.*;

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

/**
<pre>
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
</pre>
@author KISHI Yasuhiro 
*/

public class UpperPanel extends JPanel {

    JButton dummy = new JButton( "UPPER" );

    public UpperPanel() {

        this.add( dummy );

    }
}
::::::::::::::
WaveModel.java
::::::::::::::
import java.util.*;

/**
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class WaveModel {

    private List list;
    private double angle;
    private int count;
    private int horizontalPitch;
    private double x, y, maxHeight, phasePitch, angularSpeed;

    public WaveModel( double x, double y ) {
        this.x = x;
        this.y = y;
    }

    /** 粒子の数と水平のピッチを決める */
    public void setParticleCount( int count, int horizontalPitch ) {
        this.count = count;
        this.horizontalPitch = horizontalPitch;
    }

    public void setIntialAngle( double initialAngle ) {
        this.angle = initialAngle;
    }

    public void setMaxHeight( double maxHeight ) {
        this.maxHeight = maxHeight;
    }

    public void setPhasePitch( double phasePitch ) {
        this.phasePitch = phasePitch;
    }

    public void setAnglularSpeed( double angularSpeed ) {
        this.angularSpeed = angularSpeed;
    }

    /** 初期状態をセットする */
    public void init() {
        list = new ArrayList();

        for ( int i = 0;i < count;i++ ) {
            double point[] = new double[ 2 ];
            point[ 0 ] = x + i * horizontalPitch;
            point[ 1 ] = y + getVerticalDisplacement( i );

            list.add( point );
        }

    }

    /** 角速度分移動する */
    public void move( ) {
        angle += angularSpeed;
        for ( int i = 0;i < list.size();i++ ) {
            // 各々のY座標を変更する
            rotate( ( double[] ) list.get( i ), i );
        }
    }

    public List getList() {
        return this.list;
    }

    /** 垂直方向の変位を求める */
    private double getVerticalDisplacement ( int i ) {
        return maxHeight * Math.sin( ( angle + i * phasePitch ) * Math.PI / 180.0 );
    }

    private void rotate( double[] point, int i ) {
        point[ 1 ] = y + getVerticalDisplacement( i );
    }

    public void dump() {

        System.out.println( "angle=" + angle );
        for ( int i = 0;i < list.size();i++ ) {
            double[] point = ( double[] ) list.get( i );
            System.out.print( "(x=" + point[ 0 ] );
            System.out.print( " " );
            System.out.print( "y=" + point[ 1 ] + ")" );
            System.out.print( " " );
        }
        System.out.println();

    }

    static public void main( String[] args ) {
        WaveModel wm = new WaveModel( 0.0, 0.0 );

        wm.setParticleCount( 5, 10 );
        wm.setMaxHeight( 30.0 );
        wm.setIntialAngle( 20.0 );
        wm.setAnglularSpeed( 6 );
        wm.setPhasePitch( 15.0 );
        wm.init();

        wm.dump();
        for ( int i = 0;i < 3;i++ ) {
            wm.move( );
            wm.dump();
        }

    }
}
::::::::::::::
WaveView.java
::::::::::::::
import java.awt.*;
import java.util.*;

/**
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class WaveView implements Paintable {

    private WaveModel wm;
    private Color color;

    public void setModel( WaveModel wm, Color color ) {
        this.wm = wm;
        this.color = color;
    }

    public void paint( Graphics g ) {
        java.util.List list = wm.getList();
        for ( int i = 1;i < list.size();i++ ) {

            g.setColor( color );
            double[] from = ( double[] ) list.get( i - 1 );
            double[] to = ( double[] ) list.get( i );
            g.drawLine( ( int ) from[ 0 ], ( int ) from[ 1 ], ( int ) to[ 0 ], ( int ) to[ 1 ] );

        }

    }
}
::::::::::::::
WormModel.java
::::::::::::::
import java.util.*;

/**
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class WormModel {

    List list;

    public WormModel( double x, double y, int pitch, int count ) {

        list = new ArrayList();

        for ( int i = 0;i < count;i++ ) {
            double[] point = new double[ 2 ];
            point[ 0 ] = x;
            point[ 1 ] = y;
            list.add( point );
            x += pitch;
        }
    }

    /** 指定した節目の位置を求める */
    public double[] getPosition( int index ) {
        return ( double[] ) list.get( index );
    }

    /** アトラクタの影響を受けて移動する
    @param attractor アトラクタ
    @param influence 影響の度合い
    */
    public void move( double[] attractor, double influence ) {

        getNextPosition( ( double[] ) list.get( 0 ), attractor, influence );

        for ( int i = 1;i < list.size();i++ ) {
            getNextPosition( ( double[] ) list.get( i ), ( double[] ) list.get( i - 1 ), 0.1 );
        }

    }

    public void dump() {

        for ( int i = 0;i < list.size();i++ ) {
            double[] point = ( double[] ) list.get( i );
            System.out.print( "(x=" + point[ 0 ] );
            System.out.print( " " );
            System.out.print( "y=" + point[ 1 ] + ")" );
            System.out.print( " " );
        }
        System.out.println();

    }

    public List getList() {
        return this.list;
    }

    /** 参照先との距離方向から次の位置を求める */
    private void getNextPosition( double[] self, double[] target, double strength ) {
        double xDiff = target[ 0 ] - self[ 0 ];
        double yDiff = target[ 1 ] - self[ 1 ];

        self[ 0 ] += strength * xDiff;
        self[ 1 ] += strength * yDiff;

    }

    /** 2点間の距離を求める */
    private double getDistance( double[] from, double[] to ) {
        double xDiff = from[ 0 ] - to[ 0 ];
        double yDiff = from[ 1 ] - to[ 1 ];
        return Math.sqrt( xDiff * xDiff + yDiff * yDiff );
    }

    static public void main( String[] args ) {

        double[] attractor = new double[ 2 ];
        attractor[ 0 ] = 0;
        attractor[ 1 ] = 0;

        WormModel wm = new WormModel( 200, 200, -3, 5 );

        for ( int i = 0;i < 10;i++ ) {
            wm.move( attractor, 0.5 );
            wm.dump();
            attractor[ 0 ] += 0.5;
        }
    }
}
::::::::::::::
WormView.java
::::::::::::::
import java.awt.*;
import java.util.*;

/**
$Id: forest.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
@author KISHI Yasuhiro
*/

public class WormView implements Paintable {

    private WormModel wm;
    private Color color;

    public void setModel( WormModel wm, Color color ) {
        this.wm = wm;
        this.color = color;
    }

    public void paint( Graphics g ) {
        java.util.List list = wm.getList();
        for ( int i = 1;i < list.size();i++ ) {

            g.setColor( color );
            double[] from = ( double[] ) list.get( i - 1 );
            double[] to = ( double[] ) list.get( i );
            g.drawLine( ( int ) from[ 0 ], ( int ) from[ 1 ], ( int ) to[ 0 ], ( int ) to[ 1 ] );

        }

    }
}
戻る inserted by FC2 system