You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by je...@apache.org on 2007/12/19 16:10:45 UTC
svn commit: r605569 - in /xmlgraphics/commons/branches/TempNewImagePackage:
examples/java/image/loader/
src/java/org/apache/xmlgraphics/image/loader/impl/
test/java/org/apache/xmlgraphics/image/loader/
Author: jeremias
Date: Wed Dec 19 07:10:43 2007
New Revision: 605569
URL: http://svn.apache.org/viewvc?rev=605569&view=rev
Log:
Added a default ImageSessionContext implementation for convenience.
Added a very simple image viewer demo application to show how the package is used.
Added:
xmlgraphics/commons/branches/TempNewImagePackage/examples/java/image/loader/
xmlgraphics/commons/branches/TempNewImagePackage/examples/java/image/loader/ImageViewer.java (with props)
xmlgraphics/commons/branches/TempNewImagePackage/examples/java/image/loader/ViewerFrame.java (with props)
xmlgraphics/commons/branches/TempNewImagePackage/src/java/org/apache/xmlgraphics/image/loader/impl/DefaultImageSessionContext.java (with props)
Modified:
xmlgraphics/commons/branches/TempNewImagePackage/test/java/org/apache/xmlgraphics/image/loader/MockImageContext.java
xmlgraphics/commons/branches/TempNewImagePackage/test/java/org/apache/xmlgraphics/image/loader/MockImageSessionContext.java
Added: xmlgraphics/commons/branches/TempNewImagePackage/examples/java/image/loader/ImageViewer.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/TempNewImagePackage/examples/java/image/loader/ImageViewer.java?rev=605569&view=auto
==============================================================================
--- xmlgraphics/commons/branches/TempNewImagePackage/examples/java/image/loader/ImageViewer.java (added)
+++ xmlgraphics/commons/branches/TempNewImagePackage/examples/java/image/loader/ImageViewer.java Wed Dec 19 07:10:43 2007
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+/* $Id$ */
+
+package image.loader;
+
+import java.awt.BasicStroke;
+import java.awt.Color;
+import java.awt.Dimension;
+import java.awt.Graphics2D;
+import java.awt.geom.Rectangle2D;
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.xmlgraphics.image.loader.ImageContext;
+import org.apache.xmlgraphics.image.loader.ImageException;
+import org.apache.xmlgraphics.image.loader.ImageFlavor;
+import org.apache.xmlgraphics.image.loader.ImageInfo;
+import org.apache.xmlgraphics.image.loader.ImageManager;
+import org.apache.xmlgraphics.image.loader.ImageSessionContext;
+import org.apache.xmlgraphics.image.loader.ImageSize;
+import org.apache.xmlgraphics.image.loader.impl.DefaultImageContext;
+import org.apache.xmlgraphics.image.loader.impl.DefaultImageSessionContext;
+import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
+import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
+
+/**
+ * Very simple image viewer application that demonstrates the use of the image loader framework.
+ */
+public class ImageViewer {
+
+ private ImageContext imageContext;
+ private ImageManager imageManager;
+
+ public ImageViewer() {
+ //These two are set up for the whole application
+ this.imageContext = new DefaultImageContext();
+ this.imageManager = new ImageManager(this.imageContext);
+ }
+
+ public void display(File f) throws IOException {
+ //The ImageSessionContext might for each processing run
+ ImageSessionContext sessionContext = new DefaultImageSessionContext(
+ this.imageContext, null);
+
+ //Construct URI from filename
+ String uri = f.toURI().toASCIIString();
+
+ ImageGraphics2D g2dImage = null;
+ try {
+ //Preload image
+ ImageInfo info = this.imageManager.getImageInfo(uri, sessionContext);
+
+ //Load image and request Graphics2D image
+ g2dImage = (ImageGraphics2D)this.imageManager.getImage(
+ info, ImageFlavor.GRAPHICS2D, sessionContext);
+
+ } catch (ImageException e) {
+ e.printStackTrace();
+
+ //Create "error image" if the image cannot be displayed
+ g2dImage = createErrorImage();
+ }
+
+ //Display frame with image
+ ViewerFrame frame = new ViewerFrame(g2dImage);
+ frame.setVisible(true);
+ }
+
+ private ImageGraphics2D createErrorImage() {
+ Graphics2DImagePainter painter = new Graphics2DImagePainter() {
+
+ public Dimension getImageSize() {
+ return new Dimension(10, 10);
+ }
+
+ public void paint(Graphics2D g2d, Rectangle2D area) {
+ g2d.translate(area.getX(), area.getY());
+ double w = area.getWidth();
+ double h = area.getHeight();
+
+ //Fit in paint area
+ Dimension imageSize = getImageSize();
+ double sx = w / imageSize.getWidth();
+ double sy = h / imageSize.getHeight();
+ if (sx != 1.0 || sy != 1.0) {
+ g2d.scale(sx, sy);
+ }
+
+ g2d.setColor(Color.RED);
+ g2d.setStroke(new BasicStroke(0));
+ g2d.drawRect(0, 0, imageSize.width, imageSize.height);
+ g2d.drawLine(0, 0, imageSize.width, imageSize.height);
+ g2d.drawLine(0, imageSize.height, imageSize.width, 0);
+ }
+
+ };
+ Dimension dim = painter.getImageSize();
+
+ ImageSize size = new ImageSize();
+ size.setSizeInMillipoints(dim.width, dim.height);
+ size.setResolution(imageContext.getSourceResolution());
+ size.calcPixelsFromSize();
+
+ ImageInfo info = new ImageInfo(null, null);
+ info.setSize(size);
+ return new ImageGraphics2D(info, painter);
+ }
+
+ /**
+ * The application's main method.
+ * @param args the command-line arguments
+ */
+ public static void main(String[] args) {
+ try {
+ ImageViewer app = new ImageViewer();
+ if (args.length < 1) {
+ throw new IllegalArgumentException("No filename given as application argument");
+ }
+ app.display(new File(args[0]));
+ } catch (Throwable t) {
+ t.printStackTrace();
+ System.exit(-1);
+ }
+ }
+
+}
Propchange: xmlgraphics/commons/branches/TempNewImagePackage/examples/java/image/loader/ImageViewer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: xmlgraphics/commons/branches/TempNewImagePackage/examples/java/image/loader/ImageViewer.java
------------------------------------------------------------------------------
svn:keywords = Id
Added: xmlgraphics/commons/branches/TempNewImagePackage/examples/java/image/loader/ViewerFrame.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/TempNewImagePackage/examples/java/image/loader/ViewerFrame.java?rev=605569&view=auto
==============================================================================
--- xmlgraphics/commons/branches/TempNewImagePackage/examples/java/image/loader/ViewerFrame.java (added)
+++ xmlgraphics/commons/branches/TempNewImagePackage/examples/java/image/loader/ViewerFrame.java Wed Dec 19 07:10:43 2007
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+/* $Id$ */
+
+package image.loader;
+
+import java.awt.Dimension;
+import java.awt.Frame;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.Rectangle;
+import java.awt.event.WindowAdapter;
+import java.awt.event.WindowEvent;
+import java.awt.geom.Rectangle2D;
+
+import javax.swing.JPanel;
+
+import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
+import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
+
+/**
+ * Viewer frame for the image viewer.
+ */
+public class ViewerFrame extends Frame {
+
+ public static final String TITLE = "Very Simple Image Viewer";
+
+ public ViewerFrame(ImageGraphics2D g2dImage) {
+ super(TITLE);
+ addWindowListener(new WindowHandler());
+ buildGUI(g2dImage);
+ setSize(500, 400);
+ }
+
+ private void buildGUI(final ImageGraphics2D g2dImage) {
+ JPanel imagePanel = new JPanel() {
+ /** {@inheritDoc} */
+ protected void paintComponent(Graphics graphics) {
+ super.paintComponent(graphics);
+ Graphics2D g2d = (Graphics2D)graphics.create();
+ try {
+ Rectangle paintRect = new Rectangle(
+ 30, 30,
+ getWidth() - 60, getHeight() - 60);
+ //g2d.translate(paintRect.getX(), paintRect.getY());
+
+ Graphics2DImagePainter painter = g2dImage.getGraphics2DImagePainter();
+ Dimension dim = painter.getImageSize();
+ double sx = paintRect.getWidth() / dim.getWidth();
+ double sy = paintRect.getHeight() / dim.getHeight();
+ //g2d.scale(sx, sy);
+
+ /*
+ Rectangle2D targetRect = new Rectangle2D.Double(
+ paintRect.x * sx, paintRect.y * sy,
+ dim.width, dim.height);
+ */
+ Rectangle2D targetRect = new Rectangle2D.Double(
+ paintRect.x, paintRect.y,
+ paintRect.width, paintRect.height);
+
+
+ g2d.draw(targetRect);
+ painter.paint(g2d, targetRect);
+ } finally {
+ g2d.dispose();
+ }
+ }
+ };
+ add("Center", imagePanel);
+ }
+
+ private class WindowHandler extends WindowAdapter {
+ public void windowClosing(WindowEvent we) {
+ System.exit(0);
+ }
+ }
+
+}
Propchange: xmlgraphics/commons/branches/TempNewImagePackage/examples/java/image/loader/ViewerFrame.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: xmlgraphics/commons/branches/TempNewImagePackage/examples/java/image/loader/ViewerFrame.java
------------------------------------------------------------------------------
svn:keywords = Id
Added: xmlgraphics/commons/branches/TempNewImagePackage/src/java/org/apache/xmlgraphics/image/loader/impl/DefaultImageSessionContext.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/TempNewImagePackage/src/java/org/apache/xmlgraphics/image/loader/impl/DefaultImageSessionContext.java?rev=605569&view=auto
==============================================================================
--- xmlgraphics/commons/branches/TempNewImagePackage/src/java/org/apache/xmlgraphics/image/loader/impl/DefaultImageSessionContext.java (added)
+++ xmlgraphics/commons/branches/TempNewImagePackage/src/java/org/apache/xmlgraphics/image/loader/impl/DefaultImageSessionContext.java Wed Dec 19 07:10:43 2007
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2007 Jeremias Maerki
+ *
+ * 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.
+ */
+
+/* $Id$ */
+
+package org.apache.xmlgraphics.image.loader.impl;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.stream.StreamSource;
+
+import org.apache.xmlgraphics.image.loader.ImageContext;
+
+/**
+ * Very simple implementation of the ImageSessionContext interface. It works for absolute URLs
+ * and local filenames only. Consider writing your own implementation of the ImageSessionContext
+ * if you need more sophisticated functionality.
+ */
+public class DefaultImageSessionContext extends AbstractImageSessionContext {
+
+ private ImageContext context;
+ private File baseDir;
+
+ /**
+ * Main constructor.
+ * @param context the parent image context
+ * @param baseDir the base directory for resolving relative filenames
+ */
+ public DefaultImageSessionContext(ImageContext context, File baseDir) {
+ this.context = context;
+ this.baseDir = baseDir;
+ }
+
+ /** {@inheritDoc} */
+ public ImageContext getParentContext() {
+ return this.context;
+ }
+
+ /** {@inheritDoc} */
+ protected Source resolveURI(String uri) {
+ try {
+ URL url = new URL(uri);
+ return new StreamSource(url.openStream(), url.toExternalForm());
+ } catch (MalformedURLException e) {
+ File f = new File(baseDir, uri);
+ if (f.isFile()) {
+ return new StreamSource(f);
+ } else {
+ return null;
+ }
+ } catch (IOException ioe) {
+ return null;
+ }
+ }
+
+ /** {@inheritDoc} */
+ public float getTargetResolution() {
+ return getParentContext().getSourceResolution(); //same as source resolution
+ }
+
+}
Propchange: xmlgraphics/commons/branches/TempNewImagePackage/src/java/org/apache/xmlgraphics/image/loader/impl/DefaultImageSessionContext.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: xmlgraphics/commons/branches/TempNewImagePackage/src/java/org/apache/xmlgraphics/image/loader/impl/DefaultImageSessionContext.java
------------------------------------------------------------------------------
svn:keywords = Id
Modified: xmlgraphics/commons/branches/TempNewImagePackage/test/java/org/apache/xmlgraphics/image/loader/MockImageContext.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/TempNewImagePackage/test/java/org/apache/xmlgraphics/image/loader/MockImageContext.java?rev=605569&r1=605568&r2=605569&view=diff
==============================================================================
--- xmlgraphics/commons/branches/TempNewImagePackage/test/java/org/apache/xmlgraphics/image/loader/MockImageContext.java (original)
+++ xmlgraphics/commons/branches/TempNewImagePackage/test/java/org/apache/xmlgraphics/image/loader/MockImageContext.java Wed Dec 19 07:10:43 2007
@@ -19,7 +19,6 @@
package org.apache.xmlgraphics.image.loader;
-
/**
* Mock implementation for testing.
*/
Modified: xmlgraphics/commons/branches/TempNewImagePackage/test/java/org/apache/xmlgraphics/image/loader/MockImageSessionContext.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/TempNewImagePackage/test/java/org/apache/xmlgraphics/image/loader/MockImageSessionContext.java?rev=605569&r1=605568&r2=605569&view=diff
==============================================================================
--- xmlgraphics/commons/branches/TempNewImagePackage/test/java/org/apache/xmlgraphics/image/loader/MockImageSessionContext.java (original)
+++ xmlgraphics/commons/branches/TempNewImagePackage/test/java/org/apache/xmlgraphics/image/loader/MockImageSessionContext.java Wed Dec 19 07:10:43 2007
@@ -20,46 +20,18 @@
package org.apache.xmlgraphics.image.loader;
import java.io.File;
-import java.io.IOException;
-import java.net.MalformedURLException;
-import java.net.URL;
-import javax.xml.transform.Source;
-import javax.xml.transform.stream.StreamSource;
-
-import org.apache.xmlgraphics.image.loader.impl.AbstractImageSessionContext;
+import org.apache.xmlgraphics.image.loader.impl.DefaultImageSessionContext;
/**
* Mock implementation for testing.
*/
-public class MockImageSessionContext extends AbstractImageSessionContext {
-
- /** {@inheritDoc} */
- public ImageContext getParentContext() {
- return MockImageContext.getInstance();
- }
+public class MockImageSessionContext extends DefaultImageSessionContext {
- /** {@inheritDoc} */
- protected Source resolveURI(String uri) {
- try {
- URL url = new URL(uri);
- return new StreamSource(url.openStream(), url.toExternalForm());
- } catch (MalformedURLException e) {
- File baseDir = new File("./test/images/");
- if (!baseDir.isDirectory()) {
- throw new IllegalStateException("Base directory for test was not found.");
- }
- File f = new File(baseDir, uri);
- if (f.isFile()) {
- return new StreamSource(f);
- } else {
- return null;
- }
- } catch (IOException ioe) {
- return null;
- }
+ public MockImageSessionContext() {
+ super(MockImageContext.getInstance(), new File("./test/images/"));
}
-
+
/** {@inheritDoc} */
public float getTargetResolution() {
return 300;
---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org