บทความนี้อธิบายวิธีการ สร้างผังงานใน Java ประกอบด้วยอัลกอริธึมทีละขั้นตอนและข้อมูลโค้ดที่รันได้เพื่อพัฒนา ตัวสร้างผังงานใน Java นอกจากนี้ คุณยังสามารถปรับปรุงกระบวนการได้โดยการจัดการสคีมา จำนวนรูปร่าง การเชื่อมต่อ และคุณสมบัติอื่นๆ เพื่อสร้าง flowchart ตามความต้องการของคุณ
ขั้นตอนในการสร้าง Flowchart ใน Java
- เตรียมสภาพแวดล้อมโดยการติดตั้ง Aspose.Diagram API เพื่อสร้างผังงาน
- พัฒนาสคีมาสำหรับผังงาน
- สร้างออบเจ็กต์ของคลาส Diagram และโหลดรูปร่างหลักสำหรับการแทรกรูปร่าง
- ตั้งค่าเค้าโครงของผังงานและส่งออกผังงานที่สร้างขึ้นโดยใช้วิธีบันทึก Save
ขั้นตอนเหล่านี้สาธิตกระบวนการพัฒนา ตัวสร้างผังงานใน Java ขั้นแรก สร้างสคีมาเพื่อกำหนดบทบาท การกำหนด และคุณสมบัติอื่นๆ จากนั้น ให้ใช้รูปร่างหลักเพื่อเพิ่มรูปร่างต่างๆ เช่น สี่เหลี่ยม ตัวเชื่อมต่อ ฯลฯ ก่อนที่จะตั้งค่าเค้าโครงและบันทึกผังงานเอาต์พุต
รหัสเพื่อสร้าง Flowchart Generator ใน 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"); | |
} | |
} |
ข้อมูลโค้ดนี้เป็นเวอร์ชันพื้นฐานสำหรับ วาดผังงานใน Java คลาส Diagram ใช้เพื่อโหลดรูปร่างหลักบางรูปร่าง โดยระบุเค้าโครงและส่งออกผังงานเอาต์พุต อย่างไรก็ตาม คลาสแบบกำหนดเองบางคลาสยังใช้เพื่อสร้างตัวเชื่อมต่อและรูปทรงที่แตกต่างกัน ซึ่งคุณสามารถปรับแต่งเพิ่มเติมได้เพื่อให้ตรงตามความต้องการของคุณ
บทช่วยสอนนี้ได้รวบรวมข้อมูลเพื่อสร้าง ตัวสร้างโฟลว์ชาร์ตใน Java นอกจากนี้ หากคุณต้องการแปลงรูปภาพเป็นไดอะแกรม Visio ให้ไปที่บทความเรื่อง วิธีแปลงรูปภาพเป็น Visio ใน Java