Commit e5ffeb74 authored by stram's avatar stram
Browse files

Graphs can now be saved as jpg.

git-svn-id: https://dynaq.opendfki.de/repos/application/trunk@1046 8b50620d-ddf8-0310-9f5d-8b2f31fd9ac4
parent 02ff7ca6
......@@ -4,19 +4,26 @@ import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileFilter;
import java.io.FileWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
......@@ -24,6 +31,7 @@ import javax.swing.JSlider;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.dynaq.core.DynaQDocument;
import org.dynaq.core.DynaQResultList;
......@@ -71,6 +79,7 @@ public class DynaQDocumentGraph extends JPanel implements ActionListener, Compon
JPanel graphPanel = null;
JSlider slider;
JButton graph_button = null;
JButton save_button = null;
private Rectangle2D rectangle;
private String baseTitle;
......@@ -87,8 +96,10 @@ public class DynaQDocumentGraph extends JPanel implements ActionListener, Compon
final static String LAYOUT = "layout";
final static String EDGE_COUNT = "edge_count";
final static String GET_GRAPH = "get_graph";
final static String SAVE_GRAPH = "save_graph";
final static String BUTTON_TEXT = "Display graph";
final static String SAVE_BUTTON_TEXT = "Save graph";
final static String SAVE_DIR = "f:\\snapshot.jpg";
public DynaQDocumentGraph(){
......@@ -113,7 +124,14 @@ public class DynaQDocumentGraph extends JPanel implements ActionListener, Compon
graph_button.setActionCommand(GET_GRAPH);
graph_button.addActionListener(this);
}
if(save_button==null){
save_button = new JButton(SAVE_BUTTON_TEXT);
save_button.setActionCommand(SAVE_GRAPH);
save_button.addActionListener(this);
save_button.setEnabled(false);
}
buttonsPanel.add(graph_button);
buttonsPanel.add(save_button);
this.add(buttonsPanel, BorderLayout.NORTH);
} else {
......@@ -202,11 +220,45 @@ public class DynaQDocumentGraph extends JPanel implements ActionListener, Compon
showGraph(null);
}
if(strCommand.equals(SAVE_GRAPH)){
saveGraph();
}
} catch (Exception ex){
throw new RuntimeException(ex);
}
}
private void saveGraph(){
if(graphPanel==null){
JOptionPane.showMessageDialog(this, "No graph available");
} else {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");
chooser.setFileFilter(filter);
int retrival = chooser.showSaveDialog(this);
if (retrival == JFileChooser.APPROVE_OPTION) {
try {
Dimension size = graphPanel.getSize();
BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = image.createGraphics();
graphPanel.paint(g2);
try
{
ImageIO.write(image, "jpg", new File(chooser.getSelectedFile()+".jpg"));
}
catch(Exception e)
{
e.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
//returns the first edge with the same nodes and same date
private Edge getSimilarEdge(Graph graph, Edge edge, Date date){
Node source = edge.getSourceNode();
......@@ -488,6 +540,9 @@ public class DynaQDocumentGraph extends JPanel implements ActionListener, Compon
this.add(graphPanel, BorderLayout.CENTER);
this.setVisible(true); // show the window
//enabling the save button
save_button.setEnabled(true);
graph_visualization.run(COLOR);
graph_visualization.run(LAYOUT);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment