多色边缘绘制油漆变压器

尝试使用 JUNG 绘制地铁图。是否可以使用变压器绘制平行使用 2 种或更多颜色的直线边缘?



aluckdog
浏览 113回答 3
3回答

慕森王

我认为您可以通过对我在之前的答案中发布的代码示例进行小的修改来做到这一点。我假设您知道每个边缘想要什么颜色。制作一个映射,其中包含从每个边缘到该边缘所需颜色的映射。然后修改我回答的代码,如下所示:public class NotSimpleEdgeRenderer<V,E> extends BasicEdgeRenderer<V,E> {&nbsp; &nbsp; Map<E,Color[]> colorMap;&nbsp; &nbsp; public NotSimpleEdgeRenderer(Map<E,Color[]> colorMap) {&nbsp; &nbsp; &nbsp; &nbsp; this.colorMap = colorMap;&nbsp; &nbsp; }&nbsp; &nbsp; protected void drawSimpleEdge(RenderContext<V,E> rc, Layout<V,E> layout, E e) {&nbsp; &nbsp; &nbsp; &nbsp; GraphicsDecorator g = rc.getGraphicsContext();&nbsp; &nbsp; &nbsp; &nbsp; Graph<V,E> graph = layout.getGraph();&nbsp; &nbsp; &nbsp; &nbsp; Pair<V> endpoints = graph.getEndpoints(e);&nbsp; &nbsp; &nbsp; &nbsp; V v1 = endpoints.getFirst();&nbsp; &nbsp; &nbsp; &nbsp; V v2 = endpoints.getSecond();&nbsp; &nbsp; &nbsp; &nbsp; Point2D p1 = layout.transform(v1);&nbsp; &nbsp; &nbsp; &nbsp; Point2D p2 = layout.transform(v2);&nbsp; &nbsp; &nbsp; &nbsp; p1 = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, p1);&nbsp; &nbsp; &nbsp; &nbsp; p2 = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, p2);&nbsp; &nbsp; &nbsp; &nbsp; float x1 = (float) p1.getX();&nbsp; &nbsp; &nbsp; &nbsp; float y1 = (float) p1.getY();&nbsp; &nbsp; &nbsp; &nbsp; float x2 = (float) p2.getX();&nbsp; &nbsp; &nbsp; &nbsp; float y2 = (float) p2.getY();&nbsp; &nbsp; &nbsp; &nbsp; boolean edgeHit;&nbsp; &nbsp; &nbsp; &nbsp; Rectangle deviceRectangle = null;&nbsp; &nbsp; &nbsp; &nbsp; JComponent vv = rc.getScreenDevice();&nbsp; &nbsp; &nbsp; &nbsp; if(vv != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dimension d = vv.getSize();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deviceRectangle = new Rectangle(0,0,d.width,d.height);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1);&nbsp; &nbsp; &nbsp; &nbsp; float dx = x2-x1;&nbsp; &nbsp; &nbsp; &nbsp; float dy = y2-y1;&nbsp; &nbsp; &nbsp; &nbsp; float thetaRadians = (float) Math.atan2(dy, dx);&nbsp; &nbsp; &nbsp; &nbsp; xform.rotate(thetaRadians);&nbsp; &nbsp; &nbsp; &nbsp; float dist = (float) Math.sqrt(dx*dx + dy*dy);&nbsp; &nbsp; &nbsp; &nbsp; xform.scale(dist, 1.0);&nbsp; &nbsp; &nbsp; &nbsp; Paint oldPaint = g.getPaint();&nbsp; &nbsp; &nbsp; &nbsp; // get the colors for this edge from the map&nbsp; &nbsp; &nbsp; &nbsp; Color[] colors = colorMap.get(e);&nbsp; &nbsp; &nbsp; &nbsp; int count = colors.length;&nbsp; &nbsp; &nbsp; &nbsp; // make the Shapes for this edge here&nbsp; &nbsp; &nbsp; &nbsp; Shape[] shapes = new Shape[count];&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<count; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// this code offsets the lines enough to see the colors&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shapes[i] = new Line2D.Double(0, -count/2+(2*i), 1, -count/2+(2*i));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // iterate over the edge shapes and draw them with the corresponding colors&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<shapes.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Shape edgeShape = shapes[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edgeShape = xform.createTransformedShape(edgeShape);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MutableTransformer vt = rc.getMultiLayerTransformer().getTransformer(Layer.VIEW);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edgeHit = vt.transform(edgeShape).intersects(deviceRectangle);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(edgeHit == true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Paint fill_paint = colors[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (fill_paint != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.setPaint(fill_paint);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.fill(edgeShape);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Paint draw_paint = colors[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (draw_paint != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.setPaint(draw_paint);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.draw(edgeShape);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // restore old paint&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.setPaint(oldPaint);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

拉莫斯之舞

好的,如果你想要很多颜色,试试这个:&nbsp; &nbsp; vv.getRenderer().setEdgeRenderer(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new NotSimpleEdgeRenderer(new Color[]{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Color.red, Color.blue, Color.pink, Color.green, Color.magenta,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Color.cyan, Color.black, Color.orange, Color.yellow&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }));&nbsp; &nbsp; vv.getRenderContext().setEdgeStrokeTransformer(new ConstantTransformer(new BasicStroke(2)));public class NotSimpleEdgeRenderer<V,E> extends BasicEdgeRenderer<V,E> {&nbsp; &nbsp; Color[] colors;&nbsp; &nbsp; Shape[] shapes;&nbsp; &nbsp; public NotSimpleEdgeRenderer(Color... colors) {&nbsp; &nbsp; &nbsp; &nbsp; int count = colors.length;&nbsp; &nbsp; &nbsp; &nbsp; this.colors = colors;&nbsp; &nbsp; &nbsp; &nbsp; shapes = new Shape[count];&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<count; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; shapes[i] = new Line2D.Double(0, -count/2+(2*i), 1, -count/2+(2*i));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; protected void drawSimpleEdge(RenderContext<V,E> rc, Layout<V,E> layout, E e) {&nbsp; &nbsp; &nbsp; &nbsp; GraphicsDecorator g = rc.getGraphicsContext();&nbsp; &nbsp; &nbsp; &nbsp; Graph<V,E> graph = layout.getGraph();&nbsp; &nbsp; &nbsp; &nbsp; Pair<V> endpoints = graph.getEndpoints(e);&nbsp; &nbsp; &nbsp; &nbsp; V v1 = endpoints.getFirst();&nbsp; &nbsp; &nbsp; &nbsp; V v2 = endpoints.getSecond();&nbsp; &nbsp; &nbsp; &nbsp; Point2D p1 = layout.transform(v1);&nbsp; &nbsp; &nbsp; &nbsp; Point2D p2 = layout.transform(v2);&nbsp; &nbsp; &nbsp; &nbsp; p1 = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, p1);&nbsp; &nbsp; &nbsp; &nbsp; p2 = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, p2);&nbsp; &nbsp; &nbsp; &nbsp; float x1 = (float) p1.getX();&nbsp; &nbsp; &nbsp; &nbsp; float y1 = (float) p1.getY();&nbsp; &nbsp; &nbsp; &nbsp; float x2 = (float) p2.getX();&nbsp; &nbsp; &nbsp; &nbsp; float y2 = (float) p2.getY();&nbsp; &nbsp; &nbsp; &nbsp; boolean edgeHit;&nbsp; &nbsp; &nbsp; &nbsp; Rectangle deviceRectangle = null;&nbsp; &nbsp; &nbsp; &nbsp; JComponent vv = rc.getScreenDevice();&nbsp; &nbsp; &nbsp; &nbsp; if(vv != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dimension d = vv.getSize();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deviceRectangle = new Rectangle(0,0,d.width,d.height);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1);&nbsp; &nbsp; &nbsp; &nbsp; float dx = x2-x1;&nbsp; &nbsp; &nbsp; &nbsp; float dy = y2-y1;&nbsp; &nbsp; &nbsp; &nbsp; float thetaRadians = (float) Math.atan2(dy, dx);&nbsp; &nbsp; &nbsp; &nbsp; xform.rotate(thetaRadians);&nbsp; &nbsp; &nbsp; &nbsp; float dist = (float) Math.sqrt(dx*dx + dy*dy);&nbsp; &nbsp; &nbsp; &nbsp; xform.scale(dist, 1.0);&nbsp; &nbsp; &nbsp; &nbsp; Paint oldPaint = g.getPaint();&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<shapes.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Shape edgeShape = shapes[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edgeShape = xform.createTransformedShape(edgeShape);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MutableTransformer vt = rc.getMultiLayerTransformer().getTransformer(Layer.VIEW);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; edgeHit = vt.transform(edgeShape).intersects(deviceRectangle);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(edgeHit == true) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Paint fill_paint = colors[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (fill_paint != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.setPaint(fill_paint);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.fill(edgeShape);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Paint draw_paint = colors[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (draw_paint != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.setPaint(draw_paint);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.draw(edgeShape);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // restore old paint&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.setPaint(oldPaint);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}它看起来像这样:

撒科打诨

你可以做这样的事情......复制并修改修改为矩形的 EdgeShape 类中的一些代码(它使蝴蝶结形状),然后设置边缘填充油漆、绘制油漆和描边,使其看起来像你想要的public class RectangleEdge<V,E> extends AbstractEdgeShapeTransformer<V,E> {&nbsp; &nbsp; private static GeneralPath rectangle;&nbsp; &nbsp; public RectangleEdge(int width)&nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; rectangle = new GeneralPath(GeneralPath.WIND_EVEN_ODD);&nbsp; &nbsp; &nbsp; &nbsp; rectangle.moveTo(0, width/2); // change points to make rectangle&nbsp; &nbsp; &nbsp; &nbsp; rectangle.lineTo(1, width/2);&nbsp; &nbsp; &nbsp; &nbsp; rectangle.lineTo(1, -width/2);&nbsp; &nbsp; &nbsp; &nbsp; rectangle.lineTo(0, -width/2);&nbsp; &nbsp; &nbsp; &nbsp; rectangle.closePath();&nbsp; &nbsp; }&nbsp; &nbsp; public Shape transform(Context<Graph<V,E>,E> context) {&nbsp; &nbsp; &nbsp; &nbsp; return rectangle;&nbsp; &nbsp; }}&nbsp; &nbsp; vv.getRenderContext().setEdgeShapeTransformer(new RectangleEdge<>(4));&nbsp; &nbsp; vv.getRenderContext().setEdgeFillPaintTransformer(new ConstantTransformer(Color.red));&nbsp; &nbsp; vv.getRenderContext().setEdgeStrokeTransformer(new ConstantTransformer(new BasicStroke(2)));&nbsp; &nbsp; vv.getRenderContext().setEdgeDrawPaintTransformer(new ConstantTransformer(Color.blue));这是一张图片:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java