KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > java2d > Java2DGraphics2DAdapter


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: Java2DGraphics2DAdapter.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.render.java2d;
21
22 import java.awt.Color JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.geom.AffineTransform JavaDoc;
25 import java.awt.geom.Rectangle2D JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 import org.apache.fop.render.Graphics2DAdapter;
29 import org.apache.fop.render.Graphics2DImagePainter;
30 import org.apache.fop.render.RendererContext;
31
32 /**
33  * Graphics2DAdapter implementation for Java2D.
34  */

35 public class Java2DGraphics2DAdapter implements Graphics2DAdapter {
36
37     private Java2DGraphicsState state;
38
39     /**
40      * Main constructor
41      * @param state the state tracker for this rendering run
42      */

43     public Java2DGraphics2DAdapter(Java2DGraphicsState state) {
44         this.state = state;
45     }
46     
47     /** @see org.apache.fop.render.Graphics2DAdapter */
48     public void paintImage(Graphics2DImagePainter painter,
49             RendererContext context,
50             int x, int y, int width, int height) throws IOException JavaDoc {
51         
52         float fwidth = width / 1000f;
53         float fheight = height / 1000f;
54         float fx = x / 1000f;
55         float fy = y / 1000f;
56         
57         // get the 'width' and 'height' attributes of the SVG document
58
Dimension JavaDoc dim = painter.getImageSize();
59         float imw = (float)dim.getWidth() / 1000f;
60         float imh = (float)dim.getHeight() / 1000f;
61
62         float sx = fwidth / (float)imw;
63         float sy = fheight / (float)imh;
64
65         Java2DRenderer renderer = (Java2DRenderer)context.getRenderer();
66         renderer.saveGraphicsState();
67         state.getGraph().setColor(Color.black);
68         state.getGraph().setBackground(Color.black);
69         
70         //TODO Clip to the image area.
71

72         // transform so that the coordinates (0,0) is from the top left
73
// and positive is down and to the right. (0,0) is where the
74
// viewBox puts it.
75
state.getGraph().translate(fx, fy);
76         AffineTransform JavaDoc at = AffineTransform.getScaleInstance(sx, sy);
77         if (!at.isIdentity()) {
78             state.getGraph().transform(at);
79         }
80
81         Rectangle2D JavaDoc area = new Rectangle2D.Double JavaDoc(0.0, 0.0, imw, imh);
82         painter.paint(state.getGraph(), area);
83
84         renderer.restoreGraphicsState();
85     
86     }
87
88 }
89
Popular Tags