【Java开源代码栏目提醒】:以下是网学会员为您推荐的Java开源代码-CSOW_Wheel.java,希望本篇文章对您学习有所帮助。
/*
* Copyright (C) 2010 The CSOW Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.chapter13;
import java.io.ByteArrayInputStream;
import org.chapter13.DU.*;
import org.chapter13.LR.*;
import com.Chapter13.RD.*;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
enum ActionType {
R, L, D, U
}
enum ParseType {
OSCILLATION_LR, OSCILLATION_DU, WHEEL
}
public class CSOW_Wheel extends Activity {
float x,y;
int state;
int deltaRow, deltaCol;
int nowButtonX,nowButtonY,nowSiteX,nowSiteY,lastButtonX,lastButtonY;
String edgeFSM, FSM;
boolean isMoveReset=true, isClick=true;
int w, h;
View btn = null;
LR_Parser parserLR = null;
DU_Parser parserDU = null;
RD_Parser parserRD = null;
String sParse = "";
int iComposed = 0;
int iDir;
int iIncDec = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
btn = (BtnView) findViewById(R.id.Button01);
btn.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
state = event.getAction();
if(state==MotionEvent.ACTION_DOWN||state==MotionEvent.ACTION_MOVE){
x=event.getX();//读X坐标
y=event.getY();//读Y坐标
nowcheck(x,y);//设定当前所指Button
if(isMoveReset==true){
nowSiteX=nowButtonX;
nowSiteY=nowButtonY;
isMoveReset=false;
}
if(state==MotionEvent.ACTION_MOVE) {
if(nowSiteX!=nowButtonX||nowSiteY!=nowButtonY) {
lastcheck(x,y);//设定离开所指Button
isClick=false;
deltaRow = nowSiteX - lastButtonX;
deltaCol = nowSiteY - lastButtonY;
forward(); // 检查上、下、左、右
// Highlight two buttons & setTitle
if(edgeFSM.length()<2) setTitle(edgeFSM);
markButtonFlag(nowSiteX, nowSiteY);
markButtonFlag(lastButtonX, lastButtonY);
v.invalidate(); // 拿掉这行变成无实时性
restates(); // 从 ACTION_UP区块移到ACTION_MOVE
isMoveReset=true;
if( edgeFSM=="\\" ) //如果方向不对
init();
}
}
else if(state==MotionEvent.ACTION_DOWN) {
// Highlight one button
markButtonFlag(nowButtonX, nowButtonY);
v.invalidate(); // 拿掉这行变成无实时性
}
}
else if(state==MotionEvent.ACTION_UP){//手指离开
if(isClick) {
printText();
//init();
}
else {
//restates();
isClick=true;
}
init();
isMoveReset=true;
v.invalidate();
}
return true;
} // onTouch
});
}
private void markButtonFlag(int buttonX, int buttonY) {
if(buttonX==0 && buttonY == 0) BtnView.iAreaFocus[0] = 1;
else if(buttonX==0 && buttonY == 1) BtnView.iAreaFocus[1] = 1;
else if(buttonX==1 && buttonY == 0) BtnView.iAreaFocus[2] = 1;
else if(buttonX==1 && buttonY == 1) BtnView.iAreaFocus[3] = 1;
if(edgeFSM.length()>0) {
char dir = edgeFSM.charAt(0);
System.out.println("dir= "+dir);
for(int i=0; i<BtnView.iAreaColor.length; i++)
if(dir=='R')
BtnView.iAreaColor[i] = Color.YELLOW;
else if(dir=='L')
BtnView.iAreaColor[i] = Color.BLUE;
else if(dir=='D')
BtnView.iAreaColor[i] = Color.GREEN;
else if(dir=='U')
BtnView.iAreaColor[i] = Color.RED;
if(edgeFSM.length()>=2)
for(int i=0; i<BtnView.iAreaColor.length; i++) {
if(dir=='R')
BtnView.iAreaColor[i] = Color.YELLOW - ((iComposed*5)%0xff*0x100);
else if(dir=='L')
BtnView.iAreaColor[i] = Color.BLUE + (iComposed*5)%0xff;
else if(dir=='D')
BtnView.iAreaColor[i] = Color.GREEN - ((iComposed*5)%0xff*0x100);
else if(dir=='U')
BtnView.iAreaColor[i] = Color.RED + ((iComposed*5)%0xff<<8);
setTitle(" Color: "+BtnView.iAreaColor[i]);
}
}
}
public void nowcheck(float x,float y){
double w = btn.getWidth()/2.0;
double h = btn.getHeight()/2.0;
nowButtonX = (int) (y / h);
nowButtonY = (int) (x / w);
};
public void lastcheck(float x,float y){
double w = btn.getWidth()/2.0;
double h = btn.getHeight()/2.0;
lastButtonX = (int) (y / h);
lastButtonY = (int) (x / w);
};
public void forward(){//新一步的计算
if( deltaRow == -1 ) {edgeFSM += "D"; }
else if( deltaRow == 1 ) {edgeFSM += "U"; }
else if( deltaCol == -1 ) {edgeFSM += "R"; }
else if( deltaCol == 1 ) {edgeFSM += "L"; }
else {edgeFSM += "\\";}
};
public void restates(){//判断是否是正确图形
ByteArrayInputStream Bis1 = null;
if(edgeFSM.length()>=2) {
try {
Bis1 = new ByteArrayInputStream((edgeFSM+"!").getBytes("UTF-8"));
if(!edgeFSM.contains("D") && !edgeFSM.contains("U") ) {
parserLR = new LR_Parser(Bis1);
parseAndCompute("OSCILLATION_LR");
}
else if(!edgeFSM.contains("L") && !edgeFSM.contains("R") ) {
parserDU = new DU_Parser(Bis1);
parseAndCompute("OSCILLATION_DU");
}
else {
parserRD = new RD_Parser(Bis1);
parseAndCompute("WHEEL");
}
} catch(java.io.UnsupportedEncodingException e) {
}
} // if(edgeFSM.length()>=2)
else {
System.out.println("edgeFSM.length()<2, edgeFSM= "+edgeFSM+" sParse= "+sParse);
try{
switch( ActionType.valueOf( edgeFSM ) ){
case R://右
case L://左
case D://下
case U://上
break;
default:
FSM = "";
}
}catch(IllegalArgumentException e){
FSM = "";
setTitle("");
System.out.println( e );
}finally{
printText();
//init();
}
}
};//restates
private void parseAndCompute(String sType) {
try {
switch( ParseType.valueOf( sType ) ){
case OSCILLATION_LR:
sParse = parserLR.Goal();
break;
case OSCILLATION_DU:
sParse = parserDU.Goal();
break;
case WHEEL:
sParse = parserRD.Goal();
break;
}
System.out.print(sType + "sParse:");
System.out.println(sParse);
try {
MathEvaluator m = new MathEvaluator(sParse);
iComposed = m.getValue().intValue();
try {
iDir = Integer.pars
上一篇:
CSOW_Shift.java
下一篇:
凉凉的清晨