2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
18
/* $Id: ImageViewer.java 750418 2009-03-05 11:03:54Z vhennebert $ */
22
import java.awt.BasicStroke;
23
import java.awt.Color;
24
import java.awt.Dimension;
25
import java.awt.Graphics2D;
26
import java.awt.geom.Rectangle2D;
28
import java.io.IOException;
30
import org.apache.xmlgraphics.image.loader.ImageException;
31
import org.apache.xmlgraphics.image.loader.ImageFlavor;
32
import org.apache.xmlgraphics.image.loader.ImageInfo;
33
import org.apache.xmlgraphics.image.loader.ImageManager;
34
import org.apache.xmlgraphics.image.loader.ImageSessionContext;
35
import org.apache.xmlgraphics.image.loader.ImageSize;
36
import org.apache.xmlgraphics.image.loader.impl.DefaultImageContext;
37
import org.apache.xmlgraphics.image.loader.impl.DefaultImageSessionContext;
38
import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
39
import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
42
* Very simple image viewer application that demonstrates the use of the image loader framework.
44
public class ImageViewer {
46
private ImageManager imageManager;
48
public ImageViewer() {
49
//The ImageManager is set up for the whole application
50
this.imageManager = new ImageManager(new DefaultImageContext());
53
public void display(File f) throws IOException {
54
//The ImageSessionContext might for each processing run
55
ImageSessionContext sessionContext = new DefaultImageSessionContext(
56
this.imageManager.getImageContext(), null);
58
//Construct URI from filename
59
String uri = f.toURI().toASCIIString();
61
ImageGraphics2D g2dImage = null;
64
ImageInfo info = this.imageManager.getImageInfo(uri, sessionContext);
66
//Load image and request Graphics2D image
67
g2dImage = (ImageGraphics2D)this.imageManager.getImage(
68
info, ImageFlavor.GRAPHICS2D, sessionContext);
70
} catch (ImageException e) {
73
//Create "error image" if the image cannot be displayed
74
g2dImage = createErrorImage();
77
//Display frame with image
78
ViewerFrame frame = new ViewerFrame(g2dImage);
79
frame.setVisible(true);
82
private ImageGraphics2D createErrorImage() {
83
Graphics2DImagePainter painter = new Graphics2DImagePainter() {
85
public Dimension getImageSize() {
86
return new Dimension(10, 10);
89
public void paint(Graphics2D g2d, Rectangle2D area) {
90
g2d.translate(area.getX(), area.getY());
91
double w = area.getWidth();
92
double h = area.getHeight();
95
Dimension imageSize = getImageSize();
96
double sx = w / imageSize.getWidth();
97
double sy = h / imageSize.getHeight();
98
if (sx != 1.0 || sy != 1.0) {
102
g2d.setColor(Color.RED);
103
g2d.setStroke(new BasicStroke(0));
104
g2d.drawRect(0, 0, imageSize.width, imageSize.height);
105
g2d.drawLine(0, 0, imageSize.width, imageSize.height);
106
g2d.drawLine(0, imageSize.height, imageSize.width, 0);
110
Dimension dim = painter.getImageSize();
112
ImageSize size = new ImageSize();
113
size.setSizeInMillipoints(dim.width, dim.height);
114
size.setResolution(imageManager.getImageContext().getSourceResolution());
115
size.calcPixelsFromSize();
117
ImageInfo info = new ImageInfo(null, null);
119
return new ImageGraphics2D(info, painter);
123
* The application's main method.
124
* @param args the command-line arguments
126
public static void main(String[] args) {
128
ImageViewer app = new ImageViewer();
129
if (args.length < 1) {
130
throw new IllegalArgumentException("No filename given as application argument");
132
app.display(new File(args[0]));
133
} catch (Throwable t) {