diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/Graphics2DRenderer.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/Graphics2DRenderer.java index 1b2e9a1b4..c266de366 100755 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/Graphics2DRenderer.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/simple/Graphics2DRenderer.java @@ -35,7 +35,7 @@ * for rendering documents directly to images.
**
Graphics2DRenderer supports the {@link XHTMLPanel#setDocument(Document)}, - * {@link XHTMLPanel#doLayout()}, and {@link XHTMLPanel#render()} methods from + * {@link XHTMLPanel#doLayout()} method from * {@link XHTMLPanel}, as well as easy-to-use static utility methods. * For example, to render a document in an image that is 600 pixels wide use the * {@link #renderToImageAutoSize(String,int,int)} method like this:
@@ -46,25 +46,22 @@ * * @author Joshua Marinacci */ - -public class Graphics2DRenderer { +public final class Graphics2DRenderer { /** * The panel we are using to render the document. */ private final XHTMLPanel panel; - /** - * Dimensions of the image to render, in pixels. - */ - @Nullable - protected Dimension dim; + public Graphics2DRenderer(String url) { + panel = new XHTMLPanel(); + panel.setInteractive(false); + panel.setDocument(url); + } - /** - * Creates a new renderer with no document specified. - */ - public Graphics2DRenderer() { + public Graphics2DRenderer(Document doc, String base_url) { panel = new XHTMLPanel(); panel.setInteractive(false); + panel.setDocument(doc, base_url); } // ASK maybe we could change the graphics2d to be a font rendering context? @@ -76,7 +73,6 @@ public Graphics2DRenderer() { * @param dim dimensions of the container for the document */ public void layout(Graphics2D g2, @Nullable Dimension dim) { - this.dim = dim; if (dim != null) { panel.setSize(dim); } @@ -96,27 +92,6 @@ public void render(Graphics2D g2) { panel.paintComponent(g2); } - - /** - * Set the document to be rendered, lays it out, and - * renders it. - * - * @param url the URL for the document to render. - */ - public void setDocument(String url) { - panel.setDocument(url); - } - - /** - * Sets the document to render, lays it out, and renders it. - * - * @param doc the Document to render - * @param base_url base URL for relative links within the Document. - */ - public void setDocument(Document doc, String base_url) { - panel.setDocument(doc, base_url); - } - /** * Returns the size image needed to render the document without anything * going off the side. Could be different from the dimensions passed into @@ -174,8 +149,8 @@ public static BufferedImage renderToImage(String url, int width, int height) { * @return Returns an Image containing the rendered document. */ public static BufferedImage renderToImage(String url, int width, int height, int bufferedImageType) { - Graphics2DRenderer g2r = new Graphics2DRenderer(); - g2r.setDocument(url); + Graphics2DRenderer g2r = new Graphics2DRenderer(url); + Dimension dim = new Dimension(width, height); BufferedImage buff = new BufferedImage((int) dim.getWidth(), (int) dim.getHeight(), bufferedImageType); withGraphics(buff, g -> { @@ -188,8 +163,8 @@ public static BufferedImage renderToImage(String url, int width, int height, int /** * A static utility method to automatically create an image from a * document, where height is determined based on document content. - * To estimate a size before rendering, use {@link #setDocument(String)} - * and then {@link #getMinimumSize()}. The rendered image supports transparency. + * To estimate a size before rendering, use {@link #getMinimumSize()}. + * The rendered image supports transparency. * * @param url java.net.URL for the document to render. * @param width Width in pixels of the layout container @@ -202,8 +177,7 @@ public static BufferedImage renderToImageAutoSize(String url, int width){ /** * A static utility method to automatically create an image from a * document, where height is determined based on document content. - * To estimate a size before rendering, use {@link #setDocument(String)} - * and then {@link #getMinimumSize()}. + * To estimate a size before rendering, use {@link #getMinimumSize()}. * * @param url java.net.URL for the document to render. * @param width Width in pixels of the layout container @@ -212,8 +186,7 @@ public static BufferedImage renderToImageAutoSize(String url, int width){ * @return Returns java.awt.Image containing the rendered document. */ public static BufferedImage renderToImageAutoSize(String url, int width, int bufferedImageType) { - Graphics2DRenderer g2r = new Graphics2DRenderer(); - g2r.setDocument(url); + Graphics2DRenderer g2r = new Graphics2DRenderer(url); Dimension dim = new Dimension(width, 1000); // do layout with temp buffer diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/DocumentDiffTest.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/DocumentDiffTest.java index 3f86cb854..e11230715 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/DocumentDiffTest.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/DocumentDiffTest.java @@ -49,8 +49,7 @@ public static void generateTestFile(String test, String diff, int width, int hei public static String xhtmlToDiff(String xhtml, int width, int height) throws Exception { Document doc = XMLUtil.documentFromFile(xhtml); - Graphics2DRenderer renderer = new Graphics2DRenderer(); - renderer.setDocument(doc, new File(xhtml).toURI().toURL().toString()); + Graphics2DRenderer renderer = new Graphics2DRenderer(doc, new File(xhtml).toURI().toURL().toString()); BufferedImage buff = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); withGraphics(buff, g -> {