📄 linkfishcanvas.java
字号:
/*
* 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 link fish sub-mode of the assign references mode.
* This class contains the code that handles the clicking and
* dragging that links one fish to another.
*
* @author Bill Venners
*/
public class LinkFishCanvas extends AssignReferencesCanvas {
private boolean iconClicked = false;
private boolean yellowLocalVarClicked = false;
private boolean blueLocalVarClicked = false;
private boolean redLocalVarClicked = false;
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;
LinkFishCanvas(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 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;
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 = objectIndexOfIconThatCanBeDroppedUpon;
}
else if (blueLocalVarClicked) {
localVars.blueFish = objectIndexOfIconThatCanBeDroppedUpon;
}
else if (redLocalVarClicked) {
localVars.redFish = objectIndexOfIconThatCanBeDroppedUpon;
}
repaint();
}
}
}
}
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);
}
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(lineStart.x, lineStart.y, oh.fish.getFishPosition().x,
oh.fish.getFishPosition().y);
mouseIsOverAnIconThatCanBeDroppedUpon = false;
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 they dropped on the same fish they started from, don't link.
if ((objectIndexOfIconThatCanBeDroppedUpon != objectIndexOfFishIconThatWasClicked)
&& fishCanLink(fishIconThatWasClicked, oh.fish)) {
int offset = getInstanceVariableOffset(fishIconThatWasClicked, oh.fish);
// Set the clicked upon fish variable to equal the dropped upon
// fish object.
gcHeap.setObjectPool(fishObjectThatWasClicked.objectPos + offset, objectIndexOfIconThatCanBeDroppedUpon);
repaint();
}
}
}
}
yellowLocalVarClicked = false;
blueLocalVarClicked = false;
redLocalVarClicked = false;
iconClicked = false;
return true;
}
public boolean mouseDrag(Event evt, int x, int y) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -