Question

I am looking for APIs that can draw UML Class diagrams and present them in a JPanel (or any other suitable UI entity) for a window application. It has to be embedded within the application, so I am not looking for some standalone tool that can generate UMLs based on java files or some plugin. I need actual jars that can be implemented for creating class diagrams so I can use them in a window application. I've looked into several but all of the sources that I am finding are either standalone programs or cannot be implemented within an application and need to take the user's focus away from the app. I am using NetBeans IDE, but I also have Eclipse installed.

SOLVED:

I used PlantUML API. I manually input a string in accordance with the PlantUML input language syntax and then used a simple and straightforward generateImage method to populate a byte array which I then converted into an image and saved it to my desktop. This fits what I wanted because it keeps the user focused on my application and mine alone. Alternatively, one can produce the buffered image on a window or something. The PlantUML API needs to be imported to the application package. This code creates an image in my desktop (don't forget to change the directory path) with a UML class image for the class, 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 = "@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);
}
}
Was it helpful?

Solution

Have you seen PlantUML?

http://plantuml.sourceforge.net

It's open source so you might be able to pick some bits to suit.

OTHER TIPS

Have a look at the Eclipse UML2 API and Eclipse Papyrus. They should provide the functionality what your are searching for. For drawing support in JPanels, you might need to do some extra work.

The Eclipse UML2 API provides a Java interface for UML2 meta model. Papyrus is a set of components allowing to build diagrams and graphical editors for UML models.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top