Reputation: 85
edit "second version"
I have building an Java swing using ANTLR to get the tree of source code. For get the tree image that build from ANTLR, I am using GRAPHVIZ, http://www.graphviz.org/ .
So, Graphviz will be write to a file that have extension .dot and then I will load it into my Swing application. My code is like this
String OS = System.getProperty("os.name");
Runtime rt = Runtime.getRuntime();
BufferedWriter bw = null;
File tempDir = new File(System.getProperty("java.io.tmpdir"));
File tempInput;
try {
tempInput = File.createTempFile("output", ".dot", tempDir);
File tempOutput = File.createTempFile("tree", ".png", tempDir);
if (OS.equals("Linux")) {
try {
bw = new BufferedWriter(new FileWriter(tempInput.getAbsoluteFile()));
bw.write(st2);
bw.close();
Process pr = rt.exec("dot -Tpng /tmp/output.dot -o "
+ "/tmp/tree.png");
} catch (IOException ex) {
System.out.println("failed to write the image file");
}
} else {
try {
bw = new BufferedWriter(new FileWriter(tempInput.getAbsoluteFile()));
bw.write(st2);
bw.close();
String dotPath = "C:\\Program Files (x86)\\Graphviz2.38\\bin\\dot.exe";
String fileInputPath = tempInput.toString();
String fileOutputPath = tempOutput.toString();
String tParam = "-Tpng";
String tOParam = "-o";
String[] cmd = new String[5];
cmd[0] = dotPath;
cmd[1] = tParam;
cmd[2] = fileInputPath;
cmd[3] = tOParam;
cmd[4] = fileOutputPath;
rt.exec(cmd);
} catch (IOException ex) {
System.out.println("Failed to write to file");
} finally {
}
}
} catch (IOException ex) {
Logger.getLogger(MainAlgoritma.class.getName()).log(Level.SEVERE, null, ex);
}
you know, the output in windows should be named tree.png. But windows not gives me that name. The name is dynamically change like tree2593490478729479216.png and sometimes like tree9133268802668231475.png and etc .
My question is :
how to delete the image after process after the image is read and loaded to app ?
edit
Now, how to read that image and then load it into app... ? I make a class again to load the image, but you know, I am still confused.
private BufferedImage image;
String tmpfolder = System.getProperty("java.io.tmpdir");
public FileImage() {
if (OS.equals("Linux")) {
try {
image = ImageIO.read(new File(tmpfolder+"/tree.png"));
} catch (IOException ex) {
System.out.println("Image failed to load..!!! ");
}
} else {
try {
image = ImageIO.read(new File(tmpfolder+"\\tree.png"));
} catch (IOException ex) {
System.out.println("Image failed to load...!!! ");
}
}
JLabel jLabel = new JLabel(new ImageIcon(image));
jPanel3.add(jLabel);
}
Upvotes: 0
Views: 219
Reputation: 36611
how to get the name just tree.png
You should read the javadoc of the createTempFile
method you are using. It clearly states that the strings you pass in as parameters are a prefix and suffix.
If you replace
File.createTempFile("output", ".dot", tempDir);
by
tempInput = new File( tempDir, "output.dot" );
tempInput.createNewFile();
it should work.
Note how I used the File
constructor taking two arguments. This avoids the ugly switch statement on the OS you are using.
Upvotes: 0
Reputation: 3656
Instead of hard-coding an absolute path, you can use relative paths or use a JFileChooser
in Swing to open a file chooser that will let the user select a file for input and/or for output.
Upvotes: 1