sensoranalyzer.java

来自「用于传感器网络的节点操作系统 TinyOS 结构设计非常有意思」· Java 代码 · 共 718 行 · 第 1/3 页

JAVA
718
字号
	          //*****---Edge Clicked---******//
              //------------------------------------------------------------------------
    
    
    
    
    
              //************************************************************************
              //************************************************************************
              //the following two functions correspond to the 
              //NodeDialogContributor, EdgeDialogContributor interfaces and will
              //only work if you register as a contributor as shown in the constructor.
              //You must define the ProprietaryNodeInfoPanel and ProprietaryEdgeInfoPanel
              //classes and they will automaticallyshow up when a node/edge is clicked
              //by using the following two functions.
              //************************************************************************
              //************************************************************************

    
              //------------------------------------------------------------------------
	          //GET PROPRIETARY NODE INFO PANEL
	          //This function returns the Panel that you define it to retunr
	          //which will then automatically appear ina dialog when a node is clicked.
	          //this function is called by DisplayManager
	public ActivePanel GetProprietaryNodeInfoPanel(Integer pNodeNumber)
	{
		NodeInfo nodeInfo = (NodeInfo)proprietaryNodeInfo.get(pNodeNumber);
		if(nodeInfo==null) 
			return null;
		ProprietaryNodeInfoPanel panel = new ProprietaryNodeInfoPanel(nodeInfo);
		return (ActivePanel)panel;
	}
	          //GET PROPRIETARY NODE INFO PANEL
              //------------------------------------------------------------------------


              //------------------------------------------------------------------------
	          //GET PROPRIETARY EDGE INFO PANEL
	          //This function returns the Panel that you define it to retunr
	          //which will then automatically appear ina dialog when an edge is clicked.
	          //this function is called by DisplayManager
/*	public ActivePanel GetProprietaryEdgeInfoPanel(Integer pSourceNodeNumber, Integer pDestinationNodeNumber) 
	{
		EdgeInfo edgeInfo = (EdgeInfo)proprietaryEdgeInfo.get(pSourceNodeNumber, pDestinationNodeNumber);
		if(edgeInfo==null)
			return null;
		ProprietaryEdgeInfoPanel panel = new ProprietaryEdgeInfoPanel(edgeInfo);
		return (ActivePanel)panel;
	}*/
	          //GET PROPRIETARY EDGE INFO PANEL
              //------------------------------------------------------------------------


    
    
    
    
              //************************************************************************
              //************************************************************************
              //the following three functions correspond to the 
              //NodePainter, EdgePainter, and ScreenPainter interfaces and will
              //only work if you register as a Painter as shown in the constructor.
              //Whatever  painting function you implement here will be called every
              //time a node or edge is painted, and after all the nodes/edges are painted
              //the paintScreen functions are called.  You are called in the order that
              //you register as a painter.
              //************************************************************************
              //************************************************************************

              
              //------------------------------------------------------------------------
	          //NODE PAINTER
	          //Put some function here to paint whatever you want over the node.
	          //The x1,y1 coordinates are the top left corner within which the node will be drawn
	          //The x2,y2 coordinates are the bottom right corner
	          //Paint everything on the graphics object
	          //this function is called by DisplayManager
	public void PaintNode(Integer pNodeNumber, int x1, int y1, int x2, int y2, Graphics g) 
	{
		NodeInfo nodeInfo = (NodeInfo)proprietaryNodeInfo.get(pNodeNumber);
		if(nodeInfo==null) return;
		int light = nodeInfo.GetValue();
		if(light == -1) return;
		nodeInfo.centerX = (x1 + x2)/2;
		nodeInfo.centerY = (y1 + y2)/2;
		//System.out.println(pNodeNumber);
	}
	          //NODE PAINTER
              //------------------------------------------------------------------------
              
              
              
              //------------------------------------------------------------------------
	          //SCREEN PAINTER
	          //Put some function here to paint whatever you want over the screen before and after
	          //all edges and nodes have been painted.
	public void PaintScreenBefore(Graphics g) 
	{

		Dimension d = MainClass.mainFrame.GetGraphDisplayPanel().getSize();
		NodeInfo nodeInfo;
		int x = 0;
		int y = 0;
		int step = 10;	

		for(;x < d.width; x += step){
			for(y = 0;y < d.height; y += step){
				double val = 0;
				double sum = 0;
				double total = 0;
				double min = 10000000;
				for(Enumeration nodes = proprietaryNodeInfo.elements();nodes.hasMoreElements();){
					nodeInfo = (NodeInfo) nodes.nextElement();
					double dist = distance(x, y, nodeInfo.centerX, nodeInfo.centerY);	
					if(nodeInfo.value != -1){ 
						if(dist < min) min = dist;
						val += ((double)nodeInfo.value)  / dist /dist;
						sum += (1/dist/dist);
					}
				}
				int reading = (int)(val / sum);
				reading = reading >> 2;
				if (reading > 255)
					reading = 255;
				g.setColor(new Color(reading, reading, reading));
				g.fillRect(x, y, step, step);
			}
		}
	}
	public double distance(int x, int y, int x1, int y1){
		return Math.sqrt( (x-x1)*(x-x1)+(y-y1)*(y-y1));
	}
	public void PaintScreenAfter(Graphics g) {
		      //paint something on the graphics object
	}
	          //SCREEN PAINTER
              //------------------------------------------------------------------------


              
              //************************************************************************
              //************************************************************************
              //the following functions correspond to the thread instantiated in the
              //constructor and will only work if you actually instantiate it.
              //The Run function is what the thread does.  The functions following
              //it are wrappers of the thread function to let us control the thread.
              //************************************************************************
              //************************************************************************
         
              
              //------------------------------------------------------------------------
	          //*****---Run---******//
              //this function runs in a seperate thread whenever you call the two lines: 
              //   thread = new Thread(this); 
              //   thread.start();
/*    public void run()
    {
		while(true)
    	{
    		//do something here which will run in the background
    		//then sleep for a while
    		try
			{
	    			thread.sleep(1000);//time is in milliseconds
	    	}
			catch(Exception e){e.printStackTrace();}
		}
		
    }*/
	          //*****---Run---******//
              //------------------------------------------------------------------------
    

              //------------------------------------------------------------------------
	          //*****---Thread commands---******//
	          //you might want to add these thread commands 
/*    public void start(){ try{ thread=new Thread(this);thread.start();} catch(Exception e){e.printStackTrace();}}
    public void stop(){ try{ thread.stop();} catch(Exception e){e.printStackTrace();}}
    public void sleep(long p){ try{ thread.sleep(p);} catch(Exception e){e.printStackTrace();}}
    public void setPriority(int p) { try{thread.setPriority(p);} catch(Exception e){e.printStackTrace();}}    */
			//*****---Thread commands---******//
              //------------------------------------------------------------------------

              //------------------------------------------------------------------------
              //INTERFACE TO PROPRIETARY DATA
              //write some functions here that will let other Analyzers find and user your data
	public NodeInfo GetNodeInfo(Integer nodeNumber){return (NodeInfo)proprietaryNodeInfo.get(nodeNumber);}
//	public EdgeInfo GetEdgeInfo(Integer sourceNumber, Integer destinationNumber){return (EdgeInfo)proprietaryEdgeInfo.get(sourceNumber,destinationNumber);}
	public Enumeration GetNodeInfo(){return proprietaryNodeInfo.elements();}
//	public Enumeration GetEdgeInfo(){return proprietaryEdgeInfo.elements();}
              //INTERFACE TO PROPRIETARY DATA
              //------------------------------------------------------------------------




              //------------------------------------------------------------------------
	          //*****---GET/SET COMMANDS---******//
//	public synchronized int GetVariableName(){ return variableName;}
//	public synchronized void SetVariableName(int pVariableName){variableName = pVariableName;}
			  //*****---GET/SET COMMANDS---******//
              //------------------------------------------------------------------------




              //------------------------------------------------------------------------
	          //*****---SHOW PROPERTIES DIALOG---******//
	          //this function can be called by MainFrame (by the menus, in particular)
	          //and should simply show the dialog as shown here.
	          //You need to define the class "PacketAnalyzerTemplatePropertiesPanel"
	          //in order for this to do anything.  it is useful for setting parameters
	          //on your analyzer.
	public void ShowPropertiesDialog() 
	{
		StandardDialog newDialog = new StandardDialog(new DisplayPropertiesPanel(this));
		newDialog.show();
	}
			  //*****---SHOW PROPERTIES DIALOG---******//
              //------------------------------------------------------------------------





	        //*********************************************************
	        //*********************************************************
	        //*********************************************************
              //NODE INFO CLASS
              //this class should hold any special information you hold about the
              //node, for example time created or a history of the number of packets
              //forwarded through this mote or whetever it is you are studying
	public class NodeInfo
	{
		protected Integer nodeNumber;
		protected int value;
		protected int centerY;
		protected int centerX;
		
		public NodeInfo(Integer pNodeNumber)

⌨️ 快捷键说明

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