?? midpsvgcanvas.java
字號(hào):
/****************************************************************** * 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 com.tinyline.app;import java.io.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;import java.util.*;import com.tinyline.tiny2d.*;import com.tinyline.svg.*;import com.tinyline.util.GZIPInputStream;/** * This class represents a TinyLine SVG Canvas for MIDP 2.0. * * @author <a href="mailto:andrewgirow@yahoo.com">Andrew Girow</a> * @version 1.11 */public class MIDPSVGCanvas extends Canvasimplements Runnable, ImageConsumer, ImageLoader,org.w3c.dom.events.EventTarget{ /** The SVG renderer */ public SVGRaster raster; MIDPSVGImageProducer imageProducer; /** The events queue */ SVGEventQueue eventQueue; /** The events dispatching thread */ Thread thread; /** The events listeners */ TinyVector listeners; /* The clock image */ Image wait; /* The current SVG document URL */ String currentURL=""; /* The current loading status */ boolean load = true; /* The current display */ Display display; // Records data structure Vector bookmarks; List bookmarkList; /* The image cash */ Hashtable imageCash; /* The UI modes */ final static int MODE_NEXTPREV = 0; // Navigation mode === Default mode final static int MODE_PAN = 1; // Pan mode final static int MODE_LINK = 2; // Link mode final static int MODE_ZOOM = 3; // Zoom mode final static int MODE_MAXCOUNT = 4; // MAX /* The mode index */ int index; /* The current mode */ int mode = MODE_NEXTPREV; /* The pointer device data */ int pressedX; int pressedY; int draggedX; int draggedY; static final int PAN_STEP = 4; static final int MENU_HEIGHT = 18; int x,y,width,height; /* Contructor a new MIDPSVGCanvas */ public MIDPSVGCanvas(Display display) { this.display = display; width = getWidth(); height = getHeight(); // Creates the SVG raster TinyPixbuf buffer = new TinyPixbuf(width, height); raster = new SVGRaster(buffer); imageProducer = new MIDPSVGImageProducer(raster); imageProducer.setConsumer(this); raster.setSVGImageProducer(imageProducer); // Sets the ImageLoader implementation needed for // loading bitmaps SVGImageElem.setImageLoader(this); // Uncomment the following line for full antialiasing raster.setAntialiased(true); // Creates the event queue and listeners array. eventQueue = new SVGEventQueue(); listeners = new TinyVector(4); // Creates the image cash. imageCash = new Hashtable(); } /** Starts the events dispatching thread */ public synchronized void start() { thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); } /** Stops the event dispatching thread */ public synchronized void stop() { thread = null; SVGEvent event = new SVGEvent(SVGEvent.EVENT_UNLOAD, null); postEvent(event); } /** * The events dispatching thread run() */ public void run() { Thread currentThread = Thread.currentThread(); try { while (currentThread == thread) { eventQueue.handleEvent(eventQueue.getNextEvent()); } } catch (InterruptedException e) { return; } catch( Throwable thr) { thr.printStackTrace(); alertError("Internal Error"); } } /** * Loads and dispalys an SVGT document from the given URL. * External hyperlinks handling */ synchronized public void goURL(String url) { SVGEvent event = new SVGEvent(SVGEvent.EVENT_LOAD,url); postEvent(event); } /** * Returns the current SVGT document to its original view. */ public void origView() { SVGEvent event = new SVGEvent(SVGEvent.EVENT_ORIGVIEW, null); postEvent(event); } /** * Switches the rendering quality. */ public void switchQuality() { SVGEvent event = new SVGEvent(SVGEvent.EVENT_QUALITY, null ); postEvent(event); } /** * Suspends or unsuspends all animations that are defined * within the current SVGT document fragment. */ public void pauseResumeAnimations() { SVGEvent event = new SVGEvent(SVGEvent.EVENT_PAUSERESUME, null ); postEvent(event); } /** * Inits this canvas. Loads icons */ public void init() { try { wait = Image.createImage("/tinyline/wait.png"); // Load svg font load = false; // ? More or less the same time // Faster to use HelveticaFont class // HelveticaFont.getFont(); } catch(Exception e) { alertError("Resources (helvetica.svg and/or icons) could not be loaded!"); } } /** * Delivers the pixels of the image. The pixel (px,py) is * stored in the pixels array at index (px * scansize + py + off). * @param x, y the coordinates of the upper-left corner of the * area of pixels to be set * @param w the width of the area of pixels * @param h the height of the area of pixels * @see ImageConsumer */ public void newPixels(int x, int y, int w, int h) { repaint(x,y,w,h); // paint it now! serviceRepaints(); } /** * 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); TinyBitmap bitmap = null; if( imgRef.startsWith("..") || !imgRef.startsWith("http:")) { // This is relative path, then attach the basePath int p = currentURL.lastIndexOf('/'); if(p!=-1) { imgRef = currentURL.substring(0,p)+'/' + imgRef;// System.out.println("imgRef "+imgRef); } else { return null; } } try { // check in the cash bitmap = (TinyBitmap)imageCash.get(imgRef); // not found if(bitmap == null) { Image image = createImage(imgRef); bitmap = new TinyBitmap(); bitmap.width = image.getWidth(); bitmap.height = image.getHeight(); // Grap bits bitmap.pixels32 = new int[bitmap.width * bitmap.height]; image.getRGB(bitmap.pixels32, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height); imageCash.put(imgRef, bitmap); } } catch (Exception ex) { } return bitmap; } /** * Loads <tt> TinyBitmap </tt> raster image. * @param imageData The input image data buffer. * @param imageOffset The input image data buffer pointer. * @param imageLength The input image data buffer length. * @return The raster image. * @ see ImageLoader Interface */ public TinyBitmap createTinyBitmap(byte[] imageData, int imageOffset, int imageLength) { TinyBitmap bitmap = new TinyBitmap(); try { Image image = Image.createImage(imageData, imageOffset, imageLength); bitmap.width = image.getWidth(); bitmap.height = image.getHeight(); // Grap bits bitmap.pixels32 = new int[bitmap.width * bitmap.height]; image.getRGB(bitmap.pixels32, 0, bitmap.width, 0, 0, bitmap.width, bitmap.height); } catch (Throwable thr) { // alertError(imgRef + " image could not be loaded."); return null; } return bitmap; } /** * Selects mode. * @param newmode The new mode id. */ void selectMode( int newmode) { if(mode == MODE_LINK) { SVGEvent event = new SVGEvent(SVGEvent.EVENT_FOCUSHIDE, null ); postEvent(event); } mode = newmode; if(mode == MODE_LINK) { SVGEvent event = new SVGEvent(SVGEvent.EVENT_FOCUSSHOW, null ); postEvent(event); } } /** * Loads an SVG document. * @param url The SVG document URL. * @return The loaded document. */ public SVGDocument loadSVG(String url) {// System.out.println(""+url); load = true; repaint(0, height, getWidth(), MENU_HEIGHT); SVGDocument doc = raster.createSVGDocument(); ContentConnection c = null; InputStream is = null; Runtime.getRuntime().gc(); try { if (url.startsWith("/")) { is = getClass().getResourceAsStream(url); } else if (url.startsWith("http:")) { c = (ContentConnection)Connector.open(url); is = c.openInputStream(); if(url.endsWith("svgz")) { is = new GZIPInputStream(is); } } else { alertError("Wrong URL "+ url); load = false; return doc; // The stream is not open so it is safe to return } // Read and parse the SVGT stream TinyPixbuf pixbuf = raster.getPixelBuffer(); // Create the SVGT attributes parser SVGAttr attrParser = new SVGAttr(pixbuf.width, pixbuf.height); // Create the SVGT stream parser SVGParser parser = new SVGParser(attrParser); // Parse the input SVGT stream parser into the document parser.load(doc,is); load = true; } catch( IOException ioe) { doc = null; alertError(ioe.getMessage() ); } catch(OutOfMemoryError memerror) { doc = null; alertError("Not enought memory"); Runtime.getRuntime().gc(); } catch( Throwable thr) { doc = null;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -