萍聚社区-德国热线-德国实用信息网

 找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

12
返回列表 发新帖
楼主: yiyiyaya^^

求教如何把JAVA运行的程序结果生成图表

[复制链接]
发表于 2007-3-30 00:11 | 显示全部楼层
原帖由 yiyiyaya^^ 于 2007-3-29 23:51 发表


那个好像不能这样储存吧,要定义成png格式或者jpg吧。不知道怎么调用阿,用google没有查到,$郁闷$

可以存成PNG啊~难道我又记错了~:(
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
发表于 2007-3-30 16:40 | 显示全部楼层
原帖由 yiyiyaya^^ 于 2007-3-29 23:21 发表
我用jfreechart导出了两个统计图了。哈哈!

不过不知道能不能把跳出的Fester所显示的图像储存 $frage$

应该是可以的.而且可以根据自己设定的图象格式(PNG,JPG,BMP等等)进行存储.
Beispiel:
import java.awt.*;
import java.awt.image.*;
import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

/**
* Wrapper class for an image that includes a rendered image (graphic content).
*
*
*/
@SuppressWarnings("serial")
public class Image
        implements Serializable
{
        /** The graphic content of the Image object */
        private transient BufferedImage content;

        /** Default width for new Image objects */
        private static int defaultWidth = 400;
        /** Default height for new Image objects */
        private static int defaultHeight = 400;

        /** Width of the Image object */
        private int width;
        /** Height of the Image object */
        private int height;
       
        /**
         * Creates a new image object with the default width and height.
         */
        public Image()
        {
                this(defaultWidth, defaultHeight);
        }

        /**
         * Creates a new image object with the specified width and height.
         *
         * @param width Width of the image object
         * @param height Height of the image object
         */
        public Image(int width, int height)
        {
                this.width = width;
                this.height = height;
                restoreContent();
        }
       
        /**
         * Sets the default image size for all new images, instanciated with no
         * dimension specification.
         *
         * @param dim Default dimension for new images
         */
        public static void setDefaultSize(Dimension dim)
        {
                defaultWidth = dim.width;
                defaultHeight = dim.height;
        }
       
        /**
         * Returns the graphics content of this image.
         */
        public BufferedImage getContent()
        {
                return content;
        }

        /**
         * Sets a new graphics content.
         *
         * @param content The new graphics content
         */
        public void setContent(BufferedImage content)
        {
                this.content = content;
        }

        /**
         * Returns the height of the image.
         */
        public int getHeight()
        {
                return height;
        }

        /**
         * Sets the height of the image.
         *
         * @param height The height of the image
         */
        public void setHeight(int height)
        {
                this.height = height;
        }

        /**
         * Returns the width of the image.
         */
        public int getWidth()
        {
                return width;
        }

        /**
         * Sets the width of the image.
         *
         * @param width The width of the image
         */
        public void setWidth(int width)
        {
                this.width = width;
        }
       
        /**
         * Restores the graphics content of the image by assigning it to a new
         * BufferedImage with the current dimensions.
         */
        public void restoreContent()
        {
                content = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        }

        /**
         * Dialog for selecting a target to save the rendered image of an
         * algorithm.
         */
        public static class SaveImageDialog
        {
                File returnFile = null;
                String returnTyp = null;
               
                /**
                 * Creates a new SaveImageDialog for the specified BufferedImage and
                 * saves the image if the OK button is pushed.
                 */
                public SaveImageDialog(BufferedImage image)
                {
                        JFileChooser fileChooser = new JFileChooser();
                        fileChooser.setAcceptAllFileFilterUsed(false);
                        FileFilter jpgFileFilter = new FileFilter()
                        {
                                public boolean accept(File f)
                                {
                                        return f.isDirectory() ||
                                                f.getName().toLowerCase().endsWith(".jpg");
                                }

                                public String getDescription()
                                {
                                        return "JPEG";
                                }
                        };
                        fileChooser.addChoosableFileFilter(jpgFileFilter);

                        FileFilter bmpFileFilter = new FileFilter()
                        {
                                public boolean accept(File f)
                                {
                                        return f.isDirectory() ||
                                                f.getName().toLowerCase().endsWith(".bmp");
                                }

                                public String getDescription()
                                {
                                        return "BMP";
                                }
                        };
                        fileChooser.addChoosableFileFilter(bmpFileFilter);
                       
                        FileFilter pngFileFilter = new FileFilter()
                        {
                                public boolean accept(File f)
                                {
                                        return f.isDirectory() ||
                                                f.getName().toLowerCase().endsWith(".png");
                                }

                                public String getDescription()
                                {
                                        return "PNG";
                                }
                        };
                        fileChooser.addChoosableFileFilter(pngFileFilter);

                        int state = fileChooser.showSaveDialog(null);
                       
                        if (state == JFileChooser.APPROVE_OPTION)
                        {
                                String typ = "bmp";
                               
                                if (fileChooser.getFileFilter()==jpgFileFilter)
                                        typ = "jpg";
                                else if (fileChooser.getFileFilter()==bmpFileFilter)
                                        typ = "bmp";
                                else if (fileChooser.getFileFilter()==pngFileFilter)
                                        typ = "png";

                                File file = fileChooser.getSelectedFile();

                                boolean hasEnd = true;
                                if (file.getAbsolutePath().toLowerCase().endsWith (".jpg"))
                                        typ = "jpg";
                                else if (file.getAbsolutePath().toLowerCase().endsWith (".png"))
                                        typ = "png";
                                else if (file.getAbsolutePath().toLowerCase().endsWith (".bmp"))
                                        typ = "bmp";
                                else
                                        hasEnd = false;
                       
                                returnTyp=typ;
                                String name = file.getPath();
                                try
                                {
                                        if (!hasEnd)
                                                returnFile = new File(name + "." + typ);
                                        else
                                                returnFile = file;

                                        if (image!=null)
                                                ImageIO.write(image, typ, returnFile);
                                }
                                catch (IOException ex)
                                {
                                }
                        }
                }
                public File getFile()
                {
                        return returnFile;
                }
               
                public String getTyp()
                {
                        return returnTyp;
                }
        }
}
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
发表于 2007-3-30 16:41 | 显示全部楼层
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
 楼主| 发表于 2007-3-30 16:49 | 显示全部楼层
谢谢谢谢斑斑的code,学妹这里有礼了! $送花$
再次谢谢ls热心提供宝贵信息的前辈们。

以后一定来我们的INFO天空多多学习。
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
发表于 2007-3-30 16:57 | 显示全部楼层
原帖由 yiyiyaya^^ 于 2007-3-30 16:49 发表
谢谢谢谢斑斑的code,学妹这里有礼了! $送花$
再次谢谢ls热心提供宝贵信息的前辈们。

以后一定来我们的INFO天空多多学习。

欢迎欢迎.有空常来.
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
头像被屏蔽

TA的专栏

发表于 2007-3-31 22:47 | 显示全部楼层
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
发表于 2007-4-1 14:48 | 显示全部楼层
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

手机版|Archiver|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网

GMT+2, 2025-5-1 17:21 , Processed in 0.076588 second(s), 13 queries , Redis On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表