注册 登录
编程论坛 JAVA论坛

拜托大神帮帮忙,这些代码看不太懂,麻烦给我解释一下,谢谢了

听秋 发布于 2016-05-12 12:23, 1734 次点击
程序代码:
/**

 *    $Id:DrawRect.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.event.MouseEvent;

import com.jfimagine.jfgraph.geom.JFPoint;

import com.jfimagine.jfgraph.shape.base.AbstractObject;
import com.jfimagine.jfgraph.shape.base.AbstractShape;
import com.jfimagine.jfgraph.shape.base.Port;
import com.jfimagine.jfgraph.shape.base.ShapeConst;
import com.jfimagine.jfgraph.shape.rectangle.AbstractRectangle;
import com.jfimagine.jfgraph.shape.decorate.GridFormat;

import com.jfimagine.jfdraw.draw.AbstractDrawEvent;
import com.jfimagine.jfdraw.draw.DrawConst;



 

 /**

 * Draw rect class. It's a class to manipulate JFRectangle operations.

 *

 *
@author     CookieMaker   

 *

 *
@version $Revision: 1.00 $

 
*/  

 public class DrawRect extends AbstractDrawEvent{


        /**
         *  When a modifier, shift or ctrl button is pressed down. We need to force this line be vertical or horizontal one.
         *
         *  
@param e Current mouse event
         *  
@param pnt Current mouse position
         
*/
        private void adjustPointByModifier(MouseEvent e,JFPoint pnt){
            if (e==null || pnt==null)
                return;

        int shapeType    =m_drawState.getShapeType();
        boolean isHLabelLine    =shapeType==ShapeConst.SHAPETYPE_HLABELLINE;
        boolean isVLabelLine    =shapeType==ShapeConst.SHAPETYPE_VLABELLINE;
               
        JFPoint startPoint    =m_drawState.getStartPoint();

            if (startPoint!=null && (isModifierDown(e) || isHLabelLine || isVLabelLine)){
            
            //x,y offsets
                double x    =pnt.getX()-startPoint.getX();
                double y    =pnt.getY()-startPoint.getY();

        
                switch (shapeType){
                  
                    //force vertical or horizontal line:
                    case ShapeConst.SHAPETYPE_LABELLINE:
                    case ShapeConst.SHAPETYPE_HLABELLINE:
                    case ShapeConst.SHAPETYPE_VLABELLINE:
                case ShapeConst.SHAPETYPE_ARC:
                case ShapeConst.SHAPETYPE_CHORD:
                case ShapeConst.SHAPETYPE_PIE:
                case ShapeConst.SHAPETYPE_CURVE:
                        if ((Math.abs(x)>Math.abs(y) && !isVLabelLine) || isHLabelLine)
                            pnt.setY(startPoint.getY());
                        else
                            pnt.setX(startPoint.getX());
                        
                        break;
                    
                    //force square:
                    default:
                        if (Math.abs(x)>Math.abs(y))
                            pnt.setY(startPoint.getY()+x);
                        else
                            pnt.setX(startPoint.getX()+y);
                        
                        break;
                }
        }
    }
   
        
        /** Do mouse release
         *  
@param e Current mouse event.
         *  
@param lastPort The port can be attached for current object.
         
*/
        public void doMouseRelease(MouseEvent e,Port attachPort){
            double zoom    =m_drawCanvas.getZoomScale();
                JFPoint pnt=new JFPoint(e.getX()/zoom,e.getY()/zoom);
            
            //left mouse down, always doing some confirm operations.
            if (isLeftDown(e) || isModifierDown(e)){
                //if not a start point assigned, then assign a start point here.
                if (!m_drawState.getStartPointAssigned()){

                    GridFormat gridFormat    =m_drawCanvas.getGridFormat();
                    JFPoint snapPos    =gridFormat.getSnapPos(pnt.getX(),pnt.getY());
                    pnt.setValue(snapPos);

                    //set start point as current mouse position.
                    
                    m_drawState.setStartPoint(pnt);
                    startPort       =attachPort;   

                  

                //start point assigned, then process the next step of drawing/dragging/transforming.
                }else{  
                    adjustPointByModifier(e,pnt);
                    
                GridFormat gridFormat    =m_drawCanvas.getGridFormat();
                JFPoint snapPos        =gridFormat.getSnapPos(pnt.getX(),pnt.getY());
                pnt.setValue(snapPos);
               
               
                        //correct current position according to the possible attach port.
                                if (attachPort!=null){
                                        pnt.setValue(attachPort.getXOffset(),attachPort.getYOffset());
                                }
                                                
                    if (m_drawState.getDrawState()==DrawConst.DRAWSTATE_DRAW){
                        //draw a new xor drag shape to eliminate the last dragging xor shape.
                        drawDragShape();
                        //add a new point for shape.
                    m_drawState.setLastPoint(pnt);
                    m_drawState.setLastTempPoint(pnt);
                }
               
                //if a new point of rectangle is settled, then we finish drawing rectangle here.
                if (!m_drawState.getStartPoint().equals(pnt) && ifCompleteDrawing(true)){
                        endPort      =attachPort;
                                   
                        //Graphics context
                        Graphics    g    =m_drawCanvas.getGraphics();
                    finalizeMouseEvent(g);
                    g.dispose();
                }
                }

            //right mouse down, always doing some cancel operations.
            }else if (isRightDown(e)){
                if (m_drawState.getStartPointAssigned()){
                    if (m_drawState.getDrawState()==DrawConst.DRAWSTATE_DRAW){
                        //draw xor drag shape to eliminate the last xor shape.
                        drawDragShape();
                     
                    //cancel last point.
                    m_drawState.cancelLastPoint(pnt);
                    }   
                }
            }
        }

   /** Draw xor rectangle between lastPoint and lastTempPoint
   
*/
   protected void drawDragShape(){
       Graphics    g=m_drawCanvas.getGraphics();
       drawCurrentShape(g,true);
       g.dispose();
  }


   /** draw current shape in xor or paint mode*/
   public void drawCurrentShape(Graphics g,boolean isXorMode){

        if (!ifCompleteDrawing(false)) return;            

        if (isXorMode)    {
            setPaintMode(g,true);
        }else{
            setPaintMode(g,false);
        }
            
        AbstractObject obj    =m_drawState.getCurrentShape();
            if (m_page.isHidePorts())
                obj.setShowDesign(false);
            else
                obj.setShowDesign(true);        
        
        if (obj instanceof AbstractShape){
            ((AbstractShape)obj).draw(g,isXorMode);
            if (!isXorMode){
                ((AbstractShape)obj).drawPicked(g,false);
            }
        }   
    }

   /**A temporary moving piont for mouse move event*/
   private JFPoint m_movingPoint    =new JFPoint();
        
   /** Do mouse move*/
   public void doMouseMove(MouseEvent e){

            double zoom    =m_drawCanvas.getZoomScale();
            m_movingPoint.setValue(e.getX()/zoom,e.getY()/zoom);
        adjustPointByModifier(e,m_movingPoint);

            if (m_drawState.getStartPointAssigned()){

            GridFormat gridFormat    =m_drawCanvas.getGridFormat();
            JFPoint snapPos        =gridFormat.getSnapPos(m_movingPoint.getX(),m_movingPoint.getY());

                if (m_drawState.getDrawState()==DrawConst.DRAWSTATE_DRAW){

                 drawDragShape();

                    //add a new point for shape.
                    
//for adding a new point to rectangle,
                    
//it's only modifying the second end point for rectangle actually.
                m_drawState.setLastPoint(snapPos.getX(),snapPos.getY());

                 //draw new shape in dragging.
                 m_drawState.setLastTempPoint(snapPos.getX(),snapPos.getY());
                 drawDragShape();
             }
            }
        }



 }
1 回复
#2
听秋2016-05-12 12:42
能给我解释一下思路就好,我看的不是太明白
1