Created
March 21, 2021 07:27
-
-
Save diagramsascode/90b1617830e66a7c3cbec1d7c085f6f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///usr/bin/env jbang "$0" "$@" ; exit $? | |
//REPOS mavencentral | |
//DEPS org.diagramsascode:diagramsascode-image:0.1.2 | |
import org.diagramsascode.core.Diagram; | |
import org.diagramsascode.image.SequenceDiagramImage; | |
import org.diagramsascode.sequence.constraint.SequenceDiagramConstraints; | |
import org.diagramsascode.sequence.edge.Message; | |
import org.diagramsascode.sequence.node.Participant; | |
import java.io.File; | |
import javax.swing.JFrame; | |
import javax.swing.ImageIcon; | |
import javax.swing.JLabel; | |
class DiagramGenerator{ | |
public static void main(String[] args) throws Exception | |
{ | |
// Create the participants (that exchange messages) | |
var participant1 = new Participant("Client"); | |
var participant2 = new Participant("Server"); | |
// Create the request and response messages | |
var message1 = new Message(participant1, participant2, "Request Message"); | |
var message2 = new Message(participant2, participant1, "Response Message"); | |
var diagram = Diagram.builder() | |
.withNodes(participant1, participant2) | |
.withEdges(message1, message2) | |
.withConstraints(new SequenceDiagramConstraints()) | |
.build(); | |
var outputFile = File.createTempFile("sequence", ".png"); | |
SequenceDiagramImage.of(diagram).writeToPngFile(outputFile); | |
System.out.println(outputFile); | |
// Show the image using Java Swing. | |
// This could be extracted to a library. | |
JFrame frame = new JFrame(); | |
ImageIcon icon = new ImageIcon(outputFile.getAbsolutePath()); | |
JLabel label = new JLabel(icon); | |
frame.add(label); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment