?? 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 + -