Created
April 21, 2017 14:24
-
-
Save EmilEriksen/a39a8ccc145f1299d01b3858ab6e0808 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import regaut.FA; | |
import javax.imageio.ImageIO; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
import java.io.*; | |
public class DotViewer { | |
private String dot; | |
public DotViewer(FA fa) { | |
this.dot = fa.toDot(); | |
} | |
public void view() { | |
try { | |
File dotFile = File.createTempFile("finite-automaton", ".dot"); | |
dotFile.deleteOnExit(); | |
File imageFile = File.createTempFile("finite-automaton", ".jpg"); | |
imageFile.deleteOnExit(); | |
BufferedWriter dotFileWriter = new BufferedWriter(new FileWriter(dotFile)); | |
dotFileWriter.write(this.dot); | |
dotFileWriter.close(); | |
// Virker sandsynligvis ikke på Windows. Virker måske med "cmd /c dot.exe ..." | |
executeCommand("dot -Tjpg -o " + imageFile.getAbsolutePath() + " " + dotFile.getAbsolutePath()); | |
BufferedImage image = ImageIO.read(imageFile); | |
JFrame frame = new JFrame(); | |
frame.setLayout(new FlowLayout()); | |
frame.setSize(image.getWidth() + 20,image.getHeight() + 30); | |
JLabel imageLabel = new JLabel(); | |
imageLabel.setIcon(new ImageIcon(image)); | |
frame.add(imageLabel); | |
frame.setLocationRelativeTo(null); | |
frame.setVisible(true); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private InputStream executeCommand(String command) { | |
final InputStream[] result = new InputStream[1]; | |
final Process p; | |
try { | |
p = Runtime.getRuntime().exec(command); | |
new Thread(new Runnable() { | |
public void run() { | |
result[0] = p.getInputStream(); | |
} | |
}).start(); | |
p.waitFor(); | |
} catch (IOException | InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return result[0]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For at vise en FA: