Created
September 27, 2016 19:23
-
-
Save insac/5fc487ee2e2d7d43e094316af493e176 to your computer and use it in GitHub Desktop.
An example of a multi-value node in JGraphX using "custom shape"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Color; | |
import java.awt.FontMetrics; | |
import java.awt.Graphics2D; | |
import java.awt.Rectangle; | |
import java.util.HashMap; | |
import java.util.Map; | |
import javax.swing.JFrame; | |
import com.mxgraph.canvas.mxGraphics2DCanvas; | |
import com.mxgraph.layout.mxGraphLayout; | |
import com.mxgraph.model.mxICell; | |
import com.mxgraph.shape.mxBasicShape; | |
import com.mxgraph.swing.mxGraphComponent; | |
import com.mxgraph.util.mxConstants; | |
import com.mxgraph.util.mxUtils; | |
import com.mxgraph.view.mxCellState; | |
import com.mxgraph.view.mxGraph; | |
import com.mxgraph.view.mxStylesheet; | |
public class Example2 extends JFrame { | |
mxGraph graph=null; | |
mxGraphLayout layout=null; | |
public static void main(String[] args) { | |
Example2 frame = new Example2(); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setSize(400, 320); | |
frame.setVisible(true); | |
} | |
public Example2() { | |
graph = new mxGraph() { | |
public boolean isCellFoldable(Object cell, boolean collapse) | |
{ | |
return false; | |
} | |
public boolean isCellSelectable(Object cell) { | |
return !model.isEdge(cell); | |
} | |
}; | |
Object parent = graph.getDefaultParent(); | |
graph.getModel().beginUpdate(); | |
try | |
{ | |
mxStylesheet stylesheet = graph.getStylesheet(); | |
mxGraphics2DCanvas.putShape("mynode", new ExampleNode()); | |
Map<String, Object> style = new HashMap<String, Object>(); | |
style.put(mxConstants.STYLE_SHAPE, "mynode"); | |
stylesheet.putCellStyle("mynode", style); | |
mxICell containerNode=(mxICell)graph.insertVertex(parent, null, "", 10, 10, 100, 100, "mynode"); | |
mxICell sampleNode=(mxICell)graph.insertVertex(parent, null, "Sample", 200, 10, 100, 100, ""); | |
graph.insertEdge(parent, null, null, containerNode, sampleNode); | |
} | |
finally | |
{ | |
graph.getModel().endUpdate(); | |
} | |
mxGraphComponent graphComponent = new mxGraphComponent(graph); | |
getContentPane().add(graphComponent); | |
graphComponent.doLayout(); | |
} | |
static class ExampleNode extends mxBasicShape { | |
public void paintShape(mxGraphics2DCanvas canvas, mxCellState state) { | |
super.paintShape(canvas, state); | |
Graphics2D g=canvas.getGraphics(); | |
Rectangle rectContainer = new Rectangle((int)(state.getX()),(int)(state.getY()),100,100); | |
Rectangle rectTitle = new Rectangle((int)(state.getX()),(int)(state.getY()+10),100,65); | |
Rectangle rectLabel = new Rectangle((int)(state.getX()),(int)(state.getY()+75),50,25); | |
Rectangle rectValue = new Rectangle((int)(state.getX()+50),(int)(state.getY()+75),50,25); | |
g.setColor(Color.decode((String)state.getStyle().get(mxConstants.STYLE_FILLCOLOR))); | |
g.fill(rectContainer); | |
g.setColor(Color.decode((String)state.getStyle().get(mxConstants.STYLE_FONTCOLOR))); | |
g.draw(rectContainer); | |
g.draw(rectTitle); | |
g.draw(rectLabel); | |
g.draw(rectValue); | |
g.setFont(mxUtils.getFont(state.getStyle())); | |
drawStringCentered(g, rectTitle, "Title"); | |
drawStringCentered(g, rectLabel, "Label"); | |
drawStringCentered(g, rectValue, "Value"); | |
} | |
private void drawStringCentered(Graphics2D g, Rectangle rect, String s) { | |
FontMetrics metrics = g.getFontMetrics(); | |
int width=metrics.stringWidth(s); | |
int height=metrics.getHeight(); | |
int ascent=metrics.getAscent(); | |
g.drawString(s, (float)(rect.getCenterX()-(width/2.0)), (float)(rect.getCenterY()-height/2.0)+ascent); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment