KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.awt.image.ByteLookupTable JavaDoc;
29 import java.awt.image.LookupTable JavaDoc;
30
31 /**
32  * This test validates the convertion of Java 2D LookupOp
33  * into an SVG filer.
34  *
35  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
36  * @author <a HREF="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
37  * @version $Id: Lookup.java,v 1.5 2004/08/18 07:16:45 vhardy Exp $
38  */

39 public class Lookup implements Painter {
40     public void paint(Graphics2D JavaDoc g) {
41         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
42                            RenderingHints.VALUE_ANTIALIAS_ON);
43         //
44
// Load Image
45
//
46
Image JavaDoc image = Toolkit.getDefaultToolkit().createImage("test-resources/org/apache/batik/svggen/resources/vangogh.png");
47         MediaTracker JavaDoc tracker = new MediaTracker JavaDoc(new Button JavaDoc(""));
48         tracker.addImage(image, 0);
49         try{
50             tracker.waitForAll();
51         }catch(InterruptedException JavaDoc e){
52             tracker.removeImage(image);
53             image = null;
54         }finally {
55             if(image != null)
56                 tracker.removeImage(image);
57             if(tracker.isErrorAny())
58                 image = null;
59             if(image != null){
60                 if(image.getWidth(null)<0 ||
61                    image.getHeight(null)<0)
62                     image = null;
63             }
64         }
65
66         if(image == null){
67             throw new Error JavaDoc("Could not load image");
68         }
69
70         BufferedImage JavaDoc bi = new BufferedImage JavaDoc(image.getWidth(null),
71                                              image.getHeight(null), BufferedImage.TYPE_INT_RGB);
72         Graphics2D JavaDoc ig = bi.createGraphics();
73         ig.drawImage(image, 0, 0, null);
74
75         byte lookup[] = new byte[256];
76         for(int i=0; i<256; i++)
77             lookup[i] = (byte)(255 - i);
78
79         LookupTable JavaDoc table = new ByteLookupTable JavaDoc(0, lookup);
80         java.awt.image.LookupOp JavaDoc inverter = new java.awt.image.LookupOp JavaDoc(table, null);
81
82         // Simply paint the image without and with the lookup filter
83
g.setPaint(Color.black);
84         g.drawString("Normal / Inverted", 10, 20);
85         g.drawImage(image, 10, 30, null);
86         g.drawImage(bi, inverter, 10 + bi.getWidth() + 10, 30);
87     }
88 }
89
Popular Tags