mapcanvas.java

来自「这是一个手机上的J2ME程序」· Java 代码 · 共 169 行

JAVA
169
字号
// J2ME Compass
// Copyright (C) 2007 Dana Peters
// http://www.qcontinuum.org/compass

package org.qcontinuum.compass;

import javax.microedition.lcdui.*;
import java.io.IOException;
import henson.midp.Float;

// derived from http://www.microjava.com/developer/fss/graphics?content_id=1835

public class MapCanvas extends Canvas implements Runnable {
    
    static private Image mMapImage;
    static private Image[] mMapImages = { null, null, null, null, null };
    static private int mImageWidth, mImageHeight;

    private Float mLatitude = new Float(0);
    private Float mLongitude = new Float(0);
    private int mFocusX, mFocusY;
    private int mGameAction;
    private long mRepeatTime;
    private int mZoom;
    private int mScreenWidth, mScreenHeight, mCenterX, mCenterY;

    final static private int mTargetRadius = 8;
    final static private int mTargetGap = 3;
    
    final static private Float cSlowRepeatIncrement = new Float(25, -2);
    final static private Float cMediumRepeatIncrement = new Float(1);
    final static private Float cFastRepeatIncrement = new Float(4);
    
    final static private Float c0_5 = new Float(5, -1);
    final static private Float c0_4 = new Float(4, -1);
    final static private Float c1_25 = new Float(125, -2);
    final static private Float c360 = new Float(360);
    final static private Float c180 = new Float(180);
    final static private Float cneg180 = new Float(-180);
    final static private Float c90 = new Float(90);
    final static private Float cneg90 = new Float(-90);
    
    public MapCanvas() {
        mScreenWidth = getWidth();
        mScreenHeight = getHeight();
        mCenterX = mScreenWidth / 2;
        mCenterY = mScreenHeight / 2;

        int zoom = 0;
        while ((1 << zoom) * 75 <= mScreenWidth * 2)
            zoom++;
        setZoom(--zoom);
        while (mMapImage == null && zoom > 0)
            setZoom(--zoom);
    }

    protected void zoomIn() {
        if (mZoom < mMapImages.length - 1) {
            setZoom(mZoom + 1);
            repaint();
        }
    }
    
    protected void zoomOut() {
        if (mZoom > 0) {
            setZoom(mZoom - 1);
            repaint();
        }
    }

    private void setZoom(int newZoom) {
        if (newZoom > mMapImages.length - 1)
            newZoom = mMapImages.length - 1;
        if (newZoom < 0)
            newZoom = 0;
        if (mMapImages[newZoom] == null) {
            try {
                mMapImages[newZoom] = Image.createImage("/org/qcontinuum/compass/Worldmap" + newZoom + ".png");
            } catch (Exception e) { }
        }
        if (mMapImages[newZoom] != null && mMapImages[newZoom].getWidth() == (1 << newZoom) * 75) {
            mZoom = newZoom;
            mMapImage = mMapImages[mZoom];
            mImageWidth = mMapImage.getWidth();
            mImageHeight = mMapImage.getHeight();
        }
    }
    
    protected void showNotify() {
        new Thread(this).start();
    }

    public Float getLatitude() {
        return mLatitude;
    }
    
    public Float getLongitude() {
        return mLongitude;
    }
    
    public void setLatitude(Float latitude) {
        mLatitude = latitude;
    }
    
    public void setLongitude(Float longitude) {
        mLongitude = longitude;
    }
    
    protected void paint(Graphics g) {
        if (mMapImage != null) {
            int x, y;
            int focusX = longitudeToX(mLongitude);
            int focusY = latitudeToY(mLatitude);
            g.setColor(255, 255, 255);
            if (mImageWidth <= mScreenWidth) {
                x = (mImageWidth - mScreenWidth) / 2;
                g.fillRect(0, 0, -x, mScreenHeight);
                g.fillRect(-x + mImageWidth, 0, -x, mScreenHeight);
            } else {
                x = focusX - mCenterX;
                if (x < 0)
                    x = 0;
                else if (x > mImageWidth - mScreenWidth)
                    x = mImageWidth - mScreenWidth;
            }
            if (mImageHeight <= mScreenHeight) {
                y = (mImageHeight - mScreenHeight) / 2;
                g.fillRect(0, 0, mScreenWidth, -y);
                g.fillRect(0, -y + mImageHeight, mScreenWidth, -y);
            } else {
                y = focusY - mCenterY;
                if (y < 0)
                    y = 0;
                else if (y > mImageHeight - mScreenHeight)
                    y = mImageHeight - mScreenHeight;
            }
            g.drawImage(mMapImage, -x, -y, Graphics.TOP|Graphics.LEFT);
            drawTarget(g, focusX - x, focusY - y);
        } else {
            g.setColor(255, 255, 255);
            g.fillRect(0, 0, mScreenWidth, mScreenHeight);
            g.setColor(0, 0, 0);
            g.drawString("Map Not", mCenterX, mCenterY, g.BOTTOM|g.HCENTER);
            g.drawString("Available", mCenterX, mCenterY, g.TOP|g.HCENTER);
        }
        g.setColor(0, 0, 0);
        g.drawString(getLatitudeString() + ", " + getLongitudeString(),
                0, mScreenHeight, Graphics.BOTTOM|Graphics.LEFT);
    }

    private void drawTarget(Graphics g, int x, int y) {
        g.drawArc(x - mTargetRadius, y - mTargetRadius, mTargetRadius * 2, mTargetRadius * 2, 0, 360);
        g.drawLine(x, y - mTargetRadius - mTargetGap, x, y - mTargetGap);
        g.drawLine(x, y + mTargetGap, x, y + mTargetRadius + mTargetGap);
        g.drawLine(x - mTargetRadius - mTargetGap, y, x - mTargetGap, y);
        g.drawLine(x + mTargetGap, y, x + mTargetRadius + mTargetGap, y);
    }

    private String getLatitudeString() {
        return degreesToString(mLatitude) + (mLatitude.Less(0) ? "S" : "N");
    }

    private String getLongitudeString() {
        return degreesToString(mLongitude) + (mLongitude.Less(0) ? "W" : "E");
    }
    
    private String degreesToString(Float degrees) {
        int minutes = (int)Float.Int(degrees.Mul(60)).toLong();
        return Math.abs(minutes / 60) + "

⌨️ 快捷键说明

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