6.2.1 Heat1d_all.java


//
// Heat1d_all.java
//

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

/*
<applet code ="Heat1d_all" width=800 height=600>
</applet>
*/

public class Heat1d_all extends Applet
    implements ActionListener, ItemListener, Runnable {
    
    public int N = 20;
    public double lambda = 0.5;
    public double Tmax = 0.5;
    public double theta =0.5;
    public double A = 0.0;
    public double B = 0.0;
    public int erabuBC = 0;
    public int erabuF = 0;
    public int anime = 0;

    // ユーザーとのインターフェイス
    public Label labelLambda, labelN, labelTmax, 
        labelTheta, labelA, labelB, labelanime;
    public TextField inputLambda, inputN, inputTmax, 
        inputTheta, inputA, inputB;
    public Button startB;
    public Choice DirOrNeu, fchoi, animechoi;
    // 差分法による熱方程式シミュレーション
    public HeatCanvas_all c1;
    public Thread th;
  
    public void init() {
        //画面のレイアウト、レイアウトマネージャーの設定
        setLayout(new BorderLayout());

        //Northパネルの設定(入力フォーム)
        Panel pn = new Panel();

        //North-Westパネルの設定(Dirichlet or Neumann choice)
        Panel pnw = new Panel();
        pnw.setLayout(new GridLayout(2,1));

        DirOrNeu = new Choice();
        DirOrNeu.addItem("Dirichlet B C");
        DirOrNeu.addItem("Neumann B C");
        DirOrNeu.addItem("Neumann & Dirichlet B C");
        DirOrNeu.addItem("Dirichlet & Neumann B C");
        DirOrNeu.addItemListener(this);
        pnw.add(DirOrNeu);

        fchoi = new Choice();
        fchoi.addItem("f(x) | {x(0<x<0.5), 1-x(0.5<x<1.0)}");
        fchoi.addItem("f(x)=1.0");
        fchoi.addItem("f(x)=sin2πx");
        fchoi.addItemListener(this);
        pnw.add(fchoi);
        pn.add(pnw, "West");
        
        //North-Centerパネルの設定
        Panel pnc = new Panel();
        pnc.setLayout(new GridLayout(6,2));
        
        //テキストフィールドの準備
        String s1 = "lambda (should be <= 1/2)";
        labelLambda = new Label(s1, Label.LEFT);
        pnc.add(labelLambda);

        inputLambda = new TextField("" + lambda,10);
        pnc.add(inputLambda);

        labelN = new Label("N", Label.LEFT);
        pnc.add(labelN);

        inputN = new TextField("" + N,10);
        pnc.add(inputN);

        labelTmax = new Label("Tmax", Label.LEFT);
        pnc.add(labelTmax);

        inputTmax = new TextField("" + Tmax,10);
        pnc.add(inputTmax);

        String s2 = "theta (0.0 <= theta <= 1.0)";
        labelTheta = new Label(s2, Label.LEFT);
        pnc.add(labelTheta);

        inputTheta = new TextField("" + theta,10);
        pnc.add(inputTheta);

        labelA = new Label("A", Label.LEFT);
        pnc.add(labelA);

        inputA = new TextField("" + A,10);
        pnc.add(inputA);

        labelB = new Label("B", Label.LEFT);
        pnc.add(labelB);

        inputB = new TextField("" + B,10);
        pnc.add(inputB);

        inputLambda.addActionListener(this);
        inputN.addActionListener(this);
        inputTmax.addActionListener(this);
        inputTheta.addActionListener(this);
        inputA.addActionListener(this);
        inputB.addActionListener(this);

        pn.add(pnc, "Center");
        add(pn, "North");

        //North-Eastパネルの設定
        Panel pne = new Panel();
        pne.setLayout(new GridLayout(2,1));

        labelanime = new Label("Anime", Label.LEFT);
        pne.add(labelanime);

        animechoi = new Choice();
        animechoi.addItem("OFF");
        animechoi.addItem("ON(fast)");
        animechoi.addItem("ON(normal)");
        animechoi.addItem("ON(slow)");
        animechoi.addItem("ON(super slow)");
        animechoi.addItemListener(this);
        pne.add(animechoi);

        pn.add(pne, "East");
        add(pn, "North");

        //Centerパネルの設定
        Panel pc = new Panel();
        c1 = new HeatCanvas_all();
        c1.setSize(400,400);
        pc.add(c1);
        add(pc,"Center");

        //Eastパネルの設定
        Panel pe = new Panel();
        startB = new Button("Restart");

        pe.add(startB);
        
        add(pe,"East");
        startB.addActionListener(this);
        //スレッドの作成、開始
        th = new Thread(this);
        th.start();
    } // init の終了

    //スレッドの実行
    public void run(){
        c1.compute(lambda, N, Tmax, theta, A, B, erabuBC, erabuF, anime);
    }

    //イベント処理(ボタン)
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == startB) {
            String str1 = inputLambda.getText().trim();
            String str2 = inputN.getText().trim();
            String str3 = inputTmax.getText().trim();
            String str4 = inputTheta.getText().trim();
            String str5 = inputA.getText().trim();
            String str6 = inputB.getText().trim();

            lambda = Double.valueOf(str1).doubleValue();
            N = Integer.parseInt(str2);
            Tmax = Double.valueOf(str3).doubleValue();
            theta = Double.valueOf(str4).doubleValue();
            A = Double.valueOf(str5).doubleValue();
            B = Double.valueOf(str6).doubleValue();
            c1.compute(lambda, N, Tmax, theta, A, B);
        }
    }

    //イベント処理2(チョイス)
    public void itemStateChanged(ItemEvent e) {
        if (e.getSource() == DirOrNeu) {
            erabuBC = DirOrNeu.getSelectedIndex();
            c1.compute(lambda, N, Tmax, theta, A, B, erabuBC);  
        }

        if (e.getSource() == fchoi) {
            erabuF = fchoi.getSelectedIndex();
            c1.compute(lambda, N, Tmax, theta, A, B, erabuBC, erabuF);  
         }

        if (e.getSource() == animechoi) {
            anime = animechoi.getSelectedIndex();
            if (anime == 4) {
                anime = anime * 3;
            }
            c1.compute(lambda, N, Tmax, theta, A, B, erabuBC, erabuF, anime);
        }
    }
}



桂田 祐史