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

📄 frame1.java

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

import java.awt.*;

public class Frame1 extends Frame
{
    MazeGraphics myMaze;
    
	public Frame1()
	{
		// 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 + 556,insets().top + insets().bottom + 274);
		button1 = new java.awt.Button();
		button1.setActionCommand("button");
		button1.setLabel("Do this first!");
		button1.setBounds(insets().left + 450,insets().top + 14,91,23);
		button1.setBackground(new Color(12632256));
		add(button1);
		button2 = new java.awt.Button();
		button2.setActionCommand("button");
		button2.setLabel("Click for continuous motion");
		button2.setBounds(insets().left + 360,insets().top + 84,180,24);
		button2.setBackground(new Color(12632256));
		add(button2);
		button3 = new java.awt.Button();
		button3.setActionCommand("button");
		button3.setLabel("Click for discrete motion");
		button3.setBounds(insets().left + 360,insets().top + 48,182,24);
		button3.setBackground(new Color(12632256));
		add(button3);
		setTitle("Untitled");
		//}}

		//{{INIT_MENUS
		//}}

		//{{REGISTER_LISTENERS
		SymWindow aSymWindow = new SymWindow();
		this.addWindowListener(aSymWindow);
		SymMouse aSymMouse = new SymMouse();
		button1.addMouseListener(aSymMouse);
		button2.addMouseListener(aSymMouse);
		button3.addMouseListener(aSymMouse);
		SymAction lSymAction = new SymAction();
		button1.addActionListener(lSymAction);
		//}}
		setVisible(true);
	}

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

	//{{DECLARE_MENUS
	//}}

	class SymWindow extends java.awt.event.WindowAdapter
	{
		public void windowClosing(java.awt.event.WindowEvent event)
		{
			Object object = event.getSource();
			if (object == Frame1.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 == button1)
				button1_MouseClick(event);
			else if (object == button2)
				button2_MouseClick(event);
			else if (object == button3)
				button3_MouseClick(event);
		}
	}

	void button1_MouseClick(java.awt.event.MouseEvent event)
	{
		// to do: code goes here.
		myMaze = new MazeGraphics(this, 6, 6, 5, 30);
		myMaze.addWall(3,3,0);
		myMaze.addWall(3,3,3);
		myMaze.addGold(3,3,2,3);
		myMaze.addGold(0,1,0,2);
		myMaze.addGold(1,1);
		myMaze.addRobot(2,2,0,2);
		myMaze.update(); // redraw everything (just for fun) //
	}

	void button2_MouseClick(java.awt.event.MouseEvent event)
	{
        double x = 2;
        double y = 2;
        double dir = 0;
        double ds = 0.1;

        for(int i = 0; i < 20; i++) {
            myMaze.removeSmoothRobot(x,y,dir,2);
            x += ds * Math.cos(dir);
            y += ds * Math.sin(dir);
            if (x > 5.0) x = 5.0;
            if (x < -0.25) x = -0.25;
            if (y > 5.0) y = 5.0;
            if (y < -0.25) y = -0.25;
            if(Math.random() < 0.3) {
                dir += Math.random();
            }
            myMaze.addSmoothRobot(x,y,dir,2);
            sleep(100);
        }
	}
	
	void sleep(int time) {
	    try {
	        Thread.sleep(time);
	    } catch (InterruptedException e) {
	        
	    }
	}
	
	public void paint(Graphics g)
	{
	    if (this.myMaze != null)
	        myMaze.update();	   
	} // paint()

// This button will demonstrate just moving discretely from cell center
// to cell center.
	void button3_MouseClick(java.awt.event.MouseEvent event)
	{	    
	    myMaze.removeRobot(2,2,0,2);
	    myMaze.addRobot(3,2,3,2);
	    try {
	        Thread.sleep(250);
	    } catch (InterruptedException e) {  }
	    myMaze.removeRobot(3,2,3,2);
	    myMaze.addRobot(3,2,0,2);
	    try {
	        Thread.sleep(250);
	    } catch (InterruptedException e) {  }
	    myMaze.removeRobot(3,2,0,2);
	    myMaze.addRobot(3,3,0,2);
	    try {
	        Thread.sleep(250);
	    } catch (InterruptedException e) {  }
	    myMaze.removeRobot(3,3,0,2);
	    myMaze.addRobot(3,4,0,2);
	    try {
	        Thread.sleep(250);
	    } catch (InterruptedException e) {  }
	   
	}

	class SymAction implements java.awt.event.ActionListener
	{
		public void actionPerformed(java.awt.event.ActionEvent event)
		{
			Object object = event.getSource();
			if (object == button1)
				button1_Action(event);
		}
	}

	void button1_Action(java.awt.event.ActionEvent event)
	{
		// to do: code goes here.
	}
}

⌨️ 快捷键说明

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