Skip to content

Commit cfab18a

Browse files
committed
add code for jgrapht image gen
1 parent 5afdce7 commit cfab18a

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

algorithms-miscellaneous-2/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
<artifactId>jgrapht-core</artifactId>
3434
<version>${org.jgrapht.core.version}</version>
3535
</dependency>
36+
<dependency>
37+
<groupId>org.jgrapht</groupId>
38+
<artifactId>jgrapht-ext</artifactId>
39+
<version>${org.jgrapht.ext.version}</version>
40+
</dependency>
3641
<dependency>
3742
<groupId>pl.allegro.finance</groupId>
3843
<artifactId>tradukisto</artifactId>
@@ -83,6 +88,7 @@
8388
<commons-math3.version>3.6.1</commons-math3.version>
8489
<tradukisto.version>1.0.1</tradukisto.version>
8590
<org.jgrapht.core.version>1.0.1</org.jgrapht.core.version>
91+
<org.jgrapht.ext.version>1.0.1</org.jgrapht.ext.version>
8692
<org.assertj.core.version>3.9.0</org.assertj.core.version>
8793
<commons-codec.version>1.11</commons-codec.version>
8894
</properties>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.jgrapht;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import java.awt.Color;
5+
import java.awt.image.BufferedImage;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import javax.imageio.ImageIO;
9+
import org.jgrapht.ext.JGraphXAdapter;
10+
import org.jgrapht.graph.DefaultDirectedGraph;
11+
import org.jgrapht.graph.DefaultEdge;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
import com.mxgraph.layout.mxCircleLayout;
15+
import com.mxgraph.layout.mxIGraphLayout;
16+
import com.mxgraph.util.mxCellRenderer;
17+
18+
public class GraphImageGenerationUnitTest {
19+
static DefaultDirectedGraph<String, DefaultEdge> g;
20+
21+
@Before
22+
public void createGraph() throws IOException {
23+
File imgFile = new File("src/test/resources/graph.png");
24+
imgFile.createNewFile();
25+
g = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
26+
String x1 = "x1";
27+
String x2 = "x2";
28+
String x3 = "x3";
29+
g.addVertex(x1);
30+
g.addVertex(x2);
31+
g.addVertex(x3);
32+
g.addEdge(x1, x2);
33+
g.addEdge(x2, x3);
34+
g.addEdge(x3, x1);
35+
}
36+
37+
@Test
38+
public void givenAdaptedGraph_whenWriteBufferedImage_ThenFileShouldExist() throws IOException {
39+
JGraphXAdapter<String, DefaultEdge> graphAdapter = new JGraphXAdapter<String, DefaultEdge>(g);
40+
mxIGraphLayout layout = new mxCircleLayout(graphAdapter);
41+
layout.execute(graphAdapter.getDefaultParent());
42+
File imgFile = new File("src/test/resources/graph.png");
43+
BufferedImage image = mxCellRenderer.createBufferedImage(graphAdapter, null, 2, Color.WHITE, true, null);
44+
ImageIO.write(image, "PNG", imgFile);
45+
assertTrue(imgFile.exists());
46+
}
47+
}
Loading

0 commit comments

Comments
 (0)