KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > optional > image > Rotate


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 package org.apache.tools.ant.types.optional.image;
19
20 import javax.media.jai.PlanarImage;
21 import javax.media.jai.InterpolationNearest;
22 import javax.media.jai.JAI;
23 import java.awt.image.renderable.ParameterBlock JavaDoc;
24 import java.awt.image.BufferedImage JavaDoc;
25 import java.awt.Graphics2D JavaDoc;
26
27 /**
28  * ImageOperation to rotate an image by a certain degree
29  *
30  * @see org.apache.tools.ant.taskdefs.optional.image.Image
31  */

32 public class Rotate extends TransformOperation implements DrawOperation {
33     private static final float HALF_CIRCLE = 180.0F;
34
35     // CheckStyle:VisibilityModifier OFF - bc
36
protected float angle = 0.0F;
37     // CheckStyle:VisibilityModifier ON
38

39     /**
40      * Sets the angle of rotation in degrees.
41      * @param ang The angle at which to rotate the image
42      */

43     public void setAngle(String JavaDoc ang) {
44         angle = Float.parseFloat(ang);
45     }
46
47
48     /**
49      * Rotate an image.
50      * @param image the image to rotate.
51      * @return the rotated image.
52      */

53     public PlanarImage performRotate(PlanarImage image) {
54         float tAngle = (float) (angle * (Math.PI / HALF_CIRCLE));
55         ParameterBlock JavaDoc pb = new ParameterBlock JavaDoc();
56         pb.addSource(image);
57         pb.add(0.0F);
58         pb.add(0.0F);
59         pb.add(tAngle);
60         pb.add(new InterpolationNearest());
61         return JAI.create("Rotate", pb, null);
62     }
63
64
65     /**
66      * Performs the image rotation when being handled as a TransformOperation.
67      * @param image The image to perform the transformation on.
68      * @return the transformed image.
69      */

70     public PlanarImage executeTransformOperation(PlanarImage image) {
71         BufferedImage JavaDoc bi = null;
72         Graphics2D JavaDoc graphics = null;
73         for (int i = 0; i < instructions.size(); i++) {
74             ImageOperation instr = ((ImageOperation) instructions.elementAt(i));
75             if (instr instanceof DrawOperation) {
76                 // If this TransformOperation has DrawOperation children
77
// then Rotate the first child and return.
78
System.out.println("Execing Draws");
79                 PlanarImage op = ((DrawOperation) instr).executeDrawOperation();
80                 image = performRotate(op);
81                 return image;
82             } else if (instr instanceof TransformOperation) {
83                 bi = image.getAsBufferedImage();
84                 graphics = (Graphics2D JavaDoc) bi.getGraphics();
85                 System.out.println("Execing Transforms");
86                 image = ((TransformOperation) instr)
87                     .executeTransformOperation(PlanarImage.wrapRenderedImage(bi));
88                 bi = image.getAsBufferedImage();
89             }
90         }
91         System.out.println("Execing as TransformOperation");
92         image = performRotate(image);
93         System.out.println(image);
94         return image;
95     }
96
97     /**
98      * Performs the image rotation when being handled as a DrawOperation.
99      * It absolutely requires that there be a DrawOperation nested beneath it,
100      * but only the FIRST DrawOperation will be handled since it can only return
101      * ONE image.
102      * @return the image.
103      */

104     public PlanarImage executeDrawOperation() {
105         for (int i = 0; i < instructions.size(); i++) {
106             ImageOperation instr = ((ImageOperation) instructions.elementAt(i));
107             if (instr instanceof DrawOperation) {
108                 // If this TransformOperation has DrawOperation children
109
// then Rotate the first child and return.
110
PlanarImage op = ((DrawOperation) instr).executeDrawOperation();
111                 op = performRotate(op);
112                 return op;
113             }
114         }
115         return null;
116     }
117
118 }
119
Popular Tags