1 18 package org.apache.batik.svggen; 19 20 import java.awt.Button ; 21 import java.awt.Color ; 22 import java.awt.Graphics2D ; 23 import java.awt.Image ; 24 import java.awt.MediaTracker ; 25 import java.awt.RenderingHints ; 26 import java.awt.Toolkit ; 27 import java.awt.image.BufferedImage ; 28 import java.awt.image.ByteLookupTable ; 29 import java.awt.image.LookupTable ; 30 31 39 public class Lookup implements Painter { 40 public void paint(Graphics2D g) { 41 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 42 RenderingHints.VALUE_ANTIALIAS_ON); 43 Image image = Toolkit.getDefaultToolkit().createImage("test-resources/org/apache/batik/svggen/resources/vangogh.png"); 47 MediaTracker tracker = new MediaTracker (new Button ("")); 48 tracker.addImage(image, 0); 49 try{ 50 tracker.waitForAll(); 51 }catch(InterruptedException 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 ("Could not load image"); 68 } 69 70 BufferedImage bi = new BufferedImage (image.getWidth(null), 71 image.getHeight(null), BufferedImage.TYPE_INT_RGB); 72 Graphics2D 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 table = new ByteLookupTable (0, lookup); 80 java.awt.image.LookupOp inverter = new java.awt.image.LookupOp (table, null); 81 82 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 |