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

📄 shapes2applet.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.applet.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;import java.util.zip.*;import java.awt.image.*;import com.tinyline.tiny2d.*;import com.tinyline.svg.*;import com.tinyline.app.PPSVGCanvas;import com.tinyline.app.PlayerListener;import com.tinyline.app.SVGEvent;/** * The <tt>ShapesApplet</tt> is the J2ME Personal Profile * implementation of the SVGT Viewer. * <p> * @author (C) Andrew Girow * @version 1.10 * <p> */public class Shapes2Applet extends Applet  implements ActionListener, MouseListener, org.w3c.dom.events.EventListener{    String appletInfo = new String("Viewer Applet Copyright (C) 2002-2006 Andrew Girow. All rights reserved.");    // The viewer size    Dimension dim;    // Bounds    Rectangle canvasBounds;    // SVG canvas!    PPSVGCanvas canvas;    Thread thread;    Runtime r;    // The svg file t be drawn    String svgfile;    /** Constructs the viewer */    public Shapes2Applet()    {       setBackground(Color.white);    }    /** Returns information about this applet */    public String getAppletInfo()    {       return appletInfo;    }    /* Returns the applet params */    public String[][] getParameterInfo()    {        String as[][] =        {          {             "svgfile", "url", "the SVG Tiny file"          }        };        return as;    }    /**     * Called by the browser or applet viewer to inform     * this applet that it should start its execution. It is called after     * the <code>init</code> method and each time the applet is revisited     * in a Web page.     */    public void start()    {        canvas.start();        try        {           URL url = new URL(getDocumentBase(),svgfile);           canvas.goURL(url.toExternalForm());           r.gc();        }        catch(Exception e)        {        }    }    /**     * Called by the browser or applet viewer to inform     * this applet that it should stop its execution. It is called when     * the Web page that contains this applet has been replaced by     * another page, and also just before the applet is to be destroyed.     */    public void stop()    {        if(canvas!= null) canvas.stop();    }    /**     * Called by the browser or applet viewer to inform     * this applet that it is being reclaimed and that it should destroy     * any resources that it has allocated.     */    public void destroy()    {        if(canvas!= null) canvas.stop();    }    /**     * Called by the browser or applet viewer to inform     * this applet that it has been loaded into the system. It is always     * called before the first time that the <code>start</code> method is     * called.     * <p>     * Builds the viewer layout and inits the viewer     * <p>     */    public void init()    {        r = Runtime.getRuntime();        setLayout(null);        dim = getSize();        setBackground(new Color(184,200,216));        // Calculates bounds        canvasBounds = new Rectangle(0,0,dim.width,dim.height);        // Creates the SVG canvas        canvas = new PPSVGCanvas(canvasBounds.width,canvasBounds.height);        canvas.setBounds(canvasBounds.x,canvasBounds.y,canvasBounds.width,canvasBounds.height);        add(canvas);        // Sets the applet mouse handler        initMouseHandler();        canvas.addMouseListener(this);        // Gets graphics        repaint(); ///????        // Loads the default font!!!        SVGDocument doc = canvas.loadSVG(getImage("images/helvetica.svg").toExternalForm());        SVGFontElem font = SVGDocument.getFont(doc,SVG.VAL_DEFAULT_FONTFAMILY);        SVGDocument.defaultFont = font;        // Gets the 'svgfile' parameter        svgfile = this.getParameter("svgfile");        // Adds the default events listener        PlayerListener defaultListener = new PlayerListener(canvas);        canvas.addEventListener("default", defaultListener, false);        // Add the custom events listener        canvas.addEventListener("shapes", this, false);    }    /** Updates this viewer */    public void update(Graphics g)    {        paint(g);    }    /** Shows an error message */    public void alertError(String s)    {        System.out.println(s);        showStatus(s);    }    /** Shows a wait message */    public void alertWait(String s)    {        showStatus(s);    }    /** Shows an init message */    public void alertInit(String s)    {        showStatus(s);    }    /** Shows an exteranal resource */    public void showDocument(String s)    {        try        {           URL url = new URL(s);           getAppletContext().showDocument(url);        }        catch(Exception e)        {        }    }    /**     * Returns the PPSVGCanvas.     * @return an PPSVGCanvas object.     */    public PPSVGCanvas getSVGCanvas()    {        return canvas;    }    PopupMenu m_menu = new PopupMenu();    MenuItem circleButton, rectButton;    /**     * Constructs a Mouse Handler object with the given canvas. The     * handler will perform some function on the canvas when it     * is given mouse events.     *     */    public void initMouseHandler()    {    // Function buttons       circleButton = new MenuItem("Circle");       circleButton.addActionListener(this);       m_menu.add(circleButton);       rectButton = new MenuItem("Rect");       rectButton.addActionListener(this);       m_menu.add(rectButton);       // Add to applet, as menu must have a parent component       canvas.add(m_menu);    }  /*   * Popup menus are triggered differently on different systems.   * Therefore, isPopupTrigger() should be checked in both   * mousePressed() and mouseReleased() for proper cross-platform   * functionality.   */  private boolean evaluatePopup(MouseEvent e)  {//System.out.println("e " + e.paramString());       if (e.isPopupTrigger())       {///System.out.println("e.isPopupTrigger()");          // show the pop up menu...          int x = e.getX();          int y = e.getY();          m_menu.show ( canvas, x, y );          return true;       }       return false;  }  /**   * Invoked when the mouse has been clicked in the Canvas canvas.   */  public void mouseClicked(MouseEvent e)  {  }  /**   * Invoked when the mouse has been pressed in the canvas.   */  public void mousePressed(MouseEvent e)  {      if(evaluatePopup(e)) return;  }  /**   * Invoked when the mouse has been released in the canvas.   */  public void mouseReleased(MouseEvent e)  {      if(evaluatePopup(e)) return;  }  /**   * Invoked when the mouse has entered the canvas.   */  public void mouseEntered(MouseEvent e) {}  /**   * Invoked when the mouse has exited the canvas.   */  public void mouseExited(MouseEvent e) {}    /** Handles action events */    public void actionPerformed(ActionEvent e)    {        SVGEvent event;        Object src = e.getSource();        if(src == circleButton)        {           event = new SVGEvent(USER_EVENT, new TinyNumber(SHAPE_CIRCLE ));           canvas.postEvent(event);        }        else if(src == rectButton)        {           event = new SVGEvent(USER_EVENT, new TinyNumber(SHAPE_RECT ));           canvas.postEvent(event);        }    }    /** Gets an image URL by name */    private URL getImage(String name)    {        return getClass().getResource(name);    }    // The red color    TinyColor redColor    =  new TinyColor(0xFFFF0000);    // The yellow color    TinyColor yellowColor =  new TinyColor(0xFFFFFF00);    // The navy color    TinyColor navyColor   =  new TinyColor(0xFF000080);    public static final int SHAPE_CIRCLE   = 0;    public static final int SHAPE_RECT     = 1;    public static final int USER_EVENT     = 100;    /**     * Creates the circle SVG element.     */    void Circle()    {        // We should add the circle element after the 'whiteboard'        // <circle id="mycircle" cx="70" cy="100" r="30" fill="red"        // stroke="yellow" stroke-width="4"  />        //        //        // Get the raster        SVGRaster   raster   = canvas.raster;        // Get the SVGT document        SVGDocument document = raster.getSVGDocument();        // Get the root of the SVGT document        SVGSVGElem root = (SVGSVGElem)document.root;        // If there is a node with id 'mycircle' then remove it.        // Otherwise create and add it.        SVGNode node = SVGNode.getNodeById(root, new TinyString("mycircle".toCharArray()));        if(node != null)        {           // 3. Fire the update event           SVGNode parent = node.parent;           int index = parent.children.indexOf(node,0);           parent.removeChild(index);           SVGEvent event = new SVGEvent(SVGEvent.EVENT_UPDATE, node.getDevBounds(raster) );           canvas.postEvent(event);           return;        }        // Create a new circle element        SVGEllipseElem circle =          (SVGEllipseElem)document.createElement(SVG.ELEM_CIRCLE);        // Add the circle element AFTER the whiteboard element        node = SVGNode.getNodeById(root, new TinyString("whiteboard".toCharArray()));        if(node != null)        {           SVGNode parent = node.parent;           int index = parent.children.indexOf(node,0);           parent.addChild(circle, index + 1);        }        // Use the DOM-like API to change properties        TinyNumber cx = new TinyNumber(70<<TinyUtil.FIX_BITS);        TinyNumber cy = new TinyNumber(100<<TinyUtil.FIX_BITS);        TinyNumber r = new TinyNumber(30<<TinyUtil.FIX_BITS);        TinyNumber stroke_width = new TinyNumber(4<<TinyUtil.FIX_BITS);        TinyColor fillColor = redColor;        TinyColor strokeColor = yellowColor;        try        {           circle.setAttribute(SVG.ATT_ID, new TinyString("mycircle".toCharArray()));           circle.setAttribute(SVG.ATT_CX,cx);           circle.setAttribute(SVG.ATT_CY,cy);           circle.setAttribute(SVG.ATT_R,r);           circle.setAttribute(SVG.ATT_FILL,fillColor);           circle.setAttribute(SVG.ATT_STROKE,strokeColor);           circle.setAttribute(SVG.ATT_STROKE_WIDTH,stroke_width);           // Init the element.           circle.createOutline();        }        catch(Exception ex)        {           ex.printStackTrace();        }        // 3. Fire the update event        SVGEvent event = new SVGEvent(SVGEvent.EVENT_UPDATE, circle.getDevBounds(raster) );        canvas.postEvent(event);    }    /**     * Creates the rect SVG element.     */    void Rect()    {        // We should add the rect element after the 'whiteboard'        // <rect id="myrect" x="120" y="80" width="40" height="40"        // fill="yellow" stroke="navy" stroke-width="4"  />        // Get the raster        SVGRaster   raster   = canvas.raster;        // Get the SVGT document        SVGDocument document = raster.getSVGDocument();        // Get the root of the SVGT document        SVGSVGElem root = (SVGSVGElem)document.root;        // If there is a node with id 'myrect' then remove it.        // Otherwise create and add it.        SVGNode node = SVGNode.getNodeById(root, new TinyString("myrect".toCharArray()));        if(node != null)        {           // 3. Fire the update event           SVGNode parent = node.parent;           int index = parent.children.indexOf(node,0);           parent.removeChild(index);           SVGEvent event = new SVGEvent(SVGEvent.EVENT_UPDATE, node.getDevBounds(raster) );           canvas.postEvent(event);           return;        }        // Create a new rect element        SVGRectElem rect =          (SVGRectElem)document.createElement(SVG.ELEM_RECT);        // Add the rect element AFTER the whiteboard element        node = SVGNode.getNodeById(root, new TinyString("whiteboard".toCharArray()));        if(node != null)        {           SVGNode parent = node.parent;           int index = parent.children.indexOf(node,0);           parent.addChild(rect, index + 1);        }        // Use the Direct TinyLine API to change properties        rect.id = new TinyString("myrect".toCharArray());        rect.x = 120 << TinyUtil.FIX_BITS;        rect.y =  80 << TinyUtil.FIX_BITS;        rect.width = 40 << TinyUtil.FIX_BITS;        rect.height = 40 << TinyUtil.FIX_BITS;        rect.fill   = yellowColor;        rect.stroke = navyColor;        rect.strokeWidth = 4 << TinyUtil.FIX_BITS;        // Create the outline.        rect.createOutline();        // 3. Fire the update event        SVGEvent event = new SVGEvent(SVGEvent.EVENT_UPDATE, rect.getDevBounds(raster) );        canvas.postEvent(event);    }    /**     *  <b>uDOM:</b> Invoked whenever an event occurs of the type for which     *  the <tt> EventListener</tt> interface was registered.     *  @param evt The <tt>Event</tt> contains contextual information     *             about the event.     */    public void handleEvent(org.w3c.dom.events.Event evt)    {//  System.out.println("handleEvent " + evt.getType());         SVGEvent theEvent = (SVGEvent) evt;         switch(theEvent.id)         {              case Shapes2.USER_EVENT:                   TinyNumber subtype = (TinyNumber)theEvent.data;                   switch ( subtype.val )                   {                        case Shapes2.SHAPE_CIRCLE:                             Circle();                             break;                        case Shapes2.SHAPE_RECT:                             Rect();                             break;                   }                   break;         } //switch    }}

⌨️ 快捷键说明

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