Source Code Cross Referenced for UploadDownload.java in  » Ajax » dwr » org » getahead » dwrdemo » files » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Ajax » dwr » org.getahead.dwrdemo.files 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2005 Joe Walker
003:         *
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         *
008:         *     http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:        package org.getahead.dwrdemo.files;
017:
018:        import java.awt.Font;
019:        import java.awt.Graphics2D;
020:        import java.awt.geom.AffineTransform;
021:        import java.awt.image.AffineTransformOp;
022:        import java.awt.image.BufferedImage;
023:        import java.io.ByteArrayOutputStream;
024:
025:        import org.directwebremoting.io.FileTransfer;
026:        import org.getahead.dwrdemo.util.ColorUtil;
027:
028:        import com.lowagie.text.Document;
029:        import com.lowagie.text.Paragraph;
030:        import com.lowagie.text.pdf.PdfWriter;
031:
032:        /**
033:         * A demonstration of uploading files and images
034:         * @author Joe Walker [joe at getahead dot ltd dot uk]
035:         */
036:        public class UploadDownload {
037:            /**
038:             * Take 2 uploaded files and return an image based on them
039:             * @param uploadImage The uploaded image
040:             * @param uploadFile The uploaded file
041:             * @param color The selected color
042:             * @return A mangled image based on the 2 uploaded files
043:             */
044:            public BufferedImage uploadFiles(BufferedImage uploadImage,
045:                    String uploadFile, String color) {
046:                uploadImage = scaleToSize(uploadImage);
047:                uploadImage = grafitiTextOnImage(uploadImage, uploadFile, color);
048:
049:                return uploadImage;
050:            }
051:
052:            /**
053:             * Generates a PDF file with the given text
054:             * http://itext.ugent.be/itext-in-action/
055:             * @return A PDF file as a byte array
056:             */
057:            public FileTransfer downloadPdfFile(String contents)
058:                    throws Exception {
059:                if (contents == null || contents.length() == 0) {
060:                    contents = "[BLANK]";
061:                }
062:
063:                ByteArrayOutputStream buffer = new ByteArrayOutputStream();
064:
065:                Document document = new Document();
066:                PdfWriter.getInstance(document, buffer);
067:
068:                document.addCreator("DWR.war using iText");
069:                document.open();
070:                document.add(new Paragraph(contents));
071:                document.close();
072:
073:                return new FileTransfer("example.pdf", "application/pdf",
074:                        buffer.toByteArray());
075:            }
076:
077:            /**
078:             * Voodoo to scale the image to 200x200
079:             * @param uploadImage The image to work on
080:             * @return The altered image
081:             */
082:            private BufferedImage scaleToSize(BufferedImage uploadImage) {
083:                AffineTransform atx = new AffineTransform();
084:                atx.scale(200d / uploadImage.getWidth(), 200d / uploadImage
085:                        .getHeight());
086:                // AffineTransformOp.TYPE_BILINEAR is very slow
087:                AffineTransformOp afop = new AffineTransformOp(atx,
088:                        AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
089:                uploadImage = afop.filter(uploadImage, null);
090:                return uploadImage;
091:            }
092:
093:            /**
094:             * And scrawl the text on the image in 10 rows of 21 chars
095:             * @param uploadImage The image to work on
096:             * @param uploadFile The text to write on the image
097:             * @param color The selected color
098:             * @return The altered image
099:             */
100:            private BufferedImage grafitiTextOnImage(BufferedImage uploadImage,
101:                    String uploadFile, String color) {
102:                StringBuilder buffer = new StringBuilder();
103:                while (buffer.length() < 200) {
104:                    buffer.append(" ");
105:                    buffer.append(uploadFile);
106:                }
107:
108:                Graphics2D g2d = uploadImage.createGraphics();
109:                for (int row = 0; row < 10; row++) {
110:                    String output = null;
111:                    if (buffer.length() > (row + 1) * CHARS_PER_LINE) {
112:                        output = buffer.substring(row * CHARS_PER_LINE,
113:                                (row + 1) * CHARS_PER_LINE);
114:                    } else {
115:                        output = buffer.substring(row * CHARS_PER_LINE);
116:                    }
117:
118:                    g2d.setFont(new Font("SansSerif", Font.BOLD, 16));
119:                    g2d.setColor(ColorUtil.decodeHtmlColorString(color));
120:                    g2d.drawString(output, 5, (row + 1) * CHARS_PER_LINE);
121:                }
122:
123:                return uploadImage;
124:            }
125:
126:            /**
127:             * 
128:             */
129:            private static final int CHARS_PER_LINE = 21;
130:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.