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

📄 decluttermatrix.java

📁 openmap java写的开源数字地图程序. 用applet实现,可以像google map 那样放大缩小地图.
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @param pixelHeight the pixel height from bottom to top.     * @param pixelAwayLimit the pixel distance away from the original     *        location that where an object will be discarded if it's     *        not at least that close. -1 means find anywhere on the     *        map where the object will fit.     * @return Point of closest open space.     */    public Point setNextOpen(Point point, int pixelLength, int pixelHeight,                             int pixelAwayLimit) {        Debug.message("declutterdetail",                "DeclutterMatrix: Trying to find an open space.");        if (needToRecreate)            create();        boolean set = false;        // mark the original spot. These are indexes, not pixels.        int windex = point.x / x_pix_interval;        int hindex = point.y / y_pix_interval;        // intermediate values used for ew/ns spanning. This are not        // pixels, they are indexes into the matrix        int xpoint;        int ypoint;        // The point to be returned with the new, declutter postition        // for the object.        Point ret = null;        // Test point, for memory allocation savings.        Point testPoint = new Point();        // A round is a cycle through the positions in the search        // algorithm. With every round the distance from the original        // position increases. The search pattern looks like an        // expanding square, and it's broken into different pieces:        // Each side, not including the corners, and each corner.        int round = 0; // Round 1, round 2 - get it!?!?        int pos, i;        // Set up the indexes for the original spot.        indexes.setFromPixels(point.x, point.y, pixelLength, pixelHeight);        // Make sure the graphic is on the visible screen        if (matrix == null || !indexes.withinMatrix) {            return point;        }        // Do check for the center here, before looping. Who knows,        // the space might be open.        ret = isAreaClearR(windex, hindex, testPoint);        int round_limit;        if (pixelAwayLimit < 0) {            round_limit = Math.abs(maxy / 2 - indexes.yStart) + (maxy / 2);        } else {            round_limit = pixelAwayLimit;        }        // Now that we know it is not open, move on and start        // searching for        // the right place.        while (ret == null) {            Debug.message("declutterdetail", "DeclutterMatrix: round " + round                    + "\n");            round++;            for (pos = 0; (dcPos[pos].position != DCP_MIDDLE) && (ret == null); pos++) {                // Need to do this based on the length, so as to skip                // unnecessary checks. The xpoint and ypoint are the                // starting point for each little incremental search.                // This point, in every round, spirals outward from                // the original desired location.                xpoint = windex + (round * dcPos[pos].ewindex);                ypoint = hindex + (round * dcPos[pos].nsindex);                // checks to keep starting point away from being                // offscreen                if (xpoint <= maxx && ypoint <= maxy && xpoint >= 0                        && ypoint >= 0) {                    // Start at handling the parts of the search                    // square that make up the sides of the square -                    // DCD_NS refers to the fact that the search                    // pattern is traversing up the side of the                    // square, for either side.                    // for the east and west checks, look                    // a north and south variations                    if (dcPos[pos].direction == DCD_NS) {                        for (i = -1 * (round - 1); (i < round - 1) && !set; i++) {                            // isAreaClearR used to be used here in                            // order to prevent a name from being                            // written over an icon. You know, if the                            // icon shouldn't be written over, let it                            // be added to the matrix before the name.                            // Then it won't be covered.                            //                          // Don't look both ways if directly to                            // the                            //                          // east, you'll write over the icon                            //                          if(dcPos[pos].position == DCP_EAST &&                            //                             i <= pixelHeight) {                            //                              ret = isAreaClearR(xpoint,                            //                                                 ypoint+i,                            //                                                 testPoint);                            //                          } else {                            ret = isAreaClearBW(xpoint, ypoint + i, testPoint);                            //                          }                            // If we've found a clear spot, jump out.                            if (ret != null)                                break;                        }                    } else {                        // Now we're checking the top and bottom of                        // the search square, which moves East-West                        // for the north and south check, look                        // at EW variations                        if (dcPos[pos].direction == DCD_EW) {                            for (i = round - 1; (i >= -1 * (round - 1)) && !set; i--) {                                ret = isAreaClearBW(xpoint + i,                                        ypoint,                                        testPoint);                                // If we've found a clear spot, jump                                // out.                                if (ret != null)                                    break;                            }                        } else {                            // This part of the code handles the                            // corners of the square.                            // looking at the corners for the                            // search pattern                            ret = isAreaClearBW(xpoint, ypoint, testPoint);                        }                    }                }            }            if (round > round_limit) {                break;            }        } //while (ret == null)        if (ret != null) {            if (Debug.debugging("declutter")) {                Debug.output("Placing object at " + ret.x + " | " + ret.y);            }        } else {            Debug.message("declutter", "Decluttering: No space for entry.");            // If you got here, space not found, toss            // oject offscreen            ret = testPoint;            ret.x = -1 * windex;            ret.y = -1 * hindex;        }        return ret;    }    /*****************************************************************     * Protected Methods     ****************************************************************/    /**     * Check to see if there is space to the right of the desired     * place.     *      * @return point for a good clear space, null if not.     */    protected Point isAreaClearR(int xPoint, int yPoint, Point point) {        Debug.message("declutterdetail",                "Decluttering: Checking to the right...");        if (!indexes.setFromPixels(xPoint, yPoint)) {            return null;        }        if (isClear(indexes, true)) {            point.x = xPoint;            point.y = yPoint;            Debug.message("declutterdetail",                    "*******Decluttering: found a spot");            return point;        }        return null;    }    /**     * Check to see if there is space to the left and right. This just     * checks for all the way to the right, and all the way to the     * left.     *      * @return Point for a good space, null if not.     */    protected Point isAreaClearBWT(int xPoint, int yPoint, Point point) {        Debug.message("declutterdetail", "Decluttering: Checking both ways...");        if (!indexes.setFromPixels(xPoint, yPoint)) {            return null;        }        if (isClear(indexes, true)) {            point.x = xPoint;            point.y = yPoint;            Debug.message("declutterdetail",                    "*******Decluttering: found a spot");            return point;        }        int leftMostIndex = indexes.origXIndex - indexes.origIndexLength;        if (!indexes.set(leftMostIndex, indexes.origYIndex)) {            return null;        }        if (isClear(indexes, true)) {            point.x = leftMostIndex * x_pix_interval;            point.y = yPoint;            Debug.message("declutterdetail",                    "*******Decluttering: found a spot");            return point;        }        return null;    }    /**     * Looks both ways for a clear space BW = Both ways = look both     * ways = check left and right. This method will look all the way     * to the rigth, and then check incrementally to the left until     * it's looking all the way there.     *      * @return point of some good place is found, null if not.     */    protected Point isAreaClearBW(int xPoint, int yPoint, Point point) {        Debug.message("declutterdetail", "Decluttering: Checking both ways...");        // Check to see if it's totally offscreen. If it is, keep it        // there.. Also check to see if the first location is good        // (isClear)        if (!indexes.setFromPixels(xPoint, yPoint) || isClear(indexes, true)) {            point.x = xPoint;            point.y = yPoint;            return point;        }        // Guess not. Step our way to the left to see if anything is        // available...        // We're going to test the right, and then work our way left        // until the original spot is the rightmost spot.        int leftMostIndex = indexes.origXIndex - indexes.origIndexLength;        // Start by keeping track of clear vertical cells. If we get        // a run of them that equals the origIndexLength, then we have        // space right there.        int count = 0;        int currentXIndex = indexes.origXIndex + indexes.origIndexLength;        while (count < indexes.origIndexLength && currentXIndex > leftMostIndex) {            if (!indexes.set(currentXIndex, indexes.origYIndex)) {                // Still off the matrix                count = 0;                currentXIndex--;                continue;            }            if (currentXIndex >= 0 && currentXIndex <= maxx) {                if (!isMatrixLocationTaken(currentXIndex,                        indexes.yStart,                        indexes.yEnd - indexes.yStart + 1)) {                    count++; // clear column                } else {                    count = 0; // Start counting again                }            } else {                // If we are off the matrix, check and see if we want                // to consider those space as open autmatically.                if (allowPartials) {                    count++;                } else {                    // This will not, Because it makes it look like                    // we ran out of space.                    count = 0;                }            }            if (count < indexes.origIndexLength) {                currentXIndex--;            }        }        // So, either we ran out of space, or we found a space big        // enough for the text.        if (count >= indexes.origIndexLength) {            point.x = currentXIndex * x_pix_interval;            point.y = yPoint;            indexes.xStart = currentXIndex;            setTaken(indexes);            Debug.message("declutterdetail", "Decluttering: found a spot");            return point;        }        // Ran out of space.        return null;    }    private static java.awt.Graphics2D workingGraphics = null;    /**     * This is a graphics that is only available to fiddle around with     * text and fonts, in order to get pre-measurements. DO NOT write     * anything into this thing.     *      * @return java.awt.Graphics2D     */    public static java.awt.Graphics2D getGraphics() {        if (workingGraphics == null) {            BufferedImage bi = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();            workingGraphics = ge.createGraphics(bi);        }        return workingGraphics;    }}

⌨️ 快捷键说明

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