轨道
package APIs;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import org.jgraph.graph.CellViewRenderer;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.EdgeView;
import org.jgraph.graph.VertexRenderer;
import org.jgraph.graph.VertexView;
//轨道的可视化
//Define TrackCell
public class TrackCell extends DefaultGraphCell{
/**
*
*/
private static final long serialVersionUID = 1L;
// Empty Constructor
public TrackCell() {
this(null);
}
// Construct Cell for Userobject
public TrackCell(Object userObject) {
super(userObject);
}
}
class TrackView extends VertexView {
/**
*
*/
private static final long serialVersionUID = 1L;
static TrackRenderer renderer = new TrackRenderer();
public TrackView(Object cell) {
super(cell);
}
public Point2D getPerimeterPoint(EdgeView edge, Point2D source, Point2D p){
if(getRenderer() instanceof TrackRenderer)
return ((TrackRenderer) getRenderer()).getPerimeterPoint(this,source, p);
return super.getPerimeterPoint(edge, source, p);
}
public CellViewRenderer getRenderer(){
return renderer;
}
static class TrackRenderer extends VertexRenderer {
/**
*
*/
private static final long serialVersionUID = 1L;
public void paint(Graphics g){
try{
if(gradientColor != null && !preview && isOpaque()) {
setOpaque(false);
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(new GradientPaint(0, 0, getBackground(), getWidth(), getHeight(), gradientColor, true));
g2d.drawOval(0, 0, getWidth(), getHeight());
}
super.paint(g);
paintSelectionBorder(g);
} catch (IllegalArgumentException e){
}
}
}
}
class MyView extends DefaultCellViewFactory {
/**
*
*/
private static final long serialVersionUID = 1L;
protected VertexView createVertexView(Object v){
if(v instanceof TrackCell) {
return new TrackView(v);
}
else if (v instanceof PhysicalObjectCell) {
return new PhysicalObjectView(v);
}
return super.createVertexView(v);
}
}
轨道物体
package APIs;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import org.jgraph.graph.CellViewRenderer;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.EdgeView;
import org.jgraph.graph.VertexRenderer;
import org.jgraph.graph.VertexView;
import APIs.TrackView.TrackRenderer;
//这是我添加的类,是JGraph的Cell,用来表示物体
public class PhysicalObjectCell extends DefaultGraphCell{
/**
*
*/
private static final long serialVersionUID = 1L;
public PhysicalObjectCell() {
this(null);
}
public PhysicalObjectCell(Object userObject) {
super(userObject);
}
}
class PhysicalObjectView extends VertexView {
/**
*
*/
private static final long serialVersionUID = 1L;
static PhysicalObjectRenderer renderer = new PhysicalObjectRenderer();
public PhysicalObjectView(Object cell) {
super(cell);
}
public Point2D getPerimeterPoint(EdgeView edge, Point2D source, Point2D p){
if(getRenderer() instanceof TrackRenderer)
return ((TrackRenderer) getRenderer()).getPerimeterPoint(this,source, p);
return super.getPerimeterPoint(edge, source, p);
}
public CellViewRenderer getRenderer(){
return renderer;
}
static class PhysicalObjectRenderer extends VertexRenderer {
/**
*
*/
private static final long serialVersionUID = 1L;
public void paint(Graphics g){
try{
if(gradientColor != null && !preview && isOpaque()) {
setOpaque(false);
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(new GradientPaint(0, 0, getBackground(), getWidth(), getHeight(), gradientColor, true));
g2d.drawOval(0, 0, getWidth(), getHeight());
g2d.fillOval(0, 0, getWidth(), getHeight());
}
super.paint(g);
paintSelectionBorder(g);
} catch (IllegalArgumentException e){
}
}
}
}
执行代码
package APIs;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import org.jgraph.graph.CellViewRenderer;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.EdgeView;
import org.jgraph.graph.VertexRenderer;
import org.jgraph.graph.VertexView;
import APIs.TrackView.TrackRenderer;
//这是我添加的类,是JGraph的Cell,用来表示物体
public class PhysicalObjectCell extends DefaultGraphCell{
/**
*
*/
private static final long serialVersionUID = 1L;
public PhysicalObjectCell() {
this(null);
}
public PhysicalObjectCell(Object userObject) {
super(userObject);
}
}
class PhysicalObjectView extends VertexView {
/**
*
*/
private static final long serialVersionUID = 1L;
static PhysicalObjectRenderer renderer = new PhysicalObjectRenderer();
public PhysicalObjectView(Object cell) {
super(cell);
}
public Point2D getPerimeterPoint(EdgeView edge, Point2D source, Point2D p){
if(getRenderer() instanceof TrackRenderer)
return ((TrackRenderer) getRenderer()).getPerimeterPoint(this,source, p);
return super.getPerimeterPoint(edge, source, p);
}
public CellViewRenderer getRenderer(){
return renderer;
}
static class PhysicalObjectRenderer extends VertexRenderer {
/**
*
*/
private static final long serialVersionUID = 1L;
public void paint(Graphics g){
try{
if(gradientColor != null && !preview && isOpaque()) {
setOpaque(false);
Graphics2D g2d = (Graphics2D) g;
g2d.setPaint(new GradientPaint(0, 0, getBackground(), getWidth(), getHeight(), gradientColor, true));
g2d.drawOval(0, 0, getWidth(), getHeight());
g2d.fillOval(0, 0, getWidth(), getHeight());
}
super.paint(g);
paintSelectionBorder(g);
} catch (IllegalArgumentException e){
}
}
}
}