📄 legendpainter.java
字号:
package net.sourceforge.jpowergraph.painters;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.util.Iterator;
import javax.swing.AbstractAction;
import net.sourceforge.jpowergraph.Legend;
import net.sourceforge.jpowergraph.pane.JGraphPane;
/**
* @author Mick Kerrigan
*
* Created on 07-Aug-2005
* Committed by $Author: morcen $
*
* $Source: /cvsroot/jpowergraph/swing/src/net/sourceforge/jpowergraph/painters/LegendPainter.java,v $,
* @version $Revision: 1.2 $ $Date: 2005/08/12 13:09:18 $
*/
public class LegendPainter {
private Color enabledButtonColor;
private Color disabledButtonColor;
public LegendPainter() {
this.enabledButtonColor = Color.BLACK;
this.disabledButtonColor = Color.lightGray;
}
public Rectangle paintLegend(JGraphPane graphPane, Graphics2D g, final Legend theLegend){
int padding = 2;
int widthPad = 20;
int heightPad = 10;
int width=widthPad;
int height=heightPad;
int buttonWidth = 10;
for (Iterator i = theLegend.getNodePainters().iterator(); i.hasNext();) {
NodePainter nodePainter = (NodePainter) i.next();
String label = theLegend.getDescriptionForPainter(nodePainter);
Dimension d = nodePainter.getLegendItemSize(graphPane, label);
width = Math.max(width, (int) d.getWidth() + widthPad);
height += (int) d.getHeight();
}
if (theLegend.isNodeFilterInUse()){
width += buttonWidth + (padding*2);
}
int x = 10;
int y = graphPane.getHeight() - (height + 10);
Color oldColor2=g.getColor();
g.setColor(Color.white);
g.fillRoundRect(x, y, width, height, 10, 10);
g.setColor(Color.black);
g.drawRoundRect(x, y, width, height, 10, 10);
g.setColor(oldColor2);
int itemX = x + widthPad/2;
int itemY = y + heightPad;
for (Iterator i = theLegend.getNodePainters().iterator(); i.hasNext();) {
NodePainter nodePainter = (NodePainter) i.next();
String label = theLegend.getDescriptionForPainter(nodePainter);
final Class theClass = theLegend.getNodeClassForPainter(nodePainter);
Dimension d = nodePainter.getLegendItemSize(graphPane, label);
nodePainter.paintLegendItem(graphPane, g, new Point(itemX, itemY), label);
int buttonX = x + (width - (buttonWidth + (padding*2)));
int buttonY = itemY - (padding/2);
int buttonHeight = (int) d.getHeight() - (padding * 4);
if (theLegend.isNodeFilterInUse()){
boolean canFilterNode = theLegend.getNodeFilterLens().canFilterNode(theClass);
Rectangle r = new Rectangle(buttonX, buttonY, buttonWidth, buttonHeight);
if (!canFilterNode){
g.setColor(disabledButtonColor);
}
else{
g.setColor(enabledButtonColor);
}
g.drawRect(r.x, r.y, r.width, r.height);
if (theLegend.getNodeFilterLens().getFilterValue(theClass)){
g.drawLine(r.x + 3, r.y + 3, r.x + r.width - 3, r.y + r.height - 3);
g.drawLine(r.x + 3, r.y + r.height - 3, r.x + r.width - 3, r.y + 3);
}
if (canFilterNode){
theLegend.addActionRectangle(r, new AbstractAction() {
public void actionPerformed(ActionEvent e) {
theLegend.getNodeFilterLens().setFilterValue(theClass, !theLegend.getNodeFilterLens().getFilterValue(theClass));
}
});
}
}
itemY += (int) d.getHeight();
}
return new Rectangle(x, y, width, height);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -