📄 drawline.java
字号:
/**
* $Id:DrawLine.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.geom.LineSeg;
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.union.JFPage;
import com.jfimagine.jfgraph.shape.line.JFLine;
import com.jfimagine.jfgraph.shape.decorate.GridFormat;
import com.jfimagine.jfdraw.draw.AbstractDrawEvent;
import com.jfimagine.jfdraw.draw.DrawConst;
/**
* Draw line class. It's a class to manipulate JFLine operations.
*
* @author CookieMaker
*
* @version $Revision: 1.00 $
*/
public class DrawLine 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;
JFPoint lastPoint =m_drawState.getLastPoint();
if (lastPoint!=null && isModifierDown(e)){
//force vertical or horizontal line now
double x =Math.abs(lastPoint.getX()-pnt.getX());
double y =Math.abs(lastPoint.getY()-pnt.getY());
if (x>y)
pnt.setY(lastPoint.getY());
else
pnt.setX(lastPoint.getX());
}
}
/** 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);
//correct current position according to the possible attach port.
if (attachPort!=null){
pnt.setValue(attachPort.getXOffset(),attachPort.getYOffset());
}
//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{
GridFormat gridFormat =m_drawCanvas.getGridFormat();
JFPoint snapPos =gridFormat.getSnapPos(pnt.getX(),pnt.getY());
pnt.setValue(snapPos);
adjustPointByModifier(e,pnt);
//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 line to eliminate the last dragging xor line.
drawDragShape();
//draw current shape in xor mode to eliminate the shape
drawCurrentShape(true);
//add a new point for shape.
m_drawState.setLastPoint(pnt);
m_drawState.setLastTempPoint(pnt);
//re-draw a new current shape in xor mode.
drawCurrentShape(true);
}
//if it seems attached to a port, then confirm and end this operation.
if ((attachPort!=null) && ifCompleteDrawing(true)){
endPort =attachPort;
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 a new xor drag line to eliminate the last xor line.
drawDragShape();
//draw a new shape in xor mode to eliminate the shape
drawCurrentShape(true);
//cancel last point.
m_drawState.cancelLastPoint(pnt);
//draw a new shape in xor mode.
drawCurrentShape(true);
if (m_drawState.getStartPointAssigned()){
//draw a new xor drag line from a new last point to temporary point
drawDragShape();
}
}
}
}
}
/** Do mouse click
* @param e Current mouse event.
* @param lastPort The port can be attached for current object.
*/
public void doMouseClick(MouseEvent e,Port attachPort){
//left mouse down, always doing some confirm operations.
if ((isLeftDown(e) || isModifierDown(e)) && m_drawState.getStartPointAssigned()){
//if double click, then confirm and end this operation.
if (e.getClickCount()==2 && ifCompleteDrawing(true)){
endPort =attachPort;
Graphics g=m_drawCanvas.getGraphics();
finalizeMouseEvent(g);
g.dispose();
}
}
}
/** Draw xor line between lastPoint and lastTempPoint
*/
protected void drawDragShape(){
//avoid a xor end point, so draw an xor point here.
drawDragLine(m_drawState.getLastPoint(),m_drawState.getLastPoint());
//draw the xor line.
drawDragLine(m_drawState.getLastPoint(),m_drawState.getLastTempPoint());
}
/** draw current shape in xor or paint mode*/
public void drawCurrentShape(Graphics g,boolean isXorMode){
if (!ifCompleteDrawing(false)) return;
AbstractObject obj =m_drawState.getCurrentShape();
if (isXorMode) {
setPaintMode(g,true);
}else{
setPaintMode(g,false);
}
if (obj instanceof AbstractShape){
((AbstractShape)obj).draw(g,isXorMode);
if (!isXorMode){
((AbstractShape)obj).drawPicked(g,false);
}
}
}
/**A temporary moving point 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();
//draw new line in dragging.
m_drawState.setLastTempPoint(snapPos.getX(),snapPos.getY());
drawDragShape();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -