KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dspace > app > mediafilter > Brand


1 /*
2  * Brand.java
3  *
4  * Version: $Revision: 1.1 $
5  *
6  * Date: $Date: 2006/03/10 20:30:03 $
7  *
8  * Copyright (c) 2002-2006, Hewlett-Packard Company and Massachusetts
9  * Institute of Technology. All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are
13  * met:
14  *
15  * - Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * - Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution.
21  *
22  * - Neither the name of the Hewlett-Packard Company nor the name of the
23  * Massachusetts Institute of Technology nor the names of their
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
38  * DAMAGE.
39  */

40 package org.dspace.app.mediafilter;
41
42 import java.io.File JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.awt.image.BufferedImage JavaDoc;
45 import java.awt.Color JavaDoc;
46 import java.awt.Font JavaDoc;
47 import java.awt.FontMetrics JavaDoc;
48 import java.awt.Graphics2D JavaDoc;
49 import java.awt.Rectangle JavaDoc;
50 import javax.imageio.ImageIO JavaDoc;
51 import org.apache.log4j.Logger;
52 import org.dspace.core.ConfigurationManager;
53
54 /**
55  * Class to attach a footer to an image using ImageMagick.
56  * Thanks Ninh Nguyen from the National Library of Australia for providing the source code.
57  * This version of the code is basically Ninh's but reorganised a little. Used with permission.
58  */

59
60 public class Brand
61 {
62     private int brandWidth;
63     private int brandHeight;
64     private Font JavaDoc font;
65     private int xOffset;
66
67     /**
68      * Constructor to set up footer image attributes.
69      *
70      * @param footerWidth length of the footer in pixels
71      * @param footerHeight height of the footer in pixels
72      * @param font font to use for text on the footer
73      * @param xOffset number of pixels text should be indented from left-hand side of footer
74      *
75      */

76     public Brand(int brandWidth,
77               int brandHeight,
78               Font JavaDoc font,
79               int xOffset)
80     {
81         this.brandWidth = brandWidth;
82         this.brandHeight = brandHeight;
83         this.font = font;
84         this.xOffset = xOffset;
85     }
86
87     /**
88      * Create the brand image
89      *
90      * @param brandLeftText text that should appear in the bottom left of the image
91      * @param shortLeftText abbreviated form of brandLeftText that will be substituted if
92      * the image is resized such that brandLeftText will not fit. <code>null</code> if not
93      * required
94      * @param brandRightText text that should appear in the bottom right of the image
95      *
96      * @return BufferedImage a BufferedImage object describing the brand image file
97      */

98     public BufferedImage JavaDoc create(String JavaDoc brandLeftText,
99                String JavaDoc shortLeftText,
100                String JavaDoc brandRightText)
101     {
102         BrandText[] allBrandText = null;
103
104         BufferedImage JavaDoc brandImage =
105             new BufferedImage JavaDoc(brandWidth, brandHeight, BufferedImage.TYPE_INT_RGB);
106
107         if (brandWidth >= 350)
108         {
109             allBrandText = new BrandText[]
110             {
111                 new BrandText(BrandText.BL, brandLeftText),
112                 new BrandText(BrandText.BR, brandRightText)
113             };
114         }
115         else if (brandWidth >= 190)
116         {
117             allBrandText = new BrandText[]
118             {
119                 new BrandText(BrandText.BL, shortLeftText),
120                 new BrandText(BrandText.BR, brandRightText)
121             };
122         }
123         else
124         {
125             allBrandText = new BrandText[]
126             {
127                 new BrandText(BrandText.BR, brandRightText)
128             };
129         }
130
131         if (allBrandText != null && allBrandText.length > 0)
132         {
133             for (int i = 0; i < allBrandText.length; ++i)
134             {
135                 drawImage(brandImage, allBrandText[i]);
136             }
137         }
138
139         return brandImage;
140     }
141
142
143     /**
144      * do the text placements and preparatory work for the brand image generation
145      *
146      * @param brandImage a BufferedImage object where the image is created
147      * @param identifier and Identifier object describing what text is to be placed in what
148      * position within the brand
149      */

150     private void drawImage(BufferedImage JavaDoc brandImage,
151             BrandText brandText)
152     {
153         int imgWidth = brandImage.getWidth();
154         int imgHeight = brandImage.getHeight();
155
156         int bx, by, tx, ty, bWidth, bHeight;
157
158         Graphics2D JavaDoc g2 = brandImage.createGraphics();
159         g2.setFont(font);
160         FontMetrics JavaDoc fm = g2.getFontMetrics();
161
162
163         bWidth = fm.stringWidth(brandText.getText()) + xOffset * 2 + 1;
164         bHeight = fm.getHeight();
165
166         bx = by = 0;
167
168         if (brandText.getLocation().equals(BrandText.TL))
169         {
170             bx = 0;
171             by = 0;
172         }
173         else if (brandText.getLocation().equals(BrandText.TR))
174         {
175             bx = imgWidth - bWidth;
176             by = 0;
177         }
178         else if (brandText.getLocation().equals(BrandText.BL))
179         {
180             bx = 0;
181             by = imgHeight - bHeight;
182         }
183         else if (brandText.getLocation().equals(BrandText.BR))
184         {
185             bx = imgWidth - bWidth;
186             by = imgHeight - bHeight;
187         }
188
189         Rectangle JavaDoc box = new Rectangle JavaDoc(bx, by, bWidth, bHeight);
190         tx = bx + xOffset;
191         ty = by + fm.getAscent();
192
193         g2.setColor(Color.black);
194         g2.fill(box);
195         g2.setColor(Color.white);
196         g2.drawString(brandText.getText(), tx, ty);
197     }
198 }
199
Popular Tags