「無理やりSwing!」から始まったSwingプログラミングが所期の目的を果たすことができました。(火曜日にはGUIフロントエンドが完成していましたが、肝心の数値計算プログラムの完成が遅れていました。理由:昨日のアレ)
このプログラムが完成したことにより、
プログラムを実行して、パラメータを入力。
計算が終わったらファイルAをBにコピーしてから再びプログラムを実行してパラメータを入力。
また計算が終わったらファイルAを・・・
という億劫な作業を直感的に操作できるようになりました。おまけに、自分で作ったGUIプログラムを使うことで数値計算も楽しさが倍増です。
このプログラムは思いつきで急造したので、「パラメータを入力して実行」、「特定のファイルをコピー」、「入力欄をクリア」と最小限の機能しか実装していません。使用したコンポーネントも以前に挙げたもののみです。
簡単に言うと、このプログラムはexec()で呼び出す外部プログラムをボタンで選択しているだけですが、この方法は「ボタン」と「外部プログラム呼び出しメソッド」を増やすだけで機能を増加できるので、今後は出力されたデータをGnuplotでグラフ化したり、グラフを画像データ化する機能を追加したいと思っています。(Javaを理解しているとは言えないので、Javaをもっと理解し、コードが安全か危険かを判断できるようにしたいとも思う)
/*========================
GUI FrontEnd For MDcalc
Input Form
Using Java Swing
========================*/
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
//---Run MDcalc---
class MDcalculator
{
//---Calc---
public String calc(int nstep, int istart,
int ivs, int nvs, int temp)
{
String result;
try{
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("./MDcalc "+nstep+" "+istart+
" "+ivs+" "+nvs+" "+temp);
InputStream is = p.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
result = br.readLine();
}catch(IOException ex){
ex.printStackTrace();
result = "error";
}
return result;
}
//---Copy---
public void copy()
{
String re2;
try{
Runtime rt2 = Runtime.getRuntime();
Process p2 = rt2.exec("cp restart.out restart.in");
}catch(IOException ex){
ex.printStackTrace();
}
}
}
//---Main Class---
public class InputForm extends JFrame implements ActionListener
{
//---InputForm Parts---
JTextField form1;
JComboBox form2;
JComboBox form3;
JTextField form4;
JTextField form5;
JLabel caution;
public static void main(String[] args)
{
InputForm frame = new InputForm();
frame.setTitle("MD Calculator Client");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(350, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
InputForm()
{
//---Base---
JPanel base = new JPanel();
//---Input nstep---
JPanel panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(350, 50));
panel1.setLayout(new FlowLayout());
form1 = new JTextField(10);
form1.setHorizontalAlignment(JLabel.RIGHT);
panel1.add(new JLabel("NSTEP:"));
panel1.add(form1);
//---Input istart---
JPanel panel2 = new JPanel();
panel2.setPreferredSize(new Dimension(350, 50));
panel2.setLayout(new FlowLayout());
String[] f2_ini = {"ON", "OFF"};
form2 = new JComboBox(f2_ini);
panel2.add(new JLabel("Read restart.in:"));
panel2.add(form2);
//---Input ivs---
JPanel panel3 = new JPanel();
panel3.setPreferredSize(new Dimension(350, 50));
panel3.setLayout(new FlowLayout());
String[] f3_ini = {"ON", "OFF"};
form3 = new JComboBox(f3_ini);
panel3.add(new JLabel("Velocity Scale:"));
panel3.add(form3);
//---Input nvs---
JPanel panel4 = new JPanel();
panel4.setPreferredSize(new Dimension(350, 50));
panel4.setLayout(new FlowLayout());
form4 = new JTextField(10);
form4.setHorizontalAlignment(JLabel.RIGHT);
panel4.add(new JLabel("Scale:"));
panel4.add(form4);
panel4.add(new JLabel("every times"));
//---Input temp---
JPanel panel5 = new JPanel();
panel5.setPreferredSize(new Dimension(350, 50));
panel5.setLayout(new FlowLayout());
form5 = new JTextField(10);
form5.setHorizontalAlignment(JLabel.RIGHT);
panel5.add(new JLabel("TARGET TEMP[K]:"));
panel5.add(form5);
//---Run Button---
JPanel panel6 = new JPanel();
panel6.setPreferredSize(new Dimension(350, 50));
panel6.setLayout(new FlowLayout(FlowLayout.RIGHT));
JButton run_btn = new JButton("Run");
run_btn.addActionListener(this);
run_btn.setActionCommand("run");
JButton reset_btn = new JButton("Reset");
reset_btn.addActionListener(this);
reset_btn.setActionCommand("reset");
JButton copy_btn = new JButton("Copy");
copy_btn.addActionListener(this);
copy_btn.setActionCommand("copy");
caution = new JLabel("");
panel6.add(caution);
panel6.add(run_btn);
panel6.add(copy_btn);
panel6.add(reset_btn);
//---Put Form Panel on Base---
base.add(panel1);
base.add(panel2);
base.add(panel3);
base.add(panel4);
base.add(panel5);
//---Set Panels on Frame---
getContentPane().add(base, BorderLayout.CENTER);
getContentPane().add(panel6, BorderLayout.PAGE_END);
}
//---Action Event Method---
public void actionPerformed(ActionEvent e)
{
MDcalculator mdc = new MDcalculator();
String f1str, f4str, f5str, end_str, act_cmd;
int f1, f2=0, f3=0, f4, f5;
int f2index, f3index;
act_cmd = e.getActionCommand();
if(act_cmd.equals("run")){
//---form1---
f1str = form1.getText();
f1 = Integer.parseInt(f1str);
//---form2---
f2index = form2.getSelectedIndex();
if(f2index == -1){
caution.setText("Error!");
}else{
if(f2index == 0) f2 = 1;
else if(f2index == 1) f2 = 0;
}
//---form3---
f3index = form3.getSelectedIndex();
if(f3index == -1){
caution.setText("Error!");
}else{
if(f3index == 0) f3 = 1;
else if(f3index == 1) f3 = 0;
}
//---form4---
f4str = form4.getText();
f4 = Integer.parseInt(f4str);
//---form5---
f5str = form5.getText();
f5 = Integer.parseInt(f5str);
//---Run MDcalc---
caution.setText("Start!");
end_str = mdc.calc(f1, f2, f3, f4, f5);
caution.setText(end_str);
}else if(act_cmd.equals("reset")){
form1.setText("");
form4.setText("");
form5.setText("");
caution.setText("Reset!");
}else if(act_cmd.equals("copy")){
mdc.copy();
caution.setText("Copy!");
}
}
}
最近のコメント