Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/arranger/jarl/test/GradientLoaderTest.java


1   package com.arranger.jarl.test;
2   
3   import com.arranger.jarl.util.IOUtil;
4   import com.arranger.jarl.util.ImageUtil;
5   import com.jhlabs.image.MarbleFilter;
6   import junit.framework.TestCase;
7   
8   import java.awt.*;
9   import java.awt.geom.Ellipse2D;
10  import java.awt.geom.Rectangle2D;
11  import java.awt.image.*;
12  import java.io.File;
13  
14  /**
15   * GradientLoaderTest created on Mar 25, 2003
16   */
17  public class GradientLoaderTest extends TestCase {
18  
19      public static final String GRAD_FILE = "src/com/arranger/jarl/test/gradients.ser";
20  
21      protected int m_imageWidth = 800;
22      protected int m_imageHeight = 500;
23  
24      protected int m_circleWidth = 400;
25      protected int m_circleHeight = 300;
26  
27      protected int m_rectWidth = 650;
28      protected int m_rectHeight = 450;
29  
30      protected int m_rectWidth2 = 525;
31      protected int m_rectHeight2 = 50;
32  
33      public void testGradient() throws Exception {
34  
35          //Object[] gradients = (Object[])ObjectUtil.load(GRAD_FILE);
36          /*for (int index = 0; index < gradients.length; index++) {
37              Gradient gradient = (Gradient)gradients[index];
38              System.out.println(gradient.toString());
39          }*/
40  
41  
42          //paint a shape on to graphics (backed by an image of course)
43          //create a small little image.
44          //  paint a shape onto that image
45          //  filter that image
46          //merge the filtered image with the normal image
47  
48  
49          Image image = createImage();
50          Graphics2D graphics2D = (Graphics2D)image.getGraphics();
51          paintRectangle(graphics2D, m_rectWidth, m_rectHeight, Color.green);
52  
53          //filter a little widget
54          Image image2 = createImage();
55          Graphics2D graphics2D2 = (Graphics2D)image2.getGraphics();
56          graphics2D2.setColor(ImageUtil.TRANSPARENT);
57          paintCircle(graphics2D2, m_circleWidth, m_circleHeight);
58  
59          MarbleFilter marbleFilter = new MarbleFilter();
60          marbleFilter.setXScale(50);
61          marbleFilter.setYScale(20);
62          marbleFilter.setTurbulence(20);
63  
64          image2 = dofilter(marbleFilter, image2, m_imageWidth, m_imageHeight);
65          image2 = ImageUtil.createTransparentImage(image2, Color.black);
66          //image2 = ImageUtil.createTransparentImageInverse(image2, Color.red);
67  
68          //filter another little widget
69          /*Image image3 = createImage();
70          Graphics2D graphics2D3 = (Graphics2D)image3.getGraphics();
71          paintRectangle(graphics2D3, m_rectWidth2, m_rectWidth2, Color.BLUE);
72  
73          MarbleFilter marbleFilter2 = new MarbleFilter();
74          marbleFilter2.setXScale(10);
75          marbleFilter2.setYScale(75);
76          marbleFilter2.setTurbulence(200);
77  
78          image3 = dofilter(marbleFilter2, image3, m_imageWidth, m_imageHeight);
79          image3 = ImageUtil.createTransparentImage(image3, Color.black);
80  
81          ImageUtil.overlayTransparentImage(graphics2D, image3);*/
82          ImageUtil.overlayTransparentImage(graphics2D, image2);
83  
84          /*Color color = graphics2D.getColor();
85          Composite composite = graphics2D.getComposite();
86          //graphics2D.setColor(TRANSPARENT);
87          graphics2D.setComposite(AlphaComposite.SrcOver);
88          graphics2D.drawImage(image2, null, null);
89          //graphics2D.setColor(color);
90          graphics2D.setComposite(composite);*/
91  
92  
93          IOUtil.save(image, new File("gradientTest.bmp"));
94      }
95  
96      protected Image createImage() {
97          Image image = new BufferedImage(m_imageWidth,
98              m_imageHeight,
99              BufferedImage.TYPE_INT_RGB);
100         return image;
101     }
102 
103     protected void paintRectangle(Graphics2D graphics2D, int width, int height, Color color) {
104         Shape rectangle2D = new Rectangle2D.Double((m_imageWidth / 2) - (width / 2),
105             (m_imageHeight / 2) - (height / 2),
106             width,
107             height);
108 
109         graphics2D.setPaint(color);
110         graphics2D.fill(rectangle2D);
111 
112     }
113 
114     protected void paintCircle(Graphics2D graphics2D, int width, int height) {
115         Shape ellipse2D = new Ellipse2D.Double((m_imageWidth / 2) - (width / 2),
116             (m_imageHeight / 2) - (height / 2),
117             width,
118             height);
119 
120         graphics2D.setPaint(Color.red);
121         graphics2D.fill(ellipse2D);
122     }
123 
124     protected Image dofilter(ImageFilter imageFilter, Image image, int width, int height) throws InterruptedException {
125         image = prepareImage(image, width, height);
126         FilteredImageSource filteredImageSource = new FilteredImageSource(image.getSource(), imageFilter);
127         image = Toolkit.getDefaultToolkit().createImage(filteredImageSource);
128         ImageFilterObserver myObserver = new ImageFilterObserver();
129         image.getWidth(myObserver);
130         while (!myObserver.m_done) {
131             Thread.sleep(100);
132         }
133         return image;
134     }
135 
136     protected Image prepareImage(Image image, int width, int height) throws InterruptedException {
137         PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, width, height, true);
138         pixelGrabber.grabPixels();
139         return Toolkit.getDefaultToolkit().createImage(new MemoryImageSource(width,
140             height,
141             (int[])pixelGrabber.getPixels(),
142             0,
143             width));
144     }
145 
146     public void testShift() throws Exception {
147 
148         Color redColor = new Color(255, 0, 0, 255);
149         Color redTrans = new Color(255, 0, 0, 128);
150         int rgb = redColor.getRGB();
151         int alpha = (rgb >> 24) & 0xFF;
152         int red = (rgb >> 16) & 0xFF;
153         int green = (rgb >> 8) & 0xFF;
154         int blue = (rgb >> 0) & 0xFF;
155 
156         alpha = 128;
157         rgb = (alpha << 24)|(red << 16)|(green << 8)|(blue << 0);
158 
159         alpha = (rgb >> 24) & 0xFF;
160         red = (rgb >> 16) & 0xFF;
161         green = (rgb >> 8) & 0xFF;
162         blue = (rgb >> 0) & 0xFF;
163 
164         int rgbTrans = redTrans.getRGB();
165         assertEquals(rgbTrans, rgb);
166     }
167 
168     protected static class ImageFilterObserver implements ImageObserver {
169 
170         protected boolean m_done = false;
171 
172         public boolean imageUpdate(Image img, int infoflags,
173                                    int x, int y, int width, int height) {
174             m_done = true;
175             return false;
176         }
177     }
178 }