?? unlinkfishcanvas.java
字號:
}
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;
}
}
// 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 (i + 1 == objectIndexOfFishIconThatWasClicked) {
// If they dropped on the same fish they started from, don't link.
break;
}
if (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, i + 1);
repaint();
}
break;
}
}
}
yellowLocalVarClicked = false;
blueLocalVarClicked = false;
redLocalVarClicked = false;
iconClicked = false;
return true;
}
public boolean mouseDrag(Event evt, int x, int y) {
if (!iconClicked && !yellowLocalVarClicked && !blueLocalVarClicked
&& !redLocalVarClicked) {
return true;
}
if (yellowLocalVarClicked || blueLocalVarClicked || redLocalVarClicked) {
Graphics g = getGraphics();
g.setColor(Color.blue);
Color colorOfClickedLocalVar = Color.yellow;
int xLineStart = xLocalVarRectStart + localVarRectWidth;
int yLineStart = yYellowFishLocalVarStart + (localVarRectHeight / 2);
if (blueLocalVarClicked) {
colorOfClickedLocalVar = Color.cyan;
yLineStart = yBlueFishLocalVarStart + (localVarRectHeight / 2);
}
else if (redLocalVarClicked) {
colorOfClickedLocalVar = Color.red;
yLineStart = yRedFishLocalVarStart + (localVarRectHeight / 2);
}
g.setXORMode(colorOfClickedLocalVar);
if (!dragging) {
dragging = true;
}
else {
// Check to see if the mouse has left an icon that can be dropped upon. If
// so, need to erase the outline rectangle and erase the line from the nose of
// the "from" fish to top left corner of outline rectangle of "to" fish.
if (mouseIsOverAnIconThatCanBeDroppedUpon) {
ObjectHandle oh = gcHeap.getObjectHandle(objectIndexOfIconThatCanBeDroppedUpon);
Point o = oh.fish.getFishPosition();
if (x < o.x || x >= o.x + oh.fish.getFishWidth() || y < o.y
|| y >= o.y + oh.fish.getFishHeight()) {
g.drawRect(oh.fish.getFishPosition().x, oh.fish.getFishPosition().y, oh.fish.getFishWidth(),
oh.fish.getFishHeight());
// Draw a 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;
}
}
else {
// No drop-on-able fish is beneath the mouse, so erase the new line between
// the nose of the fromFish and the previous mouse position.
g.drawLine(xLineStart, yLineStart, currentMouseDragPosition.x, currentMouseDragPosition.y);
}
}
// Check to see if a fish is beneath the mouse that can be dropped upon.
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 (!mouseIsOverAnIconThatCanBeDroppedUpon) {
if (oh.fish.getFishColor() == colorOfClickedLocalVar) {
mouseIsOverAnIconThatCanBeDroppedUpon = true;
}
if (mouseIsOverAnIconThatCanBeDroppedUpon) {
objectIndexOfIconThatCanBeDroppedUpon = i + 1;
g.drawRect(oh.fish.getFishPosition().x, oh.fish.getFishPosition().y, oh.fish.getFishWidth(),
oh.fish.getFishHeight());
// Draw a 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);
}
}
break;
}
}
}
if (!mouseIsOverAnIconThatCanBeDroppedUpon) {
// No drop-on-able fish is beneath the mouse, so just draw the new line between
// the nose of the fromFish and the current mouse position.
g.drawLine(xLineStart, yLineStart, x, y);
}
currentMouseDragPosition.x = x;
currentMouseDragPosition.y = y;
g.setPaintMode();
return true;
}
FishIcon fishIconThatWasClicked = gcHeap.getObjectHandle(objectIndexOfFishIconThatWasClicked).fish;
Color colorOfClickedFish = gcHeap.getObjectHandle(objectIndexOfFishIconThatWasClicked).fish.getFishColor();
// Don't start dragging unless the mouse has moved a threshold number of
// pixels in x or y.
if (!dragging) {
int thresholdPixels = 5;
Point iconOrigin = fishIconThatWasClicked.getFishPosition();
int xOriginalClick = iconOrigin.x + posOfMouseInsideIconWhenFirstPressed.x;
int yOriginalClick = iconOrigin.y + posOfMouseInsideIconWhenFirstPressed.y;
int xDifference = x - xOriginalClick;
if (xDifference < 0) {
xDifference = 0 - xDifference;
}
int yDifference = y - yOriginalClick;
if (yDifference < 0) {
yDifference = 0 - yDifference;
}
if (xDifference < thresholdPixels && yDifference < thresholdPixels) {
return true;
}
}
Graphics g = getGraphics();
g.setColor(Color.blue);
g.setXORMode(colorOfClickedFish);
Point fishNose = fishIconThatWasClicked.getFishNosePosition();
if (!dragging) {
dragging = true;
}
else {
// Check to see if the mouse has left an icon that can be dropped upon. If
// so, need to erase the outline rectangle and erase the line from the nose of
// the "from" fish to top left corner of outline rectangle of "to" fish.
if (mouseIsOverAnIconThatCanBeDroppedUpon) {
ObjectHandle oh = gcHeap.getObjectHandle(objectIndexOfIconThatCanBeDroppedUpon);
Point o = oh.fish.getFishPosition();
if (x < o.x || x >= o.x + oh.fish.getFishWidth() || y < o.y
|| y >= o.y + oh.fish.getFishHeight()) {
g.drawRect(oh.fish.getFishPosition().x, oh.fish.getFishPosition().y, oh.fish.getFishWidth(),
oh.fish.getFishHeight());
// Draw a line between the nose of the from fish and the top of the tail of
// of the to fish.
g.drawLine(fishNose.x, fishNose.y, oh.fish.getFishPosition().x,
oh.fish.getFishPosition().y);
mouseIsOverAnIconThatCanBeDroppedUpon = false;
}
}
else {
// No drop-on-able fish is beneath the mouse, so erase the new line between
// the nose of the fromFish and the previous mouse position.
g.drawLine(fishNose.x, fishNose.y, currentMouseDragPosition.x, currentMouseDragPosition.y);
}
}
// Check to see if a fish is beneath the mouse that can be dropped upon.
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 (!mouseIsOverAnIconThatCanBeDroppedUpon) {
if (i + 1 == objectIndexOfFishIconThatWasClicked) {
// If they are over the same fish they started from, don't
// draw a rectangle.
break;
}
mouseIsOverAnIconThatCanBeDroppedUpon = fishCanLink(fishIconThatWasClicked, oh.fish);
if (mouseIsOverAnIconThatCanBeDroppedUpon) {
objectIndexOfIconThatCanBeDroppedUpon = i + 1;
g.drawRect(oh.fish.getFishPosition().x, oh.fish.getFishPosition().y, oh.fish.getFishWidth(),
oh.fish.getFishHeight());
// Draw a line between the nose of the from fish and the top of the tail of
// of the to fish.
g.drawLine(fishNose.x, fishNose.y, oh.fish.getFishPosition().x,
oh.fish.getFishPosition().y);
}
}
break;
}
}
}
if (!mouseIsOverAnIconThatCanBeDroppedUpon) {
// No drop-on-able fish is beneath the mouse, so just draw the new line between
// the nose of the fromFish and the current mouse position.
g.drawLine(fishNose.x, fishNose.y, x, y);
}
currentMouseDragPosition.x = x;
currentMouseDragPosition.y = y;
g.setPaintMode();
return true;
}
private boolean fishCanLink(FishIcon fromFish, FishIcon toFish) {
// Red fish can link with anything.
if (fromFish.getFishColor() == Color.red) {
return true;
}
// Blue fish can link with anything except red.
if (fromFish.getFishColor() == Color.cyan) {
if (toFish.getFishColor() != Color.red) {
return true;
}
else {
return false;
}
}
// Yellow fish can only link with yellow fish.
if (toFish.getFishColor() == Color.yellow) {
return true;
}
return false;
}
// getInstanceVariableOffset returns the offset from the beginning of the "from" fish
// object in the objectPool of the variable to which the "to" fish reference should
// be assigned. The offset is in number of ints. This assumes that the toFish is
// a valid fish color for the from fish, e.g., yellow fromFish will only have
// yellow toFish. The fish objects are defined as:
//
// class RedFish {
// RedFish myFriend;
// BlueFish myLunch;
// YellowFish mySnack;
// }
//
// class BlueFish {
// BlueFish myFriend;
// YellowFish myLunch;
// }
//
// class YellowFish {
// YellowFish myFriend;
// }
//
private int getInstanceVariableOffset(FishIcon fromFish, FishIcon toFish) {
// Red fish can link with anything.
if (fromFish.getFishColor() == Color.red) {
if (toFish.getFishColor() == Color.red) {
return 0;
}
else if (toFish.getFishColor() == Color.cyan) {
return 1;
}
else {
return 2; // yellow fish
}
}
// Blue fish can link with anything except red.
if (fromFish.getFishColor() == Color.cyan) {
if (toFish.getFishColor() == Color.cyan) {
return 0;
}
else {
return 1; // yellow fish
}
}
// Yellow fish can only link with yellow fish.
return 0;
}
*/
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -