KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > util > IconManagerTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.openide.util;
20
21 import java.awt.Color JavaDoc;
22 import java.awt.Image JavaDoc;
23 import java.awt.Transparency JavaDoc;
24 import java.awt.image.BufferedImage JavaDoc;
25 import junit.framework.*;
26 import java.lang.ref.*;
27 import java.util.*;
28
29 /**
30  *
31  * @author Radim Kubacki
32  */

33 public class IconManagerTest extends TestCase {
34     
35     public IconManagerTest (String JavaDoc testName) {
36         super (testName);
37     }
38
39     public void testMergeImages() throws Exception JavaDoc {
40         // test if merged image preserves alpha (#90862)
41
BufferedImage JavaDoc img1 = new BufferedImage JavaDoc(16, 16, BufferedImage.TYPE_INT_ARGB);
42 // System.out.println("img1 transparency "+img1.getTransparency());
43
java.awt.Graphics2D JavaDoc g = img1.createGraphics();
44         Color JavaDoc c = new Color JavaDoc(255, 255, 255, 128);
45         g.setColor(c);
46         g.fillRect(0, 0, 16, 16);
47         g.dispose();
48         
49         BufferedImage JavaDoc img2 = new BufferedImage JavaDoc(1, 1, BufferedImage.TYPE_INT_ARGB);
50 // System.out.println("img2 transparency "+img2.getTransparency());
51
g = img2.createGraphics();
52         c = new Color JavaDoc(255, 255, 255);
53         g.setColor(c);
54         g.fillRect(0, 0, 2, 2);
55         g.dispose();
56         
57         Image JavaDoc mergedImg = IconManager.mergeImages(img1, img2, 0, 0);
58         if (!(mergedImg instanceof BufferedImage JavaDoc)) {
59             fail("It is assumed that mergeImages returns BufferedImage. Need to update test");
60         }
61                 
62         BufferedImage JavaDoc merged = (BufferedImage JavaDoc)mergedImg;
63 // System.out.println("pixels " + Integer.toHexString(merged.getRGB(10, 10)) +", "+ Integer.toHexString(merged.getRGB(0, 0)));
64
assertNotSame("transparency has to be kept for pixel <1,1>", merged.getRGB(10, 10), merged.getRGB(0, 0));
65     }
66     
67     public void testMergeBitmaskImages() throws Exception JavaDoc {
68         // test if two bitmask images are merged to bitmask again to avoid use of alpha channel
69
BufferedImage JavaDoc img1 = new BufferedImage JavaDoc(16, 16, BufferedImage.TYPE_INT_RGB);
70 // System.out.println("img1 transparency "+img1.getTransparency());
71
java.awt.Graphics2D JavaDoc g = img1.createGraphics();
72         g.setColor(Color.BLUE);
73         g.fillRect(0, 0, 16, 16);
74         g.dispose();
75         
76         BufferedImage JavaDoc img2 = new BufferedImage JavaDoc(2, 2, BufferedImage.TYPE_INT_RGB);
77 // System.out.println("img2 transparency "+img2.getTransparency());
78
g = img2.createGraphics();
79         g.setColor(Color.RED);
80         g.fillRect(0, 0, 2, 2);
81         g.dispose();
82         
83         Image JavaDoc mergedImg = IconManager.mergeImages(img1, img2, 0, 0);
84         if (!(mergedImg instanceof BufferedImage JavaDoc)) {
85             fail("It is assumed that mergeImages returns BufferedImage. Need to update test");
86         }
87                 
88         BufferedImage JavaDoc merged = (BufferedImage JavaDoc)mergedImg;
89         assertEquals("Should create bitmask image", Transparency.BITMASK, merged.getTransparency());
90         assertEquals(Color.RED, new Color JavaDoc(merged.getRGB(1, 1)));
91         assertEquals(Color.BLUE, new Color JavaDoc(merged.getRGB(10, 10)));
92     }
93     
94 }
95
Popular Tags