⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 viewercanvas.java

📁 一个用Java写的
💻 JAVA
字号:
/****************************************************************** * Copyright (C) 2002-2006 Andrew Girow. All rights reserved.     * * ---------------------------------------------------------------* * This software is published under the terms of the TinyLine     * * License, a copy of which has been included with this           * * distribution in the TINYLINE_LICENSE.TXT file.                 * *                                                                * * For more information on the TinyLine,                          * * please see <http://www.tinyline.com/>.                         * *****************************************************************/package tinyapp;import java.awt.*;import java.awt.event.*;import java.awt.image.*;import java.io.*;import java.util.*;import java.net.URL;import java.util.zip.*;import com.tinyline.tiny2d.*;import com.tinyline.svg.*;import com.tinyline.app.PPSVGImageProducer;import com.tinyline.app.PPSVGBitmap;/** * The <tt>PPSVGCanvas</tt> is the J2ME Personal Profile * implementation of the SVG Tiny Static Canvas. * <p> * @author (C) Andrew Girow * @version 1.10 * <p> */public class ViewerCanvas extends  Canvas  implements ImageLoader{    /** The canvas offscreen image */    Image bimg;    /** The canvas bounds */    int width,height;    /** The SVG renderer */    SVGRaster raster;    PPSVGImageProducer imageProducer;    /** The base URL */    URL      baseURL;    /** True if the loading is finished */    boolean loaded;    /** The current URL to go */    String   currentURL="";    Hashtable imageCash;    MediaTracker tracker;    /**     * Constructs a new PPSVGCanvas instance.     *     * @param w The width of this canvas.     * @param h The height of this canvas.     */    public ViewerCanvas(int w, int h)    {        // Creates the SVG raster        TinyPixbuf      buffer = new TinyPixbuf(w, h);        raster = new SVGRaster(buffer);        imageProducer = new PPSVGImageProducer(raster);        raster.setSVGImageProducer(imageProducer);        // Sets the SVG raster size and the color model        imageProducer.setColorModel(ColorModel.getRGBdefault());        // Sets the ImageLoader implementation needed for        // loading bitmaps        SVGImageElem.setImageLoader(this);        // Uncomment the following line for full antialiasing        raster.setAntialiased(true);        imageCash = new Hashtable();        tracker   = new MediaTracker(this);    }    /**     * Returns a TinyBitmap for the given image URL or path.     * @param imgRef The image URL or path.     * @return a TinyBitmap object which gets its pixel data from     *                         the specified URL or path.     */    public TinyBitmap createTinyBitmap(TinyString uri)    {       String imgRef = new String(uri.data);       PPSVGBitmap bitmap = null;       try       {           URL url = new URL(baseURL,imgRef);           // check in the cash           bitmap = (PPSVGBitmap)imageCash.get(url);           // not found           if(bitmap == null)           {             bitmap = new PPSVGBitmap(tracker,url);             imageCash.put(url, bitmap);           }       }       catch (Exception ex)       {       }       return bitmap;    }    /**     * Creates a TinyBitmap which decodes the image stored in the specified     * byte array, and at the specified offset and length.     * @param     imageData   an array of bytes, representing     *                         image data in a supported image format.     * @param     imageOffset  the offset of the beginning     *                         of the data in the array.     * @param     imageLength  the length of the data in the array.     * @return    a TinyBitmap object.     */    public TinyBitmap createTinyBitmap(byte[] imageData, int imageOffset, int imageLength)    {       return new PPSVGBitmap(tracker,imageData, imageOffset, imageLength);    }    /**     * Loads and dispalys an SVG document from the given URL.     * External hyperlinks handling     */    public void goURL(String url)    {        currentURL = url;        SVGDocument document = loadSVG(currentURL);        raster.setSVGDocument(document);        raster.setCamera();        raster.update();        raster.sendPixels();    }    /**     * This method is called when information about an image which was     * previously requested using an asynchronous interface becomes     * available.  Asynchronous interfaces are method calls such as     * getWidth(ImageObserver) and drawImage(img, x, y, ImageObserver)     * which take an ImageObserver object as an argument.     *     * @param     img   the image being observed.     * @param     flags the image status flags.     * @param     x   the <i>x</i> coordinate.     * @param     y   the <i>y</i> coordinate.     * @param     width    the width.     * @param     height   the height.     * @return    <code>false</code> if the infoflags indicate that the     *            image is completely loaded; <code>true</code> otherwise.     */    public boolean imageUpdate(Image img, int flags, int x, int y, int width, int height)    {        if((flags & 0x30) != 0)            repaint(x, y, width, height);        return (flags & 0x60) == 0;    }    /**     * Updates this canvas     * @param      g   the specified Graphics context.     */    public void update(Graphics g)    {          paint(g);    }    /**     * Paints this canvas     * @param      g   the specified Graphics context.     */    public void paint(Graphics g)    {//       if(!loaded) return;       if(bimg == null)       {           raster.setCamera();           raster.update();           raster.sendPixels();           bimg = createImage(imageProducer);       }       if(bimg != null)       {          g.drawImage(bimg, 0, 0, this);          Toolkit.getDefaultToolkit().sync();// ????       }    }    /**     * The minimum size of the canvas.     *     * @return The minimum size of the canvas.     */    public Dimension getMinimumSize()    {        return new Dimension(width, height);    }    /**     * The preferred size of the canvas.     *     * @return The preferred size of the canvas.     */    public Dimension getPreferredSize()    {       return new Dimension(width, height);    }    /** Flushes the allocated resources. */    public void flush()    {        raster.flush();        if(bimg!= null)        {            bimg.flush();            bimg = null;        }    }    /**     * Loads an SVGT document from the given URL.     * @param urlStr  The SVGT document URL or path.     * @return     An SVGT document.     */    public SVGDocument loadSVG(String urlStr)    {System.out.println(""+urlStr);        loaded = false;        String str = "";        SVGDocument doc = raster.createSVGDocument();        InputStream is = null;        Runtime.getRuntime().gc();        int error;        try        {            URL url = new URL(baseURL,urlStr);            baseURL = url;            is = url.openStream();            if(url.toString().endsWith("svgz"))            {                is = new GZIPInputStream(is);            }            // Reads and parses the stream            TinyPixbuf pixbuf = raster.getPixelBuffer();            SVGAttr attrParser = new SVGAttr(pixbuf.width, pixbuf.height);            SVGParser parser = new SVGParser(attrParser);            parser.load(doc,is);            str = "www.tinyline.com";            loaded = true;        }        catch( IOException ioe)        {System.out.println("ioe" + ioe);            ioe.printStackTrace();        }        catch(OutOfMemoryError memerror)        {System.out.println("Not enought memory");        }        catch(SecurityException secex)        {System.out.println("Security violation");        }        catch( Exception ex)        {System.out.println("ioe" + ex);System.out.println("Not in SVGT format");            ex.printStackTrace();        }        catch( Throwable thr)        {System.out.println("ioe" + thr);System.out.println("Not in SVGT format");            thr.printStackTrace();        }        finally        {            try            {               if (is != null) is.close();            }            catch( IOException ioe)            {System.out.println("ioe" + ioe);            }        }        return doc;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -