public String transform(Object v) {
if(isEnabled()) {
return map.get(v);
} else {
return "";
}
}
/**
* @return Returns the enabled.
*/
public boolean isEnabled() {
return enabled;
}
/**
* @param enabled The enabled to set.
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
/**
* create some vertices
* @param count how many to create
* @return the Vertices in an array
*/
private Number[] createVertices(int count) {
Number[] v = new Number[count];
for (int i = 0; i < count; i++) {
v[i] = new Integer(i);
graph.addVertex(v[i]);
}
return v;
}
/**
* create edges for this demo graph
* @param v an array of Vertices to connect
*/
void createEdges(Number[] v) {
graph.addEdge(new Double(Math.random()), v[0], v[1], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[3], v[0], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[0], v[4], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[4], v[5], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[5], v[3], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[2], v[1], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[4], v[1], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[8], v[2], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[3], v[8], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[6], v[7], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[7], v[5], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[0], v[9], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[9], v[8], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[7], v[6], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[6], v[5], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[4], v[2], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[5], v[4], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[4], v[10], EdgeType.DIRECTED);
graph.addEdge(new Double(Math.random()), v[10], v[4], EdgeType.DIRECTED);
}
public static class PickWithIconListener implements ItemListener {
DefaultVertexIconTransformer<Number> imager;
Icon checked;
public PickWithIconListener(DefaultVertexIconTransformer<Number> imager) {
this.imager = imager;
checked = new Checkmark(Color.red);
}
public void itemStateChanged(ItemEvent e) {
Icon icon = imager.transform((Number)e.getItem());
if(icon != null && icon instanceof LayeredIcon) {
if(e.getStateChange() == ItemEvent.SELECTED) {
((LayeredIcon)icon).add(checked);
} else {
((LayeredIcon)icon).remove(checked);
}
}
}
}
/**
* a simple Icon that draws a checkmark in the lower-right quadrant of its
* area. Used to draw a checkmark on Picked Vertices.
*/
// public static class Checkmark implements Icon {
//
// GeneralPath path = new GeneralPath();
// AffineTransform highlight = AffineTransform.getTranslateInstance(-1,-1);
// AffineTransform lowlight = AffineTransform.getTranslateInstance(1,1);
// AffineTransform shadow = AffineTransform.getTranslateInstance(2,2);
// Color color;
// public Checkmark() {
// this(Color.red);
// }
// public Checkmark(Color color) {
// this.color = color;
// path.moveTo(10,17);
// path.lineTo(13,20);
// path.lineTo(20,13);
// }
// public void paintIcon(Component c, Graphics g, int x, int y) {
// Shape shape = AffineTransform.getTranslateInstance(x, y).createTransformedShape(path);
// Graphics2D g2d = (Graphics2D)g;
// g2d.addRenderingHints(Collections.singletonMap(RenderingHints.KEY_ANTIALIASING,
// RenderingHints.VALUE_ANTIALIAS_ON));
// g2d.setStroke(new BasicStroke(4));
// g2d.setColor(Color.darkGray);
// g2d.draw(shadow.createTransformedShape(shape));
// g2d.setColor(Color.black);
// g2d.draw(lowlight.createTransformedShape(shape));
// g2d.setColor(Color.white);
// g2d.draw(highlight.createTransformedShape(shape));
// g2d.setColor(color);
// g2d.draw(shape);
// }
//
// public int getIconWidth() {
// return 20;
// }
//
// public int getIconHeight() {
// return 20;
// }
// }
// /**
// * An icon that is made up of a collection of Icons.
// * They are rendered in layers starting with the first
// * Icon added (from the constructor).
// *
// * @author Tom Nelson
// *
// */
// public static class LayeredIcon extends ImageIcon {
//
// /**
// *
// */
// private static final long serialVersionUID = -2975294939874762164L;
// Set<Icon> iconSet = new LinkedHashSet<Icon>();
//
// public LayeredIcon(Image image) {
// super(image);
// }
//
// public void paintIcon(Component c, Graphics g, int x, int y) {
// super.paintIcon(c, g, x, y);
// Dimension d = new Dimension(getIconWidth(), getIconHeight());
// for (Iterator iterator = iconSet.iterator(); iterator.hasNext();) {
// Icon icon = (Icon) iterator.next();
// Dimension id = new Dimension(icon.getIconWidth(), icon.getIconHeight());
// int dx = (d.width - id.width)/2;
// int dy = (d.height - id.height)/2;
// icon.paintIcon(c, g, x+dx, y+dy);
// }
// }
//
// public void add(Icon icon) {
// iconSet.add(icon);
// }
//
// public boolean remove(Icon icon) {
// return iconSet.remove(icon);
// }
// }
/**
* a driver for this demo
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
Container content = frame.getContentPane();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
content.add(new PerspectiveVertexImageShaperDemo());