我正在寻找能够绘制UML类图并在JPanel(或任何其他适合的UI实体)中呈现它们的API,用于窗口应用程序。它必须嵌入到应用程序中,因此我不需要一些可以基于Java文件生成UML的独立工具或某些插件。我需要实际的jar文件,以便创建类图,这样我就可以在窗口应用程序中使用它们。我已经调查了几个,但是我发现所有的来源都是独立的程序,或者无法在应用程序中实现,并且需要将用户的注意力从应用程序中转移开来。我正在使用NetBeans IDE,但我也安装了Eclipse。
解决方法:
我使用了PlantUML API。我手动输入符合PlantUML输入语言语法的字符串,然后使用一个简单明了的generateImage方法填充一个字节数组,然后将其转换为图像并保存到我的桌面上。这符合我的要求,因为它使用户集中精力在我的应用程序上。或者,可以在窗口或其他地方生成缓冲图像。 PlantUML API需要导入应用程序包。以下代码在我的桌面上创建了一个图像(不要忘记更改目录路径),其中包含Person类的UML类图像:
解决方法:
我使用了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\n";
source += "class Person {\n";
source += "String name\n";
source += "int age\n";
source += "int money\n";
source += "String getName()\n";
source += "void setName(String name)\n";
source += "}\n";
source += "@enduml\n";
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);
}
}