📄 chartutilities.java
字号:
if (file == null) throw new IllegalArgumentException("Null 'file' argument.");
if (chart == null) throw new IllegalArgumentException("Null 'chart' argument.");
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
writeChartAsJPEG(out, chart, width, height, info);
out.close();
}
/**
* Saves a chart to a file in JPEG format. This method allows you to pass in a
* {@link ChartRenderingInfo} object, to collect information about the chart
* dimensions/entities. You will need this info if you want to create an HTML image map.
*
* @param file the file name (<code>null</code> not permitted).
* @param quality the quality setting.
* @param chart the chart (<code>null</code> not permitted).
* @param width the image width.
* @param height the image height.
* @param info the chart rendering info (<code>null</code> permitted).
*
* @throws IOException if there are any I/O errors.
*/
public static void saveChartAsJPEG(File file,
float quality,
JFreeChart chart,
int width,
int height,
ChartRenderingInfo info) throws IOException {
if (file == null) throw new IllegalArgumentException("Null 'file' argument.");
if (chart == null) throw new IllegalArgumentException("Null 'chart' argument.");
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
writeChartAsJPEG(out, quality, chart, width, height, info);
out.close();
}
/**
* Writes a {@link BufferedImage} to an output stream in JPEG format.
*
* @param out the output stream (<code>null</code> not permitted).
* @param image the image (<code>null</code> not permitted).
*
* @throws IOException if there are any I/O errors.
*/
public static void writeBufferedImageAsJPEG(OutputStream out, BufferedImage image)
throws IOException {
// defer argument checking...
writeBufferedImageAsJPEG(out, 0.75f, image);
}
/**
* Writes a {@link BufferedImage} to an output stream in JPEG format.
*
* @param out the output stream (<code>null</code> not permitted).
* @param quality the image quality (0.0f to 1.0f).
* @param image the image (<code>null</code> not permitted).
*
* @throws IOException if there are any I/O errors.
*/
public static void writeBufferedImageAsJPEG(OutputStream out, float quality,
BufferedImage image) throws IOException {
EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out, quality);
}
/**
* Writes a {@link BufferedImage} to an output stream in PNG format.
*
* @param out the output stream (<code>null</code> not permitted).
* @param image the image (<code>null</code> not permitted).
*
* @throws IOException if there are any I/O errors.
*/
public static void writeBufferedImageAsPNG(OutputStream out, BufferedImage image)
throws IOException {
EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, out);
}
/**
* Writes a {@link BufferedImage} to an output stream in PNG format.
*
* @param out the output stream (<code>null</code> not permitted).
* @param image the image (<code>null</code> not permitted).
* @param encodeAlpha encode alpha?
* @param compression the compression level (0-9).
*
* @throws IOException if there are any I/O errors.
*/
public static void writeBufferedImageAsPNG(OutputStream out,
BufferedImage image,
boolean encodeAlpha,
int compression) throws IOException {
EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, out, compression, encodeAlpha);
}
/**
* Encodes a {@link BufferedImage} to PNG format.
*
* @param image the image (<code>null</code> not permitted).
*
* @return A byte array in PNG format.
*
* @throws IOException if there is an I/O problem.
*/
public static byte[] encodeAsPNG(BufferedImage image) throws IOException {
return EncoderUtil.encode(image, ImageFormat.PNG);
}
/**
* Encodes a {@link BufferedImage} to PNG format.
*
* @param image the image (<code>null</code> not permitted).
* @param encodeAlpha encode alpha?
* @param compression the PNG compression level (0-9).
*
* @return The byte array in PNG format.
*
* @throws IOException if there is an I/O problem.
*/
public static byte[] encodeAsPNG(BufferedImage image, boolean encodeAlpha, int compression) throws IOException {
return EncoderUtil.encode(image, ImageFormat.PNG, compression, encodeAlpha);
}
/**
* Writes an image map to an output stream.
*
* @param writer the writer (<code>null</code> not permitted).
* @param name the map name (<code>null</code> not permitted).
* @param info the chart rendering info (<code>null</code> not permitted).
*
* @throws IOException if there are any I/O errors.
*/
public static void writeImageMap(PrintWriter writer, String name, ChartRenderingInfo info)
throws IOException {
// defer argument checking...
ImageMapUtil.writeImageMap(
writer, name, info,
new StandardToolTipTagFragmentGenerator(),
new StandardURLTagFragmentGenerator()
);
}
/**
* Writes an image map to an output stream.
*
* @param writer the writer (<code>null</code> not permitted).
* @param name the map name (<code>null</code> not permitted).
* @param info the chart rendering info (<code>null</code> not permitted).
* @param useOverLibForToolTips whether to use OverLIB for tooltips
* (http://www.bosrup.com/web/overlib/).
*
* @throws IOException if there are any I/O errors.
*/
public static void writeImageMap(PrintWriter writer,
String name,
ChartRenderingInfo info,
boolean useOverLibForToolTips) throws IOException {
ToolTipTagFragmentGenerator toolTipTagFragmentGenerator = null;
if (useOverLibForToolTips) {
toolTipTagFragmentGenerator = new OverLIBToolTipTagFragmentGenerator();
}
else {
toolTipTagFragmentGenerator = new StandardToolTipTagFragmentGenerator();
}
ImageMapUtil.writeImageMap(
writer, name, info, toolTipTagFragmentGenerator, new StandardURLTagFragmentGenerator()
);
}
/**
* Writes an image map to an output stream.
*
* @param writer the writer (<code>null</code> not permitted).
* @param name the map name (<code>null</code> not permitted).
* @param info the chart rendering info (<code>null</code> not permitted).
* @param toolTipTagFragmentGenerator the tool tip generator.
* @param urlTagFragmentGenerator the url generator.
*
* @throws IOException if there are any I/O errors.
*/
public static void writeImageMap(PrintWriter writer, String name, ChartRenderingInfo info,
ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
URLTagFragmentGenerator urlTagFragmentGenerator)
throws IOException {
writer.println(
ImageMapUtil.getImageMap(
name, info, toolTipTagFragmentGenerator, urlTagFragmentGenerator
)
);
}
/**
* Creates an HTML image map.
*
* @param name the map name (<code>null</code> not permitted).
* @param info the chart rendering info (<code>null</code> not permitted).
*
* @return the map tag.
*/
public static String getImageMap(String name, ChartRenderingInfo info) {
return ImageMapUtil.getImageMap(
name,
info,
new StandardToolTipTagFragmentGenerator(),
new StandardURLTagFragmentGenerator()
);
}
/**
* Creates an HTML image map.
*
* @param name the map name (<code>null</code> not permitted).
* @param info the chart rendering info (<code>null</code> not permitted).
* @param toolTipTagFragmentGenerator the tool tip generator.
* @param urlTagFragmentGenerator the url generator.
*
* @return the map tag.
*/
public static String getImageMap(String name,
ChartRenderingInfo info,
ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
URLTagFragmentGenerator urlTagFragmentGenerator) {
return ImageMapUtil.getImageMap(name, info, toolTipTagFragmentGenerator, urlTagFragmentGenerator);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -