📄 controllergui.java
字号:
import javax.swing.JApplet;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import java.awt.event.ActionEvent;
import java.awt.geom.Point2D;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.Container;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class ControllerGUI extends JFrame implements HeadPointListener.HeadPointUpdatedListener,MouseListener,VisionUpdatedListener,PointPick.PointPickedListener,MechaController.MechaUpdatedListener,ActionListener,EStopListener.UpdatedListener{
//============paramaters===========================
private JButton estopBtn,connectBtn;
private JLabel statusLabel;
private VisionPanel visionPane;
private JPanel controlPane;
private JPanel headPointPane;
private PointPick pp;
private PointPick pp1;
private JTextField textField;
private JRadioButton horizRotateBtn;
private JRadioButton horizStrafeBtn;
private JRadioButton panBtn;
private JRadioButton rollBtn;
private HeadPointListener hpcomm;
//JButton btn;
//private JTextField textField;
private ControllerListener comm;
private MechaController mechacomm;
private EStopListener estoplistener;
private String server=new String("192.168.2.2");
private int port=10020;
//JList menu;
//JScrollPane menuScroll;
//JComboBox title;
String status;
//JList scripts;
boolean isUpdating=false;
//DefaultListModel scriptsModel=new DefaultListModel();
boolean connected=false;
float mspf=0;
long lastFrameTime=0;
float mspfGamma=.9f;
public class StatusUpdateThread extends Thread {
ControllerGUI gui;
public StatusUpdateThread(ControllerGUI gui) { super("StatusUpdate"); this.gui=gui; setDaemon(true);}
public void run() {
VisionListener l=gui.visionPane.getListener();
while(true) {
String status=gui.status;
gui.statusLabel.setText(status);
try {
sleep(100);
} catch(Exception ex) {break;}
}
}
}
public void init(){
estoplistener=new EStopListener(server,10053);
statusLabel=new JLabel();
status=new String("initialized");
comm=new ControllerListener(server,port);
comm.listener=this;
Container cp=getContentPane();
cp.setLayout(new BorderLayout());
//=========initial the panel in the center of the container
//=======the buttons control the motion of aibo will be here
Box center=Box.createVerticalBox();
Box centerup=Box.createHorizontalBox();
Box centerdown=Box.createHorizontalBox();
{ //==========add the vision Panel=====================
JPanel visiontmp=new JPanel();
visiontmp.setLayout(new BorderLayout());
visiontmp.add(Box.createVerticalStrut(20),BorderLayout.NORTH);
visiontmp.add(Box.createVerticalStrut(20),BorderLayout.SOUTH);
visiontmp.add(Box.createHorizontalStrut(10),BorderLayout.WEST);
visiontmp.add(Box.createHorizontalStrut(10),BorderLayout.EAST);
visionPane=new VisionPanel();
visiontmp.setPreferredSize(new Dimension(300,300));
visiontmp.add(visionPane,BorderLayout.CENTER);
centerup.add(visiontmp);
}
JSeparator sep=new JSeparator(SwingConstants.VERTICAL);
sep.setMaximumSize(new Dimension(sep.getMaximumSize().width,sep.getMaximumSize().height));
centerup.add(sep);
//======add control Panel================
{
controlPane=new JPanel();
initControlPane(controlPane);
centerup.add(controlPane);
}
center.add(centerup);
JSeparator sep1=new JSeparator(SwingConstants.HORIZONTAL);
sep1.setMaximumSize(new Dimension(sep1.getMaximumSize().width,sep1.getMaximumSize().height));
center.add(sep1);
//=========================================================================
{
headPointPane=new JPanel();
initHPPane(headPointPane);
centerdown.add(headPointPane);
}
JSeparator sep2=new JSeparator(SwingConstants.VERTICAL);
sep2.setMaximumSize(new Dimension(sep2.getMaximumSize().width,sep2.getMaximumSize().height));
centerdown.add(sep2);
{
JPanel connectPane=new JPanel();
connectPane.setLayout(null);
JLabel lb=new JLabel("Global Control GUI");
lb.setHorizontalAlignment(SwingConstants.CENTER);
connectPane.add(lb);
lb.setBounds(0,20,300,30);
JLabel label=new JLabel("Enter the IP address here:");
connectPane.add(label);
label.setBounds(30,80,200,20);
textField=new JTextField("192.168.2.2");
connectPane.add(textField);
textField.setBounds(40,120,220,30);
connectBtn=new JButton("connect");
connectBtn.addActionListener(this);
connectPane.add(connectBtn);
connectBtn.setBounds(160,160,100,30);
JLabel label1=new JLabel("Change Aibo's status:");
connectPane.add(label1);
label1.setBounds(30,200,200,30);
estopBtn=new JButton();
estopBtn.addActionListener(this);
estopBtn.setEnabled(false);
estopBtn.setText(estoplistener.getEStop()?"Un-Stop":"Stop!");
connectPane.add(estopBtn);
estopBtn.setBounds(160,230,100,30);
connectPane.setPreferredSize(new Dimension(300,280));
centerdown.add(connectPane);
}
center.add(centerdown);
JSeparator sep3=new JSeparator(SwingConstants.HORIZONTAL);
sep3.setMaximumSize(new Dimension(sep3.getMaximumSize().width,sep1.getMaximumSize().height));
center.add(sep3);
cp.add(center,BorderLayout.CENTER);
//=====the things in ths south of the panel====
Box temp=Box.createVerticalBox();
temp.add(Box.createVerticalStrut(10));
JPanel tmp=new JPanel(new BorderLayout());
tmp.add(new JLabel(" ==> "),BorderLayout.WEST);
tmp.add(statusLabel,BorderLayout.CENTER);
JLabel label2=new JLabel(">< by Lin.ZiJing @2006.5 ");
label2.setHorizontalAlignment(SwingConstants.RIGHT);
tmp.add(label2,BorderLayout.EAST);
temp.add(tmp);
temp.add(Box.createVerticalStrut(10));
cp.add(temp,BorderLayout.SOUTH);
//====the status update thread
(new StatusUpdateThread(this)).start();
//========================================
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==estopBtn)
{
if(estoplistener.isConnected())
{
//======= output "start\n " to 10053========
estoplistener.toggleEStop();
System.out.println("**estotbtn pressed\n");
estoplistener.getEStop();
}
}else if (e.getSource()==horizStrafeBtn){
horizRotateBtn.setSelected(!horizStrafeBtn.isSelected());
pp.doSetPoint(0, 0);
}
else if (e.getSource()==horizRotateBtn){
horizStrafeBtn.setSelected(!horizRotateBtn.isSelected());
pp.doSetPoint(0, 0);
}
else if (e.getSource()==connectBtn){
if(connectBtn.getText()=="connect"){
server=textField.getText();
textField.setEditable(false);
comm=new ControllerListener(server,port);
comm.listener=this;
estoplistener=new EStopListener(server,10053);
estoplistener.addUpdatedListener(this);
status="connecting ... please wait";
comm.needConnection();
connectBtn.setText("disconnect");
connectBtn.setEnabled(false);
}else{
((UDPVisionListener)visionPane.getListener()).removeListener(visionPane);
visionPane.close();
mechacomm.kill();
hpcomm.kill();
comm.removeDynObjs();
comm.kill();
estoplistener.close();
connectBtn.setText("connect");
textField.setEditable(true);
}
}
}
public void gotConnection(){
updated();
repaint();
}
public void lostConnection(){
updated();
repaint();
}
public void updated(){
isUpdating=true;
//Point p=menuScroll.getViewport().getViewPosition();
if(comm._isConnected)
{
//walkSerBtn.setEnabled(true);
//rawBtn.setEnabled(true);
//==========added=============================
/*
if(comm._connectCount>0) {
if(comm._connectCount==1) {
for(int i=0; i<scriptsModel.getSize(); i++)
if(scriptsModel.get(i).toString().equals("STARTUP"))
comm.sendInput(((ScriptEntry)scriptsModel.get(i)).cmd);
//restart servers for any windows we still have open
Iterator mons=comm.dynObjSrcs.values().iterator();
while(mons.hasNext())
{
String str=new String();
str=(String)mons.next();
comm.sendInput("!root "+str);
System.out.println("!root"+str);
}
//Set focus to the main menu
menu.requestFocus();
}
for(int i=0; i<scriptsModel.getSize(); i++)
if(scriptsModel.get(i).toString().equals("CONNECT"))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -