📄 positionctr.java
字号:
import java.io.*;
import java.lang.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.applet.*;
import vrml.external.Browser;
import vrml.external.Node;
import vrml.external.field.EventInSFVec3f;
import vrml.external.field.EventInSFRotation;
public class PositionCtr extends Applet implements Runnable
{
Thread readDataTh;
Thread timerTh;
Label timerLabel;
Button[] Bt_Orie=new Button[5];
Label[] Label_Posi=new Label[3];
Label[] Label_Pdanwei=new Label[3];
TextField[] Textf_Posi=new TextField[3];
Label carPosition;
Label carSpeed;
Label Speeddanwei;
TextField Text_Speed =new TextField("10",10);
float X_Position, Y_Position, Z_Position;
float value_Position[]=new float[3];
float Speed;
Scrollbar Speed_Sbar;
Label speed;
Label high;
Label middle;
Label low;
Label empty;
boolean stateBtn=true;
boolean isScenceLoading=true;
Browser browser;
Node myMaterial;
public void init()
{
setLayout(null);
resize(200,600);
readDataTh = new Thread(this);
timerLabel=new Label("");
timerLabel.reshape(5,620,175,30);
// timerLabel.setBackground(Color.green);
add(timerLabel);
timerTh = new Thread(new Timer(timerLabel));
String str_Orie[]={"Front","Left","Back","Right","Stop"};
String str_Posi[]={"X:","Y:","Z:"};
X_Position=0.0f;
Y_Position=0.0f;
Z_Position=-1.5f;
Speed=15.0f;
carPosition = new Label("小车的位置:");
carSpeed = new Label("小车的速度:");
Speeddanwei=new Label("cm/s");
carPosition.reshape(20,30,120,20);
carSpeed.reshape(20,145,120,20);
Text_Speed.reshape(50,170,50,20);
Speeddanwei.reshape(100,170,50,20);
add(carPosition);
add(carSpeed);
add(Text_Speed);
Text_Speed.setEditable(false);
add(Speeddanwei);
Textf_Posi[0]=new TextField(Float.toString(X_Position),10);
Textf_Posi[1]=new TextField(Float.toString(Y_Position),10);
Textf_Posi[2]=new TextField(Float.toString(Z_Position),10);
// Text_Speed=new TextField(Float.toString(Speed),10);
speed=new Label("选择小车档位:");
high=new Label("高速");
middle=new Label("中速");
low=new Label("低速");
empty=new Label("空档");
Speed_Sbar=new Scrollbar(Scrollbar.VERTICAL, 4, 1, 0, 4);
Speed_Sbar.reshape(120,230,10,150);
speed.reshape(20,210,120,20);
high.reshape(60,250,50,20);
middle.reshape(60,280,50,20);
low.reshape(60,310,50,20);
empty.reshape(60,340,50,20);
add(speed);
add(high);
add(middle);
add(low);
add(empty);
add(Speed_Sbar);
Label createLabel =new Label("南京理工大学自动化学院");
createLabel.reshape(25,600,170,30);
add(createLabel);
for(int i=0;i<3;i++)
{
Label_Posi[i]=new Label(str_Posi[i]);
Label_Pdanwei[i]=new Label("cm");
add(Label_Posi[i]);
add(Textf_Posi[i]);
add(Label_Pdanwei[i]);
Textf_Posi[i].setEditable(false);
Label_Posi[i].reshape(45,55+25*i,20,20);
Textf_Posi[i].reshape(70,55+25*i,50,20);
Label_Pdanwei[i].reshape(120,55+25*i,50,20);
}
for(int i=0;i<5;i++)
{
Bt_Orie[i]=new Button(str_Orie[i]);
add(Bt_Orie[i]);
if (0==i)
{
Bt_Orie[i].reshape(70,400,50,50);
}
else if (1==i)
{
Bt_Orie[i].reshape(20,450,50,50);
}
else if (2==i)
{
Bt_Orie[i].reshape(70,500,50,50);
}
else if (3==i)
{
Bt_Orie[i].reshape(120,450,50,50);
}
else
{
Bt_Orie[i].addMouseListener(new MouseListener(){
public void mousePressed(MouseEvent e)
{
changeText();
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
});
Bt_Orie[i].reshape(70,450,50,50);
}
}
}
public void start()
{
while (isScenceLoading)
{
try
{
browser=Browser.getBrowser(this);
myMaterial=(Node)(browser.getNode("CarPosition"));
isScenceLoading=false;
}
catch(Exception e){}
}
readDataTh.start();
timerTh.start();
}
public void run()
{
try
{
FileInputStream fis = new FileInputStream("E:\\毕业课题\\EAI_Position\\int.txt");
InputStreamReader isr = new InputStreamReader(fis);
LineNumberReader lnr = new LineNumberReader(isr);
String s = null;
int i=0;
while (true)
{
do
{
s = lnr.readLine();
int n=i%3;
switch (n)
{
case 0:
{
X_Position = Integer.parseInt(s);
System.out.println("The decimal read: " + X_Position);
break;
}
case 1:
{
Y_Position = Integer.parseInt(s);
System.out.println("The decimal read: " + Y_Position);
break;
}
case 2:
{
Z_Position = Integer.parseInt(s);
System.out.println("The decimal read: " + Z_Position);
break;
}
}
}while ((i++)%3!=2 && s!=null);
setTextf_Posi();
try
{
Thread.sleep(1000); //等待读入下组数据
}
catch (InterruptedException e){ }
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public boolean handleEvent(Event event)
{
if (event.id==Event.SCROLL_LINE_UP || event.id==Event.SCROLL_LINE_DOWN
|| event.id==Event.SCROLL_ABSOLUTE)
{
setK_speed();
}
return true;
}
public void setK_speed()
{
}
public void setTextf_Posi()
{
Textf_Posi[0].setText(Float.toString((X_Position+35)/10));
Textf_Posi[1].setText(Float.toString(Y_Position/10));
Textf_Posi[2].setText(Float.toString(Z_Position/10));
EventInSFVec3f newPosition=(EventInSFVec3f)myMaterial.getEventIn("set_translation");
value_Position[0]=(X_Position+35)/10;
value_Position[1]=Y_Position/10;
value_Position[2]=Z_Position/10;
newPosition.setValue(value_Position);
}
public void changeText()
{
if (stateBtn)
{
Bt_Orie[4].setLabel("Start");
stateBtn=false;
}
else
{
Bt_Orie[4].setLabel("Stop");
stateBtn=true;
}
}
}
class Timer implements Runnable
{
public Label timerLabel;
public Timer(final Label timerLabel)
{
this.timerLabel=timerLabel;
}
public void run()
{
while(true)
{
try
{
Thread.sleep(100);
}
catch (InterruptedException e) {}
timerLabel.setText((new Date()).toString());
}
}
}
//<applet code="PositionCtr.class" height="600" width="180" mayscript>
//</applet>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -