📄 tinyline.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 com.tinyline.app;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>TinyLineApplication</tt> is the J2ME Personal Profile * implementation of the SVGT Viewer. * <p> * @author (C) Andrew Girow * @version 1.9 * <p> */public class TinyLine extends Frame implements ActionListener,ItemListener, StatusBar{ // The viewer size Dimension dim; // Bounds Rectangle toolbarBounds, statusBounds, canvasBounds; // event handlers MouseHandler currentMouseHandler; // Toolbar buttons Button fileButton, goButton; // Toolbar URL input field TextField documentUrlField; // File Menu int m_x, m_y; PopupMenu m_menu = new PopupMenu(); // Function buttons MenuItem openButton; // Toogle buttons CheckboxMenuItem linkButton, panButton, zoomInButton, zoomOutButton; CheckboxMenuItem current; // Function buttons MenuItem pauseButton, refreshButton, exitButton; // Status bar Label alertLabel; // SVG canvas! PPSVGCanvas canvas; // The current Runtime Runtime r; /** Constructs the viewer */ public TinyLine() { super(); } /** Builds the viewer layout and inits the viewer */ public void init() { r = Runtime.getRuntime(); // Use the null layout. setLayout(null); setTitle("TinyLine 1.10"); setBackground(new Color(184,200,216)); setResizable(false); // Gets the insets dim = this.getSize(); Insets insets = this.getInsets(); int top = insets.top; int bottom = dim.height - insets.bottom; int left = insets.left; int right = dim.width - insets.right; // Calculates bounds // the north component toolbarBounds = new Rectangle(left, top, right - left, 30); top += 30; // the south component statusBounds = new Rectangle(left, bottom - 30, right - left, 30); bottom -= 30; // the center component canvasBounds = new Rectangle(left, top, right - left, bottom - top); // Layouts the toolbar top = toolbarBounds.y + 3; left = toolbarBounds.x + 3; // the left component fileButton = new Button("File"); fileButton.setSize(40,24); fileButton.setBounds(left, top, fileButton.getPreferredSize().width, 24); fileButton.addActionListener(this); this.add(fileButton); // the menu location m_x = fileButton.getBounds().x; m_y = fileButton.getBounds().y + fileButton.getBounds().height; left += fileButton.getSize().width + 3; // the right component goButton = new Button("Go"); goButton.setSize(30,24); right -= goButton.getPreferredSize().width + 3; goButton.setBounds(right,top,goButton.getPreferredSize().width,24); goButton.addActionListener(this); this.add(goButton); // the center component documentUrlField = new TextField(20); documentUrlField.setBounds(left, top, right - left, 24); this.add(documentUrlField); documentUrlField.setText("http://"); // Layouts the popup menu openButton = new MenuItem ("Open"); openButton.addActionListener(this); m_menu.add(openButton); m_menu.addSeparator(); linkButton = new CheckboxMenuItem ("Link",true); linkButton.addItemListener(this); m_menu.add(linkButton); current = linkButton; panButton = new CheckboxMenuItem ("Pan"); panButton.addItemListener(this); m_menu.add(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 pauseButton = new MenuItem("Pause"); pauseButton.addActionListener(this); m_menu.add(pauseButton); refreshButton = new MenuItem("Refresh"); refreshButton.addActionListener(this); m_menu.add(refreshButton); m_menu.addSeparator(); exitButton = new MenuItem("Exit"); exitButton.addActionListener(this); m_menu.add(exitButton); // Add to the frame, as the menu must have a parent component this.add(m_menu); // Layouts the SVG canvas canvas = new PPSVGCanvas(canvasBounds.width,canvasBounds.height); canvas.setBounds(canvasBounds.x,canvasBounds.y,canvasBounds.width,canvasBounds.height); add(canvas); canvas.setStatusBar(this); // Layouts the status (alert) label alertLabel = new Label(""); alertLabel.setBounds(statusBounds.x+10, statusBounds.y, statusBounds.width-20, statusBounds.height); this.add(alertLabel); alertLabel.setText("www.tinyline.com"); // Sets the mouse handler currentMouseHandler = new MouseHandler(canvas); currentMouseHandler.type = MouseHandler.LINK_MOUSE; canvas.addMouseListener(currentMouseHandler); canvas.addMouseMotionListener(currentMouseHandler); // Loads the default font!!! SVGDocument doc = canvas.loadSVG(getClass().getResourceAsStream("/com/tinyline/app/images/helvetica_svg")); SVGFontElem font = SVGDocument.getFont(doc,SVG.VAL_DEFAULT_FONTFAMILY); SVGDocument.defaultFont = font; // Adds the default events listener PlayerListener defaultListener = new PlayerListener(canvas); canvas.addEventListener("default", defaultListener, false); canvas.start(); } /** 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) { currentMouseHandler.type = MouseHandler.PAN_MOUSE; } else if(src == zoomInButton) { currentMouseHandler.type = MouseHandler.ZOOM_IN_MOUSE; } else if(src == zoomOutButton) { currentMouseHandler.type = MouseHandler.ZOOM_OUT_MOUSE; } else if(src == linkButton) { currentMouseHandler.type = MouseHandler.LINK_MOUSE; } } /** Updates this viewer */ public void update(Graphics g) { paint(g); } /** Shows an error message */ public void alertError(String s) { alertLabel.setText(s); repaint(statusBounds.x,statusBounds.y,statusBounds.width,statusBounds.height); } /** Shows a wait message */ public void alertWait(String s) { alertLabel.setText(s); repaint(statusBounds.x,statusBounds.y,statusBounds.width,statusBounds.height); } /** Shows an init message */ public void alertInit(String s) { alertLabel.setText(s); repaint(statusBounds.x,statusBounds.y,statusBounds.width,statusBounds.height); } /** Handles action events */ public void actionPerformed(ActionEvent e) {// System.out.println(""+e); Object src = e.getSource();// canvas.stop(); if(src == fileButton) { m_menu.show ( this, m_x, m_y ); } else if(src == openButton) { fileOpen(); } else if(src == pauseButton) { canvas.pauseResumeAnimations(); } else if(src == refreshButton) { canvas.origView(); } else if(src == goButton) { String selectedValue = (String) documentUrlField.getText(); if (selectedValue == null || selectedValue.equals("")) { // cut the crap values return; } else { try { r.gc(); URL svgurl = new URL(selectedValue); canvas.goURL(selectedValue); } catch (Exception e2) { return; } } } else if(src == exitButton) { System.exit(0); } } /** Open file dialog */ private void fileOpen() { FileDialog fd = new FileDialog(this, "Load file", FileDialog.LOAD); if(fd == null ) return; fd.setModal(true); fd.setVisible(true); if (fd.getFile() != null) { File file = new File(fd.getDirectory(),fd.getFile()); try { URL svgurl = new URL("file", "", slashify(file.getAbsolutePath(), file.isDirectory())); String documentUrl = svgurl.toExternalForm(); documentUrlField.setText(documentUrl); canvas.goURL(documentUrl); r.gc(); } catch (Exception e) { return; } } fd = null; } /** Slashifies the path depends on the isDirectory flag */ private static String slashify(String path, boolean isDirectory) { String p = path; if (File.separatorChar != '/') { p = p.replace(File.separatorChar, '/'); } if (!p.startsWith("/")) { p = "/" + p; } if (!p.endsWith("/") && isDirectory) { p = p + "/"; } return p; } /** * Main. Runs the viewer */ public static void main(String[] args) { TinyLine viewer = new TinyLine(); // Typical iPAQ or Zaurus dimentions 240 * 320 viewer.setBounds(0,0,240,320); // For desktops is better 480 * 480// viewer.setBounds(0,0,480,480); // The frame shoul be visible before init() in order to get right insets. viewer.setVisible(true); // Layouts the viewer components viewer.init(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -