imageviewer.java
来自「一个用java写的mail.里面的代码值得我们去研究!学习。」· Java 代码 · 共 257 行
JAVA
257 行
/*** $Id: ImageViewer.java,v 1.4 2001/05/07 12:37:22 kunugi Exp $**** Copyright (c) 2000-2001 Jeff Gay** on behalf of ICEMail.org <http://www.icemail.org>** Copyright (c) 1998 by Timothy Gerard Endres** ** This program is free software.** ** You may redistribute it and/or modify it under the terms of the GNU** General Public License as published by the Free Software Foundation.** Version 2 of the license should be included with this distribution in** the file LICENSE, as well as License.html. If the license is not** included with this distribution, you may find a copy at the FSF web** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.**** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR** REDISTRIBUTION OF THIS SOFTWARE. */package org.icemail.mail;import java.awt.BorderLayout;import java.awt.Container;import java.awt.Image;import java.io.InputStream;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import javax.activation.DataHandler;import javax.activation.CommandObject;import javax.activation.FileTypeMap;import javax.activation.MimeType;import javax.activation.MimeTypeParseException;import javax.swing.JPanel;import javax.swing.JTextArea;import org.icemail.Package;/** * Class ImageViewer implements a CommandObject for the Java Activation Framework. * ImageViewer tries to instantiate com.sun.jimi.core.component.JimiCanvas from * the Sun JIMI library, which is possible if the path to the library is included * in CLASSPATH. * <p> * The JIMI library can be obtained directly from www.javasoft.com. Look for * the library under the standard extentions. * <p> * The only verb supported is "view", loading the contents of the image into * an JPanel for display. And as you've already guessed, allows the image to * be viewed by activating the panel. */public class ImageViewer extends JPanel implements CommandObject{ private static final int Debug_ = Package.DEBUG ? Package.getLevel( "ImageViewer" ) : 0; /** * Construct an empty viewer, which can be loaded later. */ public ImageViewer() { super(); setLayout( new BorderLayout() ); if ( Package.DEBUG && Package.isTraceable( "ImageViewer" ) ) { System.out.println( "ImageViewer constructed" ); } } /** * Sets the contents of the viewer from the given content and type. * <p> * One other point is that the contentType passed is MIME formatted and * can contain a charset parameter which is set to a MIME value. This * needs to be converted to a JAVA value before it can be used. * <p> * @param content data contents, possibly character encoded * @param contentType MIME formatted Content-Type header string */ public void setContent( InputStream content, String contentType ) { if ( Package.DEBUG && Package.isTraceable( "ImageViewer" ) ) { System.out.println( "ImageViewer.setContent(is,s): " + contentType ); } // cleanup and translate the mimetype for Jimi // NOTE: It's not exactly correct to use subtypes as file extensions // but it's easy to support. See .icemime and icemime.txt. String xcontenttype = contentType.toLowerCase(); if ( Package.DEBUG && Debug_ > 0 ) { System.out.println( "ImageViewer.setContent(is,s): xcontenttype-" + xcontenttype ); } try { MimeType xmimetype = new MimeType( xcontenttype ); String xfilename = "file." + xmimetype.getSubType(); FileTypeMap xmap = FileTypeMap.getDefaultFileTypeMap(); xcontenttype = xmap.getContentType( xfilename ); } catch ( MimeTypeParseException xex ) { System.out.println( "ImageViewer.setContent(is,s) exception: " + xex.toString() ); } if ( Package.DEBUG && Debug_ > 0 ) { System.out.println( "ImageViewer.setContent(is,s): xcontenttype-" + xcontenttype ); } // create a JimiCanvas from the content stream try { Object[] xparams = new Object[2]; xparams[0] = content; xparams[1] = xcontenttype; Class[] xparamtypes = new Class[2]; xparamtypes[0] = Class.forName( "java.io.InputStream" ); xparamtypes[1] = new String().getClass(); Class xjimi = Class.forName( "com.sun.jimi.core.Jimi" ); Image ximage = (Image)invoke( xjimi, "getImage", xparamtypes, xparams ); xparams = new Object[1]; xparams[0] = ximage; xparamtypes = new Class[1]; xparamtypes[0] = Class.forName( "java.awt.Image" ); Container xcontainer = (Container)instantiate( "com.sun.jimi.core.component.JimiCanvas" ); invoke( xcontainer, "setImage", xparamtypes, xparams ); add( xcontainer ); } catch ( Exception xex ) {// FIX ME; need to try for standard images; jpg, gif System.out.println( "ImageViewer.setContent(is,s) exception: " + xex.toString() ); add( new JTextArea( "Exception: " + xex.toString() + '\n' + "Get the JIMI library from www.javasoft.com to view this image type; " + xcontenttype ) ); } } /** * Initialize the command object from the data handler as described by the verb. * <p> * Implements CommandObject.setCommandContext(). * <p> * @param verb the command from the activation framework * @param dh the datahandler containing the data contents and type * @see javax.activation.CommandObject */ public void setCommandContext( String verb, DataHandler dh ) throws IOException { if ( Package.DEBUG && Package.isTraceable( "ImageViewer" ) ) { System.out.println( "ImageViewer.setCommandContext(s,dh): " + verb ); } if ( verb == null || dh == null ) throw new IOException( "Invalid parameters to ImageViewer.setCommandContext()" ); if ( ! verb.equalsIgnoreCase( "view" ) ) throw new IOException( "Only 'view' accepted to ImageViewer.setCommandContext()" ); String contentType = dh.getContentType(); InputStream content = dh.getInputStream(); setContent( content, contentType ); // content.close(); } /** * Instantiate an object by name. * Objects can only be instantiated if it has a constructor without parameters. * * @param classname the name of the class to instantiate * @return an instantiatation of the class */ static Object instantiate( String classname ) throws ClassNotFoundException, InstantiationException, IllegalAccessException { if ( Package.DEBUG && Package.isTraceable( "ImageViewer" ) ) { System.out.println( "ImageViewer.instantiate(s): " + classname ); } try { Class xclass = Class.forName( classname ); return xclass.newInstance(); } catch ( NoClassDefFoundError xex ) { throw new ClassNotFoundException( xex.toString() ); } } /** * Invoke a static method by name on the given class. * * @param theClass the class of which to invoke the static method * @param methodName the name of the static method to invoke * @param parameters null, or the specific objects to pass when invoking the static method * @return the object returned by the invoked static method, if any */ static Object invoke( Class theClass, String methodName, Class[] paramTypes, Object[] parameters ) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException , ClassNotFoundException { if ( Package.DEBUG && Package.isTraceable( "ImageViewer" ) ) { System.out.println( "ImageViewer.invoke(c,s,c[],o[]): " + theClass ); } // locate the method Method xmethod = theClass.getMethod( methodName, paramTypes ); // invoke the static method return xmethod.invoke( null, parameters ); } /** * Invoke a method by name on the given object. * * @param object the object of which to invoke the method * @param methodName the name of the method to invoke * @param parameters null, or the specific objects to pass when invoking the method * @return the object returned by the invoked method, if any */ static Object invoke( Object theObject, String methodName, Class[] paramTypes, Object[] parameters ) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException , ClassNotFoundException { if ( Package.DEBUG && Package.isTraceable( "ImageViewer" ) ) { System.out.println( "ImageViewer.invoke(o,s,c[],o[]): " + methodName ); } // locate the method// Class[] xtypes = null;// if ( parameters != null ) {// xtypes = new Class[parameters.length];// for ( int xindex = 0; xindex < parameters.length; xindex++ ) {// xtypes[xindex] = parameters[xindex].getClass();// }////xtypes[0] = Class.forName( "java.awt.Image" );// for ( int xindex = 0; xindex < xtypes.length; xindex++ ) {// System.out.println( "invoke(o,s,o[]): f-" + xtypes[xindex] );// }// } Method xmethod = theObject.getClass().getMethod( methodName, paramTypes ); // invoke the method using the given object return xmethod.invoke( theObject, parameters ); } static void dumpMethods( Class xclass ) { System.out.println( "c: " + xclass ); Method xmethods[] = xclass.getMethods(); for ( int i = 0; i < xmethods.length; i++ ) { System.out.println( " m: " + xmethods[i] ); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?