KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > tools > plugins > DvdCover


1 /*
2  * $Id: DvdCover.java,v 1.6 2005/04/06 12:08:19 blowagie Exp $
3  * $Name: $
4  *
5  * Copyright 2005 by Bruno Lowagie.
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50 package com.lowagie.tools.plugins;
51
52 import java.awt.Color;
53 import java.io.File;
54 import java.io.FileOutputStream;
55
56 import javax.swing.JInternalFrame;
57 import javax.swing.JOptionPane;
58
59 import com.lowagie.text.Document;
60 import com.lowagie.text.DocumentException;
61 import com.lowagie.text.Element;
62 import com.lowagie.text.Image;
63 import com.lowagie.text.Rectangle;
64 import com.lowagie.text.pdf.BaseFont;
65 import com.lowagie.text.pdf.PdfContentByte;
66 import com.lowagie.text.pdf.PdfWriter;
67 import com.lowagie.tools.arguments.FileArgument;
68 import com.lowagie.tools.arguments.ImageArgument;
69 import com.lowagie.tools.arguments.PdfFilter;
70 import com.lowagie.tools.arguments.ToolArgument;
71
72 /**
73  * This is a simple tool that generates a cover for a DVD.
74  */

75 public class DvdCover extends AbstractTool {
76     
77     /**
78      * Constructs a DvdCover object.
79      */

80     public DvdCover() {
81         menuoptions = MENU_EXECUTE | MENU_EXECUTE_SHOW | MENU_EXECUTE_PRINT;
82         arguments.add(new FileArgument(this, "destfile", "The file to which the PDF has to be written", true, new PdfFilter()));
83         arguments.add(new ToolArgument(this, "title", "The title of the DVD", String.class.getName()));
84         arguments.add(new ToolArgument(this, "backgroundcolor", "The backgroundcolor of the DVD Cover (for instance 0xFFFFFF)", Color.class.getName()));
85         arguments.add(new ImageArgument(this, "front", "The front image of the DVD Cover"));
86         arguments.add(new ImageArgument(this, "back", "The back image of the DVD Cover"));
87         arguments.add(new ImageArgument(this, "side", "The side image of the DVD Cover"));
88     }
89
90     /**
91      * @see com.lowagie.tools.plugins.AbstractTool#createFrame()
92      */

93     protected void createFrame() {
94         internalFrame = new JInternalFrame("Make your own DVD Cover", true, true, true);
95         internalFrame.setSize(500, 300);
96         internalFrame.setJMenuBar(getMenubar());
97     }
98     
99     /**
100      * @see com.lowagie.tools.plugins.AbstractTool#execute()
101      */

102     public void execute() {
103         try {
104             // step 1: creation of a document-object
105
Rectangle pageSize = new Rectangle(792, 525);
106             if (getValue("backgroundcolor") != null) pageSize.setBackgroundColor((Color)getValue("backgroundcolor"));
107             Document document = new Document(pageSize);
108             // step 2:
109
// we create a writer that listens to the document
110
// and directs a PDF-stream to a file
111
if (getValue("destfile") == null) throw new DocumentException("You must provide a destination file!");
112             PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream((File)getValue("destfile")));
113                         
114             // step 3: we open the document
115
document.open();
116             
117             // step 4:
118
PdfContentByte cb = writer.getDirectContent();
119             if (getValue("title") != null) {
120                 cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, false), 24);
121                 cb.beginText();
122                 if (getValue("front") == null) {
123                     cb.showTextAligned(Element.ALIGN_CENTER, (String)getValue("title"), 600f, 262f, 0f);
124                 }
125                 if (getValue("side") == null) {
126                     cb.showTextAligned(Element.ALIGN_CENTER, (String)getValue("title"), 390f, 262f, 270f);
127                 }
128                 cb.endText();
129             }
130             cb.moveTo(376, 0);
131             cb.lineTo(376, 525);
132             cb.moveTo(416, 525);
133             cb.lineTo(416, 0);
134             cb.stroke();
135             if (getValue("front") != null) {
136                 Image front = (Image)getValue("front");
137                 front.scaleToFit(376, 525);
138                 front.setAbsolutePosition(410f + (376f - front.scaledWidth()) / 2f, (525f - front.scaledHeight()) / 2f);
139                 document.add(front);
140             }
141             if (getValue("back") != null) {
142                 Image back = (Image)getValue("back");
143                 back.scaleToFit(376, 525);
144                 back.setAbsolutePosition((376f - back.scaledWidth()) / 2f, (525f - back.scaledHeight()) / 2f);
145                 document.add(back);
146             }
147             if (getValue("side") != null) {
148                 Image side = (Image)getValue("side");
149                 side.scaleToFit(40, 525);
150                 side.setAbsolutePosition(376 + (40f - side.scaledWidth()) / 2f, (525f - side.scaledHeight()) / 2f);
151                 document.add(side);
152             }
153             
154             // step 5: we close the document
155
document.close();
156         }
157         catch(Exception e) {
158             JOptionPane.showMessageDialog(internalFrame,
159                     e.getMessage(),
160                     e.getClass().getName(),
161                     JOptionPane.ERROR_MESSAGE);
162             System.err.println(e.getMessage());
163         }
164     }
165
166     /**
167      * @see com.lowagie.tools.plugins.AbstractTool#valueHasChanged(com.lowagie.tools.arguments.ToolArgument)
168      */

169     public void valueHasChanged(ToolArgument arg) {
170         if (internalFrame == null) {
171             // if the internal frame is null, the tool was called from the commandline
172
return;
173         }
174         // represent the changes of the argument in the internal frame
175
}
176     
177     /**
178      * Generates a DVD Cover in PDF.
179      * @param args an array containing [0] a filename [1] a title [2] a backgroundcolor [3] a front image [4] a back image [5] a side image
180      */

181     public static void main(String[] args) {
182         DvdCover tool = new DvdCover();
183         if (args.length == 0) {
184             System.err.println(tool.getUsage());
185         }
186         tool.setArguments(args);
187         tool.execute();
188     }
189
190     /**
191      * @see com.lowagie.tools.plugins.AbstractTool#getDestPathPDF()
192      */

193     protected File getDestPathPDF() throws InstantiationException {
194         return (File)getValue("destfile");
195     }
196 }
Popular Tags