📄 tools.java
字号:
package com.wiziflow.gui;import javax.swing.*;import java.awt.*;/** * 工具类。用于辅助计算 */public class Tools { public static Point[] getClosestPoint(JComponent first,JComponent second){ if( first == null || second == null ) return null; Point ps [] = new Point [4]; ps[0] = new Point(first.getX()+first.getWidth()/2,first.getY());//北 ps[1] = new Point(first.getX()+first.getWidth()/2,first.getY()+first.getHeight());//南 ps[2] = new Point(first.getX()+first.getWidth(),first.getY()+first.getHeight()/2);//东 ps[3] = new Point(first.getX(),first.getY()+first.getHeight()/2);//西 //获得给定图标的中心点 Point po [] = new Point[4]; po[0] = new Point(second.getX()+second.getWidth()/2,second.getY());//北 po[1] = new Point(second.getX()+second.getWidth()/2,second.getY()+second.getHeight());//南 po[2] = new Point(second.getX()+second.getWidth(),second.getY()+second.getHeight()/2);//东 po[3] = new Point(second.getX(),second.getY()+second.getHeight()/2);//西 return getClosestPoint(ps,po); } /** * 获得距离最近的两个点。两个控件图标四边中点的比较,返回距离最近的点 * @param first 画线的第一个控件图标(开始图标) * @param second 画线的第二个控件图标(结束图标) * @return Point数组,含有两个值,Point[0]-->first;Point[1]-->second */// public static Point[] getClosestPoint(Bean first,Bean second){// return getClosestPoint((JComponent)first,(JComponent)second);// } /** * 获得距离最近的两个点。所有的点比较 * @param p1 第一组点 * @param p2 第二组点 * @return 距离最近的两个点 */ public static Point[] getClosestPoint(Point[] p1, Point[] p2){ if( p1 == null || p2 == null ) return null; Point ret[] = new Point[2]; ret[0] = ret[1] = null; int mind = Integer.MAX_VALUE , distance; for(int i = 0; i < p1.length; i++){ for(int j = 0; j < p2.length; j++){ distance = (int)p1[i].distance(p2[j]); if( distance < mind ){ ret[0] = p1[i]; ret[1] = p2[j]; mind = distance; } } } return ret; } /** * 开始画线时,鼠标的坐标和图标控件坐标(四个边中点)比较 * @param b 图标控件 * @param p 鼠标坐标 * @return 最短的距离 */// public static Point[] getClosestPoint(Bean b, Point p){// return getClosestPoint(b,p);// }// public static Point[] getClosestPoint(JComponent b, Point p){ if(b==null) return null; Point po [] = new Point[1]; po[0]=p; Point ps [] = new Point [4]; ps[0] = new Point(b.getX()+b.getWidth()/2,b.getY());//北 ps[1] = new Point(b.getX()+b.getWidth()/2,b.getY()+b.getHeight());//南 ps[2] = new Point(b.getX()+b.getWidth(),b.getY()+b.getHeight()/2);//东 ps[3] = new Point(b.getX(),b.getY()+b.getHeight()/2);//西 return getClosestPoint(ps,po); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -