因为我要用JGraph生成一副图片,在网页上显示,但是实在找不到这方面的资料,特来向各位高人求助~在官方手册上倒是有一段说明和例子:
Using the various image processing functionality available in Java, it is relatively simple
to produce an image of your graph in JPEG, bitmap (.bmp) or Portable Network Graphics
(.png) format. A utility method, getImage() is provided in the JGraph class to make
Page 98
JGraph User Manual
exporting a simple task. getImage() takes two parameters, the first is the background
color of the output image and the second is any inset to be use around every side of image
produced.
The background color, you may wish to simply be the background color of the graph, but
for the PNG output format there is the option of a transparent background. In the example
below you need to use your own graph, your own output stream and select an appropriate
background, but otherwise this code should work for all cases:
JGraph graph = getGraph(); // Replace with your own graph instance
OutputStream out = getOutputStream(); // Replace with your output stream
Color bg = null; // Use this to make the background transparent
bg = graph.getBackground(); // Use this to use the graph background
color
BufferedImage img = graph.getImage(bg, inset);
ImageIO.write(img, ext, out);
out.flush();
out.close();
但是请问这个 graph.getImage(bg, inset)方法中的inset是什么参数?ImageIO.write(img, ext, out)中ext呢?查他的API也没看懂

解决方案 »

  1.   

     ImageIO.write(img, ext, out); 是java自带的库里的,直接看文档就可以了 
    javax.imageio.ImageIO
      

  2.   


    非常感谢~!非常感谢~!非常感谢~!非常感谢~!我之前也查过这段了,上面说ext 指的是formatName - 包含格式的非正式名称的 String  我以为是指包含扩展名的文件名字,比如“1.jpg”但是不能确定,因为跑到BufferedImage img = graph.getImage(bg, inset); 这段就出错了T_T   请教我猜的是否正确?
    顺便再次请问这里的inset代表什么含义呢?
      

  3.   

    呵呵~看到ZangXT 大大在web区那边的回答了,我试过将那个inset用数字代替,不知为何全在BufferedImage img = graph.getImage(bg, inset); 
    这行报java.lang.NullPointerException 下面是我写的:
             JGraph graph = TopologyMaker(5); //通过这个方法获得graph         Color bg = graph.getBackground();
            FileOutputStream out = null;
            try
            {
                out=new FileOutputStream("D:\\testimg\\7.jpg");
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            } 
            BufferedImage img = graph.getImage(Color.WHITE,1);
            try
            {
                ImageIO.write(img, "7.jpg", out);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }不知道是不是我写的代码问题、、、 
      

  4.   

    BufferedImage img = graph.getImage(Color.WHITE,1); 这句话能有什么问题?
    奇怪,graph确定不为null吧
      

  5.   

    不好意思还真是调用失败了graph的确是空的
    代码直接输出界面显示没问题,改为直接生成图片却不能调用奇怪?import java.awt.BorderLayout;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.geom.Rectangle2D;
    import java.awt.image.BufferedImage;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Color;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;import java.util.Hashtable;
    import java.util.Map;import javax.imageio.ImageIO;
    import javax.swing.BorderFactory;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;import org.jgraph.JGraph;
    import org.jgraph.graph.BasicMarqueeHandler;
    import org.jgraph.graph.ConnectionSet;
    import org.jgraph.graph.DefaultEdge;
    import org.jgraph.graph.DefaultGraphCell;
    import org.jgraph.graph.DefaultGraphModel;
    import org.jgraph.graph.DefaultPort;
    import org.jgraph.graph.GraphConstants;
    import org.jgraph.graph.GraphModel;public class topology {
        /** *//**
         * 
         */
        public static void main(String[] args) {
            JFrame frame = new topologyFrame(7);
            
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
     
        }
    }/** *//**
     * 主画面
     */
    class topologyFrame extends JFrame {
        private static final int DEFAULT_WIDTH = 1024;
        private static final int DEFAULT_HEIGHT = 768;    public topologyFrame(int iNumber) {
            this.setTitle("拓扑图");
            this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);        final topologyPanel panel = new topologyPanel(iNumber);
            this.add(panel, BorderLayout.CENTER);    }
    }/** *//**
     * 拓扑图面板
     */
    class topologyPanel extends JPanel {
        private static final int PANEL_WIDTH = 1024;
        private static final int PANEL_HEIGHT = 768;
        private static final int SERVER_WIDTH = 110;
        private static final int SERVER_HEIGHT = 115;
        private static final int LINE_LENGTH = 250;
        private static final String IMAGE_DIR = "D:\\testimg\\4.jpg";
        private static final String IMAGE_DIR2 = "D:\\testimg\\5.jpg";    public topologyPanel(int iServerNum) {
            setSize(PANEL_WIDTH, PANEL_HEIGHT);
            this.add(new JScrollPane(setTopologyMaker(iServerNum)));
            
        }    /** *//**
         * 通过点坐标画线
         */
        private JGraph setTopologyMaker(int iServer) {        GraphModel model = new DefaultGraphModel();
            Object[] cells = null;
            final JGraph graph = new JGraph(model);
            graph.setSelectionEnabled(true);
            graph.addMouseListener(new MouseEventDemo());
            Map attributes = new Hashtable();
            Map LocServerattrib = new Hashtable();
            Icon icon1 = new ImageIcon(IMAGE_DIR);
            Icon icon2 = new ImageIcon(IMAGE_DIR2);
            double iAngle = 0;        graph.setMinimumSize(new Dimension(PANEL_WIDTH, PANEL_HEIGHT));
            graph.setAutoResizeGraph(false);
            graph.setSizeable(false);
            graph.setLocation(0, 0);
            graph.setSize(PANEL_WIDTH, PANEL_HEIGHT);        // 创建LOCAL SERVER对象及属性
            DefaultGraphCell LocServer = new DefaultGraphCell("LOCAL SERVER");
            attributes.put(LocServer, LocServerattrib);
            GraphConstants.setIcon(LocServerattrib, icon1);
            GraphConstants.setBorder(LocServerattrib, BorderFactory
                      .createCompoundBorder());
            GraphConstants.setEditable(LocServerattrib, false);        // 中心显示本地SERVER
            attributes.put(LocServer, LocServerattrib);
            Rectangle2D LocServerbounds = new Rectangle2D.Double(graph.getWidth() / 2
                      - SERVER_WIDTH / 2, graph.getHeight() / 2 - SERVER_HEIGHT / 2,
                    SERVER_WIDTH, SERVER_HEIGHT);
            GraphConstants.setBounds(LocServerattrib, LocServerbounds);
            GraphConstants.setValue(LocServerattrib, "LOCAL SERVER");
            
            GraphConstants.setFont(LocServerattrib, new Font(null, Font.BOLD, 10));        DefaultPort LocPort = new DefaultPort();
            LocServer.add(LocPort);        cells = new Object[] { LocServer };
            model.insert(cells, attributes, null, null, null);        if (iServer != 0) {
                iAngle = 360 / iServer;
            } else {
                iAngle = 0;
            }        for (int i = 0; i < iServer; i++) {
                double x;
                double y;            x = graph.getWidth()
                          / 2
                          + (java.lang.Math.cos(((iAngle) * i + 45)
                                  * java.lang.Math.PI / 180) * LINE_LENGTH)
                          - SERVER_WIDTH / 2;
                y = graph.getHeight()
                          / 2
                          - (java.lang.Math.sin(((iAngle) * i + 45)
                                  * java.lang.Math.PI / 180) * LINE_LENGTH)
                          - SERVER_HEIGHT / 2;            // 创建PEER SERVER对象及属性
                DefaultGraphCell peerServer = new DefaultGraphCell("PEER SERVER"
                          + (i + 1));
                Map PeerServerattrib = new Hashtable();
                attributes.put(peerServer, PeerServerattrib);
                GraphConstants.setIcon(PeerServerattrib, icon2);
                GraphConstants.setBorder(PeerServerattrib, BorderFactory
                          .createCompoundBorder());
                GraphConstants.setEditable(PeerServerattrib, false);            attributes.put(peerServer, PeerServerattrib);            Rectangle2D PeerMTAbounds = new Rectangle2D.Double(x, y, SERVER_WIDTH,
                        SERVER_HEIGHT);
                // System.out.println("(" + x + "," + y + ")");
                GraphConstants.setBounds(PeerServerattrib, PeerMTAbounds);
                GraphConstants.setValue(PeerServerattrib, "PEER SERVER" + (i + 1));
                GraphConstants
                          .setFont(PeerServerattrib, new Font(null, Font.BOLD, 10));            DefaultPort PeerPort = new DefaultPort();
                peerServer.add(PeerPort);            // 创建边对象及属性
                DefaultEdge edge = new DefaultEdge();
                Map edgeattrib = new Hashtable();
                attributes.put(edge, edgeattrib);
                int arrow = GraphConstants.ARROW_CLASSIC;
                GraphConstants.setLineEnd(edgeattrib, arrow);
                GraphConstants.setEditable(edgeattrib, false);
                GraphConstants.setLabelAlongEdge(edgeattrib, true);
                GraphConstants.setEndFill(edgeattrib, true);
                GraphConstants.setDisconnectable(edgeattrib, false);
                GraphConstants.setValue(edgeattrib, "R" + (i + 1));
                GraphConstants.setFont(edgeattrib, new Font(null, Font.BOLD, 10));
                GraphConstants.setLineWidth(edgeattrib, 1.5f);            // 连接边的两个节点
                ConnectionSet cs = new ConnectionSet(edge, PeerPort,LocPort );
                cells = new Object[] { edge, peerServer };            // 向model添加cells对象
                model.insert(cells, attributes, cs, null, null);
                
                
            }
            return graph;
        }
    }
    class MouseEventDemo implements MouseListener {    public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
                Object[] obj = BasicMarqueeHandler.getGraphForEvent(e)
                          .getSelectionCells();
                if (obj.length != 0) {
                    System.out.println(obj[obj.length - 1].toString());
                }
            }
        }    public void mouseEntered(MouseEvent e) {
                }    public void mouseExited(MouseEvent e) {
                }    public void mousePressed(MouseEvent e) {
                }    public void mouseReleased(MouseEvent e) {
            
        }
    }这个直接执行没有问题,但把setTopologyMaker方法单独拿出来和上面我写的调用的话,就报NULL,奇怪
    能不能帮忙看看?是不是需要修改哪些地方?