我正在寻找可以绘制UML类图并将其显示在用于窗口应用程序的JPanel(或任何其他合适的UI实体)中的API.它必须嵌入到应用程序中,所以我没有在寻找能够基于Java文件或某些插件生成UML的独立工具.我需要可以用于创建类图的实际jar,以便可以在窗口应用程序中使用它们.我已经研究了几种,但我发现的所有来源都是独立程序,或者无法在应用程序中实现,因此需要将用户的注意力从应用程序中移开.我正在使用NetBeans IDE,但也安装了Eclipse.
解决了:
我使用了PlantUML API.我根据PlantUML输入语言语法手动输入了一个字符串,然后使用一个简单直接的generateImage方法来填充一个字节数组,然后将其转换为图像并将其保存到我的桌面.这符合我的需求,因为它使用户始终专注于我的应用程序和我的应用程序.或者,可以在窗口或其他物体上生成缓冲的图像.需要将PlantUML API导入到应用程序包中.这段代码在我的桌面上创建了一个图像(不要忘记更改目录路径),该图像带有用于Person类的UML类图像:
public class PaintUML {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException, InterruptedException {
// TODO code application logic here
ByteArrayOutputStream bous = new ByteArrayOutputStream();
String source = "@startuml
";
source += "class Person {
";
source += "String name
";
source += "int age
";
source += "int money
";
source += "String getName()
";
source += "void setName(String name)
";
source += "}
";
source += "@enduml
";
SourceStringReader reader = new SourceStringReader(source);
// Write the first image to "png"
String desc = reader.generateImage(bous);
// Return a null string if no generation
byte [] data = bous.toByteArray();
InputStream in = new ByteArrayInputStream(data);
BufferedImage convImg = ImageIO.read(in);
ImageIO.write(convImg, "png", new File("C:\Users\Aaron\Desktop\image.png"));
System.out.print(desc);
}
}