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

📄 unlinkfishcanvas.java

📁 深入java 虚拟机中的一个Java程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*
* Copyright (c) 1996, 1997 Bill Venners. All Rights Reserved.
*
* This Java source file is part of the Interactive Illustrations Web
* Site, which is delivered in the applets directory of the CD-ROM
* that accompanies the book "Inside the Java Virtual Machine" by Bill
* Venners, published by McGraw-Hill, 1997,ISBN: 0-07-913248-0. This
* source file is provided for evaluation purposes only, but you can
* redistribute it under certain conditions, described in the full
* copyright notice below.
*
* Full Copyright Notice:
*
* All the web pages and Java applets delivered in the applets
* directory of the CD-ROM, consisting of ".html," ".gif," ".class,"
* and ".java" files, are copyrighted (c) 1996, 1997 by Bill
* Venners, and all rights are reserved.  This material may be copied
* and placed on any commercial or non-commercial web server on any
* network (including the internet) provided that the following
* guidelines are followed:
*
* a. All the web pages and Java Applets (".html," ".gif," ".class,"
* and ".java" files), including the source code, that are delivered
* in the applets directory of the CD-ROM that
* accompanies the book must be published together on the same web
* site.
*
* b. All the web pages and Java Applets (".html," ".gif," ".class,"
* and ".java" files) must be published "as is" and may not be altered
* in any way.
*
* c. All use and access to this web site must be free, and no fees
* can be charged to view these materials, unless express written
* permission is obtained from Bill Venners.
*
* d. The web pages and Java Applets may not be distributed on any
* media, other than a web server on a network, and may not accompany
* any book or publication.
*
* BILL VENNERS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
* SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR PARTICULAR PURPOSE, OR NON-INFRINGEMENT.  BILL VENNERS
* SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY A LICENSEE AS A
* RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES.
*/
import java.awt.*;

/**
* This class is the canvas that displays the user interface
* during the unlink fish sub-mode of the assign references mode.
* This class contains the code that handles the collision detection
* and clicking that unlinks fish.
*
* @author  Bill Venners
*/
public class UnlinkFishCanvas extends AssignReferencesCanvas {

    private boolean iconClicked = false;
    private boolean overYellowLocalVarLine = false;
    private boolean overBlueLocalVarLine = false;
    private boolean overRedLocalVarLine = false;
    private boolean overInterFishLine = false;

    private Point interFishLineStart;
    private Point interFishLineEnd;
    private int interFishRefToClear;
    private Color interFishLineColor;

    private Point posOfMouseInsideIconWhenFirstPressed = new Point(0, 0);
    private int objectIndexOfFishIconThatWasClicked;

    private boolean dragging = false;
    private Point currentMouseDragPosition = new Point(0, 0);
    private boolean mouseIsOverAnIconThatCanBeDroppedUpon = false;
    private int objectIndexOfIconThatCanBeDroppedUpon;

    private Color colorOfUnlinkableLine = Color.black;

    private final int extraZeros = 1000;

    // mouseFatness is the number of pixels around the mouse which will form the rectangle
    // that a line must cross for it to be unlinked.
    private int mouseFatness = 3;

    UnlinkFishCanvas(GCHeap heap, LocalVariables locVars, HeapOfFishTextArea ta) {
        gcHeap = heap;
        localVars = locVars;
        controlPanelTextArea = ta;
    }

    public Dimension minimumSize() {
        return new Dimension(500, 240);
    }

    public Dimension preferredSize() {
        return new Dimension(500, 240);
    }

    public boolean mouseDrag(Event evt, int x, int y) {
        return mouseMove(evt, x, y);
    }

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

        if (overYellowLocalVarLine) {
            if (mouseOverLine(x, y, localVars.yellowLineStart, localVars.yellowLineEnd)) {
                return true;
            }
            Graphics g = getGraphics();
            g.setColor(Color.yellow);
            g.drawLine(localVars.yellowLineStart.x, localVars.yellowLineStart.y,
                localVars.yellowLineEnd.x, localVars.yellowLineEnd.y);
            overYellowLocalVarLine = false;
        }

        if (overBlueLocalVarLine) {
            if (mouseOverLine(x, y, localVars.blueLineStart, localVars.blueLineEnd)) {
                return true;
            }
            Graphics g = getGraphics();
            g.setColor(Color.cyan);
            g.drawLine(localVars.blueLineStart.x, localVars.blueLineStart.y,
                localVars.blueLineEnd.x, localVars.blueLineEnd.y);
            overBlueLocalVarLine = false;
        }

        if (overRedLocalVarLine) {
            if (mouseOverLine(x, y, localVars.redLineStart, localVars.redLineEnd)) {
                return true;
            }
            Graphics g = getGraphics();
            g.setColor(Color.red);
            g.drawLine(localVars.redLineStart.x, localVars.redLineStart.y,
                localVars.redLineEnd.x, localVars.redLineEnd.y);
            overRedLocalVarLine = false;
        }

        if (overInterFishLine) {
            if (mouseOverLine(x, y, interFishLineStart, interFishLineEnd)) {
                return true;
            }
            Graphics g = getGraphics();
            g.setColor(interFishLineColor);
            g.drawLine(interFishLineStart.x, interFishLineStart.y,
                interFishLineEnd.x, interFishLineEnd.y);
            overInterFishLine = false;
        }

        // Now see if the mouse is over a new line.
        if (localVars.yellowFish != 0) {
            if (mouseOverLine(x, y, localVars.yellowLineStart, localVars.yellowLineEnd)) {
                Graphics g = getGraphics();
                g.setColor(Color.yellow);
                g.setXORMode(colorOfUnlinkableLine);
                g.drawLine(localVars.yellowLineStart.x, localVars.yellowLineStart.y,
                    localVars.yellowLineEnd.x, localVars.yellowLineEnd.y);
                g.setPaintMode();
                overYellowLocalVarLine = true;
                return true;
            }
        }

        if (localVars.blueFish != 0) {
            if (mouseOverLine(x, y, localVars.blueLineStart, localVars.blueLineEnd)) {
                Graphics g = getGraphics();
                g.setColor(Color.cyan);
                g.setXORMode(colorOfUnlinkableLine);
                g.drawLine(localVars.blueLineStart.x, localVars.blueLineStart.y,
                    localVars.blueLineEnd.x, localVars.blueLineEnd.y);
                g.setPaintMode();
                overBlueLocalVarLine = true;
                return true;
            }
        }

        if (localVars.redFish != 0) {
            if (mouseOverLine(x, y, localVars.redLineStart, localVars.redLineEnd)) {
                Graphics g = getGraphics();
                g.setColor(Color.red);
                g.setXORMode(colorOfUnlinkableLine);
                g.drawLine(localVars.redLineStart.x, localVars.redLineStart.y,
                    localVars.redLineEnd.x, localVars.redLineEnd.y);
                g.setPaintMode();
                overRedLocalVarLine = true;
                return true;
            }
        }

        for (int i = gcHeap.getHandlePoolSize() - 1; i >= 0; --i) {
            ObjectHandle oh = gcHeap.getObjectHandle(i + 1);
            if (!oh.free) {
                if (oh.gotFriend && (gcHeap.getObjectPool(oh.objectPos) != 0)) {
                    if (mouseOverLine(x, y, oh.myFriendLineStart, oh.myFriendLineEnd)) {
                        interFishLineStart = oh.myFriendLineStart;
                        interFishLineEnd = oh.myFriendLineEnd;
                        interFishRefToClear = oh.objectPos;
                        interFishLineColor = oh.fish.getFishColor();
                        Graphics g = getGraphics();
                        g.setColor(interFishLineColor);
                        g.setXORMode(colorOfUnlinkableLine);
                        g.drawLine(interFishLineStart.x, interFishLineStart.y,
                            interFishLineEnd.x, interFishLineEnd.y);
                        g.setPaintMode();
                        overInterFishLine = true;
                        return true;
                    }
                }
                if (oh.gotLunch && (gcHeap.getObjectPool(oh.objectPos + 1) != 0)) {
                    if (mouseOverLine(x, y, oh.myLunchLineStart, oh.myLunchLineEnd)) {
                        interFishLineStart = oh.myLunchLineStart;
                        interFishLineEnd = oh.myLunchLineEnd;
                        interFishRefToClear = oh.objectPos + 1;
                        interFishLineColor = oh.fish.getFishColor();
                        Graphics g = getGraphics();
                        g.setColor(interFishLineColor);
                        g.setXORMode(colorOfUnlinkableLine);
                        g.drawLine(interFishLineStart.x, interFishLineStart.y,
                            interFishLineEnd.x, interFishLineEnd.y);
                        g.setPaintMode();
                        overInterFishLine = true;
                        return true;
                    }
                }
                if (oh.gotSnack && (gcHeap.getObjectPool(oh.objectPos + 2) != 0)) {
                    if (mouseOverLine(x, y, oh.mySnackLineStart, oh.mySnackLineEnd)) {
                        interFishLineStart = oh.mySnackLineStart;
                        interFishLineEnd = oh.mySnackLineEnd;
                        interFishRefToClear = oh.objectPos + 2;
                        interFishLineColor = oh.fish.getFishColor();
                        Graphics g = getGraphics();
                        g.setColor(interFishLineColor);
                        g.setXORMode(colorOfUnlinkableLine);
                        g.drawLine(interFishLineStart.x, interFishLineStart.y,
                            interFishLineEnd.x, interFishLineEnd.y);
                        g.setPaintMode();
                        overInterFishLine = true;
                        return true;
                    }
                }
            }
        }
        return true;
    }

    public boolean mouseUp(Event evt, int x, int y) {
        if (overYellowLocalVarLine) {
            if (mouseOverLine(x, y, localVars.yellowLineStart, localVars.yellowLineEnd)) {
                localVars.yellowFish = 0;
                overYellowLocalVarLine = false;
                repaint();
            }
        }

        if (overBlueLocalVarLine) {
            if (mouseOverLine(x, y, localVars.blueLineStart, localVars.blueLineEnd)) {
                localVars.blueFish = 0;
                overBlueLocalVarLine = false;
                repaint();
            }
        }

        if (overRedLocalVarLine) {
            if (mouseOverLine(x, y, localVars.redLineStart, localVars.redLineEnd)) {
                localVars.redFish = 0;
                overRedLocalVarLine = false;
                repaint();
            }
        }

        if (overInterFishLine) {
            if (mouseOverLine(x, y, interFishLineStart, interFishLineEnd)) {
                gcHeap.setObjectPool(interFishRefToClear, 0);
                overInterFishLine = false;
                repaint();
            }
        }

        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.

        if (lineStart.x > lineEnd.x) {
            if ((x > lineStart.x || x < lineEnd.x)
                && (lineStart.x - lineEnd.x > (2 * mouseFatness))) {
                return false;
            }
        }
        else if (lineStart.x < lineEnd.x) {
            if ((x < lineStart.x || x > lineEnd.x)
                && (lineEnd.x - lineStart.x > (2 * mouseFatness))) {
                return false;
            }
        }

        if (lineStart.y > lineEnd.y) {
            if ((y > lineStart.y || y < lineEnd.y)
                && (lineStart.y - lineEnd.y > (2 * mouseFatness))) {
                return false;
            }
        }
        else if (lineStart.y < lineEnd.y) {
            if ((y < lineStart.y || y > lineEnd.y)
                && (lineEnd.y - lineStart.y > (2 * mouseFatness))) {
                return false;
            }
        }

        int xRectLeft = x - mouseFatness;
        int xRectRight = x + mouseFatness;
        int yRectTop = y - mouseFatness;
        int yRectBottom = y + mouseFatness;

        // Check for one special case, a line with infinite slope.
        if (lineStart.x == lineEnd.x) {
            if (x >= lineStart.x - mouseFatness && x <= lineStart.x + mouseFatness) {
                return true;
            }
            else {
                return false;
            }
        }

        // Calculate slope of line.
        float m = ((float) (lineEnd.y - lineStart.y) / (float) (lineEnd.x - lineStart.x));

        // Calculate y intercept of line.
        float b = ((float) lineStart.y - (m * (float) lineStart.x));

⌨️ 快捷键说明

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