⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alignshapes.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
字号:
/**
 *    $Id:AlignShapes.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfdraw.draw;


import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import java.util.List;
import java.util.Iterator;
import java.util.ArrayList;

import com.jfimagine.jfgraph.shape.base.ObjectList;
import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.AbstractShape;

import com.jfimagine.jfgraph.geom.JFPoint;
import com.jfimagine.jfgraph.geom.Rect;
import com.jfimagine.jfdraw.gui.DrawCanvas;

import com.jfimagine.jfgraph.shape.action.JFOperationManager;
import com.jfimagine.jfgraph.shape.action.JFOperationAlignment;




 /**
 * AlignShapes class is used to align shapes in a selection.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.4.0 $
 */  
 public class AlignShapes {

   
   /** align left.*/
   public static final int ALIGNMENTTYPE_LEFT		=1;
   /** align right.*/
   public static final int ALIGNMENTTYPE_RIGHT		=2;
   /** align top.*/
   public static final int ALIGNMENTTYPE_TOP		=3;
   /** align bottom.*/
   public static final int ALIGNMENTTYPE_BOTTOM		=4;
   /** align center.*/
   public static final int ALIGNMENTTYPE_CENTER		=5;
   /** align justify horizontal.*/
   public static final int ALIGNMENTTYPE_JUSTIFY_HORIZONTAL	=6;
   /** align justify vertical.*/
   public static final int ALIGNMENTTYPE_JUSTIFY_VERTICAL	=7;
   /** align proportional spacing horizontal.*/
   public static final int ALIGNMENTTYPE_PROPORTIONAL_SPACING_HORIZONTAL	=8;
   /** align proportional spacing vertical */
   public static final int ALIGNMENTTYPE_PROPORTIONAL_SPACING_VERTICAL		=9;

 
 
   /**
    *   An object List to encapsulate shape objects.
    */
   private ObjectList m_objectList = new ObjectList();
   
   
   /**
    * An operation mangaer for alignment
    */
   private JFOperationManager m_operationManager;
   
   /**
    *   Constructor for ObjectList
    */
   public AlignShapes(JFOperationManager operationManager){
   	m_operationManager	=operationManager;
   }	

   /**
    *   Get an objectList that encapsulates shape Objects
    * 
    *   @return An object list.
    *
    */ 	
   public ObjectList getList(){
   	 return m_objectList;
   }

   /**
    *   Set an objectList that encapsulates shape Objects
    * 
    *   @param list An object list.
    *
    */ 	
   public void setList(ObjectList list){
   	m_objectList		=new ObjectList();
   	if (list!=null)
   		m_objectList.setList(list.getList());
   }
   

   /**
    *   Get the bounds of this object list.
    *   @return The bounds rectangle of current list.
    *
    */ 	
   public Rect getBounds(){
	
   	double minx=10000,miny=10000,maxx=0,maxy=0;
   	AbstractObject obj;
   	Iterator it=m_objectList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		obj	=(AbstractObject)it.next();
   		if (obj!=null && obj instanceof AbstractShape){
   			AbstractShape shapeObj	=(AbstractShape)obj;

   			Rect bounds	=(shapeObj).getBounds();
   			minx		=Math.min(minx,bounds.getX());
   			miny		=Math.min(miny,bounds.getY());
   			maxx		=Math.max(maxx,bounds.getX()+bounds.getWidth());
   			maxy		=Math.max(maxy,bounds.getY()+bounds.getHeight());
   		}
   	}  	
   
       if (minx>maxx)	minx=maxx;
       if (miny>maxy)   miny=maxy;
       return new Rect(minx,miny,maxx-minx,maxy-miny);
   }
  
   
   /**
    *  Align all shapes in list at a same top position
    */
   public void alignToTop(){
   	if (m_objectList.size()<=1)
   		return;
   		
   	Rect rect	=getBounds();
	double baseTop	=rect.getY();

	JFOperationAlignment align	=m_operationManager.addAlignment();
   	Iterator it=m_objectList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		Object obj	=(AbstractObject)it.next();
   		if (obj!=null && obj instanceof AbstractShape){
   			AbstractShape shapeObj	=(AbstractShape)obj;
   			
   			if (!shapeObj.isDisableMotion()){
   				Rect bounds	=shapeObj.getBounds();
   				double top	=bounds.getY();
   				double offset	=baseTop-top;
   			
   				shapeObj.moveBy(0,offset);
   				align.addAlignmentObject(shapeObj,0,offset);
   			}
   		}
   	}
   	align.finalizeAlignment();
   } 


   /**
    *  Align all shapes in list at a same bottom position
    */
   public void alignToBottom(){
   	if (m_objectList.size()<=1)
   		return;

   	Rect rect		=getBounds();
	double baseBottom	=rect.getY()+rect.getHeight();

	JFOperationAlignment align	=m_operationManager.addAlignment();
   	Iterator it=m_objectList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		Object obj	=(AbstractObject)it.next();
   		if (obj!=null && obj instanceof AbstractShape){
   			AbstractShape shapeObj	=(AbstractShape)obj;
   			
   			if (!shapeObj.isDisableMotion()){
   				Rect bounds	=shapeObj.getBounds();
   				double bottom	=bounds.getY()+bounds.getHeight();
   				double offset	=baseBottom-bottom;
   			
   				shapeObj.moveBy(0,offset);
   				align.addAlignmentObject(shapeObj,0,offset);
   			}
   		}
   	}
   	align.finalizeAlignment();
   } 


   /**
    *  Align all shapes in list at a same left position
    */
   public void alignToLeft(){
   	if (m_objectList.size()<=1)
   		return;

   	Rect rect		=getBounds();
	double baseLeft		=rect.getX();

	JFOperationAlignment align	=m_operationManager.addAlignment();
   	Iterator it=m_objectList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		Object obj	=(AbstractObject)it.next();
   		if (obj!=null && obj instanceof AbstractShape){
   			AbstractShape shapeObj	=(AbstractShape)obj;
   			
   			if (!shapeObj.isDisableMotion()){
   				Rect bounds	=shapeObj.getBounds();
   				double left	=bounds.getX();
   				double offset	=baseLeft-left;
   			
   				shapeObj.moveBy(offset,0);
   				align.addAlignmentObject(shapeObj,offset,0);
   			}
   		}
   	}
   	align.finalizeAlignment();
   } 


   /**
    *  Align all shapes in list at a same right position
    */
   public void alignToRight(){
   	if (m_objectList.size()<=1)
   		return;

   	Rect rect		=getBounds();
	double baseRight	=rect.getX()+rect.getWidth();

	JFOperationAlignment align	=m_operationManager.addAlignment();
   	Iterator it=m_objectList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		Object obj	=(AbstractObject)it.next();
   		if (obj!=null && obj instanceof AbstractShape){
   			AbstractShape shapeObj	=(AbstractShape)obj;
   			
   			if (!shapeObj.isDisableMotion()){
   				Rect bounds	=shapeObj.getBounds();
   				double right    =bounds.getX()+bounds.getWidth();
   				double offset	=baseRight-right;
   			
   				shapeObj.moveBy(offset,0);
   				align.addAlignmentObject(shapeObj,offset,0);
   			}
   		}
   	}
   	align.finalizeAlignment();
   } 


   /**
    *  Align all shapes in list at a same center position
    */
   public void alignToCenter(){
   	if (m_objectList.size()<=1)
   		return;

   	Rect rect	=getBounds();
	double baseX	=rect.getX()+rect.getWidth()/2;
	double baseY	=rect.getY()+rect.getHeight()/2;

	JFOperationAlignment align	=m_operationManager.addAlignment();
   	Iterator it=m_objectList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		Object obj	=(AbstractObject)it.next();
   		if (obj!=null && obj instanceof AbstractShape){
   			AbstractShape shapeObj	=(AbstractShape)obj;
   			
   			if (!shapeObj.isDisableMotion()){
   				Rect bounds	=shapeObj.getBounds();
   				double x        =bounds.getX()+bounds.getWidth()/2;
   				double y        =bounds.getY()+bounds.getHeight()/2;

   				double offsetX	=baseX-x;
   				double offsetY	=baseY-y;
   			
   				shapeObj.moveBy(offsetX,offsetY);
   				align.addAlignmentObject(shapeObj,offsetX,offsetY);
   			}
   		}
   	}
   	align.finalizeAlignment();
   } 


   /**
    *  Align all shapes in list at a same horizontal justify position
    */
   public void alignToHorizontalJustify(){
   	if (m_objectList.size()<=1)
   		return;

   	Rect rect		=getBounds();
	double baseY		=rect.getY()+rect.getHeight()/2;

	JFOperationAlignment align	=m_operationManager.addAlignment();
   	Iterator it=m_objectList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		Object obj	=(AbstractObject)it.next();
   		if (obj!=null && obj instanceof AbstractShape){
   			AbstractShape shapeObj	=(AbstractShape)obj;
   			
   			if (!shapeObj.isDisableMotion()){
   				Rect bounds	=shapeObj.getBounds();
   				double y	=bounds.getY()+bounds.getHeight()/2;
   				double offset	=baseY-y;
   			
   				shapeObj.moveBy(0,offset);
   				align.addAlignmentObject(shapeObj,0,offset);
   			}
   		}
   	}
   	align.finalizeAlignment();
   } 


   /**
    *  Align all shapes in list at a same vertical justify position
    */
   public void alignToVerticalJustify(){
   	if (m_objectList.size()<=1)
   		return;

   	Rect rect		=getBounds();
	double baseX		=rect.getX()+rect.getWidth()/2;

	JFOperationAlignment align	=m_operationManager.addAlignment();
   	Iterator it=m_objectList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		Object obj	=(AbstractObject)it.next();
   		if (obj!=null && obj instanceof AbstractShape){
   			AbstractShape shapeObj	=(AbstractShape)obj;
   			
   			if (!shapeObj.isDisableMotion()){
   				Rect bounds	=shapeObj.getBounds();
   				double x	=bounds.getX()+bounds.getWidth()/2;
   				double offset	=baseX-x;
   			
   				shapeObj.moveBy(offset,0);
   				align.addAlignmentObject(shapeObj,offset,0);
   			}
   		}
   	}
   	align.finalizeAlignment();
   } 




   /**
    *  Align all shapes in list by horizontal proportional spacing.
    */
   public void alignToHorizontalProportionalSpacing(){
   	if (m_objectList.size()<=2)
   		return;

	
	//a new temporary list	
	List tmpList		=new ArrayList();
	Iterator it;
	
	//get minimum left position and maximum left position
	double minLeft		=10000;
	double maxLeft		=-10000;

   	it=m_objectList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		Object obj	=(AbstractObject)it.next();
   		if (obj!=null && obj instanceof AbstractShape){
   			AbstractShape shapeObj	=(AbstractShape)obj;
   			
   			if (!shapeObj.isDisableMotion()){
   				Rect bounds	=shapeObj.getBounds();
   				double left	=bounds.getX();
   				minLeft		=Math.min(left,minLeft);
   				maxLeft		=Math.max(left,maxLeft);
   				tmpList.add(shapeObj);
   			}
   		}
   	}
	
	int objSize	=tmpList.size();
	if (objSize<=2)
		return;
	
	//spacing
	double space		=(maxLeft - minLeft)/(objSize-1);

	//sort the new list by each left position
	List objList		=new ArrayList();
	while (tmpList.size()>0){
		
		double  currMinLeft		=10000;
		int 	currMinIndex		=0;
		Object currObj			=null;

		int index	=0;

		it	=tmpList.iterator();
		while (it!=null && it.hasNext()){
			AbstractShape shapeObj	=(AbstractShape)it.next();
   			Rect bounds	=shapeObj.getBounds();
   			double left	=bounds.getX();		
   			if (left<currMinLeft){
   				currMinLeft	=left;
   				currMinIndex	=index;
   				currObj		=shapeObj;
   			}
   			index++;
   		}
   		
   		if (currObj==null)
   			break;
   		
   		try{
   			objList.add(currObj);
   			tmpList.remove(currMinIndex);
   		}catch(Exception e){
   			break;
   		}
	}
	


	//start moving.	
	JFOperationAlignment align	=m_operationManager.addAlignment();
   	it=objList.iterator();
   	int spaceCount	=0;
   	while (it!=null && it.hasNext()){
   		AbstractShape shapeObj	=(AbstractShape)it.next();
   			
   		Rect bounds	=shapeObj.getBounds();
   		double x	=bounds.getX();
   		double offset	=minLeft-x + spaceCount * space;
  			
   		shapeObj.moveBy(offset,0);
   		align.addAlignmentObject(shapeObj,offset,0);
   		
   		spaceCount++;
   	}
   	align.finalizeAlignment();
   } 



   /**
    *  Align all shapes in list by vertical proportional spacing.
    */
   public void alignToVerticalProportionalSpacing(){

   	if (m_objectList.size()<=2)
   		return;

	
	//a new temporary list	
	List tmpList		=new ArrayList();
	Iterator it;
	
	//get minimum top position and maximum top position
	double minTop		=10000;
	double maxTop		=-10000;

   	it=m_objectList.getList().iterator();
   	while (it!=null && it.hasNext()){
   		Object obj	=(AbstractObject)it.next();
   		if (obj!=null && obj instanceof AbstractShape){
   			AbstractShape shapeObj	=(AbstractShape)obj;
   			
   			if (!shapeObj.isDisableMotion()){
   				Rect bounds	=shapeObj.getBounds();
   				double top	=bounds.getY();
   				minTop		=Math.min(top,minTop);
   				maxTop		=Math.max(top,maxTop);
   				tmpList.add(shapeObj);
   			}
   		}
   	}
	
	int objSize	=tmpList.size();
	if (objSize<=2)
		return;
	
	//spacing
	double space		=(maxTop - minTop)/(objSize-1);

	//sort the new list by each top position
	List objList		=new ArrayList();
	while (tmpList.size()>0){
		
		double  currMinTop		=10000;
		int 	currMinIndex		=0;
		Object currObj			=null;

		int index	=0;

		it	=tmpList.iterator();
		while (it!=null && it.hasNext()){
			AbstractShape shapeObj	=(AbstractShape)it.next();
   			Rect bounds	=shapeObj.getBounds();
   			double top	=bounds.getY();		
   			if (top<currMinTop){
   				currMinTop	=top;
   				currMinIndex	=index;
   				currObj		=shapeObj;
   			}
   			index++;
   		}
   		
   		if (currObj==null)
   			break;
   		
   		try{
   			objList.add(currObj);
   			tmpList.remove(currMinIndex);
   		}catch(Exception e){
   			break;
   		}
	}
	


	//start moving.	
	JFOperationAlignment align	=m_operationManager.addAlignment();
   	it=objList.iterator();
   	int spaceCount	=0;
   	while (it!=null && it.hasNext()){
   		AbstractShape shapeObj	=(AbstractShape)it.next();
   			
   		Rect bounds	=shapeObj.getBounds();
   		double y	=bounds.getY();
   		double offset	=minTop-y + spaceCount * space;
  			
   		shapeObj.moveBy(0,offset);
   		align.addAlignmentObject(shapeObj,0,offset);
   		
   		spaceCount++;
   	}
   	align.finalizeAlignment();
   }	


	
 }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -