• 沒有找到結果。

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.net.*;

import javax.swing.*;

class login extends JInternalFrame implements ActionListener{

//網路參數

public static Socket socket;

static String servername ="140.113.134.106";

static int port = 2525;

public DataOutputStream outstream;

public DataInputStream instream;

public ObjectInputStream inObj ; public ObjectOutputStream outObj ;

//=====實驗參數 static int info_no;

static String user_name;

static float Vs, cv, cc, cs, c_alpha, ped,err;

static boolean started;

static double pro_weight0,pro_weight,pro_ped;

static long startT,pro_date;

static float pro_c, pro_e;

Boolean checkpw;

//LoginFrame位置 static int offsetX ; static int offsetY ;

//GUI參數

private Container c;

JPanel jp;

JTextField text1 ;

JPasswordField text2;

JButton Button1;

JLabel text1Label, text2Label;

//輸入帳密後執行緒 tThread t1;

Long S1;

String data;

Boolean log ; ObjInfo one_Info ; ObjProcess pro_test;

public login(){

super("請先登入");

setSize(250,120);

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

offsetX = screenSize.width/2 - 200;

offsetY = screenSize.height/2 - 150;

setLocation( offsetX, offsetY);

c = getContentPane();

jp = new JPanel();

text1 = new JTextField(12); // 建立帳號輸入欄位 text2 = new JPasswordField(12); // 建立密碼輸入欄位

// 建立標籤

text1Label = new JLabel("使用者名稱: ");

text1Label.setLabelFor(text1); // 屬於文字方塊 text2Label = new JLabel("使用者密碼: ");

text2Label.setLabelFor(text2); // 屬於密碼欄位

// 建立按鈕

Button1 = new JButton("確定");

Button1.addActionListener(this);

jp.setLayout(new FlowLayout(FlowLayout.CENTER));

jp.add(text1Label);

jp.add(text1);

jp.add(text2Label);

jp.add(text2);

jp.add(Button1);

c.add(jp);

}

//=====輸入帳密後,執行動作:連線-送出帳密-Y/N-持續接收時間參數 public void actionPerformed(ActionEvent e) {

if (e.getSource()==Button1){

//=====設定帳密檢核機制=====

try{ //嘗試連線到伺服端

System.out.println("嘗試連線 ");

socket = new Socket(InetAddress.getByName(servername),port);

inObj = new ObjectInputStream(socket.getInputStream());

outObj = new ObjectOutputStream(socket.getOutputStream());

outstream = new DataOutputStream(socket.getOutputStream());

instream = new DataInputStream(socket.getInputStream());

outstream.writeUTF(text1.getText()); //送出使用者帳號 outstream.writeUTF(text2.getText()); //送出使用者密碼

checkpw = instream.readBoolean(); //接收帳密合格訊息

if(checkpw){ //密碼正確

one_Info = (ObjInfo)inObj.readObject(); //接收初始參數

user_name = one_Info.user_name; //接收使用者姓名 info_no = one_Info.info_no; //接收實驗序號 Vs = one_Info.para1; //接收參數1:土壤體積 cv = one_Info.para2; //接收參數2:壓密係數 cc = one_Info.para3; //接收參數3:壓縮指數

c_alpha = one_Info.para4; //接收參數4:二次壓縮指數 ped = one_Info.para5; //接收參數5:過去最大應力 cs = one_Info.para6; //接收參數6:膨脹指數 err = one_Info.para7; //接收參數7:自訂誤差

if(one_Info.started){ //若已啟動

pro_test = (ObjProcess)inObj.readObject();

//接收過程參數

startT = pro_test.startT; //實驗起始時間 pro_weight0 = pro_test.pro_weight0;

//累計荷重(含本次)

pro_weight = pro_test.pro_weight; //改變荷重 pro_ped = pro_test.pro_ped; //過去最大應力 pro_date = pro_test.pro_T; //本階段加重時間點 pro_c = pro_test.pro_c; //本階段加重時的沉陷量 pro_e = pro_test.pro_e; //誤差率

}

else{ //若未啟動

startT = 0; //實驗起始時間

pro_weight0 = 2.5; //預載荷重 pro_weight = 0; //未開始,無荷重

pro_date = 0; //本階段加重時間點

pro_c = 0; //本階段加重時的沉陷量 pro_e = 0; //誤差率

}

setVisible(false);

t1 = new tThread(socket,user_name,info_no, Vs, cv, cc,cs, c_alpha, ped, err, startT,pro_weight0, pro_weight, pro_ped, pro_date, pro_c, pro_e);

t1.start();

}else{//密碼錯誤

setVisible(true);

}

}catch(Exception e1){

e1.printStackTrace();

}

} }

}

class tThread extends Thread{

static int info_no;

String user_name;

float Vs, cv, cc,cs, c_alpha, err;

boolean started;

static double pro_weight0,pro_weight, pro_ped;

static long startT;

static long pro_date ; static float H2,pro_c,ped;

float pro_e;

//===計算過程中所用到的參數 static double w3, w4, ts;

float eo, Sc, M, u1, Sc_t, Ss_t;

double tv_t;

double t,t1;

int j;

static float u, u2;

static Long S1;

String nowT,Str3;

Socket socket;

DataInputStream IS;

public tThread (Socket socket,String user_name,int info_no,float Vs,float

cv,float cc,float cs,float c_alpha,float ped,float err,

long startT,double pro_weight0,double pro_weight,double pro_ped ,long pro_date,float pro_c,float pro_e)throws IOException{

this.socket = socket;

this.user_name = user_name;

this.info_no = info_no;

this.Vs = Vs;

this.cv = cv;

this.cc = cc;

this.cs = cs;

this.c_alpha = c_alpha;

this.ped = ped;

this.err = err;

this.startT = startT;

this.pro_weight0 = pro_weight0;

this.pro_weight = pro_weight;

this.pro_date = pro_date;

this.pro_c = pro_c;

this.pro_e = pro_e;

Cli034.jlabel3.setText("<html>"+user_name+"<br>&nbsp;&nbsp;您好

</html>");

Cli034.jlabel4.setText("<html>實驗經過時間:<br></html>"+Str3);

}

public void run(){

while(true){

try {Thread.sleep(250);

IS = new DataInputStream(new

BufferedInputStream(socket.getInputStream()));

S1 = IS.readLong(); //接收伺服器時間 nowT = " " + String.format("%1$tY-%1$tm-%1$td

%1$tH:%1$tM:%1$tS%n", S1);

Cli034.jlabel1.setText(nowT);

//主畫面GUI更新

if (pro_weight0 == 2.5){//初始化

w3 = 5;

w4 = 0;

}else{if(pro_weight < 0)//卸載時 {

w3 = Math.sqrt(pro_weight * pro_weight);

w4 = w3 / 2;

} else{

w3 = pro_weight * 2;

w4 = w3 / 2;

}

}

Cli034.jbutton1.setText("加載 + "+w3+"kg");

Cli034.jbutton2.setText("卸載 - "+w4+"kg");

if(startT != 0){//已啟動

t = S1 - pro_date;

t = t / 1000;

t1 = S1 - startT ; t1 = t1 / 1000;

String ST = " " + String.format("%1$tY-%1$tm-%1$td

%1$tH:%1$tM:%1$tS%n", startT);

System.out.println("");

int tD=(int) t1/86400;

t1 = t1 - tD * 86400;

int tH=(int) t1/3600;

t1 = t1 - tH * 3600;

int tM=(int) t1/60;

t1 = t1 - tM * 60;

int tS = (int) t1/1;

Cli034.jlabel4.setText("<html>實驗經過時間:<br> "+tD+"天

"+tH+"時"+tM+"分"+tS+"秒</html>");

H2 = 20 - pro_c;

System.out.println("現階段計算參數:");

System.out.println("w3: "+w3);

System.out.println("w4: "+w4);

System.out.println("t : "+t);

M = (float) ((Math.PI/2)*(2*j+1));

M = M*M; if(pro_ped < ped)

{

pro_ped = ped;

}

eo = (H2-Vs)/Vs;

//判斷是否過壓密

if(pro_weight0 >= pro_ped) {

Sc = (H2 / (1+eo));

Sc = (float)(cs *

Math.log10(pro_ped/(pro_weight0-pro_weight))+ cc * Math.log10(pro_weight0/pro_ped));

}else{

Sc = cs*H2 / (1+eo);

Sc = (float)(Sc * Math.log10(pro_weight0/(pro_weight0-pro_weight)));

} }

//主壓密

tv_t = cv * t / (H2*H2); //時間因子 u1 = 0;

for(j=0;j<=200;j++)

{

M = (float) ((Math.PI/2)*(2*j+1));

M = M*M;

u1 = (float) (u1 + 2 / (M*Math.exp(M*tv_t)));

} //u1平均壓密度(%)

u1 = 1 - u1;

System.out.println("u1 = "+u1);

Sc_t = Sc * u1; //Sc_t主壓密沉陷量

System.out.println("Sc_t = "+Sc_t);

//二次壓密 if(t < 2 * ts)

{

t1 = 2 * ts;

}else{

t1 = t;

}

Ss_t = (float) ((c_alpha/(1+eo)) * H2 * Math.log10(t1/ts));

//總沉陷量

u = (float) (Sc_t + Ss_t + pro_c);

u = u * (1 + pro_e);

if(u > u2){

u2 = u;

} else{

u = u2;

}

System.out.println("consolidation = "+u); //沉陷量 System.out.println("=============");

} else{

u = 0;

}

} catch (Exception e) { e.printStackTrace();

} } }

}

public class Cli034 extends JFrame implements ActionListener{

private JDesktopPane jdesktop;

private Container c;

login logframe ; //登入小視窗

JPanel np, ep;

public static JLabel jlabel1; //伺服器時間標籤 private static JLabel jlabel2;

public static JLabel jlabel3;

public static JLabel jlabel4;

public static JButton jbutton1;

public static JButton jbutton2;

public static AnimationPane cp; //動畫

public static String str1, str2, str3, str4;

Timer timer;

public static float u = 0 ; //沉陷量(mm) static double u2, u3; //角度(大,小) URL url;

public Cli034(){

super("單向度壓密試驗");

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

setBounds( 50 ,50, screenSize.width-100, screenSize.height-100);

jdesktop = new JDesktopPane();

logframe = new login();

logframe.setVisible(true);

jdesktop.add(logframe);

try {

logframe.setSelected(true);

} catch (Exception e) { e.printStackTrace();

}

setContentPane(jdesktop);

c = getContentPane();

jlabel1 = new JLabel(" ",JLabel.RIGHT);

jlabel1.setSize(400, 20);

jlabel1.setBackground(Color.yellow);

jlabel1.setFont(new Font("Times New Roman",Font.PLAIN,14));

String Str1 = "<html>單向度壓密試驗</html>";

jlabel2 = new JLabel(Str1,JLabel.CENTER);

jlabel2.setSize(400, 120);

jlabel2.setBackground(Color.yellow);

jlabel2.setForeground(Color.black);

jlabel2.setFont(new Font("標楷體",Font.PLAIN,90));

jlabel3 = new JLabel("<html>請先登入<br> </html>", JLabel.CENTER);

jlabel3.setSize(380, 140);

jlabel2.setFont(new Font("Times New Roman",Font.PLAIN,48));

jlabel4 = new JLabel("<html>實驗經過時間:<br> </html>", JLabel.CENTER);

jbutton1 = new JButton("加載 "+str3);

jbutton1.addActionListener(new myActionListener() );

jbutton2 = new JButton("卸載 "+str4);

jbutton2.addActionListener(new myActionListener() );

Toolkit toolkit = Toolkit.getDefaultToolkit();

url = getClass().getClassLoader().getResource("007.gif");

Image image2 = Toolkit.getDefaultToolkit().getImage(url);

//image2 = 針圖

url = getClass().getClassLoader().getResource("008.gif");

Image image1 = Toolkit.getDefaultToolkit().getImage(url);

//image1 = 底圖

url = getClass().getClassLoader().getResource("009.gif");

Image image3 = Toolkit.getDefaultToolkit().getImage(url);

//image3 = 砝碼

cp = new AnimationPane(image1,image2,image3);

np = new JPanel();

np.setLayout(new BorderLayout(2,2));

np.add(jlabel1,BorderLayout.NORTH);

np.add(jlabel2,BorderLayout.SOUTH);

JPanel gg = new JPanel();

GridBagLayout gridbag = new GridBagLayout();

GridBagConstraints gbc = new GridBagConstraints();

gg.setLayout(gridbag);

gbc.fill = GridBagConstraints.VERTICAL;

gbc.ipady = 400;

gbc.ipadx = screenSize.width*3/4-90;

gbc.gridx = 0;

gbc.gridy = 0;

gridbag.setConstraints(cp, gbc);

gg.add(cp);

ep = new JPanel();

ep.setLayout(new GridLayout(4,1));

ep.add(jlabel3);

ep.add(jlabel4);

ep.add(jbutton1);

ep.add(jbutton2);

gbc.ipadx = screenSize.width/4 -90;

gbc.gridx = 1;

gbc.gridy = 0;

gridbag.setConstraints(ep, gbc);

gg.add(ep);

c.setLayout(new BorderLayout(4,4));

c.add(np,BorderLayout.NORTH);

c.add(gg,BorderLayout.CENTER);

timer = new Timer(100, this);

timer.setInitialDelay(0);

timer.start();

}

class myActionListener implements ActionListener{

public void actionPerformed(ActionEvent evt){

if (evt.getSource() == jbutton1){ //加載 if(tThread.startT == 0){ //第一次加載 tThread.pro_weight0 = 7.5;

tThread.pro_weight = tThread.w3;

tThread.startT = tThread.S1;

}else{

tThread.pro_weight0 = tThread.pro_weight0+tThread.w3;

tThread.pro_weight = tThread.w3;

}

tThread.pro_date = tThread.S1;

tThread.pro_c = tThread.u;

tThread.H2 = 20 - tThread.pro_c;

tThread.ts = 0;

}

else if (evt.getSource() == jbutton2) {//卸載

tThread.pro_weight0 = tThread.pro_weight0 - tThread.w4;

tThread.pro_weight = 0 - tThread.w4;

tThread.pro_date = tThread.S1;

tThread.pro_c = tThread.u;

tThread.H2 = 20 - tThread.pro_c;

tThread.ts = 0;

} else{

System.out.println("邏輯錯誤");

}

//tThread.startT;

System.out.println("info_no = "+tThread.info_no);

//實驗編號

System.out.println("weight0 = "+tThread.pro_weight0);

//現階段荷重(累計)

System.out.println("weight = "+tThread.pro_weight);

//最近新荷重(5.10.20.40)

System.out.println("time_point = "+tThread.pro_date);

//加載時間點

System.out.println("consolidation = "+tThread.pro_c);

//沉陷量

System.out.println("error = 0.01");

//誤差率

//送出user_no,weight0,weight,time_point,consolidation,error Socket socket = login.socket;

try {

ObjectOutputStream outObj = new ObjectOutputStream(socket.getOutputStream());

outObj.writeObject(new

ObjProcess(tThread.info_no,tThread.pro_ped,tThread.pro_weight0,tThread.pro_

weight,tThread.startT,tThread.pro_date,tThread.pro_c,(float)0.01));

} catch (IOException e) {

// TODO Auto-generated catch block e.printStackTrace();

System.out.println("發生錯誤");

}

}

}

class AnimationPane extends JPanel{

private Image image1,image2,image3;

int x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, i;

public AnimationPane(Image image1,Image image2,Image image3){

setBackground(Color.blue);

this.image1 = image1;

this.image2 = image2;

this.image3 = image3;

x1 = 120;

y1 = 90;

x2 = 214;//x1 + 94;

y2 = 121;//y1 + 31;

x3 = 228;//x1 + 108;

y3 = 193;//y1 + 103;

x4 = 209;//x1 + 89;

y4 = 200;//y1 + 110;

x5 = 214;//x1 + 94;

y5 = 221;//y1 + 131;

}

public void paintComponent(Graphics g){

super.paintComponent(g);

g.drawImage(image1,x1, y1, 420, 336, this);

for (i = 1 ;i*5 < tThread.pro_weight0 ; i++) //數量 {

g.drawImage(image3,400, 352-i*12, 105, 30, this); //繪砝碼 }

Graphics2D g2 = (Graphics2D) g;

g2.rotate(Math.toRadians(u2),x3,y3); //大指針圖, 軸心(x3,y3) g2.drawImage(image2, x2, y2, 27, 81, this); //繪指針圖 g2.rotate(Math.toRadians(-u2),x3,y3);

g2.rotate(Math.toRadians(u3),x5,y5); //小指針圖, 軸心(x5,y5) g2.drawImage(image2, x4, y4, 10, 23, this); //繪指針圖

} }

public void actionPerformed(ActionEvent evt){

u = tThread.u;

u2 = u*360;

u3 = u2 / 10;

cp.repaint();

}

public static void main(String args[]){

Cli034 app = new Cli034();

app.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

} });

app.setVisible(true);

} }

相關文件