?? robot.java
字號:
/*
A basic extension of the java.awt.Frame class
*/
import java.awt.*;
import java.io.*;
public class Robot extends Frame
{
RobotController RC;
public Robot()
{
// This code is automatically generated by Visual Cafe when you add
// components to the visual environment. It instantiates and initializes
// the components. To modify the code, only use code syntax that matches
// what Visual Cafe can generate, or Visual Cafe may be unable to back
// parse your Java file into its visual environment.
//{{INIT_CONTROLS
setLayout(null);
setVisible(false);
setSize(insets().left + insets().right + 430,insets().top + insets().bottom + 270);
button1 = new java.awt.Button();
button1.setActionCommand("button");
button1.setLabel("Run Sample Program!");
button1.setBounds(insets().left + 36,insets().top + 36,137,35);
button1.setBackground(new Color(12632256));
add(button1);
button2 = new java.awt.Button();
button2.setActionCommand("button");
button2.setLabel("Run Sonar Loop");
button2.setBounds(insets().left + 228,insets().top + 36,147,36);
button2.setBackground(new Color(12632256));
add(button2);
button3 = new java.awt.Button();
button3.setActionCommand("button");
button3.setLabel("button3");
button3.setBounds(insets().left + 36,insets().top + 108,132,30);
button3.setBackground(new Color(12632256));
add(button3);
button4 = new java.awt.Button();
button4.setActionCommand("button");
button4.setLabel("button4");
button4.setBounds(insets().left + 36,insets().top + 156,134,24);
button4.setBackground(new Color(12632256));
add(button4);
textField1 = new java.awt.TextField();
textField1.setText("textField1");
textField1.setBounds(insets().left + 228,insets().top + 108,156,24);
add(textField1);
textField2 = new java.awt.TextField();
textField2.setText("textField2");
textField2.setBounds(insets().left + 228,insets().top + 156,157,24);
add(textField2);
setTitle("Sample Bot!");
//}}
this.setVisible(true);
RC = new RobotController();
RC.serialOpen();
//{{INIT_MENUS
//}}
//{{REGISTER_LISTENERS
SymWindow aSymWindow = new SymWindow();
this.addWindowListener(aSymWindow);
SymMouse aSymMouse = new SymMouse();
button2.addMouseListener(aSymMouse);
button1.addMouseListener(aSymMouse);
button3.addMouseListener(aSymMouse);
button4.addMouseListener(aSymMouse);
//}}
}
// this enables us to call "java Robot" from cmd line //
public static void main(String args[]) {
new Robot();
}
public Robot(String title)
{
this();
setTitle(title);
}
public synchronized void show()
{
move(50, 50);
super.show();
}
public void addNotify()
{
// Record the size of the window prior to calling parents addNotify.
Dimension d = getSize();
super.addNotify();
if (fComponentsAdjusted)
return;
// Adjust components according to the insets
setSize(insets().left + insets().right + d.width, insets().top + insets().bottom + d.height);
Component components[] = getComponents();
for (int i = 0; i < components.length; i++)
{
Point p = components[i].getLocation();
p.translate(insets().left, insets().top);
components[i].setLocation(p);
}
fComponentsAdjusted = true;
}
// Used for addNotify check.
boolean fComponentsAdjusted = false;
//{{DECLARE_CONTROLS
java.awt.Button button1;
java.awt.Button button2;
java.awt.Button button3;
java.awt.Button button4;
java.awt.TextField textField1;
java.awt.TextField textField2;
//}}
//{{DECLARE_MENUS
//}}
class SymWindow extends java.awt.event.WindowAdapter
{
public void windowClosing(java.awt.event.WindowEvent event)
{
Object object = event.getSource();
if (object == Robot.this)
Frame1_WindowClosing(event);
}
}
void Frame1_WindowClosing(java.awt.event.WindowEvent event)
{
hide(); // hide the Frame
}
class SymMouse extends java.awt.event.MouseAdapter
{
public void mouseClicked(java.awt.event.MouseEvent event)
{
Object object = event.getSource();
if (object == button2)
button2_MouseClick(event);
else if (object == button1)
button1_MouseClick(event);
else if (object == button3)
button3_MouseClick(event);
else if (object == button4)
button4_MouseClick(event);
}
}
// "Run Sonar Loop" button //
void button2_MouseClick(java.awt.event.MouseEvent event)
{
int frontsonar;
SonarConsole sonarshower;
sonarshower = new SonarConsole(); // make a new Sonar drawing window //
sonarshower.DrawSonars(RC);
RC.turnSonarsOn();
System.out.println("Hit robot bumper to quit...");
while (true) {
RC.GS(); // update the robot's state //
sonarshower.DrawSonars(RC); // draw the current sonar values! //
// print out the value of the front sonar
frontsonar = RC.stateArray[1];
System.out.println("Front sonar reads: " + String.valueOf(frontsonar));
// exit the whole program if the bumper's hit
if (RC.stateArray[RC.BUMPER] != 0) {
RC.turnSonarsOff();
System.exit(0);
}
// sleep for half a second
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("Interrupted sleep!");
}
} // end while (true)
}
void button1_MouseClick(java.awt.event.MouseEvent event)
{
// to do: code goes here.
int frontsonar, backsonar;
boolean flag = true;
// to do: code goes here.
RC.turnSonarsOn();
RC.GS();
System.out.println("Hit robot bumper to quit...");
while (flag) {
RC.GS();
// print out the value of the front sonar
frontsonar = RC.stateArray[1];
backsonar = RC.stateArray[9];
System.out.println("Front sonar: " + String.valueOf(frontsonar) +
" Back sonar: " + String.valueOf(backsonar));
// exit the whole program if the bumper's hit
if (RC.stateArray[RC.BUMPER] != 0) {
RC.turnSonarsOff();
RC.setVel(0,0);
flag = false;;
}
if (frontsonar < backsonar) {
RC.setVel(-20,-20);
} else {
RC.setVel(20,20);
}
} // end while (true)
}
void button3_MouseClick(java.awt.event.MouseEvent event)
{
// to do: code goes here.
}
void button4_MouseClick(java.awt.event.MouseEvent event)
{
// to do: code goes here.
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -