Java中画一条带箭头的线

发布于 2024-10-01 02:26:11 字数 1132 浏览 4 评论 0原文

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)); 

alt textCan anyone guide me how to code the arrow line in different direction.
wa and wl is positive the rectangle will be on top of the x-axis. Below example shown if wl is negative and wa is positive. The code below shown how i code the rectangle shape. x1 is the varaible to state where to start at x axis. e1 is the length of the shape, wa1 and wl1 is the height. wsign to determine the height wa1 or wl1 should display at negative side or positive side.

        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)); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

芯好空 2024-10-08 02:26:11

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

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);
    }
}

结果:

在此处输入图像描述

Here is a simple routine (adopted from here) for drawing arbitrary arrows:

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);
    }
}

Result:

enter image description here

围归者 2024-10-08 02:26:11

简单的箭头示例

/**
 * @param fromPoint end of the arrow
 * @param rotationDeg rotation angle of line
 * @param length arrow length
 * @param wingsAngleDeg wingspan of arrow
 * @return Path2D arrow shape
 */
public static Path2D createArrowForLine(
                 Point2D fromPoint, 
                 double rotationDeg, 
                 double length, 
                 double wingsAngleDeg) {

        double ax = fromPoint.getX();
        double ay = fromPoint.getY();

        double radB = Math.toRadians(-rotationDeg + wingsAngleDeg);
        double radC = Math.toRadians(-rotationDeg - wingsAngleDeg);

        Path2D resultPath = new Path2D.Double();
        resultPath.moveTo(length * Math.cos(radB) + ax, length * Math.sin(radB) + ay);
        resultPath.lineTo(ax, ay);
        resultPath.lineTo(length * Math.cos(radC) + ax, length * Math.sin(radC) + ay);
        return resultPath;
    }

Simple arrow sample

/**
 * @param fromPoint end of the arrow
 * @param rotationDeg rotation angle of line
 * @param length arrow length
 * @param wingsAngleDeg wingspan of arrow
 * @return Path2D arrow shape
 */
public static Path2D createArrowForLine(
                 Point2D fromPoint, 
                 double rotationDeg, 
                 double length, 
                 double wingsAngleDeg) {

        double ax = fromPoint.getX();
        double ay = fromPoint.getY();

        double radB = Math.toRadians(-rotationDeg + wingsAngleDeg);
        double radC = Math.toRadians(-rotationDeg - wingsAngleDeg);

        Path2D resultPath = new Path2D.Double();
        resultPath.moveTo(length * Math.cos(radB) + ax, length * Math.sin(radB) + ay);
        resultPath.lineTo(ax, ay);
        resultPath.lineTo(length * Math.cos(radC) + ax, length * Math.sin(radC) + ay);
        return resultPath;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文