Chinaunix首页 | 论坛 | 博客
  • 博客访问: 139567
  • 博文数量: 46
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 300
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-15 23:00
文章分类

全部博文(46)

文章存档

2018年(18)

2017年(11)

2015年(14)

2014年(3)

我的朋友

分类: Java

2018-01-15 23:46:26


点击(此处)折叠或打开

  1. import javax.swing.JFrame;
  2. import java.awt.*;
  3. import javax.swing.*;
  4.   
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseListener;
  7. import java.awt.event.MouseMotionListener;
  8. import java.awt.geom.*;

  9. public class DrawFrame extends JFrame {
  10.     /**
  11.      *
  12.      */
  13.     private static final long serialVersionUID = 1L;
  14.     // 活动图窗口大小
  15.     public static final int DEFAULT_WIDTH = 1000;
  16.     public static final int DEFAULT_HEIGHT = 800;
  17.     public DrawFrame() {
  18.         setTitle("Activity Diagram");
  19.         setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  20.         ArrowLinePanel panel = new ArrowLinePanel();
  21.         add(panel);
  22.     }
  23.       
  24.     public static void main(String args[]) {
  25.         DrawFrame frame = new DrawFrame();
  26.         frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  27.         frame.setVisible(true);
  28.     }
  29.       
  30. }

  31. class ArrowLinePanel extends JPanel implements MouseMotionListener,MouseListener{
  32.     // confirm the line position
  33.   private int x1 = 0;
  34.   private int y1 = 0;
  35.   private int x2 = 0;
  36.   private int y2 = 0;
  37.     
  38.     public ArrowLinePanel() {
  39.         setBackground(Color.white);
  40.         addMouseListener(this);
  41.         addMouseMotionListener(this);
  42.     }
  43.   
  44.     public void paintComponent(Graphics g) {
  45.         super.paintComponent(g);
  46.         Graphics2D g2 = (Graphics2D) g;
  47.         g2.setPaint(Color.black);
  48.         drawAL(x1, y1, x2, y2, g2);// 这里x1, y1, x2, y2必须要声明并且初始化,而具体声明的位置和初始化的值
  49.     }
  50.   
  51.     public static void drawAL(int sx, int sy, int ex, int ey, Graphics2D g2)
  52.     {
  53.         double H = 10; // 箭头高度
  54.         double L = 4; // 底边的一半
  55.         int x3 = 0;
  56.         int y3 = 0;
  57.         int x4 = 0;
  58.         int y4 = 0;
  59.         double awrad = Math.atan(L / H); // 箭头角度
  60.         double arraow_len = Math.sqrt(L * L + H * H); // 箭头的长度
  61.         double[] arrXY_1 = rotateVec(ex - sx, ey - sy, awrad, true, arraow_len);
  62.         double[] arrXY_2 = rotateVec(ex - sx, ey - sy, -awrad, true, arraow_len);
  63.         double x_3 = ex - arrXY_1[0]; // (x3,y3)是第一端点
  64.         double y_3 = ey - arrXY_1[1];
  65.         double x_4 = ex - arrXY_2[0]; // (x4,y4)是第二端点
  66.         double y_4 = ey - arrXY_2[1];
  67.   
  68.         Double X3 = new Double(x_3);
  69.         x3 = X3.intValue();
  70.         Double Y3 = new Double(y_3);
  71.         y3 = Y3.intValue();
  72.         Double X4 = new Double(x_4);
  73.         x4 = X4.intValue();
  74.         Double Y4 = new Double(y_4);
  75.         y4 = Y4.intValue();
  76.         // 画线
  77.         g2.drawLine(sx, sy, ex, ey);
  78.         //
  79.         GeneralPath triangle = new GeneralPath();
  80.         triangle.moveTo(ex, ey);
  81.         triangle.lineTo(x3, y3);
  82.         triangle.lineTo(x4, y4);
  83.         triangle.closePath();
  84.         //实心箭头
  85.         g2.fill(triangle);
  86.         //非实心箭头
  87.         //g2.draw(triangle);
  88.   
  89.     }
  90.   
  91.     // 计算
  92.     public static double[] rotateVec(int px, int py, double ang,
  93.             boolean isChLen, double newLen) {
  94.   
  95.         double mathstr[] = new double[2];
  96.         // 矢量旋转函数,参数含义分别是x分量、y分量、旋转角、是否改变长度、新长度
  97.         double vx = px * Math.cos(ang) - py * Math.sin(ang);
  98.         double vy = px * Math.sin(ang) + py * Math.cos(ang);
  99.         if (isChLen) {
  100.             double d = Math.sqrt(vx * vx + vy * vy);
  101.             vx = vx / d * newLen;
  102.             vy = vy / d * newLen;
  103.             mathstr[0] = vx;
  104.             mathstr[1] = vy;
  105.         }
  106.         return mathstr;
  107.     }
  108.   
  109.     @Override
  110.     public void mouseDragged(MouseEvent e) {
  111.                  //改变箭头的坐标
  112.         x2 = e.getX();
  113.         y2 = e.getY();
  114.         repaint();
  115.     }
  116.   
  117.     @Override
  118.     public void mouseMoved(MouseEvent e) {
  119.     }
  120.   
  121.     @Override
  122.     public void mouseClicked(MouseEvent e) {
  123.     }
  124.   
  125.     @Override
  126.     public void mouseEntered(MouseEvent e) {
  127.     }
  128.   
  129.     @Override
  130.     public void mouseExited(MouseEvent e) {
  131.     }
  132.   
  133.     @Override
  134.     public void mousePressed(MouseEvent e) {
  135.                 //设置箭头的起始坐标
  136.         x1 = e.getX();
  137.         y1 = e.getY();
  138.     }
  139.   
  140.     @Override
  141.     public void mouseReleased(MouseEvent e) {
  142.           
  143.     }
  144.   
  145. }

阅读(1266) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~