📄 shapenodepainter.java
字号:
package net.sourceforge.jpowergraph.painters;
import net.sourceforge.jpowergraph.Node;
import net.sourceforge.jpowergraph.manipulator.dragging.DraggingManipulator;
import net.sourceforge.jpowergraph.manipulator.selection.HighlightingManipulator;
import net.sourceforge.jpowergraph.manipulator.selection.SelectionManipulator;
import net.sourceforge.jpowergraph.pane.JGraphPane;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
/**
* The painter drawing the node as a rectangle.
*/
public class ShapeNodePainter implements NodePainter {
public static final int RECTANGLE = 0;
public static final int ELLIPSE = 1;
public static final int TRIANGLE = 2;
private Color backgroundColor;
private Color borderColor;
private Color textColor;
private FontMetrics fontMetrics;
private int shape;
private Composite parent;
private GC g;
private boolean disposed = false;
public ShapeNodePainter (Composite theParent, int theShape){
this(theParent, theShape, theParent.getDisplay().getSystemColor(SWT.COLOR_GRAY), theParent.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY), theParent.getDisplay().getSystemColor(SWT.COLOR_BLACK));
}
public ShapeNodePainter (Composite theParent, int theShape, Color theBackgroundColor, Color theBorderColor, Color theTextColor){
this.parent = theParent;
this.shape = theShape;
this.backgroundColor = theBackgroundColor;
this.borderColor = theBorderColor;
this.textColor = theTextColor;
}
/**
* Paints the supplied node.
*
* @param graphPane the graph pane
* @param g the graphics
* @param node the node to paint
*/
public void paintNode(JGraphPane graphPane,GC g,Node node, int size) {
HighlightingManipulator highlightingManipulator=(HighlightingManipulator)graphPane.getManipulator(HighlightingManipulator.NAME);
boolean isHighlighted=highlightingManipulator!=null && highlightingManipulator.getHighlightedNode()==node;
SelectionManipulator selectionManipulator=(SelectionManipulator)graphPane.getManipulator(SelectionManipulator.NAME);
boolean isSelected=selectionManipulator!=null && selectionManipulator.getNodeSelectionModel().isNodeSelected(node);
DraggingManipulator draggingManipulator=(DraggingManipulator)graphPane.getManipulator(DraggingManipulator.NAME);
boolean isDragging=draggingManipulator!=null && draggingManipulator.getDraggedNode()==node;
if (fontMetrics == null){
fontMetrics = g.getFontMetrics();
}
Point nodePoint=graphPane.getScreenPointForNode(node);
if (size == NodePainter.LARGE){
int width=20;
int height=5;
int textX=0;
int textY=0;
String label=node.getLabel();
if (label!=null) {
int stringWidth = stringWidth(g, label);
width+=stringWidth+10;
height+=fontMetrics.getAscent()+fontMetrics.getDescent()+8;
textX=nodePoint.x-stringWidth/2;
textY=nodePoint.y - 7;
if (shape == TRIANGLE){
textY += 7;
}
}
else {
width+=40;
height+=20;
}
Color oldBGColor=g.getBackground();
Color oldFGColor=g.getForeground();
g.setBackground(getBackgroundColor(isHighlighted,isSelected,isDragging));
if (shape == RECTANGLE){
g.fillRectangle(nodePoint.x-width/2,nodePoint.y-height/2,width,height);
}
else if (shape == ELLIPSE){
g.fillOval(nodePoint.x-width/2,nodePoint.y-height/2,width,height);
}
else if (shape == TRIANGLE){
int x1 = nodePoint.x - width/2;
int y1 = nodePoint.y + height/2;
int x2 = x1 + width/2;
int y2 = nodePoint.y - height/2;
int x3 = x1 + width;
int y3 = y1;
g.fillPolygon(new int[]{x1, y1, x2, y2, x3, y3});
}
if (label!=null) {
Font oldFont=g.getFont();
g.setFont(graphPane.getFont());
g.setForeground(getTextColor(isHighlighted,isSelected,isDragging));
g.drawString(label,textX,textY);
g.setFont(oldFont);
}
g.setForeground(getBorderColor(isHighlighted,isSelected,isDragging));
if (shape == RECTANGLE){
g.drawRectangle(nodePoint.x-width/2,nodePoint.y-height/2,width,height);
}
else if (shape == ELLIPSE){
g.drawOval(nodePoint.x-width/2,nodePoint.y-height/2,width,height);
}
else if (shape == TRIANGLE){
int x1 = nodePoint.x - width/2;
int y1 = nodePoint.y + height/2;
int x2 = x1 + width/2;
int y2 = nodePoint.y - height/2;
int x3 = x1 + width;
int y3 = y1;
g.drawPolygon(new int[]{x1, y1, x2, y2, x3, y3});
}
g.setBackground(oldBGColor);
g.setForeground(oldFGColor);
}
else if (size == NodePainter.SMALL){
int width=18;
int height=8;
int textX = nodePoint.x + width;
int textY = nodePoint.y - 7;
String label=node.getLabel();
Color oldBGColor=g.getBackground();
Color oldFGColor=g.getForeground();
g.setBackground(getBackgroundColor(isHighlighted,isSelected,isDragging));
if (shape == RECTANGLE){
g.fillRectangle(nodePoint.x-width/2,nodePoint.y-height/2,width,height);
}
else if (shape == ELLIPSE){
g.fillOval(nodePoint.x-width/2,nodePoint.y-height/2,width,height);
}
else if (shape == TRIANGLE){
int x1 = nodePoint.x - width/2;
int y1 = nodePoint.y + height/2;
int x2 = x1 + width/2;
int y2 = nodePoint.y - height/2;
int x3 = x1 + width;
int y3 = y1;
g.fillPolygon(new int[]{x1, y1, x2, y2, x3, y3});
}
g.setBackground(oldBGColor);
if (label!=null) {
Font oldFont=g.getFont();
g.setFont(graphPane.getFont());
g.setForeground(getTextColor(isHighlighted,isSelected,isDragging));
g.drawString(label,textX,textY);
g.setFont(oldFont);
}
g.setForeground(getBorderColor(isHighlighted,isSelected,isDragging));
if (shape == RECTANGLE){
g.drawRectangle(nodePoint.x-width/2,nodePoint.y-height/2,width,height);
}
else if (shape == ELLIPSE){
g.drawOval(nodePoint.x-width/2,nodePoint.y-height/2,width,height);
}
else if (shape == TRIANGLE){
int x1 = nodePoint.x - width/2;
int y1 = nodePoint.y + height/2;
int x2 = x1 + width/2;
int y2 = nodePoint.y - height/2;
int x3 = x1 + width;
int y3 = y1;
g.drawPolygon(new int[]{x1, y1, x2, y2, x3, y3});
}
g.setBackground(oldBGColor);
g.setForeground(oldFGColor);
}
}
/**
* Returns the border color of the node.
*
* @param isHighlighted <code>true</code> if the node is highlighted
* @param isSelected <code>true</code> if the node is selected
* @param isDragging <code>true</code> if the node is being dragged
* @return the border color
*/
public Color getBorderColor(boolean isHighlighted,boolean isSelected,boolean isDragging) {
if (isHighlighted || isDragging || isSelected){
return backgroundColor;
}
return borderColor;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -