~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to examples/java/image/loader/ImageViewer.java

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-02-11 14:15:14 UTC
  • mfrom: (8.1.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20110211141514-h67achft6x31gju1
Tags: 1.4.dfsg-3
Uploading to unstable, hoping we won't break too many things ;-)...

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
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.
 
16
 */
 
17
 
 
18
/* $Id: ImageViewer.java 750418 2009-03-05 11:03:54Z vhennebert $ */
 
19
 
 
20
package image.loader;
 
21
 
 
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;
 
27
import java.io.File;
 
28
import java.io.IOException;
 
29
 
 
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;
 
40
 
 
41
/**
 
42
 * Very simple image viewer application that demonstrates the use of the image loader framework.
 
43
 */
 
44
public class ImageViewer {
 
45
 
 
46
    private ImageManager imageManager;
 
47
 
 
48
    public ImageViewer() {
 
49
        //The ImageManager is set up for the whole application
 
50
        this.imageManager = new ImageManager(new DefaultImageContext());
 
51
    }
 
52
 
 
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);
 
57
 
 
58
        //Construct URI from filename
 
59
        String uri = f.toURI().toASCIIString();
 
60
 
 
61
        ImageGraphics2D g2dImage = null;
 
62
        try {
 
63
            //Preload image
 
64
            ImageInfo info = this.imageManager.getImageInfo(uri, sessionContext);
 
65
 
 
66
            //Load image and request Graphics2D image
 
67
            g2dImage = (ImageGraphics2D)this.imageManager.getImage(
 
68
                    info, ImageFlavor.GRAPHICS2D, sessionContext);
 
69
 
 
70
        } catch (ImageException e) {
 
71
            e.printStackTrace();
 
72
 
 
73
            //Create "error image" if the image cannot be displayed
 
74
            g2dImage = createErrorImage();
 
75
        }
 
76
 
 
77
        //Display frame with image
 
78
        ViewerFrame frame = new ViewerFrame(g2dImage);
 
79
        frame.setVisible(true);
 
80
    }
 
81
 
 
82
    private ImageGraphics2D createErrorImage() {
 
83
        Graphics2DImagePainter painter = new Graphics2DImagePainter() {
 
84
 
 
85
            public Dimension getImageSize() {
 
86
                return new Dimension(10, 10);
 
87
            }
 
88
 
 
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();
 
93
 
 
94
                //Fit in paint area
 
95
                Dimension imageSize = getImageSize();
 
96
                double sx = w / imageSize.getWidth();
 
97
                double sy = h / imageSize.getHeight();
 
98
                if (sx != 1.0 || sy != 1.0) {
 
99
                    g2d.scale(sx, sy);
 
100
                }
 
101
 
 
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);
 
107
            }
 
108
 
 
109
        };
 
110
        Dimension dim = painter.getImageSize();
 
111
 
 
112
        ImageSize size = new ImageSize();
 
113
        size.setSizeInMillipoints(dim.width, dim.height);
 
114
        size.setResolution(imageManager.getImageContext().getSourceResolution());
 
115
        size.calcPixelsFromSize();
 
116
 
 
117
        ImageInfo info = new ImageInfo(null, null);
 
118
        info.setSize(size);
 
119
        return new ImageGraphics2D(info, painter);
 
120
    }
 
121
 
 
122
    /**
 
123
     * The application's main method.
 
124
     * @param args the command-line arguments
 
125
     */
 
126
    public static void main(String[] args) {
 
127
        try {
 
128
            ImageViewer app = new ImageViewer();
 
129
            if (args.length < 1) {
 
130
                throw new IllegalArgumentException("No filename given as application argument");
 
131
            }
 
132
            app.display(new File(args[0]));
 
133
        } catch (Throwable t) {
 
134
            t.printStackTrace();
 
135
            System.exit(-1);
 
136
        }
 
137
    }
 
138
 
 
139
}