📄 lineedgepainter.java
字号:
package net.sourceforge.jpowergraph.painters;
import net.sourceforge.jpowergraph.Edge;
import net.sourceforge.jpowergraph.manipulator.dragging.DraggingManipulator;
import net.sourceforge.jpowergraph.manipulator.selection.HighlightingManipulator;
import net.sourceforge.jpowergraph.pane.JGraphPane;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
/**
* The painter for domain and range edges.
*/
public class LineEdgePainter extends AbstractEdgePainter {
/** Square root of 3 over 6. */
protected static final double SQUARE_ROOT_OF_3_OVER_2=0.866;
/** The width of the base of the arrow. */
protected static final double ARROW_BASE_LENGTH=11.0;
private Color edgeColor1;
private Color edgeColor2;
private Color edgeColor3;
private boolean disposed = false;
public LineEdgePainter(Composite theParent){
this(theParent.getDisplay().getSystemColor(SWT.COLOR_GRAY), theParent.getDisplay().getSystemColor(SWT.COLOR_RED), theParent.getDisplay().getSystemColor(SWT.COLOR_GREEN));
}
public LineEdgePainter(Color blackAndWhite, Color dragging, Color normal){
edgeColor1 = blackAndWhite;
edgeColor2 = dragging;
edgeColor3 = normal;
}
/**
* Paints the supplied edge.
*
* @param graphPane the graph pane
* @param g the graphics
* @param edge the edge to paint
*/
public void paintEdge(JGraphPane graphPane, GC g,Edge edge) {
HighlightingManipulator highlightingManipulator=(HighlightingManipulator)graphPane.getManipulator(HighlightingManipulator.NAME);
boolean isHighlighted=highlightingManipulator!=null && highlightingManipulator.getHighlightedEdge()==edge;
DraggingManipulator draggingManipulator=(DraggingManipulator)graphPane.getManipulator(DraggingManipulator.NAME);
boolean isDragging=draggingManipulator!=null && draggingManipulator.getDraggedEdge()==edge;
Point from=graphPane.getScreenPointForNode(edge.getFrom());
Point to=graphPane.getScreenPointForNode(edge.getTo());
Color oldFGColor=g.getForeground();
Color oldBGColor=g.getBackground();
g.setForeground(getEdgeColor(edge,isHighlighted,isDragging, false));
g.setBackground(getEdgeColor(edge,isHighlighted,isDragging, false));
paintArrow(g,from.x,from.y,to.x,to.y);
g.setForeground(oldFGColor);
g.setBackground(oldBGColor);
}
/**
* Returns the color for the edge.
*
* @param edge the edge to be painted
* @param isHighlighted <code>true</code> if the edge is highlighted
* @param isDragging <code>true</code> if the edge is being dragged
* @param isShowBlackAndWhite <code>true</code> if the graph should be shown in black-and-white
* @return the color for the edge
*/
protected Color getEdgeColor(Edge edge,boolean isHighlighted,boolean isDragging,boolean isShowBlackAndWhite) {
if (isShowBlackAndWhite)
return edgeColor1;
else if (isHighlighted || isDragging)
return edgeColor2;
else {
return edgeColor3;
}
}
/**
* Draws an arrow at the middle of the edge.
*
* @param g the graphics to draw on
* @param x1 the source x coordinate
* @param y1 the source y coordinate
* @param x2 the target x coordinate
* @param y2 the target y coordinate
*/
public static void paintArrow(GC g,int x1,int y1,int x2,int y2) {
double middleX=(x1+x2)/2;
double middleY=(y1+y2)/2;
double slope=Math.atan2(y2-y1,x2-x1);
double pinnacleX=middleX+SQUARE_ROOT_OF_3_OVER_2*ARROW_BASE_LENGTH*Math.cos(slope);
double pinnacleY=middleY+SQUARE_ROOT_OF_3_OVER_2*ARROW_BASE_LENGTH*Math.sin(slope);
double backwardX=pinnacleX+ARROW_BASE_LENGTH*Math.cos(slope+Math.PI+Math.PI/6.0);
double backwardY=pinnacleY+ARROW_BASE_LENGTH*Math.sin(slope+Math.PI+Math.PI/6.0);
double forwardX=pinnacleX+ARROW_BASE_LENGTH*Math.cos(slope+Math.PI-Math.PI/6.0);
double forwardY=pinnacleY+ARROW_BASE_LENGTH*Math.sin(slope+Math.PI-Math.PI/6.0);
double baseX=(forwardX+backwardX)/2.0;
double baseY=(forwardY+backwardY)/2.0;
g.drawLine(x1,y1,(int)baseX,(int)baseY);
g.drawLine((int)pinnacleX,(int)pinnacleY,x2,y2);
g.fillPolygon(new int[]{(int)backwardX, (int)backwardY, (int)pinnacleX, (int)pinnacleY, (int)forwardX, (int)forwardY});
}
public void dispose(){
if (!edgeColor1.isDisposed()){
edgeColor1.dispose();
}
if (!edgeColor2.isDisposed()){
edgeColor2.dispose();
}
if (!edgeColor3.isDisposed()){
edgeColor3.dispose();
}
disposed = true;
}
public boolean isDisposed(){
return disposed;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -