📄 port.java
字号:
float y0 =(float)y;
float offset =0;
Graphics2D g2 =(Graphics2D)g;
if (isXorMode){
offset =(float)(GeomConst.NODE_SIZE*3/4);
line.moveTo(x0-offset,y0-offset);
line.lineTo(x0+offset,y0-offset);
line.lineTo(x0+offset,y0+offset);
line.lineTo(x0-offset,y0+offset);
line.closePath();
g2.setStroke(new BasicStroke(2));
g2.setXORMode(Color.white);
g2.setColor(Color.red);
g2.draw(line);
}else{
offset =(float)(GeomConst.NODE_SIZE/2);
line.moveTo(x0-offset,y0-offset);
line.lineTo(x0+offset,y0+offset);
line.moveTo(x0+offset,y0-offset);
line.lineTo(x0-offset,y0+offset);
g2.setPaintMode();
g2.setStroke(new BasicStroke(1));
g2.setColor(new Color(100,100,200));
g2.draw(line);
}
g2.setColor(Color.black);
}
/**
* Test if current object intersects with a specified point.
*
* @param pnt A JFPoint used to test intersection.
*
*/
public boolean intersects(JFPoint pnt){
//if point is close to the port center, then return true
return pnt.contains(m_portPoint.getX(),m_portPoint.getY(), GeomConst.NODE_SIZE/2 + GeomConst.PICK_OFFSET);
}
/**
* Scale this port by a point, and specified scale.
*
* @param baseX, baseY A scale reference point.
*
* @param scale A scale value.
*
*/
public void scaleBy(double baseX, double baseY, double scale){
scaleBy(baseX,baseY,scale,scale);
}
/**
* Scale this port by a point, and specified scale.
*
* @param baseX, baseY A scale reference point.
*
* @param xScale A scale value in x coordinate.
* @param yScale A scale value in y coordinate.
*
*/
public void scaleBy(double baseX, double baseY, double xScale, double yScale){
m_portPoint.scaleBy(baseX,baseY,xScale,yScale);
m_firstPoint.scaleBy(baseX,baseY,xScale,yScale);
m_secondPoint.scaleBy(baseX,baseY,xScale,yScale);
}
/**
* Move current object by an x and y offset.
*
* @param x, y Moving offsets.
*
*/
public void moveBy(double x, double y){
m_portPoint.moveBy(x,y);
m_firstPoint.moveBy(x,y);
m_secondPoint.moveBy(x,y);
}
/**
* Rotate current object by a specified point and an angle theta.
*
* @param baseX, baseY A rotate center coordinates.
*
* @param theta A rotate angle.
*
*/
public void rotateBy(double baseX,double baseY, double theta){
m_portPoint.rotateBy(baseX,baseY,theta);
m_firstPoint.rotateBy(baseX,baseY,theta);
m_secondPoint.rotateBy(baseX,baseY,theta);
}
/**
* Mirror this object by a x coordinate. We make a left-right mirror here.
*
* @param baseX A mirror base x coordinate.
*
*/
public void mirrorBy(double baseX){
m_portPoint.mirrorBy(baseX);
m_firstPoint.mirrorBy(baseX);
m_secondPoint.mirrorBy(baseX);
}
/**
* Reverse this object by a y coordinate. We make a up-down flip here.
*
* @param baseY A flip base y coordinate.
*
*/
public void flipBy(double baseY){
m_portPoint.flipBy(baseY);
m_firstPoint.flipBy(baseY);
m_secondPoint.flipBy(baseY);
}
/**
* Convert this port to String <br>
*
* @return An string represents the content of the port
*
*/
public String toString(){
/** Collect list string */
//collectAttachedList();
StringBuffer buf=new StringBuffer();
buf.append(super.toString());
buf.append(";portType="); buf.append(m_portType);
buf.append(";parentId="); buf.append(m_parentId);
buf.append(";xOffset="); buf.append(getXOffset());
buf.append(";yOffset="); buf.append(getYOffset());
buf.append(";portPoint="); buf.append(m_portPoint.toString());
buf.append(";firstPoint="); buf.append(m_firstPoint.toString());
buf.append(";secondPoint="); buf.append(m_secondPoint.toString());
buf.append(";percentpos="); buf.append(m_percentPos);
buf.append(";attachedList="); buf.append(m_attachedListStr);
return buf.toString();
}
/**
* Creates a new AbstractObject of the same class and with the same contents as this object.
* This method implements the method defined in AbstractObject.
*
* @return A clone of this class.
*
*/
protected AbstractObject cloneMe() throws CloneNotSupportedException{
return new Port();
}
/**
* Creates a new object of the same class and with the same contents as this object.
*
* @return A clone of this instance.
*
*/
public Object clone() throws CloneNotSupportedException{
try{
/** Collect list string */
//collectAttachedList();
Port port =(Port) super.clone();
port.m_portType =this.m_portType;
port.m_parentId =this.m_parentId;
port.m_portPoint.setValue(this.m_portPoint);
port.m_firstPoint.setValue(this.m_firstPoint);
port.m_secondPoint.setValue(this.m_secondPoint);
port.m_percentPos =this.m_percentPos;
port.m_attachedListStr =this.m_attachedListStr;
/**
do not clone a list here.
if (m_attachedList!=null && m_attachedList.size()>0){
Iterator it =m_attachedList.iterator();
while (it!=null && it.hasNext()){
Port attachPort =(Port)it.next();
port.m_attachedList.add(attachPort.clone());
}
}
**/
return port;
}catch(Exception e){
throw new CloneNotSupportedException(e.getMessage());
}
}
/**
* Returns the hashcode for this Object.
*
* @return hash code for this Point2D.
*
*/
public int hashCode(){
/** Collect list string */
collectAttachedList();
long pos =Double.doubleToLongBits(m_percentPos);
return super.hashCode() ^
m_portType ^
m_parentId ^
m_portPoint.hashCode()^
m_firstPoint.hashCode()^
m_secondPoint.hashCode()^
(int)(pos ^ (pos >>> 32)) ^
m_attachedListStr.hashCode()
;
}
/**
* Determines whether or not two objects are equal.
*
* @param obj an object to be compared with this object
*
* @return true if the object to be compared is an instance of Port and has the same values; false otherwise.
*
*/
public boolean equals(Object obj){
if (!super.equals(obj))
return false;
if (obj == this)
return true;
if (!(obj instanceof Port))
return false;
/** Collect this list string */
collectAttachedList();
Port port= (Port)obj;
/** Collect list string */
port.collectAttachedList();
return (m_portType==port.m_portType) &&
(m_parentId==port.m_parentId) &&
(m_portPoint.equals(port.m_portPoint))&&
(m_firstPoint.equals(port.m_firstPoint))&&
(m_secondPoint.equals(port.m_secondPoint))&&
(Double.doubleToLongBits(m_percentPos)==Double.doubleToLongBits(port.m_percentPos)) &&
(m_attachedListStr==port.m_attachedListStr)
;
}
/**
* Append necessary xml child for current element,
* this method will be called internally by toDOM.
*
* @param element A XML element to append child xml nodes
* @param version A file version notification so this object can obey the rules to save data.
*
*/
protected void appendChildToDOM(Element element,JFVersion version){
if (element==null)
return;
super.appendChildToDOM(element,version);
element.addChild(new Element(XML_PORTTYPE, m_portType));
element.addChild(new Element(XML_PARENTID, m_parentId));
element.addChild(new Element(XML_XOFFSET, m_portPoint.getX()));
element.addChild(new Element(XML_YOFFSET, m_portPoint.getY()));
element.addChild(new Element(XML_FIRSTPOINTX, m_firstPoint.getX()));
element.addChild(new Element(XML_FIRSTPOINTY, m_firstPoint.getY()));
element.addChild(new Element(XML_SECONDPOINTX, m_secondPoint.getX()));
element.addChild(new Element(XML_SECONDPOINTY, m_secondPoint.getY()));
element.addChild(new Element(XML_PERCENTPOS, m_percentPos));
element.addChild(new Element(XML_CONNECTEDLIST, m_attachedListStr));
}
/**
* Extract needed xml child from current element,
* this method will be called internally by fromDOM.
*
* @param element An element used to extract needed xml child
* @param version A file version notification so this object can obey the rules to fetch data.
*
*/
protected void extractChildFromDOM(Element element,JFVersion version){
if (element==null)
return;
super.extractChildFromDOM(element,version);
m_portType =Element.getIntValue(element.getChild(XML_PORTTYPE));
m_parentId =Element.getIntValue(element.getChild(XML_PARENTID));
double x=0,y=0;
x =Element.getDoubleValue(element.getChild(XML_XOFFSET));
y =Element.getDoubleValue(element.getChild(XML_YOFFSET));
setPortPoint(x,y);
x =Element.getDoubleValue(element.getChild(XML_FIRSTPOINTX));
y =Element.getDoubleValue(element.getChild(XML_FIRSTPOINTY));
setFirstPoint(x,y);
x =Element.getDoubleValue(element.getChild(XML_SECONDPOINTX));
y =Element.getDoubleValue(element.getChild(XML_SECONDPOINTY));
setSecondPoint(x,y);
m_percentPos =Element.getDoubleValue(element.getChild(XML_PERCENTPOS));
m_attachedListStr =Element.getStringValue(element.getChild(XML_CONNECTEDLIST));
}
/**
* Save this Port to a binary stream <br>
*
* @param stream An binary output stream
*
* @param version A file version notification so this object can obey the rules to fetch data.
* @exception java.io.IOException
*
*/
public void saveToStream(com.jfimagine.utils.io.JFWriter stream,JFVersion version) throws IOException{
/** Collect this list string */
collectAttachedList();
super.saveToStream(stream,version);
stream.writeInt(m_portType);
stream.writeInt(m_parentId);
stream.writeDouble(m_portPoint.getX());
stream.writeDouble(m_portPoint.getY());
stream.writeDouble(m_firstPoint.getX());
stream.writeDouble(m_firstPoint.getY());
stream.writeDouble(m_secondPoint.getX());
stream.writeDouble(m_secondPoint.getY());
stream.writeDouble(m_percentPos);
stream.writeUTF(m_attachedListStr);
}
/**
* Load port data from a binary stream <br>
*
* @param stream An binary input stream
*
* @param skipHead Skip head 'TYPE' check, an shape object should always
* has its own shape-type stored, if this shape-type has already been readed,
* this loadFromStream should/could not read the type anymore.
*
* @param version A file version notification so this object can obey the rules to fetch data.
* @exception java.io.IOException
*
*/
public void loadFromStream(com.jfimagine.utils.io.JFReader stream,boolean skipHead,JFVersion version) throws IOException{
super.loadFromStream(stream,skipHead,version);
m_portType =stream.readInt();
m_parentId =stream.readInt();
double x=0,y=0;
x =stream.readDouble();
y =stream.readDouble();
m_portPoint.setValue(x,y);
x =stream.readDouble();
y =stream.readDouble();
m_firstPoint.setValue(x,y);
x =stream.readDouble();
y =stream.readDouble();
m_secondPoint.setValue(x,y);
m_percentPos =stream.readDouble();
m_attachedListStr =stream.readUTF();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -