【Java开源代码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。网学会员整理了Java开源代码-CT2.java的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
package org.chapter16;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import com.Chapter16.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
enum ActionType {
U, R, D, L,
ULLD/*n*/, ULLDR/*a*/, RULLD/*e*/,
URRD/*n*/, LURRD/*a*/, URRDL/*e*/,
DRRU/*u*/, DRRUL/*g*/, LDRRU/*o*/,
DLLU/*u*/, RDLLU/*g*/, DLLUR/*o*/,
DLD/*x*/, DLDR/*d*/, RDLD/*p*/, RDLDR/*z*/,
URU/*x*/, LURU/*d*/, URUL/*p*/, LURUL/*z*/,
DRD/*h*/, DRDL/*b*/, LDRD/*q*/, LDRDL/*s*/,
ULU/*h*/, RULU/*b*/, ULUR/*q*/, RULUR/*s*/,
RDD/*i*/, DDL/*j*/, LDD/*r*/, DDR/*l*/, ULDD/*f*/, DDRU/*t*/, LDDR/*c*/,
UUL/*i*/, RUU/*j*/, UUR/*r*/, LUU/*l*/, UURD/*f*/, DLUU/*t*/, LUUR/*c*/,
URDR/*m*/,RDRU/*v*/,URDRU/*k*/,
LULD/*m*/,DLUL/*v*/,DLULD/*k*/,
DRUR/*w*/,DRURD/*y*/,
LDLU/*w*/,ULDLU/*y*/
}
public class CT2 extends Activity {
public String IPPORT_PREF = "IPPORT_Pref";
public String SHARED_IP = "Shared_IP";
public String SHARED_PORT = "Shared_Port";
final static int SERVER_PORT = 8001;
static EditText et = null;
TextView tv = null;
MyView mv = null;
Client clientOut, clientIn;
int state = -1;
float x0 = -1.0f, y0 = -1.0f, x, y;
int nowButtonX=4,nowButtonY=4,nowSiteX=4,nowSiteY=4,lastButtonX=4,lastButtonY=4;
boolean isMoveReset=true, isClick=true;
int deltaRow = 0, deltaCol = 0;
String edgeFSM = "", FSM = "";
private LayoutInflater mInflater01;
private View mView01;
private EditText mIP, mPort;
private void showConnectForm() {
try {
mInflater01 = LayoutInflater.from(CT2.this);
mView01 = mInflater01.inflate(R.layout.connect, null);
mIP = (EditText) mView01.findViewById(R.id.ipEditText);
mPort = (EditText) mView01.findViewById(R.id.portEditText);
//
final SharedPreferences ipport = getSharedPreferences(IPPORT_PREF, 0);
mIP.setText(ipport.getString(SHARED_IP, ""));
mPort.setText(ipport.getString(SHARED_PORT, ""));
new AlertDialog.Builder(this)
.setView(mView01)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String ip = mIP.getText().toString();
String port = mPort.getText().toString();
if(ip.length()!=0 && port.length()!=0) {
clientOut = new Client(ip, Integer.parseInt(port), null);
clientIn = new Client(ip, Integer.parseInt(port)+1, tv);
clientOut.start();
clientIn.start();
ipport.edit()
.putString(SHARED_IP, mIP.getText().toString())
.putString(SHARED_PORT, mPort.getText().toString())
.commit();
}
}
}).show();
}
catch(Exception e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
System.exit(0);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et = (EditText) findViewById(R.id.EditText01);
tv = (TextView) findViewById(R.id.TextView01);
mv = (MyView) findViewById(R.id.View01);
Button bt = (Button) findViewById(R.id.Button01);
showConnectForm();
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
clientOut.messageout = et.getText().toString()+"\r\n";
clientOut.bReady = true;
tv.setText(tv.getText()+"\r\n"+"I say: "+et.getText().toString());
et.setText("");
}});
tv.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
state=event.getAction();
if(state==MotionEvent.ACTION_DOWN||state==MotionEvent.ACTION_MOVE){
x=event.getX();//读X坐标
y=e