Questo articolo illustra come creare un diagramma di flusso in Java. Include l’algoritmo passo passo e uno snippet di codice eseguibile per sviluppare un creatore di diagrammi di flusso in Java. Inoltre, puoi migliorare il processo manipolando lo schema, il numero di forme, le connessioni e altre proprietà per creare un flowchart secondo le tue esigenze.
Passaggi per creare un diagramma di flusso in Java
- Prepara l’ambiente installando l’API Aspose.Diagram per creare diagrammi di flusso
- Sviluppare lo schema per il diagramma di flusso
- Crea un oggetto della classe Diagram e carica le forme principali per l’inserimento delle forme
- Imposta il layout del diagramma di flusso ed esporta il diagramma di flusso generato utilizzando il metodo di salvataggio Save
Questi passaggi dimostrano il processo per sviluppare un generatore di diagrammi di flusso in Java. Innanzitutto, crea uno schema per impostare diversi ruoli, designazioni e altre proprietà. Successivamente, utilizza le forme principali per aggiungere forme diverse come rettangoli, connettori, ecc. prima di impostare il layout e salvare il diagramma di flusso di output.
Codice per creare un generatore di diagrammi di flusso in Java
import com.aspose.diagram.*; | |
public class Main | |
{ | |
public static void main(String[] args) throws Exception // Create flowchart in Java | |
{ | |
// Set the licenses | |
new License().setLicense("License.lic"); | |
// Create a new diagram | |
int pageNumber = 0; | |
String MasterRectangle = "Process", decisionMaster = "Decision", connectorMaster = "Dynamic connector"; | |
com.aspose.diagram.Diagram diagram = new com.aspose.diagram.Diagram("XANFLOWCHARTNEW.vss"); | |
double widthparam = 1, heightparam = 1, pinX = 4, pinY = 10; | |
long process1 = diagram.addShape(pinX, pinY, widthparam, heightparam, MasterRectangle, 0); | |
Shape processShape1 = diagram.getPages().getPage(pageNumber).getShapes().getShape(process1); | |
processShape1.getText().getValue().add(new Txt("PROCESS")); | |
processShape1.setName("PROCESS"); | |
processShape1.getXForm().getLocPinX().getUfe().setF("Width*0.5"); | |
processShape1.getXForm().getLocPinY().getUfe().setF("Height*0.5"); | |
pinY = pinY - 2; | |
long decision1 = diagram.addShape(pinX, pinY, widthparam, heightparam, decisionMaster, 0); | |
Shape decisionShape1 = diagram.getPages().getPage(pageNumber).getShapes().getShape(decision1); | |
decisionShape1.getText().getValue().add(new Txt("DECISION")); | |
decisionShape1.setName("DECISION"); | |
decisionShape1.getXForm().getLocPinX().getUfe().setF("Width*0.5"); | |
decisionShape1.getXForm().getLocPinY().getUfe().setF("Height*0.5"); | |
pinY = pinY - 2; | |
long process2 = diagram.addShape(pinX, pinY, widthparam, heightparam, MasterRectangle, 0); | |
Shape processShape2 = diagram.getPages().getPage(pageNumber).getShapes().getShape(process2); | |
processShape2.getText().getValue().add(new Txt("PROCESS")); | |
processShape2.setName("PROCESS"); | |
processShape2.getXForm().getLocPinX().getUfe().setF("Width*0.5"); | |
processShape2.getXForm().getLocPinY().getUfe().setF("Height*0.5"); | |
pinY = pinY - 2; | |
long process3 = diagram.addShape(pinX, pinY, widthparam, heightparam, MasterRectangle, 0); | |
Shape processShape3 = diagram.getPages().getPage(pageNumber).getShapes().getShape(process3); | |
processShape3.getText().getValue().add(new Txt("PROCESS")); | |
processShape3.setName("PROCESS"); | |
processShape3.getXForm().getLocPinX().getUfe().setF("Width*0.5"); | |
processShape3.getXForm().getLocPinY().getUfe().setF("Height*0.5"); | |
pinY = pinY - 2; | |
long process4 = diagram.addShape(pinX, pinY, widthparam, heightparam, MasterRectangle, 0); | |
Shape processShape4 = diagram.getPages().getPage(pageNumber).getShapes().getShape(process4); | |
processShape4.getText().getValue().add(new Txt("PROCESS")); | |
processShape4.setName("PROCESS"); | |
processShape4.getXForm().getLocPinX().getUfe().setF("Width*0.5"); | |
processShape4.getXForm().getLocPinY().getUfe().setF("Height*0.5"); | |
long connecterId = diagram.addShape(new Shape(), connectorMaster, 0); | |
diagram.getPages().getPage(pageNumber).connectShapesViaConnector(process1, ConnectionPointPlace.BOTTOM, | |
decision1, ConnectionPointPlace.TOP, connecterId); | |
long connecterId1 = diagram.addShape(new Shape(), connectorMaster, 0); | |
diagram.getPages().getPage(pageNumber).connectShapesViaConnector(decision1, ConnectionPointPlace.BOTTOM, | |
process2, ConnectionPointPlace.TOP, connecterId1); | |
long connecterId2 = diagram.addShape(new Shape(), connectorMaster, 0); | |
diagram.getPages().getPage(pageNumber).connectShapesViaConnector(process2, ConnectionPointPlace.BOTTOM, | |
process3, ConnectionPointPlace.TOP, connecterId2); | |
long connecterId3 = diagram.addShape(new Shape(), connectorMaster, 0); | |
diagram.getPages().getPage(pageNumber).connectShapesViaConnector(process3, ConnectionPointPlace.BOTTOM, | |
process4, ConnectionPointPlace.TOP, connecterId3); | |
long connecterId4 = diagram.addShape(new Shape(), connectorMaster, 0); | |
diagram.getPages().getPage(pageNumber).connectShapesViaConnector(decision1, ConnectionPointPlace.RIGHT, | |
process4, ConnectionPointPlace.TOP, connecterId4); | |
// Set automatic layout options | |
com.aspose.diagram.LayoutOptions layoutOptions = new com.aspose.diagram.LayoutOptions(); | |
layoutOptions.setLayoutStyle(com.aspose.diagram.LayoutStyle.FLOW_CHART); | |
layoutOptions.setDirection(com.aspose.diagram.LayoutDirection.BOTTOM_TO_TOP); diagram.layout(layoutOptions); | |
com.aspose.diagram.DiagramSaveOptions options = new com.aspose.diagram.DiagramSaveOptions(com.aspose.diagram.SaveFileFormat.VSDX); | |
diagram.save( "flowchart.vsdx", options); | |
System.out.println("Done"); | |
} | |
} |
Questo frammento di codice è una versione base per disegnare un diagramma di flusso in Java. La classe Diagram viene utilizzata per caricare alcune forme master, specificando il layout ed esportando il diagramma di flusso di output. Tuttavia, alcune classi personalizzate vengono utilizzate anche per creare connettori diversi e varie forme, che puoi improvvisare ulteriormente per soddisfare le tue esigenze.
Questo tutorial ha raccolto le informazioni per creare un costruttore di diagrammi di flusso in Java. Inoltre, se hai bisogno di convertire immagini in diagrammi Visio, vai all’articolo su come convertire l’immagine in Visio in Java.