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

📄 omtext.java

📁 openmap java写的开源数字地图程序. 用applet实现,可以像google map 那样放大缩小地图.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
// **********************************************************************// // <copyright>// //  BBN Technologies//  10 Moulton Street//  Cambridge, MA 02138//  (617) 873-8000// //  Copyright (C) BBNT Solutions LLC. All rights reserved.// // </copyright>// **********************************************************************// // $Source: /cvs/distapps/openmap/src/openmap/com/bbn/openmap/omGraphics/OMText.java,v $// $RCSfile: OMText.java,v $// $Revision: 1.11.2.6 $// $Date: 2006/01/27 15:20:56 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.omGraphics;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Point;import java.awt.Polygon;import java.awt.Rectangle;import java.awt.Shape;import java.awt.Stroke;import java.awt.font.FontRenderContext;import java.awt.font.GlyphVector;import java.awt.geom.AffineTransform;import java.awt.geom.GeneralPath;import java.awt.geom.PathIterator;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;import com.bbn.openmap.proj.Projection;import com.bbn.openmap.util.Debug;/** * The OMText graphic type lets you put text on the screen. The location of the * string is really the location of the lower left corner of the first letter of * the string. */public class OMText extends OMGraphic implements Serializable {    // ----------------------------------------------------------------------    // Static constants    // ----------------------------------------------------------------------    /** Align the text to the right of the location. */    public final static transient int JUSTIFY_LEFT = 0;    /** Align the text centered on the location. */    public final static transient int JUSTIFY_CENTER = 1;    /** Align the text to the left of the location. */    public final static transient int JUSTIFY_RIGHT = 2;    /**     * Parameter of Font to count toward footprint of height of Text. This     * indicates that the ascent, descent and leading of the text should count     * toward the footprint of the text. This is the same as the full height of     * the FontMetric, and is the default.     */    public final static transient int HEIGHT = 0;    /**     * Parameter of Font to count toward footprint of height of Text. This     * indicates that the ascent and the descent of the text should count toward     * the footprint of the text.     */    public final static transient int ASCENT_DESCENT = 1;    /**     * Parameter of Font to count toward footprint of height of Text. This     * indicates that the ascent and the leading of the text should count toward     * the footprint of the text.     */    public final static transient int ASCENT_LEADING = 2;    /**     * Parameter of Font to count toward footprint of height of Text. This     * indicates that just the ascent of the text should count toward the     * footprint of the text.     */    public final static transient int ASCENT = 3;    /**     * Parameter that dictates where the font baseline will be set compared to     * the location of the OMText. The BASELINE_BOTTOM setting, the default,     * means that the location will be set along the normal bottom edge of the     * text where the letters rest.     */    public final static transient int BASELINE_BOTTOM = 0;    /**     * Parameter that dictates where the font baseline will be set compared to     * the location of the OMText. The BASELINE_MIDDLE setting means that the     * location will be set along the middle of the height of the text.     */    public final static transient int BASELINE_MIDDLE = 1;    /**     * Parameter that dictates where the font baseline will be set compared to     * the location of the OMText. The BASELINE_TOP setting means that the     * location will be set along the top of the height of the text.     */    public final static transient int BASELINE_TOP = 2;    public static final Font DEFAULT_FONT = new Font("SansSerif", java.awt.Font.PLAIN, 12);    /**     * The default text matte stroke that is used to surround each character     * with the color set in the textMatteColor attribute.     */    public static final Stroke DEFAULT_TEXT_MATTE_STROKE = new BasicStroke(2f);    // ----------------------------------------------------------------------    // Fields    // ----------------------------------------------------------------------    /**     * The projected xy window location of the bottom left corner of the first     * letter of the text string.     */    protected Point pt;    /** The X/Y point or the offset amount depending on render type. */    protected Point point;    /** The Font type that the string should be displayed with. */    protected Font f;    /**     * The FontSizer set in the OMText, changing the font size appropriate for a     * projection scale.     */    protected FontSizer fontSizer = null;    /**     * The latitude location for the text, used for lat/lon or offset rendertype     * texts, in decimal degrees.     */    protected float lat = 0.0f;    /**     * The longitude location for the text, used for lat/lon or offset     * rendertype texts, in decimal degrees.     */    protected float lon = 0.0f;    /** The string to be displayed. */    protected String data = null;    /**     * Justification of the string. Will let you put the text to the right,     * centered or to the left of the given location.     */    protected int justify = JUSTIFY_LEFT;    /**     * Location of the baseline of the text compared to the location point of     * the OMText object. You can set this to be BASELINE_BOTTOM (default),     * BASELINE_MIDDLE or BASELINE_TOP, depending on if you want the bottom of     * the letters to be lined up to the location, or the middle or the top of     * them.     */    protected int baseline = BASELINE_BOTTOM;    /**     * The fmHeight is the FontMetric height to use for calculating the     * footprint for the line. This becomes important for multi-line text and     * text in decluttering, because it dictates the amount of space surrounding     * the text. The default height is to take into account the ascent, descent     * and leading of the font.     */    protected int fmHeight = HEIGHT;    protected boolean useMaxWidthForBounds = false;    /** The angle by which the text is to be rotated, in radians */    protected double rotationAngle = DEFAULT_ROTATIONANGLE;    /**     * The text matte color surrounds each character of the string with this     * color. If the color is null, the text matte is not used.     */    protected Color textMatteColor;    /**     * The stroke used to paint the outline of each character. The stroke should     * be larger than 1 to give proper effect.     */    protected Stroke textMatteStroke = DEFAULT_TEXT_MATTE_STROKE;    // ----------------------------------------------------------------------    // Caches    // These fields cache computed data.    // ----------------------------------------------------------------------    /** The bounding rectangle of this Text. */    protected transient Polygon polyBounds;    /** The Metrics of the current font. */    protected transient FontMetrics fm;    /** The text split by newlines. */    protected transient String parsedData[];    /** cached string widths. */    protected transient int widths[];    // ----------------------------------------------------------------------    // Constructors    // ----------------------------------------------------------------------    /**     * Default constructor. Produces an instance with no location and an empty     * string for text. For this instance to be useful it needs text (setData),     * a location (setX, setY, setLat, setLon) and a renderType (setRenderType).     */    public OMText() {        super(RENDERTYPE_UNKNOWN, LINETYPE_UNKNOWN, DECLUTTERTYPE_NONE);        point = new Point(0, 0);        setData("");        f = DEFAULT_FONT;    }    /**     * Creates a text object, with Lat/Lon placement, and default SansSerif     * font.     *      * @param lt latitude of the string, in decimal degrees.     * @param ln longitude of the string, in decimal degrees.     * @param stuff the string to be displayed.     * @param just the justification of the string     */    public OMText(float lt, float ln, String stuff, int just) {        this(lt, ln, stuff, DEFAULT_FONT, just);    }    /**     * Creates a text object, with Lat/Lon placement.     *      * @param lt latitude of the string, in decimal degrees.     * @param ln longitude of the string, in decimal degrees.     * @param stuff the string to be displayed.     * @param font the Font description for the string.     * @param just the justification of the string     */    public OMText(float lt, float ln, String stuff, Font font, int just) {        super(RENDERTYPE_LATLON, LINETYPE_UNKNOWN, DECLUTTERTYPE_NONE);        lat = lt;        lon = ln;        setData(stuff);        f = font;        justify = just;    }    /**     * Creates a text object, with XY placement, and default SansSerif font.     *      * @param px1 horizontal window pixel location of the string.     * @param py1 vertical window pixel location of the string.     * @param stuff the string to be displayed.     * @param just the justification of the string     */    public OMText(int px1, int py1, String stuff, int just) {        this(px1, py1, stuff, DEFAULT_FONT, just);    }    /**     * Creates a text object, with XY placement.     *      * @param px1 horizontal window pixel location of the string.     * @param py1 vertical window pixel location of the string.     * @param stuff the string to be displayed.     * @param font the Font description for the string.     * @param just the justification of the string     */    public OMText(int px1, int py1, String stuff, Font font, int just) {        super(RENDERTYPE_XY, LINETYPE_UNKNOWN, DECLUTTERTYPE_NONE);        point = new Point(px1, py1);        setData(stuff);        f = font;        justify = just;    }    /**     * Creates a Text object, with lat/lon placement with XY offset, and default     * SansSerif font.     *      * @param lt latitude of the string, in decimal degrees.     * @param ln longitude of the string, in decimal degrees.     * @param offX horizontal offset of string     * @param offY vertical offset of string     * @param aString the string to be displayed.     * @param just the justification of the string     */    public OMText(float lt, float ln, int offX, int offY, String aString,            int just) {        this(lt, ln, offX, offY, aString, DEFAULT_FONT, just);    }    /**     * Creates a Text object, with lat/lon placement with XY offset.     *      * @param lt latitude of the string, in decimal degrees.     * @param ln longitude of the string, in decimal degrees.     * @param offX horizontal offset of string     * @param offY vertical offset of string     * @param aString the string to be displayed.     * @param font the Font description for the string.     * @param just the justification of the string     */    public OMText(float lt, float ln, int offX, int offY, String aString,            Font font, int just) {        super(RENDERTYPE_OFFSET, LINETYPE_UNKNOWN, DECLUTTERTYPE_NONE);        lat = lt;        lon = ln;        point = new Point(offX, offY);        setData(aString);        f = font;        justify = just;    }    /**     * Get the font of the text object, which might have been scaled by the font     * sizer.     *      * @return the font of the object.     */    public Font getFont() {        if (f == null) {            f = DEFAULT_FONT;        }        return f;    }    /**     * Set the base font. Will take effect on the next render. If the font sizer     * is not null, this font will be set in that object as well, and the active     * font will come from the font sizer. To make the set font the constant     * font, set the font sizer to null. Flushes the cache fields     * <code>fm</code>, <code>widths</code>, and <code>polyBounds</code>.     * Calls setScaledFont.     *      * @param aFont font to be used for the text.     *      * @see #fm     * @see #widths     * @see #polyBounds     */    public void setFont(Font aFont) {        if (fontSizer != null) {            fontSizer.setFont(aFont);            setScaledFont(fontSizer.getScaledFont());        } else {            setScaledFont(aFont);        }    }    /**     * Sets the scaled font, which is the one that is used for rendering.     */    protected void setScaledFont(Font aFont) {        f = aFont;        // now flush the cached information about the old font        fm = null; // flush existing metrics.        widths = null; // flush existing width table.        polyBounds = null; // flush existing bounds.    }    /**     * If the font sizer is not null, sets the scaled font with the proper value     * for the given scale.     */    public void setFont(float scale) {        if (fontSizer != null) {            setScaledFont(fontSizer.getFont(scale));        }    }    /**     * Set the FontSizer object, which provides different font sizes at     * different scales. If set to null, the font size will remain constant     * regardless of projection scale.     */    public void setFontSizer(FontSizer fs) {        Font bf = getFont();        if (fontSizer != null) {            bf = fontSizer.getFont();        }        fontSizer = fs;        setFont(bf);    }    /**     * Get the FontSizer object, which provides different font sizes at     * different scales. If set to null, the font size will remain constant     * regardless of projection scale.     */    public FontSizer getFontSizer() {        return fontSizer;    }    /**     * Get the x location. Applies to XY and OFFSET text objects.     *      * @return the horizontal window location of the string, from the left of     *         the window.     */    public int getX() {        if (point != null) {            return point.x;        } else {            return 0;        }    }    /**     * Set the x location. Applies to XY and OFFSET text objects.     *      * @param newX the horizontal pixel location of the window to place the     *        string.     */    public void setX(int newX) {        if (point == null && getRenderType() == RENDERTYPE_LATLON) {            point = new Point();            setRenderType(RENDERTYPE_OFFSET);        }        point.x = newX;        setNeedToRegenerate(true);    }    /**     * Get the y location. Applies to XY and OFFSET text objects.     *      * @return the vertical pixel location of the string, from the top of the     *         window.     */    public int getY() {        if (point != null) {            return point.y;        } else {            return 0;        }    }    /**

⌨️ 快捷键说明

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