Bài viết này thảo luận cách tạo sơ đồ trong Java. Nó bao gồm thuật toán từng bước và một đoạn mã có thể chạy được để phát triển trình tạo sơ đồ trong Java. Hơn nữa, bạn có thể nâng cao quy trình bằng cách thao tác lược đồ, số lượng hình dạng, kết nối và các thuộc tính khác để tạo flowchart theo yêu cầu của bạn.
Các bước để tạo sơ đồ trong Java
- Chuẩn bị môi trường bằng cách cài đặt API Aspose.Diagram để tạo sơ đồ
- Phát triển lược đồ cho sơ đồ
- Tạo một đối tượng thuộc lớp Diagram và tải các hình dạng chính để chèn hình dạng
- Đặt bố cục của sơ đồ và xuất sơ đồ đã tạo bằng phương thức lưu Save
Các bước này thể hiện quy trình phát triển trình tạo sơ đồ trong Java. Đầu tiên, tạo một lược đồ để thiết lập các vai trò, chỉ định và thuộc tính khác nhau. Sau đó, sử dụng các hình dạng chính để thêm các hình dạng khác nhau như hình chữ nhật, đầu nối, v.v. trước khi thiết lập bố cục và lưu sơ đồ đầu ra.
Mã để tạo trình tạo sơ đồ trong 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"); | |
} | |
} |
Đoạn mã này là phiên bản cơ bản để vẽ sơ đồ trong Java. Lớp Sơ đồ được sử dụng để tải một số hình dạng chính, chỉ định bố cục và xuất sơ đồ đầu ra. Tuy nhiên, một số lớp tùy chỉnh cũng được sử dụng để tạo các đầu nối khác nhau và các hình dạng khác nhau mà bạn có thể ứng biến thêm để đáp ứng yêu cầu của mình.
Hướng dẫn này đã tổng hợp thông tin để tạo trình tạo biểu đồ luồng trong Java. Ngoài ra, nếu bạn cần chuyển đổi hình ảnh sang sơ đồ Visio thì hãy xem bài viết trên cách chuyển đổi hình ảnh sang Visio trong Java.