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

📄 sonarconsole.java

📁 控制移到机器人的例子程序
💻 JAVA
字号:
/*
	A basic extension of the java.awt.Frame class
 */

import java.awt.*;

public class SonarConsole extends Frame
{
    
    /** Precomputed cosine values.  With the robot heading as the positive
        x-axis, let angle[i] be the angle of the ith sonar.  Then
        SONAR_COS[i] is cos(angle[i]).  You shouldn't recompute this
        in every loop!  Use these precalculated values instead.  */
    public final static float[] SONAR_COS = preCalculateCos(16);
    /** Precomputed sine values.  With the robot heading as the positive
        x-axis, let angle[i] be the angle of the ith sonar.  Then
        SONAR_SIN[i] is sin(angle[i]). */
    public final static float[] SONAR_SIN = preCalculateSin(16);
    
    int[] maxRangeX = new int[16];
    int[] maxRangeY = new int[16];

    /* Used to compute SONAR_COS and BUMPER_COS (see above). */
    final static float[] preCalculateCos(int n) {
        float[] f = new float[n];
        double sepRadians = 2 * Math.PI / n;
        for (int i=0; i < n; i++) {
            f[i] = (float) Math.cos(sepRadians * i);
        }
        return f;
    }

    final static float[] preCalculateSin(int n) {
        float[] f = new float[n];
        double sepRadians = 2 * Math.PI / n;
        for (int i=0; i < n; i++) {
            f[i] = (float) Math.sin(sepRadians * i);
        }
        return f;
    }

	public SonarConsole()
	{
		// 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 + 300,insets().top + insets().bottom + 300);
		setTitle("Sonar Viewer");
		//}}
		this.setVisible(true);
	    int centerX, centerY;
	    Dimension d = getSize();
	    centerY = d.height / 2;
	    centerX = d.width / 2;
        for (int i=0; i < 16; i++) {
            float cosAngle = -this.SONAR_SIN[i];
            float sinAngle = this.SONAR_COS[i];
            maxRangeX[i] = (int) (centerX + cosAngle * 140 + 0.5f);
            maxRangeY[i] = (int) (centerY - sinAngle * 140 + 0.5f);
        } // end for //
        
  		//{{INIT_MENUS
		//}}

		//{{REGISTER_LISTENERS
		SymWindow aSymWindow = new SymWindow();
		this.addWindowListener(aSymWindow);
		//}}
	}

	public SonarConsole(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
	//}}

	//{{DECLARE_MENUS
	//}}

	class SymWindow extends java.awt.event.WindowAdapter
	{
		public void windowClosing(java.awt.event.WindowEvent event)
		{
			Object object = event.getSource();
			if (object == SonarConsole.this)
				Frame1_WindowClosing(event);
		}
	}
	
	void Frame1_WindowClosing(java.awt.event.WindowEvent event)
	{
		hide();		 // hide the Frame
	}
	
	// using the stateArray in RobotController RC, draw the
	// sonars on the sonar console window!
	public void DrawSonars(RobotController RC) {
	    Dimension d = getSize();
	    Graphics g = this.getGraphics();
	    int centerX, centerY;
	    int data;
	    centerY = d.height / 2;
	    centerX = d.width / 2;
	    g.clearRect(0,0,d.width, d.height);
	    int endX, endY;
	    for (int i=0; i < 16; i++) {
	        float cosAngle = -(SonarConsole.SONAR_SIN[i]);
	        float sinAngle = SonarConsole.SONAR_COS[i];
	        data = RC.stateArray[i+1];
	        if (data > 140) data = 140;
	        
	        String s = String.valueOf(data);
            // try to take into account size of text
            g.setColor(Color.black);
            g.drawString(s,maxRangeX[i]-10,maxRangeY[i]+10);
	        
	        if (data != 0) {
	            g.setColor(Color.black);
	            endX = (int) (centerX + 0.5f + (data * cosAngle));
	            endY = (int) (centerY + 0.5f - (data * sinAngle));
	            g.drawLine(centerX,centerY,endX,endY);
	        } // endif (data != 0)
	    } // end for loop
	} // end DrawSonars() //
}

⌨️ 快捷键说明

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