From 567ef79e2f4ed5efd23a9ef08107ea1751626ef6 Mon Sep 17 00:00:00 2001 From: timmolter <tim.molter@gmail.com> Date: Fri, 24 Aug 2012 15:27:35 +0200 Subject: [PATCH] Issue #2: Remove dependency on servlet jar when saving a png --- .../xeiam/xchart/{ => io}/BitmapEncoder.java | 20 ++------ .../com/xeiam/xchart/io/ServletEncoder.java | 46 +++++++++++++++++++ .../xchart/example/ChartServletExample.java | 4 +- .../com/xeiam/xchart/example/Example1.java | 11 ++--- 4 files changed, 55 insertions(+), 26 deletions(-) rename src/main/java/com/xeiam/xchart/{ => io}/BitmapEncoder.java (72%) create mode 100644 src/main/java/com/xeiam/xchart/io/ServletEncoder.java diff --git a/src/main/java/com/xeiam/xchart/BitmapEncoder.java b/src/main/java/com/xeiam/xchart/io/BitmapEncoder.java similarity index 72% rename from src/main/java/com/xeiam/xchart/BitmapEncoder.java rename to src/main/java/com/xeiam/xchart/io/BitmapEncoder.java index 6fe283f7..1d0f1e30 100644 --- a/src/main/java/com/xeiam/xchart/BitmapEncoder.java +++ b/src/main/java/com/xeiam/xchart/io/BitmapEncoder.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.xeiam.xchart; +package com.xeiam.xchart.io; import java.awt.Graphics2D; import java.awt.image.BufferedImage; @@ -21,7 +21,8 @@ import java.io.FileOutputStream; import java.io.OutputStream; import javax.imageio.ImageIO; -import javax.servlet.ServletOutputStream; + +import com.xeiam.xchart.Chart; /** * @author timmolter @@ -46,19 +47,4 @@ public class BitmapEncoder { out.close(); } - /** - * Streams a chart as a PNG file - * - * @param out - * @param chart - */ - public static void streamPNG(ServletOutputStream out, Chart chart) throws Exception { - - BufferedImage lBufferedImage = new BufferedImage(chart.getWidth(), chart.getHeight(), BufferedImage.TYPE_INT_RGB); - Graphics2D lGraphics2D = lBufferedImage.createGraphics(); - chart.paint(lGraphics2D); - - ImageIO.write(lBufferedImage, "png", out); - out.close(); - } } diff --git a/src/main/java/com/xeiam/xchart/io/ServletEncoder.java b/src/main/java/com/xeiam/xchart/io/ServletEncoder.java new file mode 100644 index 00000000..0dc8cf10 --- /dev/null +++ b/src/main/java/com/xeiam/xchart/io/ServletEncoder.java @@ -0,0 +1,46 @@ +/** + * Copyright 2011-2012 Xeiam LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.xeiam.xchart.io; + +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; + +import javax.imageio.ImageIO; +import javax.servlet.ServletOutputStream; + +import com.xeiam.xchart.Chart; + +/** + * @author timmolter + */ +public class ServletEncoder { + + /** + * Streams a chart as a PNG file + * + * @param out + * @param chart + */ + public static void streamPNG(ServletOutputStream out, Chart chart) throws Exception { + + BufferedImage lBufferedImage = new BufferedImage(chart.getWidth(), chart.getHeight(), BufferedImage.TYPE_INT_RGB); + Graphics2D lGraphics2D = lBufferedImage.createGraphics(); + chart.paint(lGraphics2D); + + ImageIO.write(lBufferedImage, "png", out); + out.close(); + } +} diff --git a/src/test/java/com/xeiam/xchart/example/ChartServletExample.java b/src/test/java/com/xeiam/xchart/example/ChartServletExample.java index 2efb359e..c6750016 100644 --- a/src/test/java/com/xeiam/xchart/example/ChartServletExample.java +++ b/src/test/java/com/xeiam/xchart/example/ChartServletExample.java @@ -25,9 +25,9 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import com.xeiam.xchart.BitmapEncoder; import com.xeiam.xchart.Chart; import com.xeiam.xchart.QuickChart; +import com.xeiam.xchart.io.ServletEncoder; /** * Generates, stores, and serves charts @@ -50,7 +50,7 @@ public class ChartServletExample extends HttpServlet { ServletOutputStream out = response.getOutputStream(); try { - BitmapEncoder.streamPNG(out, chart); + ServletEncoder.streamPNG(out, chart); } catch (Exception e) { e.printStackTrace(); } diff --git a/src/test/java/com/xeiam/xchart/example/Example1.java b/src/test/java/com/xeiam/xchart/example/Example1.java index 80d2c3cb..5322789f 100644 --- a/src/test/java/com/xeiam/xchart/example/Example1.java +++ b/src/test/java/com/xeiam/xchart/example/Example1.java @@ -18,8 +18,8 @@ package com.xeiam.xchart.example; import java.util.Arrays; import java.util.Collection; -import com.xeiam.xchart.BitmapEncoder; import com.xeiam.xchart.Chart; +import com.xeiam.xchart.io.BitmapEncoder; /** * Creates a simple charts and saves it as aPNG image file. @@ -28,7 +28,7 @@ import com.xeiam.xchart.Chart; */ public class Example1 { - public static void main(String[] args) { + public static void main(String[] args) throws Exception { Collection<Number> xData = Arrays.asList(new Number[] { 0.0, 1.0, 2.0 }); Collection<Number> yData = Arrays.asList(new Number[] { 0.0, 1.0, 2.0 }); @@ -40,10 +40,7 @@ public class Example1 { chart.setYAxisTitle("Y"); chart.addSeries("y(x)", xData, yData); - try { - BitmapEncoder.savePNG(chart, "./Sample_Chart.png"); - } catch (Exception e) { - e.printStackTrace(); - } + BitmapEncoder.savePNG(chart, "./Sample_Chart.png"); + } } -- GitLab