roundedrect.java
来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 642 行 · 第 1/2 页
JAVA
642 行
case SIDETYPE_BOTTOM: //the line constructed by left-bottom vertex and right-bottom vertex,trucated the two end 'RADIUS' distance.
pnt1 =m_leftBottom.nearPoint(m_rightBottom,m_radius);
pnt2 =m_rightBottom.nearPoint(m_leftBottom,m_radius);
break;
default:
return null;
}
return new LineSeg(pnt1,pnt2);
}
/**
* Pick a line segment of this rectangle by a point with a pickoffset.
* @param x X coordinate of this point.
* @param y Y coordinate of this point.
* @param pickOffset An analog pick offset that great equal than 0.
*
* @return A line segment picked.
*
*/
public LineSeg pickLine(double x, double y,double pickOffset){
LineSeg line;
line =getSide(Rect.SIDETYPE_TOP);
if (line.contains(x,y,pickOffset)) return line;
line =getSide(Rect.SIDETYPE_RIGHT);
if (line.contains(x,y,pickOffset)) return line;
line =getSide(Rect.SIDETYPE_BOTTOM);
if (line.contains(x,y,pickOffset)) return line;
line =getSide(Rect.SIDETYPE_LEFT);
if (line.contains(x,y,pickOffset)) return line;
return null;
}
/**
* Test if a point(x, y coordinates for instead) is within this rectangle.
*
* @param x X coordinate of this point.
*
* @param y Y coordinate of this point.
*
* @return True if the point is inside this rectangle, false otherwise.
*
*/
/*
public boolean contains(double x, double y){
return super.contains(x,y);
*/
/*
//if point is inside this rectangle.
boolean inRect =super.contains(x,y);
if (!inRect)
return false;
//test if current point is inside or outside the arc of rounded corner but inside the whole rectangle acutally.
for (int vertex=Rect.VERTEXTYPE_LEFTTOP; vertex<=Rect.VERTEXTYPE_RIGHTBOTTOM; vertex++){
Rect rect =getCornerRect(vertex);
Arc arc =getCornerArc(rect);
if (rect.contains(x,y)){
if (arc.contains(x,y))
return true;
else
return false;
}
}
return true;
*/
//}
/**
* Tests if the specified point intersects any side of this rectangle.
*
* @param x1, y1 the specified point.
*
* @param pickOffset An analog offset for 'pick' this line.
*
* @return <code>true</code> if the specified point intersects one side of this rectangle.
* false otherwise.
*/
/*
public boolean intersects(double x1, double y1, int pickOffset) {
return super.intersects(x1,y1,pickOffset);
*/
/*
LineSeg line =null;
//test if one side intersects with this point.
for (int side=Rect.SIDETYPE_LEFT; side<=Rect.SIDETYPE_BOTTOM; side++){
line =getSide(side);
if (line.contains(x1,y1,pickOffset)){
return true;
}
}
//test if one vertex intersects with this point.
for (int vertex=Rect.VERTEXTYPE_LEFTTOP; vertex<=Rect.VERTEXTYPE_RIGHTBOTTOM; vertex++){
JFPoint pnt =getVertex(vertex);
if (pnt.distance(x1,y1)<=pickOffset){
return true;
}
}
//test if one arc intersects with this point.
for (int vertex=Rect.VERTEXTYPE_LEFTTOP; vertex<=Rect.VERTEXTYPE_RIGHTBOTTOM; vertex++){
Rect rect =getCornerRect(vertex);
Arc arc =getCornerArc(rect);
System.out.println("check arc of vertex "+vertex+" "+new java.util.Date());
if (arc.intersects(x1,y1,pickOffset)){
System.out.println(" arc of vertex "+vertex+" is passed!");
return true;
}else{
System.out.println(" arc of vertex "+vertex+" is failed!");
}
}
return false;
*/
//}
/**
* Tests if the specified line segment intersects the interior of this
* <code>RoundedRect</code>.
* @param x1, y1 the first endpoint of the specified
* line segment
* @param x2, y2 the second endpoint of the specified
* line segment
* @return <code>true</code> if the specified line segment intersects
* the interior of this <code>RoundedRect</code>; <code>false</code>
* otherwise.
*/
/*
public boolean intersects(double x1, double y1, double x2, double y2) {
return super.intersects(x1,y1,x2,y2);
*/
/*
LineSeg line =null;
//test if one side intersects with this line.
for (int side=Rect.SIDETYPE_LEFT; side<=Rect.SIDETYPE_BOTTOM; side++){
line =getSide(side);
if (line.intersects(x1,y1,x2,y2)){
return true;
}
}
//test if one arc intersects with this line.
for (int vertex=Rect.VERTEXTYPE_LEFTTOP; vertex<=Rect.VERTEXTYPE_RIGHTBOTTOM; vertex++){
Rect rect =getCornerRect(vertex);
Arc arc =getCornerArc(rect);
if (arc.intersects(x1,y1,x2,y2))
return true;
}
return false;
*/
//}
/**
* Tests if the specified line segment intersects the interior of this
* <code>RoundedRect</code>.
* @param line the specified {@link LineSeg} to test for intersection
* with the interior of this <code>RoundedRect</code>
* @return <code>true</code> if the specified <code>LineSeg</code>
* intersects the interior of this <code>RoundedRect</code>;
* <code>false</code> otherwise.
* @since 1.2
*/
/*
public boolean intersects(LineSeg line) {
if (line==null)
return false;
else
return intersects(line.getX1(),line.getY1(),line.getX2(),line.getY2());
}
*/
/**
* Tests if the specified rectangle intersects the interior of this
* <code>Rect</code>.
* @param rect the specified {@link Rect} to test for intersection
* with the interior of this <code>Rect</code>
* @return <code>true</code> if the specified <code>Rect</code>
* intersects the interior of this <code>Rect</code>;
* <code>false</code> otherwise.
* @since 1.2
*/
/*
public boolean intersects(Rect rect) {
return super.intersects(rect);
*/
/*
LineSeg side =null;
//test if each side of rectangle intersects.
for (int i = SIDETYPE_LEFT; i<=SIDETYPE_BOTTOM; i++){
side =rect.getSide(i);
if (intersects(side))
return true;
}
//test if one of current round rectangle's corner end points is inside the target rectangle.
return rect.contains(getCornerEndPoint(VERTEXTYPE_LEFTTOP,VERTEXTYPE_RIGHTTOP)) ||
rect.contains(getCornerEndPoint(VERTEXTYPE_LEFTTOP,VERTEXTYPE_LEFTBOTTOM)) ||
rect.contains(getCornerEndPoint(VERTEXTYPE_RIGHTTOP,VERTEXTYPE_LEFTTOP)) ||
rect.contains(getCornerEndPoint(VERTEXTYPE_RIGHTTOP,VERTEXTYPE_RIGHTBOTTOM)) ||
rect.contains(getCornerEndPoint(VERTEXTYPE_LEFTBOTTOM,VERTEXTYPE_LEFTTOP)) ||
rect.contains(getCornerEndPoint(VERTEXTYPE_LEFTBOTTOM,VERTEXTYPE_RIGHTBOTTOM)) ||
rect.contains(getCornerEndPoint(VERTEXTYPE_RIGHTBOTTOM,VERTEXTYPE_RIGHTTOP)) ||
rect.contains(getCornerEndPoint(VERTEXTYPE_RIGHTBOTTOM,VERTEXTYPE_LEFTBOTTOM))
;
*/
//}
/**
* Convert this object to String
*
* @return An string represents the content of the object
*
*/
public String toString(){
StringBuffer buf =new StringBuffer();
buf.append(super.toString());
buf.append(";cornerRadius=");
return buf.toString();
}
/**
* 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{
return new RoundedRect(this);
}catch(Exception e){
throw new CloneNotSupportedException(e.getMessage());
}
}
/**
* Returns the hashcode for this Object.
*
* @return hash code for this Point2D.
*
*/
public int hashCode(){
long x =Double.doubleToLongBits(m_radius);
return super.hashCode() ^ (int)(x ^ (x >>> 32));
}
/**
* 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 RoundedRect))
return false;
RoundedRect rect =(RoundedRect)obj;
return m_radius==rect.getRadius();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?