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

📄 controlpanel.java

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

import java.awt.*;
import java.io.*;
import java.util.zip.*;

public class ControlPanel extends Frame
{
    MazeGraphics myMazePict;
    MazeWorld myMazeWorld;
	public ControlPanel()
	{
		// 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 + 549,insets().top + insets().bottom + 399);
		button1 = new java.awt.Button();
		button1.setActionCommand("button");
		button1.setLabel("Display a maze!");
		button1.setBounds(insets().left + 12,insets().top + 12,114,42);
		button1.setBackground(new Color(12632256));
		add(button1);
		setTitle("Maze Displayer control panel");
		//}}

		//{{INIT_MENUS
		//}}

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

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

	//{{DECLARE_MENUS
	//}}

	class SymWindow extends java.awt.event.WindowAdapter
	{
		public void windowClosing(java.awt.event.WindowEvent event)
		{
			Object object = event.getSource();
			if (object == ControlPanel.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);
		}
	}

	void button1_MouseClick(java.awt.event.MouseEvent event)
	{	 
	    /* read in the file and set it as an object instance of MazeWorld */
	    FileDialog f = new FileDialog(this, "Load Maze", FileDialog.LOAD);
	    f.show();
	    String filename = f.getFile();
	    String directory = f.getDirectory();
	    if (filename != null) {
	        try {
	            if (this.myMazePict != null) {
	                /* if a maze is drawn already, erase it physically! */
	                this.myMazePict.wipeMaze();
	            }
	            FileInputStream fis = new FileInputStream(directory+filename);
	            GZIPInputStream gzis = new GZIPInputStream(fis);
	            ObjectInputStream in = new ObjectInputStream(gzis);
	            this.myMazeWorld = (MazeWorld)in.readObject();
	            in.close();
	            
	            myMazePict = 
	            new MazeGraphics(this, myMazeWorld.width, myMazeWorld.height,
	                             20, 100);
	                                    
	            /* now add all the walls of this new maze */
	            for (int i=0; i< myMazeWorld.width; i++) {
	                for (int j=0; j < myMazeWorld.height; j++) {
	                    for (int k=0; k < 4; k++) {
	                        if (myMazeWorld.maze[i][j][k] == 1)
	                            myMazePict.addWall(i,j,k);
	                    }
	                }
	            }
	            
	            System.out.println("Maxdepth is: " +
	                            Integer.toString(myMazeWorld.maxDepth));
	            System.out.println("Maze wall to left is: " +
	                               Integer.toString(myMazeWorld.maze[0][0][1]));
	            System.out.println("Maze wall to top is: " +
	                               Integer.toString(myMazeWorld.maze[0][0][0]));
	            System.out.println("Maze wall to left is: " +
	                               Integer.toString(myMazeWorld.maze[0][1][1]));
	            System.out.println("Maze wall to bottom is: " +
	                               Integer.toString(myMazeWorld.maze[1][0][2]));
	                               
	                               
	            /* now add all the init robot positions to this maze */
	            int robotPos [];
	            if (myMazeWorld.inits != null) {
	                for (int i=0; i < myMazeWorld.inits.size(); i++) {
	                    robotPos = (int [])myMazeWorld.inits.elementAt(i);
	                    myMazePict.addRobot(robotPos[0],robotPos[1],robotPos[2],0);
	                }
	            }
	            
	            /* now add the goal positions in G just by putting gold */
	            if (myMazeWorld.goals != null) {
	                System.out.println("Total number of goals: " + 
	                                    myMazeWorld.goals.size());
	                for (int i=0; i < myMazeWorld.goals.size(); i++) {
	                    robotPos = (int [])myMazeWorld.goals.elementAt(i);
	                    // now, goals have orientation too and Gold does not. So,
	                    // to prevent duplication, where the maze picture has multiple
	                    // pieces of gold stacked on top of itself, we do a remove here
	                    // in case there is one already down
	                    myMazePict.removeGold(robotPos[0],robotPos[1]);
	                    myMazePict.addGold(robotPos[0],robotPos[1]);
	                }
	            }
	            
	            myMazePict.update();
	            
	        } catch (Exception e) { System.out.println(e); }
	    } /* end if */	            
	} /* end of the button_click function */
	
	public void paint(Graphics g)
	{
	    if (this.myMazePict != null)
	        myMazePict.update();	   
	} // paint()
	
	
	/* Here's an example of how you would save your own MazeWorld object! */
	/*
		FileDialog f = new FileDialog(this, "Save Maze",
		    FileDialog.SAVE);
		f.show();
		String filename = f.getFile();
		String directory = f.getDirectory();
		if (filename != null) {
		    try {
		        System.out.println("we are printing!");
		        FileOutputStream fos = new FileOutputStream(directory+filename);
		        GZIPOutputStream gzos = new GZIPOutputStream(fos);
		        ObjectOutputStream out = new ObjectOutputStream(gzos);
		        out.writeObject(world);
		        out.flush();
		        out.close();
		    } // endtry
		    catch (IOException e) { System.out.println(e); }
		} // endif
    */
	/* END OF EXAMPLE *********************************** */
	
}

⌨️ 快捷键说明

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