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

📄 unlinkfishcanvas.java

📁 深入java 虚拟机中的一个Java程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        // Try left vertical line. What is the line's y value for the xRectLeft?
        // y = m(xRectLeft) + b
        float value = ((m * (float) xRectLeft) + b);

        if (value >= (float) yRectTop && value <= (float) yRectBottom) {
            return true;
        }

        // Try right vertical line. What is the line's y value for the xRectRight?
        // y = m(xRectRight) + b
        value = ((m * (float) xRectRight) + b);

        if (value >= (float) yRectTop && value <= (float) yRectBottom) {
            return true;
        }

        // Try top horizontal line. What is the line's x value for the yRectTop?
        // x = (yRecTop - b) / m
        value = (((float) yRectTop - b) / m);

        if (value >= (float) xRectLeft && value <= (float) xRectRight) {
            return true;
        }

        return false;
/*
        int xRectLeft = (x - mouseFatness) * extraZeros;
        int xRectRight = (x + mouseFatness) * extraZeros;
        int yRectTop = (y - mouseFatness) * extraZeros;
        int yRectBottom = (y + mouseFatness) * extraZeros;

        // Calculate slope of line times some factor (extraZeros). If extraZeros is 1000,
        // then the variable m will be 1000 times the slope of the line.
        int m = ((lineEnd.y - lineStart.y) * extraZeros) / (lineEnd.x - lineStart.x);

        // Calculate y intercept of line times some factor (extraZeros). If extraZeros
        // is 1000, then the variable b will be 1000 times the y intercept of the line.
        int b = (lineStart.y * extraZeros) - (m * lineStart.x);

        // Try left vertical line. What is the line's y value for the xRectLeft?
        // y = m(xRectLeft) + b
        int value = (m * (xRectLeft / extraZeros)) + b;

        if (value >=  yRectTop && value <= yRectBottom) {
            return true;
        }

        // Try right vertical line. What is the line's y value for the xRectRight?
        // y = m(xRectRight) + b
        value = (m * (xRectRight / extraZeros)) + b;

        if (value >= yRectTop && value <= yRectBottom) {
            return true;
        }

        // Try top horizontal line. What is the line's x value for the yRectTop?
        // x = (yRecTop - b) / m
        value = ((yRectTop - b) * extraZeros) / m;

        if (value >= xRectLeft && value <= xRectRight) {
            return true;
        }

        // Try bottom horizontal line. What is the line's x value for the yRectTop?
        // x = (yRecTop - b) / m
        value = ((yRectBottom - b) * extraZeros) / m;

        if (value >= xRectLeft && value <= xRectRight) {
            return true;
        }

        return false;
*/
    }

/*
    private boolean mouseOverLine(int x, int y, Point lineStart, Point lineEnd) {
        // Determine if the line defined by lineStart and lineEnd crosses a square
        // that is 2 * mouseFatness in width and height centered on the x, y mouse
        // position. Do this by looking to see if the line crosses any of 3 sides of
        // the rectangle. If not, the line does not intersect the rectangle.

        float xRectLeft = (float) (x - mouseFatness);
        float xRectRight = (float) (x + mouseFatness);
        float yRectTop = (float) (y - mouseFatness);
        float yRectBottom = (float) (y + mouseFatness);

        float xlineStart = (float) lineStart.x;
        float ylineStart = (float) lineStart.y;
        float xlineEnd = (float) lineEnd.x;
        float ylineEnd = (float) lineEnd.y;

        // Calculate slope of line
        float m = (ylineEnd - ylineStart) / (xlineEnd - xlineStart);

        // Calculate y intercept of line
        float b = ylineStart - (m * xlineStart);

        // Try left vertical line. What is the line's y value for the xRectLeft?
        // y = m(xRectLeft) + b
        float value = (m * xRectLeft) + b;

        if (value >=  yRectTop && value <= yRectBottom) {
            return true;
        }

        // Try right vertical line. What is the line's y value for the xRectRight?
        // y = m(xRectRight) + b
        value = (m * xRectRight) + b;

        if (value >= yRectTop && value <= yRectBottom) {
            return true;
        }

        // Try top horizontal line. What is the line's x value for the yRectTop?
        // x = (yRecTop - b) / m
        value = (m * xRectRight) + b;

        if (value >= xRectLeft && value <= xRectRight) {
            return true;
        }

        return true;
    }

    private boolean mouseOverLine(int x, int y, Point lineStart, Point lineEnd) {
        // Determine if the line defined by lineStart and lineEnd crosses a square
        // that is 2 * mouseFatness in width and height centered on the x, y mouse
        // position. Do this by looking to see if the line crosses any of 3 sides of
        // the rectangle. If not, the line does not intersect the rectangle.

        int xRectLeft = (x - mouseFatness) * extraZeros;
        int xRectRight = (x + mouseFatness) * extraZeros;
        int yRectTop = (y - mouseFatness) * extraZeros;
        int yRectBottom = (y + mouseFatness) * extraZeros;

        int xlineStart = lineStart.x * extraZeros;
        int ylineStart = lineStart.y * extraZeros;
        int xlineEnd = lineEnd.x * extraZeros;
        int ylineEnd = lineEnd.y * extraZeros;

        // Calculate slope of line
        int m = ylineEnd - ylineStart;
        m /= xlineEnd - xlineStart;

        // Calculate y intercept of line
        int b = ylineStart - (m * xlineStart);

        // Try left vertical line. What is the line's y value for the xRectLeft?
        // y = m(xRectLeft) + b
        int value = (m * xRectLeft) + b;

        if (value >=  yRectTop && value <= yRectBottom) {
            return true;
        }

        // Try right vertical line. What is the line's y value for the xRectRight?
        // y = m(xRectRight) + b
        value = (m * xRectRight) + b;

        if (value >= yRectTop && value <= yRectBottom) {
            return true;
        }

        // Try top horizontal line. What is the line's x value for the yRectTop?
        // x = (yRecTop - b) / m
        value = (m * xRectRight) + b;

        if (value >= xRectLeft && value <= xRectRight) {
            return true;
        }

        return false;
    }
*/
/*
    public boolean mouseDown(Event evt, int x, int y) {

        // First check to see if the mouse went down inside one of the local variable
        // rectangles.
        if (x >= xLocalVarRectStart && x < xLocalVarRectStart + localVarRectWidth
            && y >= yYellowFishLocalVarStart && y < yYellowFishLocalVarStart + localVarRectHeight) {

            yellowLocalVarClicked = true;
            return true;
        }
        if (x >= xLocalVarRectStart && x < xLocalVarRectStart + localVarRectWidth
            && y >= yBlueFishLocalVarStart && y < yBlueFishLocalVarStart + localVarRectHeight) {

            blueLocalVarClicked = true;
            return true;
        }
        if (x >= xLocalVarRectStart && x < xLocalVarRectStart + localVarRectWidth
            && y >= yRedFishLocalVarStart && y < yRedFishLocalVarStart + localVarRectHeight) {

            redLocalVarClicked = true;
            return true;
        }

        // Find out if the mouse went down inside an icon's hot area. Count down from the
        // top of the heap list so that fish that are drawn later will be found first. This
        // is because if two fish are overlapping, the one later in the array will be
        // drawn second and appear to be on top. The top fish will be found first by this
        // for loop.
        for (int i = gcHeap.getHandlePoolSize() - 1; i >= 0; --i) {
            ObjectHandle oh = gcHeap.getObjectHandle(i + 1);
            if (!oh.free) {
                Point o = oh.fish.getFishPosition();
                if (x >= o.x && x < o.x + oh.fish.getFishWidth() && y >= o.y
                    && y < o.y + oh.fish.getFishHeight()) {

                    iconClicked = true;
                    objectIndexOfFishIconThatWasClicked = i + 1;
                    posOfMouseInsideIconWhenFirstPressed.x = x - o.x;
                    posOfMouseInsideIconWhenFirstPressed.y = y - o.y;
                    break;
                }
            }
        }

        return true;
    }

    public boolean mouseUp(Event evt, int x, int y) {

        if (!iconClicked && !yellowLocalVarClicked && !blueLocalVarClicked
            && !redLocalVarClicked) {
            return true;
        }

        if (!iconClicked) {

            Color colorOfClickedLocalVar = Color.yellow;
            if (blueLocalVarClicked) {
                colorOfClickedLocalVar = Color.cyan;
            }
            else if (redLocalVarClicked) {
                colorOfClickedLocalVar = Color.red;
            }

            if (dragging) {
                dragging = false;
                // Clear old line.
                Graphics g = getGraphics();
                g.setColor(Color.blue);
                g.setXORMode(colorOfClickedLocalVar);

                int xLineStart = xLocalVarRectStart + localVarRectWidth;
                int yLineStart = yYellowFishLocalVarStart + (localVarRectHeight / 2);
                if (blueLocalVarClicked) {
                    yLineStart = yBlueFishLocalVarStart + (localVarRectHeight / 2);
                }
                else if (redLocalVarClicked) {
                    yLineStart = yRedFishLocalVarStart + (localVarRectHeight / 2);
                }

                if (!mouseIsOverAnIconThatCanBeDroppedUpon) {

                    // Clear old line
                    g.drawLine(xLineStart, yLineStart, currentMouseDragPosition.x,
                        currentMouseDragPosition.y);
                }
                else {
                    ObjectHandle oh = gcHeap.getObjectHandle(objectIndexOfIconThatCanBeDroppedUpon);

                    // Clear the rectangle
                    g.drawRect(oh.fish.getFishPosition().x, oh.fish.getFishPosition().y, oh.fish.getFishWidth(),
                        oh.fish.getFishHeight());

                    // Clear the line between the nose of the from fish and the top of the tail of
                    // of the to fish.
                    g.drawLine(xLineStart, yLineStart, oh.fish.getFishPosition().x,
                        oh.fish.getFishPosition().y);

                    mouseIsOverAnIconThatCanBeDroppedUpon = false;
                }
            }


            // Check to see if the line was dropped on a fish icon. If so,
            // connect the two fish by writing to the instance variable of the
            // class and repainting.
            for (int i = gcHeap.getHandlePoolSize() - 1; i >= 0; --i) {
                ObjectHandle oh = gcHeap.getObjectHandle(i + 1);
                if (!oh.free) {

                    Point o = oh.fish.getFishPosition();
                    if (x >= o.x && x < o.x + oh.fish.getFishWidth() && y >= o.y
                        && y < o.y + oh.fish.getFishHeight()) {

                        if (oh.fish.getFishColor() == colorOfClickedLocalVar) {

                            // Set the local variable to equal the dropped upon
                            // fish object.
                            if (yellowLocalVarClicked) {
                                localVars.yellowFish = i + 1;
                            }
                            else if (blueLocalVarClicked) {
                                localVars.blueFish = i + 1;
                            }
                            else if (redLocalVarClicked) {
                                localVars.redFish = i + 1;
                            }
                            repaint();
                        }
                        break;
                    }
                }
            }

            yellowLocalVarClicked = false;
            blueLocalVarClicked = false;
            redLocalVarClicked = false;

            return true;
        }

        ObjectHandle fishObjectThatWasClicked = gcHeap.getObjectHandle(objectIndexOfFishIconThatWasClicked);
        FishIcon fishIconThatWasClicked = fishObjectThatWasClicked.fish;
        Color colorOfClickedFish = fishObjectThatWasClicked.fish.getFishColor();

        if (dragging) {
            dragging = false;
            // Clear old line.
            Graphics g = getGraphics();
            g.setColor(Color.blue);
            g.setXORMode(colorOfClickedFish);

            Point lineStart = fishIconThatWasClicked.getFishNosePosition();

            if (!mouseIsOverAnIconThatCanBeDroppedUpon) {

                // Clear old line
                g.drawLine(lineStart.x, lineStart.y, currentMouseDragPosition.x,
                    currentMouseDragPosition.y);

⌨️ 快捷键说明

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