omscalingraster.java

来自「OpenMap是一个基于JavaBeansTM的开发工具包。利用OpenMap你」· Java 代码 · 共 588 行 · 第 1/2 页

JAVA
588
字号
// **********************************************************************// // <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/OMScalingRaster.java,v $// $RCSfile: OMScalingRaster.java,v $// $Revision: 1.3.2.12 $// $Date: 2008/01/25 17:44:27 $// $Author: dietrick $// // **********************************************************************package com.bbn.openmap.omGraphics;import java.awt.Color;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Point;import java.awt.Rectangle;import java.awt.geom.AffineTransform;import java.awt.image.AffineTransformOp;import java.awt.image.BufferedImage;import java.io.Serializable;import javax.swing.ImageIcon;import com.bbn.openmap.proj.Projection;import com.bbn.openmap.util.Debug;/** * This is an extension to OMRaster that automatically scales itelf to match the * current projection. It is only lat/lon based, and takes the coordinates of * the upper left and lower right corners of the image. It does straight scaling - * it does not force the image projection to match the map projection! So, your * mileage may vary - you have to understand the projection of the image, and * know how it fits the projection type of the map. Of course, at larger scales, * it might not matter so much. *  * This class was inspired by, and created from parts of the ImageLayer * submission from Adrian Lumsden@sss, on 25-Jan-2002. Used the scaling and * trimming code from that submission. That code was also developed with * assistance from Steve McDonald at SiliconSpaceships.com. *  * @see OMRaster * @see OMRasterObject */public class OMScalingRaster extends OMRaster implements Serializable {    /**     * The latitude of the lower right corner for the image, in decimal degrees.     */    protected float lat2 = 0.0f;    /**     * The longitude of the lower right corner for the image, in decimal     * degrees.     */    protected float lon2 = 0.0f;    /**     * This the original version of the image, which we keep around for     * rescaling later.     */    protected BufferedImage sourceImage = null;    /**     * The rectangle in screen co-ordinates that the scaled image projects to     * after clipping.     */    protected Rectangle clipRect;    /**     * Constuct a blank OMRaster, to be filled in with set calls.     */    public OMScalingRaster() {        super();    }    // /////////////////////////////////// INT PIXELS - DIRECT    // COLORMODEL    /**     * Creates an OMRaster images, Lat/Lon placement with a direct colormodel     * image.     *      * @param ullat latitude of the top of the image.     * @param ullon longitude of the left side of the image.     * @param lrlat latitude of the bottom of the image.     * @param lrlon longitude of the right side of the image.     * @param w width of the image, in pixels.     * @param h height of the image, in pixels.     * @param pix color values for the pixels.     */    public OMScalingRaster(float ullat, float ullon, float lrlat, float lrlon,            int w, int h, int[] pix) {        super(ullat, ullon, w, h, pix);        lat2 = lrlat;        lon2 = lrlon;    }    // //////////////////////////////////// IMAGEICON    /**     * Create an OMRaster, Lat/Lon placement with an ImageIcon.     *      * @param ullat latitude of the top of the image.     * @param ullon longitude of the left side of the image.     * @param lrlat latitude of the bottom of the image.     * @param lrlon longitude of the right side of the image.     * @param ii ImageIcon used for the image.     */    public OMScalingRaster(float ullat, float ullon, float lrlat, float lrlon,            ImageIcon ii) {        this(ullat, ullon, lrlat, lrlon, ii.getImage());    }    /**     * Create an OMRaster, Lat/Lon placement with an Image.     *      * @param ullat latitude of the top of the image.     * @param ullon longitude of the left side of the image.     * @param lrlat latitude of the bottom of the image.     * @param lrlon longitude of the right side of the image.     * @param ii Image used for the image.     */    public OMScalingRaster(float ullat, float ullon, float lrlat, float lrlon,            Image ii) {        super();        setRenderType(OMGraphic.RENDERTYPE_LATLON);        setColorModel(COLORMODEL_IMAGEICON);        lat = ullat;        lon = ullon;        lat2 = lrlat;        lon2 = lrlon;        setImage(ii);    }    // //////////////////////////////////// BYTE PIXELS with    // COLORTABLE    /**     * Lat/Lon placement with a indexed colormodel, which is using a colortable     * and a byte array to contruct the int[] pixels.     *      * @param ullat latitude of the top of the image.     * @param ullon longitude of the left side of the image.     * @param lrlat latitude of the bottom of the image.     * @param lrlon longitude of the right side of the image.     * @param w width of the image, in pixels.     * @param h height of the image, in pixels.     * @param bytes colortable index values for the pixels.     * @param colorTable color array corresponding to bytes     * @param trans transparency of image.     */    public OMScalingRaster(float ullat, float ullon, float lrlat, float lrlon,            int w, int h, byte[] bytes, Color[] colorTable, int trans) {        super(ullat, ullon, w, h, bytes, colorTable, trans);        lat2 = lrlat;        lon2 = lrlon;    }    /**     * Creates a BufferedImage version of the image. A new BufferedImage object     * is created, and the image is copied into it. You can get rid of the input     * image after calling this method. The OMRaster variables height, width and     * bitmap are set here to the values for the new BufferedImage.     *      * @param image the input image.     */    public void setImage(Image image) {        if (DEBUG) {            Debug.output("OMScalingRaster.setImage: " + image);        }        if (image == null) {            bitmap = null;            sourceImage = null;            return;        }        if (!(image instanceof BufferedImage)) {            sourceImage = new BufferedImage(image.getWidth(this), image.getHeight(this), BufferedImage.TYPE_INT_ARGB);            Graphics2D g2D = sourceImage.createGraphics();            g2D.drawImage(image, 0, 0, this);        } else {            sourceImage = (BufferedImage) image;        }        width = sourceImage.getWidth();        height = sourceImage.getHeight();        // Just in case rendering tries to happen.        bitmap = sourceImage;    }    /**     * Since the image doesn't necessarily need to be regenerated when it is     * merely moved, raster objects have this function, called from generate()     * and when a placement attribute is changed.     *      * @return true if enough information is in the object for proper placement.     * @param proj projection of window.     */    protected boolean position(Projection proj) {        if (proj == null) {            if (DEBUG) {                Debug.error("OMScalingRaster: null projection in position!");            }            return false;        }        point1 = proj.forward(lat, lon);        point2 = proj.forward(lat2, lon2);        setNeedToReposition(false);        return true;    }    /**     * Prepare the graphics for rendering. For all image types, it positions the     * image relative to the projection. For direct and indexed colormodel     * images, it creates the ImageIcon used for drawing to the window (internal     * to object). For indexed colormodel images, it also calls computePixels,     * to resolve the colortable and the bytes to create the image pixels.     *      * @param proj Projection used to position the image on the window.     * @return true if the image is ready to paint.     */    public boolean generate(Projection proj) {        this.shape = null;        // Position sets the position for the OMRaster!!!!        if (!position(proj)) {            if (DEBUG) {                Debug.error("OMRaster.generate(): positioning failed!");            }            return false;        }        if (colorModel != COLORMODEL_IMAGEICON) {            // If the sourceImage hasn't been created, and needs to            // be, then just do what we normally do in OMRaster.            if (sourceImage == null || getNeedToRegenerate()) {                if (DEBUG) {                    Debug.output("OMScalingRaster: generating image");                }                super.generate(proj);                // bitmap is set to a BufferedImage                setImage(bitmap);                // Since we have a source image that is going to be reused,                // let's get rid of the memory that we won't use anymore.                pixels = null;                bits = null;            }        } else {            if (!updateImageForProjection(proj)) {                return false;            }        }        // point1 and point2 are already set in position()        // We assume that the image doesn't cross the dateline, and        // that p1 is upper left corner, and p2 is lower right.        // scaleTo modifies the internal bitmap image for display.        scaleTo(proj);        if (bitmap != null) {            // generate shape that is a boundary of the generated            // image.            // We'll make it a GeneralPath rectangle.            int w = bitmap.getWidth(this);            int h = bitmap.getHeight(this);            setShape(createBoxShape(point1.x, point1.y, w, h));            setNeedToRegenerate(false);        } else {            // Make the label go away if it is off-screen.

⌨️ 快捷键说明

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