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

📄 viewerapplet.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.*;/** * The <tt>TinyLineApplet</tt> is the J2ME Personal Profile * implementation of the SVGT Viewer. * <p> * @author (C) Andrew Girow * @version 1.10 * <p> */public class ViewerApplet extends Applet  implements Runnable, ActionListener, ItemListener, MouseListener, MouseMotionListener{    String appletInfo = new String("Viewer Applet Copyright (C) 2002-2005 Andrew Girow. All rights reserved.");    // The viewer size    Dimension dim;    // Bounds    Rectangle canvasBounds;    // SVG canvas!    ViewerCanvas canvas;    Thread thread;    Runtime r;    // The svg file t be drawn    String svgfile;    /** Constructs the viewer */    public ViewerApplet()    {       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 synchronized void start()    {        thread = new Thread(this);        thread.setPriority(Thread.MIN_PRIORITY);        thread.start();    }    public void run()    {        try        {           URL url = new URL(getCodeBase(),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 synchronized void stop()    {        thread = null;    }    /**     * 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()    {    }    /**     * 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 ViewerCanvas(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);        canvas.addMouseMotionListener(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");    }    /** 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 ViewerCanvas getSVGCanvas()    {        return canvas;    }    PopupMenu m_menu = new PopupMenu();    // Toogle buttons    CheckboxMenuItem  linkButton, panButton, zoomInButton, zoomOutButton;    CheckboxMenuItem  current;    MenuItem refreshButton, helpButton;    /**     * Type of the MouseHandler     */    public static final int PAN_MOUSE       = 1;    public static final int ZOOM_IN_MOUSE   = 2;    public static final int ZOOM_OUT_MOUSE  = 3;    public int type = PAN_MOUSE;    /**     * The original values to calculate pan.     */    int pressedX;    int pressedY;    int draggedX;    int draggedY;    /**     * 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()    {        // Setup the menu        panButton  = new CheckboxMenuItem ("Pan", true);        panButton.addItemListener(this);        m_menu.add(panButton);        current = panButton;        zoomInButton = new CheckboxMenuItem ("Zoom In");        zoomInButton.addItemListener(this);        m_menu.add(zoomInButton);        zoomOutButton = new CheckboxMenuItem ("Zoom Out");        zoomOutButton.addItemListener(this);        m_menu.add(zoomOutButton);        m_menu.addSeparator();        // Function buttons        refreshButton = new MenuItem("Refresh");        refreshButton.addActionListener(this);        m_menu.add(refreshButton);        // 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;      if(type == PAN_MOUSE)      {          pressedX = e.getX();          pressedY = e.getY();          draggedX = pressedX;          draggedY = pressedY;      }   }   /**    * Invoked when the mouse has been released in the canvas.    */   public void mouseReleased(MouseEvent e)   {      if(evaluatePopup(e)) return;      if(type == ZOOM_OUT_MOUSE)          zoom(1);      else if(type == ZOOM_IN_MOUSE)          zoom(0);      else if(type == PAN_MOUSE)      {          pan(pressedX -e.getX(),pressedY -e.getY());      }   }   /**    * 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) {}   /**    * Invoked when a mouse button is pressed within the Canvas canvas and then    * dragged.    */   public void mouseDragged(MouseEvent e)   {      if(type == PAN_MOUSE)      {         drawXORLine(pressedX,pressedY,draggedX,draggedY);         draggedX = e.getX();         draggedY = e.getY();         drawXORLine(pressedX,pressedY,draggedX,draggedY);      }   }   /**    * Invoked when the mouse button has been moved in the Canvas canvas,    * when there are no buttons pressed.    */   public void mouseMoved ( MouseEvent e ) {}   public void drawXORLine(int x0, int y0, int x1, int y1)   {      Graphics g = canvas.getGraphics();      if(g!=null)      {         g.setXORMode(Color.white);         g.setColor(Color.black);         g.drawLine(x0,y0,x1,y1);         g.dispose();         g = null;      }   }   /** The default zoom levels */   private  static int MAX_ZOOMLEVEL = 5;   private  static int MIN_ZOOMLEVEL = -5;   private  int zoomLevel = 0;   /**    *  Zooms in and out the current SVGT document.    *  @return true if the zoom is allowed; otherwise return false.    */   public boolean zoom(int direction)   {      // zoom in '0' size / 2      if(direction == 0)      {          zoomLevel--;          if(zoomLevel < MIN_ZOOMLEVEL)          {             zoomLevel = MIN_ZOOMLEVEL;             return false;          }      }      else //zoom out size * 2      {          zoomLevel++;          if(zoomLevel > MAX_ZOOMLEVEL)          {              zoomLevel = MAX_ZOOMLEVEL;              return false;          }      }      SVGRect newView = new SVGRect();      SVGRect view = canvas.raster.view;      int  midX = view.x + view.width/2;      int  midY = view.y + view.height/2;      // zoom in '0' size / 2      if(direction == 0)      {          newView.width = (view.width/2);          newView.height = (view.height/2);      }      else //zoom out size * 2      {          newView.width = (view.width * 2 );          newView.height = (view.height * 2);      }      newView.x = midX - (newView.width) / 2;      newView.y = midY - (newView.height) / 2;      view.x = newView.x;      view.y = newView.y;      view.width = newView.width;      view.height = newView.height;      canvas.raster.setCamera();      canvas.raster.update();      canvas.raster.sendPixels();      return true;   }   /**    *  Returns the current SVGT document to its original view.    */   public void origView()   {      SVGRaster raster = canvas.raster;      zoomLevel = 0;      raster.view = new SVGRect(raster.origview);      canvas.raster.setCamera();      canvas.raster.update();      canvas.raster.sendPixels();   }   /**    * Pans the current SVGT document.    * @param x The distance on X coordinate.    * @param y The distance on Y coordinate.    */   public void pan(int x , int y)   {      SVGRaster raster = canvas.raster;      SVGRect view = raster.view;      SVGDocument doc = raster.getSVGDocument();      SVGNode root = doc.root;      int scale = ((SVGSVGElem)root).getCurrentScale();      view.x += TinyUtil.div(x<<TinyUtil.FIX_BITS,scale);      view.y += TinyUtil.div(y<<TinyUtil.FIX_BITS,scale);      canvas.raster.setCamera();      canvas.raster.update();      canvas.raster.sendPixels();   }   /** Handles action events */   public void actionPerformed(ActionEvent e)   {//        System.out.println(""+e);        Object src = e.getSource();//        canvas.stop();        if(src == refreshButton)        {           origView();        }   }   /** Handles item events */   public void itemStateChanged(ItemEvent e)   {        CheckboxMenuItem src = (CheckboxMenuItem)e.getSource();// System.out.println("src" + src);        if (current != null && current != src)        {           current.setState(false);        }        current = src;        current.setState(true);        if(src == panButton)        {           type = PAN_MOUSE;        }        else if(src == zoomInButton)        {           type = ZOOM_IN_MOUSE;        }        else if(src == zoomOutButton)        {           type = ZOOM_OUT_MOUSE;        }   }   /** Gets an image URL by name */   private URL getImage(String name)   {        return getClass().getResource(name);   }}

⌨️ 快捷键说明

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