原帖由 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;
}
}
} |