Witam.
Mam kawałek kodu rysujący graf:
private static void constructGraph(List<Rule> rules, mxGraphComponent graphComponent) {
mxGraph graph = graphComponent.getGraph();
// modyfikacja wyglądu komponentu i styli grafu
graphComponent.getViewport().setOpaque(true);
graphComponent.getViewport().setBackground(Color.WHITE);
setGraphStyle(graph);
// konstrukcja grafu na podstawie odczytanych reguł
logger.debug("Begin create graph");
// mapa zawierająca główne wierzchołki (korzenie) grafu
Map<String, mxICell> roots = new HashMap<String, mxICell>();
Object parent = graph.getDefaultParent();
Object visibleVertex = null;
graph.getModel().beginUpdate(); // początek transakcji tworzenia grafu
try {
for (Rule rule : rules) {
logger.debug(rule.getContent());
mxICell currentVertex = null;
for (int i = 0; i < rule.getTokens().size(); i++) {
Token token = rule.getTokens().get(i);
if (i == 0) {
// pierwszy element na liście jest potencjalnym wierzchołkiem podgrafu
if (!roots.containsKey(token.getLabel())) {
mxICell rootVertex = (mxICell) graph.insertVertex(parent, token.getLabel(), token.getLabel(), 0, 0, VERTEX_SIZE, VERTEX_SIZE);
roots.put(token.getLabel(), rootVertex);
if (visibleVertex == null) {
visibleVertex = rootVertex;
}
}
currentVertex = (mxICell) roots.get(token.getLabel());
} else {
mxICell neighbour = getSasiad(currentVertex, token.getLabel());
if (neighbour == null) {
// jeśli nie ma sąsiada o takiej nazwie to dodajemy go do grafu
mxICell newVertex = (mxICell) graph.insertVertex(parent, token.getLabel(), token.getLabel(), 0, 0, VERTEX_SIZE, VERTEX_SIZE);
// wagę krawędzi mamy w poprzednim tokenie
float weight = rule.getTokens().get(i - 1).getWeight();
graph.insertEdge(parent, null, weight, currentVertex, newVertex);
currentVertex = newVertex;
} else {
// jeśli jest już taki sąsiad to przechodzimy w grafie do niego
currentVertex = neighbour;
}
}
}
}
} finally {
graph.getModel().endUpdate(); // koniec tworzenia grafu
logger.debug("End create graph");
}
do tego dochodzi HierarchicalLayout oraz
Map<String, Object> defaultVertexStyle = graph.getStylesheet().getDefaultVertexStyle();
defaultVertexStyle.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_ELLIPSE);
defaultVertexStyle.put(mxConstants.STYLE_FILLCOLOR, "blue");
defaultVertexStyle.put(mxConstants.STYLE_STROKEWIDTH, 0);
defaultVertexStyle.put(mxConstants.STYLE_FONTCOLOR, "blue");
defaultVertexStyle.put(mxConstants.STYLE_LABEL_POSITION, mxConstants.ALIGN_LEFT);
// modyfikacja domyślnego stylu krawędzi
Map<String, Object> defaultEdgeStyle = graph.getStylesheet().getDefaultEdgeStyle();
defaultEdgeStyle.put(mxConstants.STYLE_ENDARROW, mxConstants.NONE);
defaultEdgeStyle.put(mxConstants.STYLE_STROKECOLOR, "gray");
defaultEdgeStyle.put(mxConstants.STYLE_FONTCOLOR, "purple");
Jak widać wyżej kolor każdego z wierzchołków jest niebieski (STYLE_FILLCOLOR, "blue"). Moim zadaniem jest zrobienie tak aby pierwszy i ostatni wierzchołek miały różne kolory oraz tak aby można było przesuwać wierzchołki myszką po moim InternalFrame.
Co do tego drugiego to wiem że trzeba pokombinować coś z MouseListenerem lub Eventem. Prosiłbym o wytłumaczenie jak się do tego zabrać ewentualnie polecić jakiś tutorial. Z góry dzięki za pomoc.