为了账号安全,请及时绑定邮箱和手机立即绑定

多色边缘绘制油漆变压器

多色边缘绘制油漆变压器

aluckdog 2022-05-20 18:34:42
尝试使用 JUNG 绘制地铁图。是否可以使用变压器绘制平行使用 2 种或更多颜色的直线边缘?
查看完整描述

3 回答

?
慕森王

TA贡献1777条经验 获得超3个赞

我认为您可以通过对我在之前的答案中发布的代码示例进行小的修改来做到这一点。我假设您知道每个边缘想要什么颜色。制作一个映射,其中包含从每个边缘到该边缘所需颜色的映射。然后修改我回答的代码,如下所示:


public class NotSimpleEdgeRenderer<V,E> extends BasicEdgeRenderer<V,E> {


    Map<E,Color[]> colorMap;


    public NotSimpleEdgeRenderer(Map<E,Color[]> colorMap) {

        this.colorMap = colorMap;

    }


    protected void drawSimpleEdge(RenderContext<V,E> rc, Layout<V,E> layout, E e) {


        GraphicsDecorator g = rc.getGraphicsContext();

        Graph<V,E> graph = layout.getGraph();

        Pair<V> endpoints = graph.getEndpoints(e);

        V v1 = endpoints.getFirst();

        V v2 = endpoints.getSecond();


        Point2D p1 = layout.transform(v1);

        Point2D p2 = layout.transform(v2);

        p1 = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, p1);

        p2 = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, p2);

        float x1 = (float) p1.getX();

        float y1 = (float) p1.getY();

        float x2 = (float) p2.getX();

        float y2 = (float) p2.getY();


        boolean edgeHit;

        Rectangle deviceRectangle = null;

        JComponent vv = rc.getScreenDevice();

        if(vv != null) {

            Dimension d = vv.getSize();

            deviceRectangle = new Rectangle(0,0,d.width,d.height);

        }


        AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1);

        float dx = x2-x1;

        float dy = y2-y1;

        float thetaRadians = (float) Math.atan2(dy, dx);

        xform.rotate(thetaRadians);

        float dist = (float) Math.sqrt(dx*dx + dy*dy);

        xform.scale(dist, 1.0);


        Paint oldPaint = g.getPaint();


        // get the colors for this edge from the map

        Color[] colors = colorMap.get(e);

        int count = colors.length;

        // make the Shapes for this edge here

        Shape[] shapes = new Shape[count];

        for (int i=0; i<count; i++) {

             // this code offsets the lines enough to see the colors

            shapes[i] = new Line2D.Double(0, -count/2+(2*i), 1, -count/2+(2*i));

        }

        // iterate over the edge shapes and draw them with the corresponding colors

        for (int i=0; i<shapes.length; i++) {

            Shape edgeShape = shapes[i];

            edgeShape = xform.createTransformedShape(edgeShape);


            MutableTransformer vt = rc.getMultiLayerTransformer().getTransformer(Layer.VIEW);

            edgeHit = vt.transform(edgeShape).intersects(deviceRectangle);


            if(edgeHit == true) {

                Paint fill_paint = colors[i];

                if (fill_paint != null) {

                    g.setPaint(fill_paint);

                    g.fill(edgeShape);

                }

                Paint draw_paint = colors[i];

                if (draw_paint != null) {

                    g.setPaint(draw_paint);

                    g.draw(edgeShape);

                }

            }

            // restore old paint

            g.setPaint(oldPaint);

        }

    }

}



查看完整回答
反对 回复 2022-05-20
?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

好的,如果你想要很多颜色,试试这个:


    vv.getRenderer().setEdgeRenderer(

            new NotSimpleEdgeRenderer(new Color[]{

                    Color.red, Color.blue, Color.pink, Color.green, Color.magenta,

                    Color.cyan, Color.black, Color.orange, Color.yellow

            }));

    vv.getRenderContext().setEdgeStrokeTransformer(new ConstantTransformer(new BasicStroke(2)));





public class NotSimpleEdgeRenderer<V,E> extends BasicEdgeRenderer<V,E> {


    Color[] colors;

    Shape[] shapes;


    public NotSimpleEdgeRenderer(Color... colors) {

        int count = colors.length;

        this.colors = colors;

        shapes = new Shape[count];

        for (int i=0; i<count; i++) {

            shapes[i] = new Line2D.Double(0, -count/2+(2*i), 1, -count/2+(2*i));

        }

    }


    protected void drawSimpleEdge(RenderContext<V,E> rc, Layout<V,E> layout, E e) {


        GraphicsDecorator g = rc.getGraphicsContext();

        Graph<V,E> graph = layout.getGraph();

        Pair<V> endpoints = graph.getEndpoints(e);

        V v1 = endpoints.getFirst();

        V v2 = endpoints.getSecond();


        Point2D p1 = layout.transform(v1);

        Point2D p2 = layout.transform(v2);

        p1 = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, p1);

        p2 = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, p2);

        float x1 = (float) p1.getX();

        float y1 = (float) p1.getY();

        float x2 = (float) p2.getX();

        float y2 = (float) p2.getY();


        boolean edgeHit;

        Rectangle deviceRectangle = null;

        JComponent vv = rc.getScreenDevice();

        if(vv != null) {

            Dimension d = vv.getSize();

            deviceRectangle = new Rectangle(0,0,d.width,d.height);

        }


        AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1);

        float dx = x2-x1;

        float dy = y2-y1;

        float thetaRadians = (float) Math.atan2(dy, dx);

        xform.rotate(thetaRadians);

        float dist = (float) Math.sqrt(dx*dx + dy*dy);

        xform.scale(dist, 1.0);


        Paint oldPaint = g.getPaint();

        for (int i=0; i<shapes.length; i++) {

            Shape edgeShape = shapes[i];

            edgeShape = xform.createTransformedShape(edgeShape);


            MutableTransformer vt = rc.getMultiLayerTransformer().getTransformer(Layer.VIEW);

            edgeHit = vt.transform(edgeShape).intersects(deviceRectangle);


            if(edgeHit == true) {

                Paint fill_paint = colors[i];

                if (fill_paint != null) {

                    g.setPaint(fill_paint);

                    g.fill(edgeShape);

                }

                Paint draw_paint = colors[i];

                if (draw_paint != null) {

                    g.setPaint(draw_paint);

                    g.draw(edgeShape);

                }

            }

            // restore old paint

            g.setPaint(oldPaint);

        }

    }

}

它看起来像这样:

//img1.sycdn.imooc.com//62876ee30001e1e205390515.jpg

查看完整回答
反对 回复 2022-05-20
?
撒科打诨

TA贡献1934条经验 获得超2个赞

你可以做这样的事情......


复制并修改修改为矩形的 EdgeShape 类中的一些代码(它使蝴蝶结形状),然后设置边缘填充油漆、绘制油漆和描边,使其看起来像你想要的


public class RectangleEdge<V,E> extends AbstractEdgeShapeTransformer<V,E> {

    private static GeneralPath rectangle;


    public RectangleEdge(int width)  {

        rectangle = new GeneralPath(GeneralPath.WIND_EVEN_ODD);

        rectangle.moveTo(0, width/2); // change points to make rectangle

        rectangle.lineTo(1, width/2);

        rectangle.lineTo(1, -width/2);

        rectangle.lineTo(0, -width/2);

        rectangle.closePath();

    }


    public Shape transform(Context<Graph<V,E>,E> context) {

        return rectangle;

    }

}




    vv.getRenderContext().setEdgeShapeTransformer(new RectangleEdge<>(4));

    vv.getRenderContext().setEdgeFillPaintTransformer(new ConstantTransformer(Color.red));

    vv.getRenderContext().setEdgeStrokeTransformer(new ConstantTransformer(new BasicStroke(2)));

    vv.getRenderContext().setEdgeDrawPaintTransformer(new ConstantTransformer(Color.blue));

这是一张图片:

//img1.sycdn.imooc.com//62876ef30001d7cb05350529.jpg

查看完整回答
反对 回复 2022-05-20
  • 3 回答
  • 0 关注
  • 110 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信