📄 shapes.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.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;import java.util.zip.*;import java.awt.image.*;import com.tinyline.svg.*;import com.tinyline.tiny2d.*;/** * The <tt>Shapes</tt> application demostrates how to create basic * SVG shapes using the DOM-like API and the Direct TinyLine API. * <p> * This code is based on Kevin Lindsey SVG Tutorial that you can * find here at * http://www.kevlindev.com/tutorials/basics/shapes/svg/ * <p> * @author (C) Andrew Girow * @version 1.10 * <p> */public class Shapes extends Frame implements Runnable,ActionListener{ // The menu MenuBar menuBar1 = new MenuBar(); Menu menuFile = new Menu(); MenuItem menuExit; MenuItem menuCircle, menuRect; Thread thread; // The SVG canvas ViewerCanvas canvas; // The red color TinyColor redColor = new TinyColor(0xFFFF0000); // The yellow color TinyColor yellowColor = new TinyColor(0xFFFFFF00); // The navy color TinyColor navyColor = new TinyColor(0xFF000080); /** * Constructs a new <tt>Shapes</tt> instance. */ public Shapes() { } /** Starts the event queue thread */ public synchronized void start() { thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.start(); } /** Stops the event queue thread */ public synchronized void stop() { // renderer.stop(); thread = null; } public void run() { setLayout(null); setTitle("Shapes SVG Demo"); // Create the SVG canvas Dimension dim = getSize(); canvas = new ViewerCanvas(dim.width-10,dim.height-10); canvas.setBounds(5,5,dim.width-10,dim.height-10); add(canvas); repaint(); // Create and add menu items menuFile.setLabel("File"); menuExit = new MenuItem("Exit"); menuExit.addActionListener(this); menuCircle = new MenuItem("Circle"); menuCircle.addActionListener(this); menuRect = new MenuItem("Rect"); menuRect.addActionListener(this); menuFile.add(menuCircle); menuFile.add(menuRect); menuFile.add(menuExit); menuBar1.add(menuFile); setMenuBar(menuBar1); // This tells the canvas that it can draw the image. // Loads the default font!!! SVGDocument doc = canvas.loadSVG(getClass().getResource("/tinyapp/images/helvetica.svg").toExternalForm()); SVGFontElem font = SVGDocument.getFont(doc,SVG.VAL_DEFAULT_FONTFAMILY); SVGDocument.defaultFont = font; canvas.goURL( getClass().getResource("/tinyapp/images/shapes.svg").toExternalForm() ); } /** * Performs the action defined for menu items. */ public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if(source == menuExit) System.exit(0); else if(source == menuCircle) Circle(); else if(source == menuRect) Rect(); } /** * 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); // Repaint the raster. raster.setDevClip(node.getDevBounds(raster)); raster.update(); raster.sendPixels(); 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(); } // Repaint the raster. raster.setDevClip(circle.getDevBounds(raster)); raster.update(); raster.sendPixels(); } /** * 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); // Repaint the raster. raster.setDevClip(node.getDevBounds(raster)); raster.update(); raster.sendPixels(); 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(); // Repaint the raster. raster.setDevClip(rect.getDevBounds(raster)); raster.update(); raster.sendPixels(); } /** * Main. Runs the Shapes sample appplication */ public static void main(String[] args) { Shapes shape = new Shapes(); // Typical iPAQ or Zaurus dimentions 240 * 320 shape.setBounds(0,0,240,320); shape.start(); // For desktops is better 480 * 480 // shape.setBounds(0,0,480,480); // Loads the background SVGT image shape.setVisible(true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -