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 6fe283f74641057a735423a73c464d1209b4d3d5..1d0f1e302fe19f45b4ba08c7dc0d26d1afca0539 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 0000000000000000000000000000000000000000..0dc8cf10b498bf2b7bc4aa92a9b73d3604397962 --- /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 2efb359ee8e4e4b7904a6d3c83a8313fddb9570e..c6750016ffda6b546bca1a167afe8e464cee6330 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 80d2c3cb08470d8c9ba50448acf53f8a4d830d97..5322789f8d01f1f446cce553a6ad475e5edd37dd 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"); + } }