magicplanetimagecomponent.java

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

JAVA
1,090
字号
    }    /**     * @param l unix time in milliseconds     * @return String representing file name for the given time.     */    protected String getFileNameForTime(long l) {        Calendar cal = new GregorianCalendar();        cal.setTimeInMillis(l);        DecimalFormat twoDigits = new DecimalFormat("00");        String tMarker = Integer.toString(cal.get(Calendar.YEAR))                + twoDigits.format(cal.get(Calendar.MONTH) + 1)                + twoDigits.format(cal.get(Calendar.DAY_OF_MONTH))                + twoDigits.format(cal.get(Calendar.HOUR_OF_DAY))                + twoDigits.format(cal.get(Calendar.MINUTE))                + twoDigits.format(cal.get(Calendar.SECOND));        return tMarker + "."                + getImageFormatter().getFormatLabel().toLowerCase();    }    /**     * Decode the file name to see what time the file was created.     *      * @param fileName     * @return milliseconds from unix epoch.     * @throws NumberFormatException if the filename can't be decoded.     */    protected long getTimeForFileName(String fileName)            throws NumberFormatException {        int dotIndex = fileName.indexOf(".");        if (dotIndex == -1) {            // Not something we care about            throw new NumberFormatException();        }        fileName = fileName.substring(0, dotIndex);        if (fileName.length() == 14) {            // Fits our naming convention.            int year = Integer.parseInt(fileName.substring(0, 4));            int month = Integer.parseInt(fileName.substring(4, 6));            int day = Integer.parseInt(fileName.substring(6, 8));            int hour = Integer.parseInt(fileName.substring(8, 10));            int minute = Integer.parseInt(fileName.substring(10, 12));            int sec = Integer.parseInt(fileName.substring(12));            if (false && DEBUG) {                Debug.output(year + " " + month + " " + day + " " + hour + " "                        + minute + " " + sec);            }            return new GregorianCalendar(year, month - 1, day, hour, minute, sec).getTimeInMillis();        }        throw new NumberFormatException();    }    /**     * Remove old files. Checks the current time against the timestamps decoded     * by the names of files found in the output directory, and deletes them if     * the difference between those times is greater than the cleanupInterval.     *      * @param deleteAll if true, all images will be deleted, regardless of when     *        they were created.     */    public void cleanup(boolean deleteAll) {        long currentTime = System.currentTimeMillis();        File file = new File(getOutputDirectoryString());        if (file.isDirectory()) {            File[] files = file.listFiles();            for (int i = 0; i < files.length; i++) {                File f = files[i];                if (!deleteAll) {                    try {                        long ft = getTimeForFileName(f.getName());                        long tdiff = currentTime - ft;                        if (DEBUG) {                            Debug.output("MagicPlanetImageComponent considering deleting "                                    + f.getName()                                    + ", file time:"                                    + ft                                    + ", current time:"                                    + currentTime                                    + ", interval:"                                    + getCleanupInterval()                                    + ", diff:" + tdiff);                        }                        if (tdiff > getCleanupInterval()) {                            if (DEBUG)                                Debug.output("   deleting...");                            f.delete();                        }                    } catch (NumberFormatException nfe) {                        // skip it.                    }                } else {                    f.delete();                }            }        }    }    /*     * (non-Javadoc)     *      * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)     */    public void propertyChange(PropertyChangeEvent evt) {        if (evt.getPropertyName() == MapBean.BackgroundProperty) {            setBackground((Paint) evt.getNewValue());        }    }    /**     * Set the 'ocean' color of the planet. The MagicPlanetImageComponent     * listens for events from the MapBean and will call this method if the     * ocean color on the MapBean is changed.     *      * @param paint     */    protected void setBackground(Paint paint) {        background = paint;        handleUpdate();    }    /**     * Return the 'ocean' color of the planet.     *      * @return Returns the background.     */    public Paint getBackground() {        return background;    }    public Layer[] getLayers() {        return layers;    }    public void setLayers(Layer[] layers) {        this.layers = layers;        handleUpdate();    }    public String getOutputDirectoryString() {        return outputDirectoryString;    }    /**     * Set the directory where the images should be written to.     *      * @param outputDirectoryString     */    public void setOutputDirectoryString(String outputDirectoryString) {        this.outputDirectoryString = outputDirectoryString;        try {            File dir = new File(outputDirectoryString);            if (dir.exists() || dir.mkdirs()) {                return;            }        } catch (SecurityException se) {        }        // Ran into a problem        JOptionPane.showMessageDialog(getMapBean(),                "I can't access this directory to store the Magic Planet images in:\n"                        + outputDirectoryString                        + "\n\nPlease check the permissions for that directory.",                "Problem Creating Directory",                JOptionPane.ERROR_MESSAGE);    }    /**     * Get the image projection.     *      * @return current Projection of image.     */    public Projection getProj() {        return proj;    }    /**     * Set the image projection.     *      * @param proj     */    public void setProj(Projection proj) {        this.proj = proj;        handleUpdate();    }    /**     * @return the scale value of the projection.     */    public float getScale() {        return scale;    }    /**     * Sets the scale for the projection, which directly affects the size of the     * image. Larger numbers make smaller images. Calling this method causes the     * setProj() method to be called with the new projection to use for images.     *      * @param scale     */    public void setScale(float scale) {        this.scale = scale;        LatLonPoint center = new LatLonPoint();        LLXY llxy = new LLXY(center, scale, 2000, 1000);        Point p1 = llxy.forward(90f, -180f);        Point p2 = llxy.forward(-90f, 180f);        int w = (int) (p2.getX() - p1.getX());        int h = (int) (p2.getY() - p1.getY());        Projection proj = new LLXY(center, scale, w, h);        setProj(proj);        if (DEBUG) {            Debug.output("Created projection " + proj + " from " + p1 + ", "                    + p2);        }    }    /**     * @return check if a new image should be created if the layers or     *         background color changes.     */    public boolean isAutoUpdate() {        return autoUpdate;    }    /**     * Set whether a new image should be created immediately if the MapBean's     * layers change, or if the MapBean's background color changes. If false, a     * new image will be created on the next normal timer cycle.     *      * @param autoUpdate     */    public void setAutoUpdate(boolean autoUpdate) {        this.autoUpdate = autoUpdate;    }    public ImageFormatter getImageFormatter() {        return imageFormatter;    }    /**     * Set the ImageFormatter to use for creating the image files.     *      * @param iFormatter     */    public void setImageFormatter(ImageFormatter iFormatter) {        imageFormatter = iFormatter;        if (imageFormatter == null) {            imageFormatter = new SunJPEGFormatter();        }    }    public boolean isCleanup() {        return cleanup;    }    /**     * Set whether the component should delete old images.     *      * @param cleanup     */    public void setCleanup(boolean cleanup) {        this.cleanup = cleanup;    }    public int getCleanupInterval() {        return cleanupInterval;    }    /**     * Set the interval, in milliseconds, between the current time and the time     * old images were created before they are deleted. This setting only     * matters if isCleanup() returns true.     *      * @param cleanupInterval     */    public void setCleanupInterval(int cleanupInterval) {        this.cleanupInterval = cleanupInterval;    }    public int getHeight() {        return height;    }    /**     * Set the scaled pixel height of the images.-1 maintains what the scale     * setting decides.     *      * @param height pixels.     */    public void setHeight(int height) {        this.height = height;    }    public int getWidth() {        return width;    }    /**     * Set the scaled pixel width of the images. -1 maintains what the scale     * setting decides.     *      * @param width pixels.     */    public void setWidth(int width) {        this.width = width;    }    /**     * Get the location of a file that can be read to find out the name of the     * last image to be created. If null, that means no such file is being     * created.     *      * @return the file name.     */    public String getLastImageFile() {        return lastImageFile;    }    /**     * Set the location of a file that can be read to find out the name of the     * last image to be created. If null, that means no such file is being     * created.     *      * @param lastImageFile     */    public void setLastImageFile(String lastImageFile) {        this.lastImageFile = checkTrimAndNull(lastImageFile);    }    public String getPostProcessingScript() {        return postProcessingScript;    }    public void setPostProcessingScript(String postProcessingScript) {        this.postProcessingScript = checkTrimAndNull(postProcessingScript);    }    protected String checkTrimAndNull(String s) {        if (s != null) {            s = s.trim();            if (s.length() == 0) {                s = null;            }        }        return s;    }}

⌨️ 快捷键说明

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