📄 graphcanvas.java
字号:
}
public boolean mouseDown(Event evt, int x, int y) {
// System.out.println("****** mouseDown******");
clicked = true;
if (evt.shiftDown()) {
// move a node
if (nodehit(x, y, NODESIZE)) {
prevPoint = graph.node[hitnode];
node1 = hitnode;
movenode = true;
}
}
else if (evt.controlDown()) {
// delete a node
if (nodehit(x, y, NODESIZE)) {
node1 = hitnode;
if (graph.startgraph == node1) {
movestart = true;
currPoint = new Point(x, y);
graph.colorNode[graph.startgraph] = Color.gray;
}
}
}
else if (arrowhit(x, y, 5)) {
// change weight of an edge
movearrow = true;
}
else if (nodehit(x, y, NODESIZE)) {
// draw a new arrow
if (!newarrow) {
newarrow = true;
currPoint = new Point(x, y);
node1 = hitnode;
}
}
else if (!nodehit(x, y, 50) && !arrowhit(x, y, 50)) {
// draw new node
// take the next available spot in the array
if (numOfNodes < MAXNODES)
graph.node[numOfNodes++] = new Point(x, y);
}
return true;
}
public boolean mouseDrag(Event evt, int x, int y) {
if (clicked) {
if (movenode) {
// move node and adjust arrows coming into/outof the node
graph.adjustArrow(node1, numOfNodes, x, y);
repaint();
}
else if (movestart || newarrow) {
currPoint = new Point(x, y);
repaint();
}
else if (movearrow) {
graph.changeWeight(x, y, node1, node2);
repaint();
}
}
return true;
}
public boolean mouseUp(Event evt, int x, int y) {
// System.out.println("****** mouseUp******");
if (clicked) {
if (movenode) {
// move the node if the new position is not to close to
// another node or outside of the panel
graph.node[node1] = new Point(0, 0);
if (nodehit(x, y, 50) || (x < 0) || (x > this.getSize().width)
||
(y < 0) || (y > this.getSize().height)) {
graph.node[node1] = prevPoint;
}
else
graph.node[node1] = new Point(x, y);
for (int i = 0; i < numOfNodes; i++) {
if (graph.weight[i][node1] > 0)
graph.arrowUpdate(i, node1, graph.weight[i][node1]);
if (graph.weight[node1][i] > 0)
graph.arrowUpdate(node1, i, graph.weight[node1][i]);
}
movenode = false;
}
else if (newarrow) {
newarrow = false;
if (nodehit(x, y, NODESIZE)) {
node2 = hitnode;
if (node1 != node2) {
graph.arrowUpdate(node1, node2, 50);
if (graph.weight[node2][node1] > 0) {
graph.arrowUpdate(node2, node1,
graph.weight[node2][node1]);
}
}
}
}
else if (movearrow) {
movearrow = false;
if (graph.weight[node1][node2] > 0)
graph.changeWeight(x, y, node1, node2);
}
else if (movestart) {
// if new position is a node, this node becomes the startnode
if (nodehit(x, y, NODESIZE))
graph.startgraph = hitnode;
graph.colorNode[graph.startgraph] = Color.blue;
movestart = false;
}
repaint();
}
return true;
}
public boolean nodehit(int x, int y, int dist) {
// checks if you hit a node with your mouseclick
for (int i = 0; i < numOfNodes; i++) {
if ((x - graph.node[i].x) * (x - graph.node[i].x) +
(y - graph.node[i].y) * (y - graph.node[i].y) < dist * dist) {
hitnode = i;
return true;
}
}
return false;
}
public boolean arrowhit(int x, int y, int dist) {
// checks if you hit an arrow with your mouseclick
for (int i = 0; i < numOfNodes; i++) {
for (int j = 0; j < numOfNodes; j++) {
if ((graph.weight[i][j] > 0) &&
(Math.pow(x - graph.arrow[i][j].x, 2) +
Math.pow(y - graph.arrow[i][j].y, 2) < Math.pow(dist, 2))) {
node1 = i;
node2 = j;
return true;
}
}
}
return false;
}
public void paint(Graphics g) {
inPrint = true;
update();
inPrint = false;
}
private void drawAllArrows(Graphics g) {
for (int i = 0; i < numOfNodes; i++) {
for (int j = 0; j < numOfNodes; j++) {
if (graph.weight[i][j] > 0) {
// if algorithm is running then perform next step for this
// arrow
if (performalg) {
applyAlgorithm(g, i, j);
}
graph.drawArrow(g, i, j, fmetrics);
}
}
}
}
private void drawAllNodes(Graphics g) {
for (int i = 0; i < numOfNodes; i++) {
if (graph.node[i].x > 0) {
g.setColor(graph.colorNode[i]);
g.fillOval(graph.node[i].x - NODERADIX, graph.node[i].y
- NODERADIX, NODESIZE, NODESIZE);
}
}
}
/**
* draw black circles around nodes, write their names to the screen
*
*/
private void drawCircledNode(Graphics g) {
g.setFont(helvetica);
for (int i = 0; i < numOfNodes; i++) {
if (graph.node[i].x > 0) {
g.setColor(Color.black);
g.drawOval(graph.node[i].x - NODERADIX, graph.node[i].y
- NODERADIX,
NODESIZE, NODESIZE);
g.setColor(Color.blue);
g.drawString(findNodeName(i), graph.node[i].x - 14,
graph.node[i].y - 14);
}
}
}
/**
* draw a new arrow to current mouse position if needed.
*
* @param g
*/
public void drawNewLine(Graphics g) {
if (newarrow) {
g.drawLine(graph.node[node1].x, graph.node[node1].y, currPoint.x,
currPoint.y);
}
}
public void update() {
Graphics g = getGraphics();
minDistance = 0;
minNode = MAXNODES;
minStart = MAXNODES;
minEnd = MAXNODES;
g.setFont(roman);
g.setColor(Color.black);
drawNewLine(g);
// draw all arrows
drawAllArrows(g);
// if arrowhead has been dragged to 0, draw it anyway, so the user
// will have the option to make it positive again
if (movearrow && graph.weight[node1][node2] == 0) {
graph.drawArrow(g, node1, node2, fmetrics);
g.drawLine(graph.startPoint[node1][node2].x,
graph.startPoint[node1][node2].y,
graph.endPoint[node1][node2].x,
graph.endPoint[node1][node2].y);
}
// draw the nodes
drawAllNodes(g);
g.setColor(Color.black);
// finish this step of the algorithm
if (performalg) {
endStepAlgorithm(g);
}
// draw black circles around nodes, write their names to the screen
drawCircledNode(g);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -