Java中画带有箭头的线段

最近由于要画活动图,所以需要画带有箭头的线段,于是结合有关资料,写出了这个方法。

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

public class test//读者可以更改类test的名字,以适合自己定义的.java文件,因为它只是用来测试的。
{
 public static void main (String args[]){
     DrawFrame frame=new DrawFrame();
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     frame.setVisible(true);
    }
}

class DrawFrame extends JFrame
 {
 public DrawFrame()
 {
 setTitle("Activity Diagram"); 
 setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
// add panel to frame
 ArrowLinePanel panel=new ArrowLinePanel();
 add(panel);
 }
// 活动图窗口大小
 public static final int DEFAULT_WIDTH=1000;
 public static final int DEFAULT_HEIGHT=800;
 }
 class ArrowLinePanel extends JPanel
{
 // confirm the line position
 
 public ArrowLinePanel()
 {
  setBackground(Color.white);
 }
 public void paintComponent(Graphics g)
 {
  super.paintComponent(g);
 

 Graphics2D g2=(Graphics2D) g; 
  g2.setPaint(Color.black);

 drawAL(x1, y1, x2, y2, g2);//这里x1, y1, x2, y2必须要声明并且初始化,而具体声明的位置和初始化的值

//由编者自己决定,在这里,笔者没有声明它们。

}

 

 public static void drawAL(int sx, int sy, int ex, int ey,Graphics2D g2)

{

        double  H  =   6 ;  // 箭头高度   
         double  L  =   6 ; // 底边的一半  
         int  x3  =   0 ;
         int  y3  =   0 ;
         int  x4  =   0 ;
         int  y4  =   0 ;
         double  awrad  =  Math.atan(L  /  H);  // 箭头角度   
         double  arraow_len  =  Math.sqrt(L  *  L  +  H  *  H); // 箭头的长度   
         double [] arrXY_1  =  rotateVec(ex  -  sx, ey  -  sy, awrad,  true , arraow_len);
         double [] arrXY_2  =  rotateVec(ex  -  sx, ey  -  sy,  - awrad,  true , arraow_len);
         double  x_3  =  ex  -  arrXY_1[ 0 ];  // (x3,y3)是第一端点   
         double  y_3  =  ey  -  arrXY_1[ 1 ];
         double  x_4  =  ex  -  arrXY_2[ 0 ]; // (x4,y4)是第二端点   
         double  y_4  =  ey  -  arrXY_2[ 1 ];

       Double X3  =   new  Double(x_3);
       x3  =  X3.intValue();
       Double Y3  =   new  Double(y_3);
       y3  =  Y3.intValue();
       Double X4  =   new  Double(x_4);
       x4  =  X4.intValue();
       Double Y4  =   new  Double(y_4);
       y4  =  Y4.intValue();
        // g.setColor(SWT.COLOR_WHITE);
        // 画线
        g2.drawLine(sx, sy, ex, ey);
        // 画箭头的一半
        g2.drawLine(ex, ey, x3, y3);
        // 画箭头的另一半
        g2.drawLine(ex, ey, x4, y4);

     }

 


        //计算
    public  static double [] rotateVec( int  px,  int  py,  double  ang,  boolean  isChLen,
            double  newLen)   {

        double  mathstr[]  =   new   double [ 2 ];
        // 矢量旋转函数,参数含义分别是x分量、y分量、旋转角、是否改变长度、新长度   
         double  vx  =  px  *  Math.cos(ang)  -  py  *  Math.sin(ang);
        double  vy  =  px  *  Math.sin(ang)  +  py  *  Math.cos(ang);
        if  (isChLen)   {
            double  d  =  Math.sqrt(vx  *  vx  +  vy  *  vy);
           vx  =  vx  /  d  *  newLen;
           vy  =  vy  /  d  *  newLen;
           mathstr[ 0 ]  =  vx;
           mathstr[ 1 ]  =  vy;
       }
        return  mathstr;
   }

 

 

}

运行这个程序就可以看到结果了,然后编者可以根据自己的需要来修改代码中的一些参数值。

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值