📄 arrow.java
字号:
//fill arrow
g2.setColor(c);
g2.fill(path);
}
}
/** draw triangle line arrows. A triangle line is almost same as triangle, but not closed in path.
* @param reverse If it is a reversed triangle line.
* @param arrowSize A reference arrow size.
* @param startPoint The start point of the line.
* @param endPoint The end point of the line. The arrow will be drawn on this end side.
* @param g The graphics context to draw arrow.
* @param c The color of current arrow.
*/
private static void drawTriangleLine(boolean reverse, int arrowSize,JFPoint startPoint, JFPoint endPoint,Graphics g, Color c){
if (startPoint==null || endPoint==null)
return;
//slope of the vertical line of the line above.
double slope =getUprightLineSlope(startPoint,endPoint);
JFPoint end1, end2, end3,interPoint;
if (!reverse){
end1 =endPoint;
//the point that vertical side of triangle intersects the line
interPoint =end1.nearPoint(startPoint,arrowSize*2);
//the two point on the vertical line across the interpoint.
end2 =interPoint.nearPoint(slope,arrowSize,startPoint,true);
end3 =interPoint.nearPoint(slope,arrowSize,startPoint,false);
}else{
end1 =endPoint.nearPoint(startPoint,arrowSize*2);
//the two point on the vertical line across the interpoint.
end2 =endPoint.nearPoint(slope,arrowSize,startPoint,true);
end3 =endPoint.nearPoint(slope,arrowSize,startPoint,false);
}
//draw first triangle.
GeneralPath path= new GeneralPath(GeneralPath.WIND_EVEN_ODD);
path.moveTo((float)end2.getX(), (float)end2.getY());
path.lineTo((float)end1.getX(), (float)end1.getY());
path.lineTo((float)end3.getX(), (float)end3.getY());
Graphics2D g2 =(Graphics2D)g;
//draw arrow
g2.setColor(c);
g2.draw(path);
}
/** draw an upright short line segment on specified line.
* @param lineWidth line width of the line segment.
* @param offset the offset that the upright line lies to end point.
* @param startPoint The start point of the line.
* @param endPoint The end point of the line. The arrow will be drawn on this end side.
* @param g The graphics context to draw arrow.
* @param c The color of current arrow.
*/
private static void drawVLine(int lineWidth, int offset,JFPoint startPoint, JFPoint endPoint,Graphics g, Color c){
if (startPoint==null || endPoint==null)
return;
//slope of the vertical line of the line above.
double slope =getUprightLineSlope(startPoint,endPoint);
JFPoint end1, end2, interPoint;
//the point that the upright line intersects this line
interPoint =endPoint.nearPoint(startPoint,offset);
//the two end points of the upright line segment.
end1 =interPoint.nearPoint(slope,Math.max(ARROWSIZE,lineWidth * 2),startPoint,true);
end2 =interPoint.nearPoint(slope,Math.max(ARROWSIZE,lineWidth * 2),startPoint,false);
//draw first triangle.
GeneralPath path= new GeneralPath(GeneralPath.WIND_EVEN_ODD);
path.moveTo((float)end1.getX(), (float)end1.getY());
path.lineTo((float)end2.getX(), (float)end2.getY());
//draw line
Graphics2D g2 =(Graphics2D)g;
g2.setColor(c);
g2.draw(path);
}
/** draw an inclined line that will have +45 or -45 relative degrees to specified line.
* @param clockwise true if +45 relative degrees, false if -45 degrees(consider a clockwise quadrant).
* @param lineWidth line width of the line segment.
* @param startPoint The start point of the line.
* @param endPoint The end point of the line. The arrow will be drawn on this end side.
* @param g The graphics context to draw arrow.
* @param c The color of current arrow.
*/
private static void drawInclinedLine(boolean clockwise,int lineWidth, JFPoint startPoint, JFPoint endPoint,Graphics g, Color c){
if (startPoint==null || endPoint==null)
return;
//slope of the vertical line of the line above.
double tanAlpha =startPoint.getSlope(endPoint);
//tangent of +45/-45
double tanBeta =0;
if (clockwise)
tanBeta =Math.tan(Angle.PI/4);
else
tanBeta =-1 * Math.tan(Angle.PI/4);
//the slope of inclined line.
//tan(alpha+beta)=(tan(alpha)+tan(beta))/(1-tan(alpha)*tan(beta))
double slope =Angle.newSlopeByAddingSlope(tanAlpha,tanBeta);
JFPoint end1, end2;
//the two end points of the inclined line.
end1 =endPoint.nearPoint(slope,Math.max(ARROWSIZE,lineWidth * 3),startPoint,true);
end2 =endPoint.nearPoint(slope,Math.max(ARROWSIZE,lineWidth * 3),startPoint,false);
//draw first triangle.
GeneralPath path= new GeneralPath(GeneralPath.WIND_EVEN_ODD);
path.moveTo((float)end1.getX(), (float)end1.getY());
path.lineTo((float)end2.getX(), (float)end2.getY());
//draw line
Graphics2D g2 =(Graphics2D)g;
g2.setColor(c);
g2.draw(path);
}
/** draw an small circle on specified line.
* @param empty If an empty or solid circle. True if empty, false otherwise.
* @param lineWidth line width of the line segment.
* @param startPoint The start point of the line.
* @param endPoint The end point of the line. The arrow will be drawn on this end side.
* @param g The graphics context to draw arrow.
* @param c The color of current arrow.
*/
private static void drawCircle(boolean empty,int lineWidth, JFPoint startPoint, JFPoint endPoint,Graphics g, Color c){
if (startPoint==null || endPoint==null)
return;
int radius =(int)Math.max(ARROWSIZE,lineWidth * 2);
//center of circle.
JFPoint center =endPoint.nearPoint(startPoint,radius + lineWidth);
int x =(int)(center.getX() - radius);
int y =(int)(center.getY() - radius);
int w =(int)(radius*2);
int h =w;
int startAngle =0;
int arcAngle =360;
Graphics2D g2 =(Graphics2D)g;
if (empty){
g2.setColor(Color.white);
g2.fillArc(x,y,w,h,startAngle,arcAngle);
g2.setColor(c);
g2.drawArc(x,y,w,h,startAngle,arcAngle);
}else{
g2.setColor(c);
g2.fillArc(x,y,w,h,startAngle,arcAngle);
}
}
/** draw a small rectangle on specified line.
* @param empty If an empty or solid rectangle. True if empty, false otherwise.
* @param lineWidth line width of the line segment.
* @param startPoint The start point of the line.
* @param endPoint The end point of the line. The arrow will be drawn on this end side.
* @param g The graphics context to draw arrow.
* @param c The color of current arrow.
*/
private static void drawRectangle(boolean empty,int lineWidth, JFPoint startPoint, JFPoint endPoint,Graphics g, Color c){
if (startPoint==null || endPoint==null)
return;
int halfSide =(int)Math.max(ARROWSIZE,lineWidth * 2);
//slope of the vertical line of the line above.
double slope =getUprightLineSlope(startPoint,endPoint);
JFPoint end1, end2, end3, end4, interPoint;
//the point that one upright side of rectangle intersects this line
interPoint =endPoint;
//the two end points of the upright line segment.
end1 =interPoint.nearPoint(slope,halfSide,startPoint,true);
end2 =interPoint.nearPoint(slope,halfSide,startPoint,false);
//the point that other upright side of rectangle intersects this line
interPoint =interPoint.nearPoint(startPoint,halfSide*2);
//the two end points of the upright line segment.
end3 =interPoint.nearPoint(slope,halfSide,startPoint,true);
end4 =interPoint.nearPoint(slope,halfSide,startPoint,false);
//draw rectangle.
GeneralPath path= new GeneralPath(GeneralPath.WIND_EVEN_ODD);
path.moveTo((float)end1.getX(), (float)end1.getY());
path.lineTo((float)end2.getX(), (float)end2.getY());
path.lineTo((float)end4.getX(), (float)end4.getY());
path.lineTo((float)end3.getX(), (float)end3.getY());
path.closePath();
Graphics2D g2 =(Graphics2D)g;
if (empty){
//clear firstly
g2.setColor(Color.white);
g2.fill(path);
//draw arrow
g2.setColor(c);
g2.draw(path);
}else{
//fill arrow
g2.setColor(c);
g2.fill(path);
}
}
/** draw diamond arrows.
* @param empty If is an empty or solid triangle, true if empty, false solid.
* @param arrowSize A reference arrow size.
* @param startPoint The start point of the line.
* @param endPoint The end point of the line. The arrow will be drawn on this end side.
* @param g The graphics context to draw arrow.
* @param c The color of current arrow.
*/
private static void drawDiamond(boolean empty, int arrowSize,JFPoint startPoint, JFPoint endPoint,Graphics g, Color c){
if (startPoint==null || endPoint==null)
return;
//slope of the vertical line of the line above.
double slope =getUprightLineSlope(startPoint,endPoint);
JFPoint end1, end2, end3,end4,interPoint;
end1 =endPoint;
//the point that the line made by middle two vertexes of diamond intersects the line
interPoint =end1.nearPoint(startPoint,arrowSize*2);
//the two point on the vertical line across the interpoint.
end2 =interPoint.nearPoint(slope,arrowSize,startPoint,true);
end3 =interPoint.nearPoint(slope,arrowSize,startPoint,false);
//the last end point.
end4 =interPoint.nearPoint(startPoint,arrowSize*2);
//draw first triangle.
GeneralPath path= new GeneralPath(GeneralPath.WIND_EVEN_ODD);
path.moveTo((float)end1.getX(), (float)end1.getY());
path.lineTo((float)end2.getX(), (float)end2.getY());
path.lineTo((float)end4.getX(), (float)end4.getY());
path.lineTo((float)end3.getX(), (float)end3.getY());
path.closePath();
Graphics2D g2 =(Graphics2D)g;
if (empty){
//clear firstly
g2.setColor(Color.white);
g2.fill(path);
//draw arrow
g2.setColor(c);
g2.draw(path);
}else{
//fill arrow
g2.setColor(c);
g2.fill(path);
}
}
/** a rotate sign size. */
public static final int ARROWSIZE_ROTATE =6;
/** Draw a rotate arrow sign.
* @param x, y Position to draw the arrow.
* @param g The graphics context to draw arrow.
*/
public static void drawRotateArrow(double x, double y,Graphics g){
Color oldColor =g.getColor();
int arcSize =ARROWSIZE_ROTATE;
Graphics2D g2 =(Graphics2D)g;
/*
g2.setColor(Color.white);
g2.setStroke(new BasicStroke(1));
g2.fillArc((int)x-arcSize/2, (int)y-arcSize/2,arcSize,arcSize,120,240);
*/
g2.setColor(Color.white);
g2.setStroke(new BasicStroke(1));
g2.fillOval((int)x-arcSize, (int)y-arcSize, arcSize*2,arcSize*2);
g2.setColor(new Color(100,100,200));
g2.setStroke(new BasicStroke(1));
g2.drawOval((int)x-2, (int)y-2, 4,4);
g2.setColor(new Color(100,100,200));
g2.setStroke(new BasicStroke(2));
g2.drawOval((int)x-arcSize, (int)y-arcSize, arcSize*2,arcSize*2);
/*
g2.drawArc((int)x-arcSize/2, (int)y-arcSize/2,arcSize,arcSize,120,240);
//we made a isosceles triangle arrow on the right side of arc.
float x0 =(float)(x + arcSize/2);
float y0 =(float)y;
float x1 =x0-1;
float y1 =y0;
//float x2 =x0+2;
float x2 =x0;
float y2 =y0;
float x3 =x0-1;
float y3 =y0-2;
//float y3 =y0-4;
GeneralPath path= new GeneralPath(GeneralPath.WIND_EVEN_ODD);
path.moveTo(x1,y1);
path.lineTo(x2,y2);
path.lineTo(x3,y3);
path.closePath();
g2.draw(path);
*/
g.setColor(oldColor);
g2.setStroke(new BasicStroke(1));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -