By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,594 Members | 3,680 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,594 IT Pros & Developers. It's quick & easy.

Need a help in prefuse visulation graph save as jpeg file

P: 4
Hi Frnd's,

m using prefuse visulation,it's have one display class and this class have one saveImage(outPutStream, String jpg,double size);. now graph is converting ia jpg image properly.now my problem is tht,If graph is to large if it going out of screen thn ,i m getting jpg image on screen disply graph,m not getting the image of tht graph which going out of screen. this is my code



This is my code



Expand|Select|Wrap|Line Numbers
  1.  import com.sun.corba.se.impl.orbutil.graph.Node;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dimension;
  4. import java.awt.Point;
  5. import java.awt.dnd.DragSource;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.awt.event.ItemEvent;
  9. import java.awt.event.MouseAdapter;
  10. import java.awt.event.MouseEvent;
  11. import java.awt.event.MouseListener;
  12. import java.awt.event.WindowAdapter;
  13. import java.awt.event.WindowEvent;
  14. import java.awt.event.WindowListener;
  15. import java.awt.image.BufferedImage;
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import java.util.Iterator;
  21. import javax.crypto.NullCipher;
  22. import javax.swing.JButton;
  23. import javax.swing.JFrame;
  24. import javax.swing.JOptionPane;
  25. import javax.swing.border.Border;
  26.  
  27. import prefuse.Constants;
  28. import prefuse.Display;
  29. import prefuse.Visualization;
  30. import prefuse.action.Action;
  31. import prefuse.action.ActionList;
  32. import prefuse.action.RepaintAction;
  33. import prefuse.action.assignment.ColorAction;
  34. import prefuse.action.assignment.DataColorAction;
  35. import prefuse.action.layout.graph.ForceDirectedLayout;
  36. import prefuse.activity.Activity;
  37. import prefuse.activity.ActivityAdapter;
  38. import prefuse.activity.ActivityListener;
  39. import prefuse.controls.DragControl;
  40. import prefuse.controls.PanControl;
  41. import prefuse.controls.ToolTipControl;
  42. import prefuse.controls.ZoomControl;
  43. import prefuse.controls.FocusControl;
  44. import prefuse.controls.DragControl;
  45. import prefuse.data.Graph;
  46. import prefuse.data.Table;
  47. import prefuse.data.Tuple;
  48. import prefuse.data.io.DataIOException;
  49. import prefuse.data.io.GraphMLReader;
  50. import prefuse.data.io.GraphWriter;
  51. import prefuse.data.tuple.TupleSet;
  52. import prefuse.render.DefaultRendererFactory;
  53. import prefuse.render.LabelRenderer;
  54. import prefuse.render.ShapeRenderer;
  55. import prefuse.util.ColorLib;
  56. import prefuse.util.force.DragForce;
  57. import prefuse.visual.NodeItem;
  58. import prefuse.visual.VisualItem;
  59. import javax.swing.*;
  60. import java.awt.Container;
  61. import java.io.FileInputStream;
  62. import java.awt.Canvas;
  63. import java.awt.event.WindowAdapter;
  64. import java.awt.event.WindowListener;
  65. import java.awt.Image;
  66. import java.util.Set;
  67. class DrawGrahp extends JFrame{
  68.  
  69. private FileOutputStream fileOutPutStream=null;
  70. private JButton button;
  71. private JFrame frame;
  72. private Visualization vis;
  73. private LabelRenderer r;
  74. private ActionList layout;
  75. private ActionList color;
  76. private Display d ;
  77. private int a,b;
  78. private Graph graph = null;
  79. private JPanel jpanel;
  80. public DrawGrahp(){
  81. jpanel=new JPanel();
  82.  
  83.  
  84. button=new JButton("Save");
  85. Container con= getContentPane();
  86. jpanel.setLayout(null);
  87. button.setBounds(200,300,70,40);
  88. con.setSize(500,600);
  89. jpanel.setSize(1000,800);
  90. final int l= jpanel.getHeight();
  91. final int m=jpanel.getWidth();
  92. Canvas canvas=new Canvas();
  93.  
  94. jpanel.add(button);
  95.  
  96. con.setVisible(true);
  97. jpanel.setDoubleBuffered(true);
  98. jpanel.setPreferredSize(new Dimension(500,600));
  99. Point location=new Point();
  100.  
  101.  
  102. System.out.println("Panel Size"+ jpanel.getSize());
  103. System.out.println("panel height----------"+jpanel.getHeight()+"Panel width-------------"+jpanel.getWidth());
  104. con.add(jpanel,BorderLayout.NORTH);
  105.  
  106.  
  107. try {
  108. graph = new GraphMLReader().readGraph("\\JavaApplicationSwing\\graph.xml");
  109. } catch (DataIOException e ) {
  110. e.printStackTrace();
  111. System.err.println("Error loading graph. Exiting...");
  112. System.exit(1);
  113. }
  114.  
  115.  
  116.  
  117.  
  118. // nodes and edges are accessible as "graph.nodes" and "graph.edges"
  119. vis = new Visualization();
  120. vis.add("graph", graph);
  121.  
  122. vis.setInteractive("graph.edges", null, false);
  123.  
  124. // -- 3. the renderers and renderer factory ---------------------------
  125.  
  126.  
  127. r = new LabelRenderer("name");
  128. r.setRoundedCorner(8, 8); // round the corners
  129. ShapeRenderer sh=new ShapeRenderer();
  130. sh.star(2,3,4);
  131. sh.setBaseSize(4);
  132.  
  133. // return our name label renderer as the default for all non-EdgeItems
  134. // includes straight line edges for EdgeItems by default
  135. vis.setRendererFactory(new DefaultRendererFactory(r));
  136. d = new Display(vis);
  137.  
  138. // -- 4. the processing actions ---------------------------------------
  139.  
  140.  
  141.  
  142. int[] palette = new int[] {
  143. ColorLib.rgb(255,180,180), ColorLib.rgb(190,190,255)
  144.  
  145. };
  146.  
  147. // map nominal data values to colors using our provided palette
  148. DataColorAction fill = new DataColorAction("graph.nodes", "gender",
  149. Constants.NOMINAL, VisualItem.FILLCOLOR, palette);
  150.  
  151.  
  152. ColorAction text = new ColorAction("graph.nodes",
  153. VisualItem.TEXTCOLOR, ColorLib.gray(0));
  154.  
  155. if(text.equals("sriniwas"))
  156. {
  157. new ColorAction("graph.nodes",VisualItem.TEXTCOLOR, ColorLib.green(240));
  158. }
  159. ColorAction edges = new ColorAction("graph.edges",
  160. VisualItem.STROKECOLOR, ColorLib.gray(200));
  161.  
  162.  
  163. color = new ActionList();
  164. color.add(fill);
  165.  
  166. color.add(text);
  167. color.add(edges);
  168. int aa;
  169. for(int i=0;i<color.size();i++){
  170.  
  171.  
  172. System.out.println("sdhfshdfhsf" +i);
  173. }
  174.  
  175. layout = new ActionList(Activity.INFINITY);
  176. layout.add(new ForceDirectedLayout("graph"));
  177. layout.add(new RepaintAction());
  178.  
  179.  
  180.  
  181.  
  182.  
  183. vis.putAction("color", color);
  184. vis.putAction("layout", layout);
  185.  
  186.  
  187. // -- 5. the display and interactive controls -------------------------
  188. d = new Display(vis);
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195. // d.setSize(1000, 600); // set display size
  196.  
  197.  
  198. d.addControlListener(new DragControl());
  199. d.addControlListener(new PanControl());
  200. d.addControlListener(new ZoomControl());
  201. d.addControlListener(new FrameFocusControl());
  202. // d.addControlListener(new ToolTipControl(){
  203.  
  204.  
  205. //d.addControlListener(new DragSource());
  206.  
  207.  
  208. // -- 6. launch the visualization -------------------------------------
  209.  
  210. jpanel.add(d);
  211. vis.run("color");
  212. vis.run("layout");
  213.  
  214.  
  215.  
  216.  
  217. button.addActionListener(new ActionListener(){
  218. public void actionPerformed(ActionEvent e){
  219. if(e.getSource()==button){
  220. try{
  221.  
  222.  
  223.  
  224.  
  225. jpanel.setSize(new Dimension(l*10,m*10));
  226. System.out.println("Panel Hieght"+jpanel.getHeight());
  227. System.out.println("Panel width"+jpanel.getWidth());
  228. Image img =d.getOffscreenBuffer();
  229. img.getSource();
  230. System.out.println("A fter increase Panel Size"+ jpanel.getSize());
  231. fileOutPutStream=new FileOutputStream("c:/sri/img.jpg");
  232. d.saveImage(fileOutPutStream,"jpg",2.6);
  233. fileOutPutStream.flush();
  234. fileOutPutStream.close();
  235.  
  236. System.out.println("------------------"+jpanel.getHeight());
  237. }catch(IOException ex){ex.printStackTrace();}
  238.  
  239. }
  240. }
  241. });
  242.  
  243. }
  244.  
  245.  
  246.  
  247. }
  248.  
  249.  
  250. public class DrawGrahpDemo{
  251. public static void main(String Arg[]){
  252.  
  253. DrawGrahp drawGraph=new DrawGrahp();
  254.  
  255. drawGraph.setVisible(true);
  256. drawGraph.setSize(500,600);
  257. drawGraph.setDefaultCloseOperation(JFrame.EXIT_ON_ CLOSE);
  258.  
  259. }
  260. }
  261.  
  262.  
it's very importtant for me, plz help me.
Jan 12 '08 #1
Share this Question
Share on Google+
2 Replies


RedSon
Expert 5K+
P: 5,000
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Jan 14 '08 #2

P: 1
[quote=sriniwas]Hi Frnd's,

m using prefuse visulation,it's have one display class and this class have one saveImage(outPutStream, String jpg,double size);. now graph is converting ia jpg image properly.now my problem is tht,If graph is to large if it going out of screen thn ,i m getting jpg image on screen disply graph,m not getting the image of tht graph which going out of screen. this is my code

Hello sriniwas,

I had the same problem and I solved it by creating a new temporary display where I scaled the visualization onto the size of the created display. After this I am able to save a complete image of the visualized graph. The other possibility is to scale your actual display onto the size of your visualization while saving an image and decreasing it after that.
If there are other possibilities I would be glad to read about.
Feb 29 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.