📄 shapenodepainter.java
字号:
package net.sourceforge.jpowergraph.painters;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
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;
/**
* 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;
public ShapeNodePainter (int theShape){
this(theShape, Color.lightGray, Color.darkGray, Color.black);
}
public ShapeNodePainter (int theShape, Color theBackgroundColor, Color theBorderColor, Color theTextColor){
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,Graphics2D 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 = graphPane.getFontMetrics(graphPane.getFont());
}
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=fontMetrics.stringWidth(label);
width+=stringWidth+6;
height+=fontMetrics.getAscent()+fontMetrics.getDescent()+4;
textX=nodePoint.x-stringWidth/2;
textY=nodePoint.y +(fontMetrics.getAscent()-fontMetrics.getDescent())/2;
if (shape == TRIANGLE){
textY += 6;
}
}
else {
width+=40;
height+=20;
}
Color oldColor=g.getColor();
g.setColor(getBackgroundColor(isHighlighted,isSelected,isDragging));
if (shape == RECTANGLE){
g.fillRect(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, x2, x3}, new int[]{y1, y2, y3}, 3);
}
if (label!=null) {
Font oldFont=g.getFont();
g.setFont(graphPane.getFont());
g.setColor(getTextColor(isHighlighted,isSelected,isDragging));
g.drawString(label,textX,textY);
g.setFont(oldFont);
}
g.setColor(getBorderColor(isHighlighted,isSelected,isDragging));
if (shape == RECTANGLE){
g.drawRect(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, x2, x3}, new int[]{y1, y2, y3}, 3);
}
g.setColor(oldColor);
}
else if (size == NodePainter.SMALL){
int width=18;
int height=8;
int textX = nodePoint.x + width;
int textY = nodePoint.y + 4;
String label=node.getLabel();
Color oldColor=g.getColor();
g.setColor(getBackgroundColor(isHighlighted,isSelected,isDragging));
if (shape == RECTANGLE){
g.fillRect(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, x2, x3}, new int[]{y1, y2, y3}, 3);
}
if (label!=null) {
Font oldFont=g.getFont();
g.setFont(graphPane.getFont());
g.setColor(getTextColor(isHighlighted,isSelected,isDragging));
g.drawString(label,textX,textY);
g.setFont(oldFont);
}
g.setColor(getBorderColor(isHighlighted,isSelected,isDragging));
if (shape == RECTANGLE){
g.drawRect(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, x2, x3}, new int[]{y1, y2, y3}, 3);
}
g.setColor(oldColor);
}
}
/**
* 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;
}
/**
* Returns the background 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 background color
*/
public Color getBackgroundColor(boolean isHighlighted,boolean isSelected,boolean isDragging) {
if (isHighlighted || isDragging || isSelected){
return borderColor;
}
return backgroundColor;
}
/**
* Returns the text 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 background color
*/
public Color getTextColor(boolean isHighlighted,boolean isSelected,boolean isDragging) {
if (isHighlighted || isDragging || isSelected){
return textColor;
}
return textColor;
}
/**
* Checks whether given point is inside the node.
*
* @param graphPane the graph pane
* @param node the node
* @param point the point
* @return <code>true</code> if the point is in the node
*/
public boolean isInNode(JGraphPane graphPane,Node node,Point point, int size) {
Rectangle nodeScreenRectangle=new Rectangle();
getNodeScreenBounds(graphPane, node, size, nodeScreenRectangle);
return nodeScreenRectangle.contains(point);
}
/**
* Returns the outer rectangle of the node on screen.
*
* @param graphPane the graph pane
* @param node the node
* @param nodeScreenRectangle the rectangle receiving the node's coordinates
*/
public void getNodeScreenBounds(JGraphPane graphPane, Node node, int size, Rectangle nodeScreenRectangle) {
Point nodePoint=graphPane.getScreenPointForNode(node);
String label=node.getLabel();
if (fontMetrics == null){
fontMetrics = graphPane.getFontMetrics(graphPane.getFont());
}
if (size == NodePainter.LARGE){
int width=1;
int height=1;
if (label!=null) {
width+=fontMetrics.stringWidth(label)+6;
height+=fontMetrics.getAscent()+fontMetrics.getDescent()+4;
}
else {
width+=40;
height+=20;
}
nodeScreenRectangle.setBounds(nodePoint.x-width/2,nodePoint.y-height/2,width,height);
}
else if (size == NodePainter.SMALL){
int width=20 + fontMetrics.stringWidth(label);
int height=8;
nodeScreenRectangle.setBounds(nodePoint.x-width/2,nodePoint.y-height/2,width,height);
}
}
public void paintLegendItem(JGraphPane graphPane, Graphics2D g, Point thePoint, String legendText){
if (fontMetrics == null){
fontMetrics = graphPane.getFontMetrics(graphPane.getFont());
}
int padding = 2;
int imageWidth = 18;
int imageHeight = 8;
int imageX = thePoint.x;
int imageY = thePoint.y;
int textX = imageX + imageWidth + (padding * 3);
int textY = imageY + imageHeight;
Color bgColor = getBackgroundColor(false, false, false);
Color boColor = getBorderColor(false, false, false);
Color teColor = getTextColor(false, false, false);
Color oldColor = g.getColor();
if (shape == RECTANGLE){
g.setColor(bgColor);
g.fillRect(imageX, imageY, imageWidth, imageHeight);
g.setColor(boColor);
g.drawRect(imageX, imageY, imageWidth, imageHeight);
}
else if (shape == ELLIPSE){
g.setColor(bgColor);
g.fillOval(imageX, imageY, imageWidth, imageHeight);
g.setColor(boColor);
g.drawOval(imageX, imageY, imageWidth, imageHeight);
}
else if (shape == TRIANGLE){
int x1 = imageX;
int y1 = imageY + imageHeight;
int x2 = x1 + imageWidth/2;
int y2 = imageY - imageHeight/2;
int x3 = x1 + imageWidth;
int y3 = y1;
g.setColor(bgColor);
g.fillPolygon(new int[]{x1, x2, x3}, new int[]{y1, y2, y3}, 3);
g.setColor(boColor);
g.drawPolygon(new int[]{x1, x2, x3}, new int[]{y1, y2, y3}, 3);
}
g.setColor(teColor);
g.drawString(legendText, textX, textY);
g.setColor(oldColor);
}
public Dimension getLegendItemSize(JGraphPane graphPane, String legendText){
if (fontMetrics == null){
fontMetrics = graphPane.getFontMetrics(graphPane.getFont());
}
int padding = 2;
int imageWidth = 18;
int imageHeight = 8;
int stringWidth = fontMetrics.stringWidth(legendText);
int width = imageWidth + stringWidth + (padding*3);
int height= Math.max(imageHeight, fontMetrics.getAscent() + fontMetrics.getDescent() + 4);
return new Dimension(width, height);
}
public Color getBorderColor() {
return borderColor;
}
public Color getBackgroundColor() {
return backgroundColor;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -