KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > Rescale


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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.batik.svggen;
19
20 import java.awt.Button JavaDoc;
21 import java.awt.Color JavaDoc;
22 import java.awt.Graphics2D JavaDoc;
23 import java.awt.Image JavaDoc;
24 import java.awt.MediaTracker JavaDoc;
25 import java.awt.RenderingHints JavaDoc;
26 import java.awt.Toolkit JavaDoc;
27 import java.awt.image.BufferedImage JavaDoc;
28
29 /**
30  * This test validates the convertion of Java 2D RescaleOp
31  * into an SVG filer.
32  *
33  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
34  * @author <a HREF="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
35  * @version $Id: Rescale.java,v 1.4 2004/08/18 07:16:45 vhardy Exp $
36  */

37 public class Rescale implements Painter {
38     public void paint(Graphics2D JavaDoc g) {
39         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
40                            RenderingHints.VALUE_ANTIALIAS_ON);
41         //
42
// Load Image
43
//
44
Image JavaDoc image = Toolkit.getDefaultToolkit().createImage("test-resources/org/apache/batik/svggen/resources/vangogh.jpg");
45         MediaTracker JavaDoc tracker = new MediaTracker JavaDoc(new Button JavaDoc(""));
46         tracker.addImage(image, 0);
47         try{
48             tracker.waitForAll();
49         }catch(InterruptedException JavaDoc e){
50             tracker.removeImage(image);
51             image = null;
52         }finally {
53             if(image != null)
54                 tracker.removeImage(image);
55             if(tracker.isErrorAny())
56                 image = null;
57             if(image != null){
58                 if(image.getWidth(null)<0 ||
59                    image.getHeight(null)<0)
60                     image = null;
61             }
62         }
63
64         if(image == null){
65             throw new Error JavaDoc("Could not load image");
66         }
67
68         BufferedImage JavaDoc bi = new BufferedImage JavaDoc(image.getWidth(null),
69                                              image.getHeight(null), BufferedImage.TYPE_INT_RGB);
70         Graphics2D JavaDoc ig = bi.createGraphics();
71         ig.drawImage(image, 0, 0, null);
72
73         java.awt.image.RescaleOp JavaDoc brighten = new java.awt.image.RescaleOp JavaDoc(1.5f, 0, null);
74         java.awt.image.RescaleOp JavaDoc darken = new java.awt.image.RescaleOp JavaDoc(.6f, 0, null);
75
76         // Simply paint the image without and with rescale filters
77
g.setPaint(Color.black);
78         g.drawString("Brighter / Normal / Darker", 10, 20);
79         g.drawImage(bi, brighten, 10, 30);
80         g.drawImage(image, 10 + bi.getWidth() + 10, 30, null);
81         g.drawImage(bi, darken, 10 + 2*(bi.getWidth() + 10), 30);
82
83         g.translate(0, bi.getHeight() + 30 + 20);
84         g.drawString("Rescale Red / Green / Blue", 10, 20);
85         java.awt.image.RescaleOp JavaDoc redStress = new java.awt.image.RescaleOp JavaDoc(new float[]{ 2f, 1f, 1f },
86                                             new float[]{ 0, 0, 0 }, null);
87         java.awt.image.RescaleOp JavaDoc greenStress = new java.awt.image.RescaleOp JavaDoc(new float[]{ 1f, 2f, 1f },
88                                               new float[]{ 0, 0, 0 }, null);
89         java.awt.image.RescaleOp JavaDoc blueStress = new java.awt.image.RescaleOp JavaDoc(new float[]{ 1f, 1f, 2f },
90                                              new float[]{ 0, 0, 0 }, null);
91
92         g.drawImage(bi, redStress, 10, 30);
93         g.drawImage(bi, greenStress, 10 + bi.getWidth() + 10, 30);
94         g.drawImage(bi, blueStress, 10 + 2*(bi.getWidth() + 10), 30);
95     }
96 }
97
Popular Tags