📄 viewercanvas.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.io.*;import javax.microedition.io.*;import javax.microedition.lcdui.*;import java.util.Vector;import com.tinyline.tiny2d.*;import com.tinyline.svg.*;import com.tinyline.util.GZIPInputStream;import com.tinyline.app.ImageConsumer;import com.tinyline.app.MIDPSVGImageProducer;/** * This class represents an SVG canvas used for * the aimple SVG Viewer demo application. * <p> * @author (C) Andrew Girow * @version 1.10 * <p> */public class ViewerCanvas extends Canvasimplements ImageConsumer, ImageLoader{ /** * Type of the UI */ final static int TYPE_LINK = 0; // Link mode final static int TYPE_PAN = 1; // Pan mode final static int TYPE_ZOOM = 3; // Zoom mode final static int TYPE_MAXCOUNT = 4; // MAX /* The current mode */ int type = TYPE_LINK; /** * The original values to calculate pan. */ int pressedX; int pressedY; int draggedX; int draggedY; // The pan step static final int PAN_STEP = 4; static final int MENU_HEIGHT = 18; // Zooms levels private static int MAX_ZOOMLEVEL = 5; private static int MIN_ZOOMLEVEL = -5; private int zoomLevel = 0; /* The clock image */ Image wait; /** The SVG renderer */ SVGRaster raster; MIDPSVGImageProducer imageProducer; /* The current SVG document URL */ String currentURL=""; /* The current loading status */ boolean load = true; /* The current dislpay */ Display display; /* The MIDPSVGCanvas bounds */ int x,y,width,height; /* Contructor a new MIDPSVGCanvas */ public ViewerCanvas(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); SVGImageElem.setImageLoader(this); raster.setAntialiased(true); } /** * Inits this canvas */ 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(); } /** * Loads <tt>TinyBitmap</tt> raster image. * @param imgRef The raster image URL. * @return The raster image. * @ see ImageLoader Interface */ public TinyBitmap createTinyBitmap(TinyString uri) { String imgRef = new String(uri.data); TinyBitmap bitmap = new TinyBitmap(); try { if(imgRef.startsWith("..")) { // 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; } } Image image = createImage(imgRef); 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; } /** * 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; } /** * Loads and dispalys an SVG document from the given URL. * @param url The SVG document URL. * @ see LinkWalker Interface */ public void goURL(String url) { currentURL = url; SVGDocument document = loadSVG(currentURL); raster.setSVGDocument(document); raster.setCamera(); raster.update(); raster.sendPixels(); } /** * 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 stream // 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); 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; alertError("Not in SVGT format"); } finally { try { if (is != null) is.close(); if (c != null) c.close(); } catch( IOException ioe) {} } load = false; return doc; } /** * Draws the canvas * @param g The Graphics surface. */ protected void paint(Graphics g) { // pixels if(!load) { TinyPixbuf pixbuf = raster.getPixelBuffer(); // NOKIA UI Series 60/* com.nokia.mid.ui.DirectGraphics dg = com.nokia.mid.ui.DirectUtils.getDirectGraphics(g); dg.drawPixels(renderer.getPixels32(),false, 0, renderer.width, 0,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -