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

📄 myedgerenderer.java

📁 P2P模拟器P2Psim的程序源码
💻 JAVA
字号:
/*
 * Created on 21-dec-2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package gui;

import org.jgraph.graph.EdgeRenderer;
import org.jgraph.graph.GraphCell;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.EdgeView;
import org.jgraph.graph.GraphConstants;

import java.awt.Color;
import java.util.Iterator;
import java.awt.BasicStroke;
import java.awt.Shape;
import java.awt.Graphics;
import java.awt.Graphics2D;
//import java.awt.geom.Line2D.Double;
//import java.awt.geom.*;
import java.awt.Point;

import graph.EdgeProperties;
import graph.Agent;
import graph.GraphEventManager;
import graph.Pheromone;

public class MyEdgeRenderer extends EdgeRenderer
{
/*
	public void paint(Graphics g) 
	{
		//super.paint(g);
		paintTest(g);
	}
*/

public void paint(Graphics g) 
{	
	String resource = GUIEventManager.getReference().eventGetSelectedEdges();
					
	Shape edgeShape = view.getShape();
	
	if (edgeShape != null) 
	{
		Graphics2D g2 = (Graphics2D) g;
		int c = BasicStroke.CAP_BUTT;
		int j = BasicStroke.JOIN_MITER;
		
		int capacity = GraphEventManager.getReference().getEdgeProperties((DefaultEdge)view.getCell()).getCapacity();
		
		lineWidth = (float)(capacity/1.7);
		
		g2.setStroke(new BasicStroke(lineWidth, c, j));
		translateGraphics(g);
		
		
		g.setColor(getForeground());
		
		/*
		if (lineDash != null)
		{ // Dash For Line Only
			g2.setStroke(new BasicStroke(lineWidth, c, j, 10.0f, lineDash, 0.0f));
		}*/
		if (view.lineShape != null)
		{
			if (resource == null || resource.equals("All"))
			{
				g2.setColor(Color.gray);
			}
			else if (! takesPartOfPathToResource(resource))
			{
				g2.setColor(Color.lightGray);
			}
			else
			{
				g2.setColor(Color.magenta);
			}	
			g2.draw(view.lineShape); 
		} 
			
		if (selected) { // Paint Selected
			g2.setStroke(GraphConstants.SELECTION_STROKE);
			g2.setColor(graph.getHighlightColor());
		
			if (view.beginShape != null)
				g2.draw(view.beginShape);
			if (view.lineShape != null)
				g2.draw(view.lineShape);
			if (view.endShape != null)
				g2.draw(view.endShape);
		}
		
		
		if (view instanceof EdgeView) 
		{
			EdgeView edge = (EdgeView) view;
			Point first = null, second = null;
			
			if (edge.getPointCount() == 2)
			{
				first = edge.getPoint(0);
				second	= edge.getPoint(1);
			
				drawAgents(g2, first, second);
			}
		}
	}
					
	/*
	if (graph.getEditingCell() != view.getCell()) {
		Object label = graph.convertValueToString(view);
		if (label != null) {
			g2.setStroke(new BasicStroke(1));
			g.setFont(getFont());
			//paintLabel(g, label.toString());
		}
	}
	*/
		
}		

public boolean takesPartOfPathToResource(String resource)
{
	DefaultEdge edge = (DefaultEdge) (this.view.getCell());
	
	DefaultGraphCell p1 = (DefaultGraphCell)((DefaultGraphCell) ( ((DefaultEdge)this.view.getCell()).getSource())).getParent();
	DefaultGraphCell p2 = (DefaultGraphCell)((DefaultGraphCell) ( ((DefaultEdge)this.view.getCell()).getTarget())).getParent();
	
	Iterator pheromoneIterator = GraphEventManager.getReference().getProperties(p1).getInformation("Pheromones");
	if (pheromoneIterator != null)
	{
		while (pheromoneIterator.hasNext())
		{
			Pheromone tempPheromone = (Pheromone)pheromoneIterator.next();
			
			if (tempPheromone.getResource().equals(resource))
			{
				if (tempPheromone.getDirection() == p2)
				{
					return true;
				}
			}
		}
	}
	
	pheromoneIterator = GraphEventManager.getReference().getProperties(p2).getInformation("Pheromones");
	if (pheromoneIterator != null)
	{
		while (pheromoneIterator.hasNext())
		{
			Pheromone tempPheromone = (Pheromone)pheromoneIterator.next();
		
			if (tempPheromone.getResource().equals(resource))
			{
				if (tempPheromone.getDirection() == p1)
				{
					return true;
				}
			}
		}
	}
	
	return false;
}

public void drawAgents(Graphics2D g, Point first, Point second)
{
	g.setColor(Color.orange);
	
	Agent tempAgent;
	Iterator antIterator = ((EdgeProperties)GraphConstants.getValue(((GraphCell)this.view.getCell()).getAttributes())).getInformation("Agents");
	
	DefaultEdge edge = (DefaultEdge) (this.view.getCell());
			
	if (antIterator != null)
	{
		double XDistance, YDistance;
		
		while(antIterator.hasNext())
		{
			tempAgent = ((Agent)antIterator.next());
			
			Point tempFirst = first;
			Point tempSecond = second;
			
			
			if ( ((DefaultGraphCell)(edge.getSource())).getParent() == tempAgent.getDestination())
			{
				// omgekeerde boog
				tempFirst = second;
				tempSecond = first;
			}
			
			XDistance = tempSecond.getX() - tempFirst.getX();
			YDistance = tempSecond.getY() - tempFirst.getY();
			
			int capacity = ((EdgeProperties)(GraphConstants.getValue(edge.getAttributes()))).getCapacity();
			
			XDistance = XDistance * tempAgent.getProgress() /*/ ((14 - capacity)/2)*/;
			YDistance = YDistance * tempAgent.getProgress() /*/ ((14 - capacity)/2)*/;
			
			g.drawLine((int)tempFirst.getX(), (int)tempFirst.getY(), (int) (tempFirst.getX() + XDistance), (int) (tempFirst.getY() + YDistance));
			
			((Graphics2D) g).fillOval((int) (tempFirst.getX() + XDistance - 5),(int) (tempFirst.getY() + YDistance - 5), 10, 10);	
		}		
	}		
}
		


}

⌨️ 快捷键说明

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