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

📄 mazeeditor.java

📁 控制移到机器人的例子程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	    int mouse_x = event.getX()+5; int mouse_y=event.getY()+5;
	    int dx = mouse_x - this.maze_x;
	    int dy = mouse_y- this.maze_y;
	    
	    if (this.nearCenter(dx) && this.nearCenter(dy) &&
	        this.inXBounds(dx) && this.inYBounds(dy)) {
	        //in this case, gold goes in a single cell//
	        int cx=this.getXCell(dx); int cy=this.getYCell(dy);
	        if (this.maze.removeGold(cx,cy))
	            return;
	            else this.maze.addGold(cx,cy);	    
	    }  // endif
	    else if (this.nearEdge(dx) && this.inXBounds(dx) &&
	             this.inYBounds(dy)) {
	        //put gold on a vertical wall//
	        /* TEMPORARILY DISABLED 
	        int cx=this.getXCell(dx); int cy=this.getYCell(dy);
	        if (this.maze.removeGold(cx,cy,cx-1,cy))
	            return;
	            else this.maze.addGold(cx,cy,cx-1,cy);
	         END TEMPORARILY DISABLED */
	    } // end  else if (this.nearEdge(dx... //
	    else if (this.nearEdge(dy) && this.inXBounds(dx) &&
	             this.inYBounds(dy)) {
	        // put gold on a horizontal wall //
	        /* TEMPORARILY DISABLED 
	        int cx=this.getXCell(dx); int cy=this.getYCell(dy);
	        if (this.maze.removeGold(cx,cy,cx,cy+1))
	            return;
	            else this.maze.addGold(cx,cy,cx,cy+1);
	         END TEMPORARILY DISABLED */
	    } // end else if (this.nearEdge(dy... //
	} // goldClick() //
	public void robotClick(java.awt.event.MouseEvent event)
	{
	    int mouse_x = event.getX();
	    int mouse_y = event.getY();
	    int dx = mouse_x - this.maze_x;
	    int dy = mouse_y - this.maze_y;
	    
	    // if the click's near the center of a cell...
	    if (this.nearCenter(dx) &&
	        this.nearCenter(dy)  &&
            this.inXBounds(dx) &&
            this.inYBounds(dy)
           ) {
                int cx = this.getXCell(dx);
                int cy = this.getYCell(dy);
                if (roboDirection == 4) {
                    if (this.maze.removeRobot(cx,cy,0,0)) {
                        for (int i=0; i < 4; i++) this.maze.removeRobot(cx,cy,i,0);
                    } else {
                        for (int i=0; i < 4; i++) this.maze.addRobot(cx,cy,i,0);
                    }
                } else {
                    if (this.maze.removeRobot(cx,cy,roboDirection,0)) {
                        return;
                    } else {
                        this.maze.addRobot(cx,cy,roboDirection,0);
                    }
                }
                /*
                for (int i=0; i<3; i++) {
                    if (this.maze.removeRobot(cx,cy,i,0)) {
                        this.maze.addRobot(cx,cy,i+1,0);
                        return;
                    } //endif
                } //end for (int i.. //
                if (this.maze.removeRobot(cx,cy,3,0))
                    return;
                this.maze.addRobot(cx,cy,0,0);
                */
           } //endif   
	} // robotClick() //
	public void wallClick(java.awt.event.MouseEvent event)
	{
        int mouse_x = event.getX() + 5;
        int mouse_y = event.getY() + 5;
        int dx = mouse_x - this.maze_x;
        int dy = mouse_y - this.maze_y;
        int cell_x, cell_y;
        
        if (this.nearEdge(dx) && this.inXBounds(dx) &&
            this.inYBounds(dy)) {
                // we've clicked for a vertical wall!
                int cx = this.getXCell(dx);
                int cy = this.getYCell(dy);
                if (maze.walls[cx][cy][1] == 0) {
                    // System.out.println("adding wall");
                    this.maze.addWall(cx,cy,1);
                }
                    else {
                        // System.out.println("removing wall");
                        this.maze.removeWall(cx,cy,1);
                    }
        } else if (this.nearEdge(dy) &&
            this.inXBounds(dx) && this.inYBounds(dy)) {
                // we've clicked for a horizontal wall!
                int cx = this.getXCell(dx);
                int cy = this.getYCell(dy);
                if (maze.walls[cx][cy][0] == 0)                  
                    this.maze.addWall(cx,cy,0);
                    else this.maze.removeWall(cx,cy,0);
        } //endelseif
	} // wallClick() //

	void buttonWalls_MouseClick(java.awt.event.MouseEvent event)
	{
		// to do: code goes here.
		this.edit_state= MazeEditor.WALLS;
		button_walls.setBackground(Color.yellow);
		button_robotN.setBackground(new Color(12632256));
		button_robotW.setBackground(new Color(12632256));
		button_robotS.setBackground(new Color(12632256));
		button_robotE.setBackground(new Color(12632256));				
		button_golds.setBackground(new Color(12632256));
		button_robotAll.setBackground(new Color(12632256));
	} // buttonWalls_MouseClick() //

	void buttonGolds_MouseClick(java.awt.event.MouseEvent event)
	{
		this.edit_state= MazeEditor.GOLDS;
		button_golds.setBackground(Color.yellow);
		button_robotN.setBackground(new Color(12632256));
		button_robotW.setBackground(new Color(12632256));
		button_robotS.setBackground(new Color(12632256));
		button_robotE.setBackground(new Color(12632256));				
		button_walls.setBackground(new Color(12632256));
		button_robotAll.setBackground(new Color(12632256));
	} //buttonGolds_MouseClick() //

	void buttonRobotN_MouseClick(java.awt.event.MouseEvent event)
	{
		this.edit_state= MazeEditor.ROBOTS;
		this.roboDirection = 0;
		button_robotN.setBackground(Color.yellow);
		button_robotW.setBackground(new Color(12632256));
		button_robotS.setBackground(new Color(12632256));
		button_robotE.setBackground(new Color(12632256));		
		button_walls.setBackground(new Color(12632256));
		button_golds.setBackground(new Color(12632256));
		button_robotAll.setBackground(new Color(12632256));

	} //buttonRobots_MouseClick() //
	
	void buttonRobotW_MouseClick(java.awt.event.MouseEvent event)
	{
		this.edit_state= MazeEditor.ROBOTS;
		this.roboDirection = 1;
		button_robotW.setBackground(Color.yellow);
		button_robotN.setBackground(new Color(12632256));
		button_robotS.setBackground(new Color(12632256));
		button_robotE.setBackground(new Color(12632256));		
		button_walls.setBackground(new Color(12632256));
		button_golds.setBackground(new Color(12632256));
		button_robotAll.setBackground(new Color(12632256));
	}

	void buttonRobotS_MouseClick(java.awt.event.MouseEvent event)
	{
		this.edit_state= MazeEditor.ROBOTS;
		this.roboDirection = 2;
		button_robotS.setBackground(Color.yellow);
		button_robotW.setBackground(new Color(12632256));
		button_robotN.setBackground(new Color(12632256));
		button_robotE.setBackground(new Color(12632256));		
		button_walls.setBackground(new Color(12632256));
		button_golds.setBackground(new Color(12632256));
		button_robotAll.setBackground(new Color(12632256));
	}

	void buttonRobotE_MouseClick(java.awt.event.MouseEvent event)
	{
		this.edit_state= MazeEditor.ROBOTS;
		this.roboDirection = 3;
		button_robotN.setBackground(new Color(12632256));
		button_robotW.setBackground(new Color(12632256));
		button_robotS.setBackground(new Color(12632256));
		button_robotE.setBackground(Color.yellow);		
		button_walls.setBackground(new Color(12632256));
		button_golds.setBackground(new Color(12632256));
		button_robotAll.setBackground(new Color(12632256));

	}
	

	void buttonSave_MouseClick(java.awt.event.MouseEvent event)
	{
		// serialize the maze into a file!
		/* create a MazeWorld */
		myMazeWorld = new MazeWorld(this.maze.walls);
		myMazeWorld.height = this.maze.height;
		myMazeWorld.width = this.maze.width;
		myMazeWorld.maxDepth = Integer.parseInt(maxDepthField.getText());
		myMazeWorld.inits = maze.robots;
		myMazeWorld.goals = new Vector();
		for (int i=0; i < maze.golds.size(); i++) {
		    int goldspec [] = (int [])maze.golds.elementAt(i);
		    if (goldspec.length == 2) {
		        for (int j=0; j < 4; j++) {
		            int goalspec [] = new int[3];
		            goalspec[0] = goldspec[0];
		            goalspec[1] = goldspec[1];
		            goalspec[2] = j;
		            myMazeWorld.goals.addElement(goalspec);
		        }
		    }
		}		
		/* now save this MazeWorld we just created */
		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(this.myMazeWorld);
		        out.flush();
		        out.close();
		    } // endtry
		    catch (IOException e) { System.out.println(e); }
		} // endif
	} // buttonSave_MouseClick() //

	void buttonLoad_MouseClick(java.awt.event.MouseEvent event)
	{
	    // read in the file and then set the parent_frame in this object
	    // accordingly. then refresh.
	    FileDialog f = new FileDialog(this, "Load Maze", FileDialog.LOAD);
	    f.show();
	    String filename = f.getFile();
	    String directory = f.getDirectory();
	    if (filename != null) {
	        try {
	            this.maze.wipeMaze(); 
	            this.maze = new MazeGraphics(this, 2, 2, this.maze_x, this.maze_y);
	            
	            FileInputStream fis = new FileInputStream(directory+filename);
	            GZIPInputStream gzis = new GZIPInputStream(fis);
	            ObjectInputStream in = new ObjectInputStream(gzis);
	            this.myMazeWorld = (MazeWorld)in.readObject();
	            in.close();
	            this.maze.parent_frame = this;
                this.moveButtons();
	            this.maze.refresh();
	            
                maze = 
	            new MazeGraphics(this, myMazeWorld.width, myMazeWorld.height,
	                             this.maze_x,this.maze_y);
	            
	            /* set the depth field... */
	            maxDepthField.setText(Integer.toString(myMazeWorld.maxDepth));
	                                    
	            /* 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)
	                            maze.addWall(i,j,k);
	                    }
	                }
	            }
	            /* 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);
	                    maze.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("Number of goals read in is: " +
	                                    myMazeWorld.goals.size());
	                for (int i=0; i < myMazeWorld.goals.size(); i++) {
	                    robotPos = (int [])myMazeWorld.goals.elementAt(i);
	                    
	                    // new line below to ensure we don't have duplication and replication! 
	                    // Illah 10/10/1999
	                    maze.removeGold(robotPos[0],robotPos[1]);
	                    
	                    maze.addGold(robotPos[0],robotPos[1]);
	                }
	            }		
	            
                this.moveButtons();
                this.maze.refresh();
	            
	            
	        } catch (Exception e) {this.maze.refresh();
	                               System.out.println(e); }
	    } //endif
   	}// buttonLoad_MouseClick() //


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

	void buttonRobotAll_Action(java.awt.event.ActionEvent event)
	{
	}

	void buttonRobotAll_MouseClick(java.awt.event.MouseEvent event)
	{
		this.edit_state= MazeEditor.ROBOTS;
		this.roboDirection = 4;
		button_robotN.setBackground(new Color(12632256));
		button_robotW.setBackground(new Color(12632256));
		button_robotS.setBackground(new Color(12632256));
		button_robotE.setBackground(new Color(12632256));
		button_walls.setBackground(new Color(12632256));
		button_golds.setBackground(new Color(12632256));
	    button_robotAll.setBackground(Color.yellow);
	}
}

⌨️ 快捷键说明

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