好吧,我用这段代码来画图的边缘,
如果用户希望在其“流”模式下查看该图,则我绘制箭头而不是线条,一切正常,直到我更改窗口大小,然后箭头的起点(x1,y1)与我的节点不同步移动,从而使它们保持未连接状态,而drawLine()则完美。

这与AffineTransform有关吗?

(从here采用箭头绘画方法))

    void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
    Graphics2D g = (Graphics2D) g1.create();
    int arrowSize = 5;
    double dx = x2 - x1, dy = y2 - y1;
    double angle = Math.atan2(dy, dx);
    int len = (int) Math.sqrt(dx*dx + dy*dy);
    AffineTransform at = AffineTransform.getTranslateInstance(x1, y1);
    at.concatenate(AffineTransform.getRotateInstance(angle));
    g.setTransform(at);
    g.drawLine(0, 0, len, 0);
    g.setStroke(new BasicStroke(1));
    g.drawLine(len/3, arrowSize , len/3-3*arrowSize, 0);
    g.drawLine(len/3, -arrowSize , len/3-3*arrowSize, 0);
}


到任何徘徊paint Edge方法的样子的人:

    public void paintEdge(Graphics g) {
    Point uCenter = u.getCenter();
    Point vCenter = v.getCenter();
    g.setColor(color);
    Graphics2D g2=(Graphics2D) g;
    g2.setStroke(new BasicStroke(3));
    if (mode==Mode.FLOW) {
        int minx=Math.min(uCenter.x, vCenter.x),miny=Math.min(uCenter.y, vCenter.y);
        int maxx=Math.max(uCenter.x, vCenter.x),maxy=Math.max(uCenter.y, vCenter.y);
        g2.drawString(""+ f + " / " + c,10+minx + (maxx-minx)/2,10+miny+ (maxy- miny)/2);
        drawArrow(g2,uCenter.x, uCenter.y, vCenter.x, vCenter.y);
    } else {
        g2.drawLine(uCenter.x, uCenter.y, vCenter.x, vCenter.y);
    }
    g2.setStroke(new BasicStroke(1));
}

最佳答案

void drawArrow(Graphics2D g, int x1, int y1, int x2, int y2) {
    AffineTransform prev = g.getTransform();
    int arrowSize = 5;
    double dx = x2 - x1, dy = y2 - y1;
    double angle = Math.atan2(dy, dx);
    int len = (int) Math.sqrt(dx*dx + dy*dy);
    AffineTransform at = AffineTransform.getTranslateInstance(x1, y1);
    at.rotate(angle);
    g.transform(at);
    g.drawLine(0, 0, len, 0);
    g.setStroke(new BasicStroke(1));
    g.drawLine(len/3, arrowSize , len/3-3*arrowSize, 0);
    g.drawLine(len/3, -arrowSize , len/3-3*arrowSize, 0);
    g.setTransform(prev);
}


对于任何人,这都是这样做的。

问题是,我用g.setTransform(at);而不是g.transform(at);
这是很难的。

关于java - 仿射变换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9121188/

10-10 11:39