我正在寻找能够绘制UML类图并将其呈现在窗口应用程序的JPanel(或任何其他合适的UI实体)中的API。它必须嵌入到应用程序中,所以我不想寻找一些独立的工具,可以基于java文件或一些插件生成UML。我需要可以实现用于创建类图的实际jar,这样我就可以在窗口应用程序中使用它们。我已经研究了几个,但我发现的所有来源要么是独立的程序,要么无法在应用程序中实现,需要将用户的注意力从应用程序中转移开。我使用的是NetBeansIDE,但我也安装了Eclipse。
已解决:
我使用了PlantUML API。我根据PlantUML输入语言语法手动输入一个字符串,然后使用简单明了的generateImage方法填充一个字节数组,然后将其转换为图像并保存到桌面上。这符合我的要求,因为它让用户只关注我的应用程序和我的应用。或者,可以在窗口或其他东西上产生缓冲图像。PlantUML API需要导入到应用程序包中。这段代码在我的桌面上创建了一个图像(不要忘记更改目录路径),其中包含该类的UML类图像Person:
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 = "@startumln";
source += "class Person {n";
source += "String namen";
source += "int agen";
source += "int moneyn";
source += "String getName()n";
source += "void setName(String name)n";
source += "}n";
source += "@endumln";
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);
}
}
您看过PlantUML吗?
http://plantuml.sourceforge.net
它是开源的,所以你可以选择一些适合你的部分。
看看Eclipse UML2 API和Eclipse Papyrus。它们应该提供您正在搜索的功能。对于JPanels中的绘图支持,您可能需要做一些额外的工作。
EclipseUML2 API为UML2元模型提供了一个Java接口。Papyrus是一组组件,允许为UML模型构建图表和图形编辑器。