⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 robot.java

📁 控制移到机器人的例子程序
💻 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);
		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);
		//}}
	}

	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;
	//}}

	//{{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);
		}
	}

    // "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)

	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -