📄 photoframe.java
字号:
/*
*
* Copyright (c) 2007, Sun Microsystems, Inc.
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Sun Microsystems nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.sun.perseus.demo.picturedecorator;
// Exceptions
import java.io.IOException;
// MIDp packages
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.m2g.SVGImage;
// JSR 226 packages
import javax.microedition.m2g.ScalableGraphics;
import org.w3c.dom.Document;
import org.w3c.dom.svg.SVGElement;
import org.w3c.dom.svg.SVGLocatableElement;
import org.w3c.dom.svg.SVGMatrix;
import org.w3c.dom.svg.SVGRect;
import org.w3c.dom.svg.SVGSVGElement;
/**
* The photo frame represents a single photo with SVG overlays.
*/
public class PhotoFrame extends Canvas {
/**
* The namespace for xlink:href
*/
public static final String XLINK_NAMESPACE_URI = "http://www.w3.org/1999/xlink";
/**
* The namespace for the svg document.
*/
public final String SVG_NAMESPACE_URI = "http://www.w3.org/2000/svg";
/** The movement step for the cursor. */
private final int CURSOR_STEP = 3;
/**
* The anchor for the background image.
*/
private final int ANCHOR = Graphics.VCENTER | Graphics.HCENTER;
/**
* The scalable graphics instance used for rendering.
*/
protected ScalableGraphics sg = ScalableGraphics.createInstance();
/**
* The main application that can load images.
*/
private final PictureDecorator picturedecorator;
/**
* The current photo number.
*/
int photoNumber = 0;
/**
* The current background image.
*/
Image currentImage = null;
/**
* The current SVG image.
*/
SVGImage svgImage = null;
/**
* The SVG document.
*/
Document doc = null;
/**
* The <svg> element.
*/
SVGSVGElement svg = null;
/**
* The current prop.
*/
SVGLocatableElement prop = null;
/**
* The list of props that have been placed and can be picked up.
*/
Vector visibleProps = new Vector();
/**
* The crosshair cursor's horizontal position.
*/
int cursorX = 0;
/**
* The crosshair cursor's vertical position.
*/
int cursorY = 0;
/**
* The ItemPicker canvas.
*/
ItemPicker itemPicker;
/**
* The ItemPicker command.
*/
Command showItemPicker;
/**
* Constructs a new photo frame.
*/
public PhotoFrame(final PictureDecorator main) {
super();
picturedecorator = main;
// There is currently no background image or SVG overlays.
currentImage = null;
svgImage = null;
doc = null;
centerCursor();
}
/**
* Centers the cursor in the canvas.
*/
private void centerCursor() {
cursorX = getWidth() / 2;
cursorY = getHeight() / 2;
}
/**
* Establishes the current background photo.
* @param number The number of the photo in the JAD file.
*/
public void setPhoto(final int number) {
if ((number < 1) || (number > picturedecorator.getMaxPhotoNumber())) {
throw new IllegalArgumentException("Bad photo number.");
}
photoNumber = number;
final String property = "Attribute-photo" + number;
//String title = picturedecorator.getImageName(property);
//setTitle(title);
currentImage = picturedecorator.getImage(property);
repaint();
}
/**
* Establishes the current prop library, which holds definitions of props in
* its <defs> section(s). Each prop is located by its ID.
* <p>
* When a prop is used on the display, a <code><use></code> element is
* inserted into the library, effectively making the prop available for
* display.
*
* @param image The SVG image containing the prop definitions. The image may
* also contain elements that are statically positioned.
*/
public void setPropsLibrary(final SVGImage image) {
// Establish or cancel the props library.
svgImage = image;
doc = null;
svg = null;
prop = null;
if (svgImage != null) {
// Establish the new library.
doc = svgImage.getDocument();
svg = (SVGSVGElement)doc.getDocumentElement();
SVGRect viewBox = svg.getRectTrait("viewBox");
viewBox.setWidth(getWidth());
viewBox.setHeight(getHeight());
svg.setRectTrait("viewBox", viewBox);
/*
* The viewport is set to the full size of the canvas. The
* image will then be moved, scaled and rotated within this
* environment.
*/
svgImage.setViewportWidth(getWidth());
svgImage.setViewportHeight(getHeight());
}
// Re-build the item picker
itemPicker = new ItemPicker(this, svgImage, picturedecorator.display);
showItemPicker = new Command("Show Picker", Command.SCREEN, 0);
addCommand(showItemPicker);
repaint();
}
/**
* Walk the current parent node downward to find all "hiddens" (The SVG
* elements within a <defs> section that have IDs) and all "visibles"
* (The SVG elements outside the <defs> sections).
*
* @param parent The relative parent node.
* @param level The nesting level (0..n).
* @param defs The <defs> nesting level.
* @param hiddens The list of elements with IDs within a <defs> section.
* @param visibles The list of elements outside a <defs> section.
*/
static void locateProps(final SVGElement parent, int level, int defs, Vector hiddens,
Vector visibles) {
SVGElement elem = (SVGElement)parent.getFirstElementChild();
while (elem != null) {
String name = elem.getLocalName();
String id = elem.getId();
// If <defs> was found, go down one more <defs> level.
if (name.equals("defs")) {
defs++;
}
if (defs > 0) {
/*
* Don't include <defs> tag. Only accept <g> tags with ID's
* at this time to keep the prop-locating simple.
*/
// If an ID was found within <defs>, record it as a usable prop.
if (!name.equals("defs") && name.equals("g") && (id != null)) {
hiddens.addElement(elem);
}
} else {
/*
* If an element is <use> or an ID'ed element outside of <defs>,
* record it as a visible prop that can potentially be picked up
* and manipulated.
*/
if (name.equals("use") || (id != null)) {
visibles.addElement(elem);
}
}
// Recurse to try to keep walking the tree downward.
locateProps(elem, level + 1, defs, hiddens, visibles);
// Walking up again. "Walk out" of a <defs> if necessary.
if (level < defs) {
defs--;
}
// Pick up the next sibling and try walking down its subtree.
elem = (SVGElement)elem.getNextElementSibling();
}
}
/**
* Adds the prop referenced by <code>id</code> in the library.
*
* @param id The identifier of the prop within a <code><defs></code>
* section of the library.
*/
public void addProp(final String id) {
prop = (SVGLocatableElement)doc.createElementNS(SVG_NAMESPACE_URI, "use");
prop.setTraitNS(XLINK_NAMESPACE_URI, "href", "#" + id);
addProp(prop);
}
/**
* Adds the prop to the SVG document.
*
* @param prop The prop to be added.
*/
public void addProp(final SVGLocatableElement newProp) {
if (newProp != null) {
svg.appendChild(newProp);
visibleProps.addElement(newProp);
// The prop is now part of the document. Pick up its bounding box.
SVGRect r = newProp.getBBox();
float rx = 0;
float ry = 0;
float rwidth = 0;
float rheight = 0;
if (r != null) {
rx = r.getX();
ry = r.getY();
rwidth = r.getWidth() / 2;
rheight = r.getHeight() / 2;
}
translateProp(newProp, cursorX - (rx + rwidth), cursorY - (ry + rheight));
repaint();
}
}
/**
* Removes the current prop if it is displayed.
*/
public void removeProp() {
if (prop != null) {
svg.removeChild(prop);
visibleProps.removeElement(prop);
prop = null;
repaint();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -