📄 myvertexrenderer.java
字号:
package gui;
import org.jgraph.graph.VertexRenderer;
import org.jgraph.graph.GraphCell;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.GraphConstants;
import java.util.Iterator;
import java.util.LinkedList;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import graph.Properties;
import graph.Gradient;
import graph.Pheromone;
import graph.Agent;
import graph.Resource;
import graph.GraphEventManager;
public class MyVertexRenderer extends VertexRenderer
{
public void paint(Graphics g)
{
//super.paint(g);
paintTest(g);
}
protected void paintTest(Graphics g)
{
int i = 3;
int width=10;
String whatToShow = GUIEventManager.getReference().eventWhatInNode();
//g.setColor(Color.red);
int a = 0;
if (whatToShow.equals("Gradient"))
{
Iterator gradientIterator = ((Properties)((GraphCell)this.view.getCell()).getAttributes().get("value")).getInformation("Gradient");
int aantal = 0;
if (GraphEventManager.getReference().getProperties((DefaultGraphCell)this.view.getCell()).getListInformation("Gradient") != null)
{
aantal = GraphEventManager.getReference().getProperties((DefaultGraphCell)this.view.getCell()).getListInformation("Gradient").size();
}
if (aantal < 10)
{
i = (int) ((98 - (aantal * 10) ) / 2);
}
else
{
width = (int) (97 / aantal);
}
Gradient tempGradient;
if (gradientIterator != null)
{
while(gradientIterator.hasNext())
{
tempGradient = ((Gradient)gradientIterator.next());
a = tempGradient.getDistance();
g.setColor(tempGradient.getColor());
((Graphics2D) g).fillRect(i,18-a,width,a);
i=i+width;
}
}
}
else if (whatToShow.equals("Pheromones"))
{
Pheromone tempPheromone;
Iterator pheromoneIterator = GraphEventManager.getReference().getProperties((DefaultGraphCell)this.view.getCell()).getInformation("Pheromones");
int aantal = 0;
if (GraphEventManager.getReference().getProperties((DefaultGraphCell)this.view.getCell()).getListInformation("Pheromones") != null)
{
aantal = GraphEventManager.getReference().getProperties((DefaultGraphCell)this.view.getCell()).getListInformation("Pheromones").size();
}
if (aantal < 10)
{
i = (int) ((98 - (aantal * 10) ) / 2);
}
else
{
width = (int) (97 / aantal);
}
if (pheromoneIterator != null)
{
//System.out.println("MyVertexRenderer :: paint : pheromones");
while(pheromoneIterator.hasNext())
{
tempPheromone = ((Pheromone)pheromoneIterator.next());
a = tempPheromone.getStrength()*3;
g.setColor(Color.magenta);
((Graphics2D) g).fillRect(i,18-a,width,a);
i=i+width;
}
}
}
Agent tempAgent;
g.setColor(Color.orange);
Iterator antIterator = ((Properties)GraphConstants.getValue(((GraphCell)this.view.getCell()).getAttributes())).getInformation("Agents");
a = 0;
i = 20;
if (antIterator != null)
{
while(antIterator.hasNext())
{
tempAgent = ((Agent)antIterator.next());
//System.out.println("Draw Ant");
//a = tempAnt.getDistance();
//g.setColor(tempAnt.getColor());
((Graphics2D) g).fillOval(i,35,10,10);
i=i+11;
}
}
String resource = GUIEventManager.getReference().eventGetSelectedResources();
LinkedList resourceList = GraphEventManager.getReference().getProperties((DefaultGraphCell)this.view.getCell()).getListInformation("Resources");
if (resource.equals("All"))
{
g.setColor(Color.yellow);
((Graphics2D) g).fillRect(82, 32, 17, 17);
g.setColor(Color.darkGray);
String tempstring = "";
if (resourceList != null)
{
tempstring = "" + resourceList.size();
int myw = (g.getFontMetrics()).stringWidth(tempstring);
((Graphics2D) g).drawString(tempstring, Math.max(90 - myw / 2, 82), 45);
}
}
else if (resourceList != null)
{
boolean found = false;
Iterator resourceIterator = resourceList.iterator();
if (resourceIterator != null)
{
while (! found && resourceIterator.hasNext())
{
if (((Resource)resourceIterator.next()).getName().equals(resource))
{
found = true;
}
}
if (found)
{
g.setColor(Color.yellow);
((Graphics2D) g).fillRect(82, 32, 17, 17);
}
}
}
g.setColor(Color.gray);
/* OUD, met twee afzonderlijke balken
// de boven en onderbalk
((Graphics2D) g).drawRect(0, 0, 99, 18);
((Graphics2D) g).drawRect(0, 31, 99, 18);
// de middenbalk
//((Graphics2D) g).fillRect(0, 19, 100, 13);
((Graphics2D) g).fillRect(0, 18, 99, 14);
*/
((Graphics2D) g).drawRect(1, 1, 98, 48);
((Graphics2D) g).fillRect(1, 18, 98, 14);
/*
g.setColor(Color.red);
((Graphics2D) g).fillRect(4, 22, 6, 6);
((Graphics2D) g).fillRect(90, 22, 6, 6);
*/
if (((GraphCell)this.view.getCell()).getAttributes().containsKey("value"))
{
g.setColor(Color.yellow.brighter());
String tempstring = (((GraphCell)this.view.getCell()).getAttributes().get("value")).toString();
int myw = (g.getFontMetrics()).stringWidth(tempstring);
((Graphics2D) g).drawString(tempstring, Math.max(50 - myw / 2, 1), 29);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -