画线在Java中箭头 [英] Drawing a line with arrow in Java

查看:192
本文介绍了画线在Java中箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以指导我如何code在不同方向的箭头线。
WA和WL是正矩形将在x轴的顶部。下面的示例所示,如果WL为负和WA为正。下面的code所示如何code中的矩形形状。 X1是述明在X轴开始varaible。 e1为形状,WA1的长度和WL1是高度。 wsign确定高度WA1或WL1应在消极的一面还是积极的一面展示。

 如果(Math.abs(WL1)GT; Math.abs(WA1)){
            y_scale =(load_y0 - 40)/(双)Math.abs(WL1);
        }其他{
            y_scale =(load_y0 - 40)/(双)Math.abs(WA1);
        }
        g.drawLine((INT)((双)X0 + X1 * x_scale),(INT)(load_y)
                    (INT)((双)X0 + X1 * x_scale)
                    (中间体)(load_y +(WA1 * y_scale)* -1));
            g.drawLine((INT)((双)X0 +(X1 + E1)* x_scale)
                    (INT)(load_y),(INT)((双)X0 +(X1 + E1)
                            * x_scale),(INT)(load_y +(WL1 * y_scale)
                            * -1));            g.drawLine((INT)((双)X0 + X1 * x_scale)
                    (中间体)(load_y +(WA1 * y_scale * -1)),
                    (INT)((双)X0 +(X1 + E1)* x_scale)
                    (中间体)(load_y +(WL1 * y_scale)* -1));


解决方案

下面是一个简单的程序(从<一个通过href=\"http://stackoverflow.com/questions/3488419/java-draw-a-ruler-line-with-tick-marks-at-90-degree-angle/3488474#3488474\">here)绘制任意箭头:

 进口静态java.awt.geom.AffineTransform中的*。
进口java.awt中的*。
进口java.awt.geom.AffineTransform中;
进口的javax.swing *。公共类主要{
    公共静态无效的主要(字符串ARGS []){
        JFrame的T =新的JFrame();
        t.add(新的JComponent(){            私人最终诠释ARR_SIZE = 4;            无效drawArrow(图形G1,诠释X1,Y1 INT,INT X2,Y2 INT){
                Graphics2D的G =(Graphics2D的)g1.create();                双DX = X2 - X1,DY = Y2 - Y1;
                双角= Math.atan2(DY,DX);
                INT LEN =(INT)的Math.sqrt(DX * DX + DY * DY);
                的AffineTransform在= AffineTransform.getTranslateInstance(X1,Y1);
                at.concatenate(AffineTransform.getRotateInstance(角度));
                g.transform(AT);                //绘制水平箭头开始在(0,0)
                g.drawLine(0,0,LEN,0);
                g.fillPolygon(新INT [] {LEN,LEN-ARR_SIZE,LEN-ARR_SIZE,LEN},
                              新INT [] {0,-ARR_SIZE,ARR_SIZE,0},4);
            }            公共无效的paintComponent(图形G){
                对于(INT X = 15; X&LT; 200; X + = 16)
                    drawArrow(G,X,X,X,150);
                drawArrow(克,30,300,300,190);
            }
        });        t.se​​tDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        t.se​​tSize(400,400);
        t.se​​tVisible(真);
    }
}

结果:

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

解决方案

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:

这篇关于画线在Java中箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆