gpt4 book ai didi

java - 在Java中画一 strip 箭头的线

转载 作者:搜寻专家 更新时间:2023-10-30 21:16:30 25 4
gpt4 key购买 nike

alt text谁能指导我如何对不同方向的箭头线进行编码。wa 和 wl 为正矩形将位于 x 轴的顶部。下面的示例显示了 wl 是否为负而 wa 是否为正。下面的代码显示了我如何对矩形进行编码。 x1 是声明从 x 轴开始的位置的变量。 e1 是形状的长度,wa1 和 wl1 是高度。 wsign 决定 wa1 或 wl1 应显示在负侧或正侧的高度。

        if (Math.abs(wl1) > Math.abs(wa1)) {
y_scale = (load_y0 - 40) / (double) Math.abs(wl1);
} else {
y_scale = (load_y0 - 40) / (double) Math.abs(wa1);
}
g.drawLine((int) ((double) x0 + x1 * x_scale), (int) (load_y),
(int) ((double) x0 + x1 * x_scale),
(int) (load_y + (wa1 * y_scale) * -1));
g.drawLine((int) ((double) x0 + (x1 + e1) * x_scale),
(int) (load_y), (int) ((double) x0 + (x1 + e1)
* x_scale), (int) (load_y + (wl1 * y_scale)
* -1));

g.drawLine((int) ((double) x0 + x1 * x_scale),
(int) (load_y + (wa1 * y_scale * -1)),
(int) ((double) x0 + (x1 + e1) * x_scale),
(int) (load_y + (wl1 * y_scale) * -1));

最佳答案

这是一个用于绘制任意箭头的简单例程(摘自 here):

import static java.awt.geom.AffineTransform.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import javax.swing.*;

public class Main {
public static void main(String args[]) {
JFrame t = new JFrame();
t.add(new JComponent() {

private final int ARR_SIZE = 4;

void drawArrow(Graphics g1, int x1, int y1, int x2, int y2) {
Graphics2D g = (Graphics2D) g1.create();

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.transform(at);

// Draw horizontal arrow starting in (0, 0)
g.drawLine(0, 0, len, 0);
g.fillPolygon(new int[] {len, len-ARR_SIZE, len-ARR_SIZE, len},
new int[] {0, -ARR_SIZE, ARR_SIZE, 0}, 4);
}

public void paintComponent(Graphics g) {
for (int x = 15; x < 200; x += 16)
drawArrow(g, x, x, x, 150);
drawArrow(g, 30, 300, 300, 190);
}
});

t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t.setSize(400, 400);
t.setVisible(true);
}
}

结果:

enter image description here

关于java - 在Java中画一 strip 箭头的线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4112701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com