📄 workarea.java
字号:
// file: WorkArea.java//// import java libraries//import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.border.*;import javax.swing.event.*;import java.util.*;import java.io.*;import java.lang.*;import java.awt.geom.*;public class WorkArea extends JPanel implements Constants { // --------------------------------------------------- // // declare class member data // // --------------------------------------------------- // declare a vector of all elements in the graph // public Vector vertices = null; public int workAreaWidth = WORKAREA_WIDTH; public int workAreaHeight = WORKAREA_HEIGHT; // declare an instance of the directed graph // DiGraph digraph = null; // --------------------------------------------------- // // declare class constructors // // --------------------------------------------------- // method: WorkArea // // arguments: none // returns : none // public WorkArea(Vector vertices, DiGraph digraph) { // invoke the super constructor // super(); // initialize // this.vertices = vertices; // initialize the digraph // this.digraph = digraph; // invalidate the default layout // this.setLayout(null); // set the default background color of the panel // this.setBackground(Color.white); } // --------------------------------------------------- // // declare class methods // // --------------------------------------------------- // method: getAreaWidth // // arguments: none // return: none // // retrieves the width of the work area // public int getAreaWidth() { // return the width // return workAreaWidth; } // method: getAreaHeight // // arguments: none // return: none // // retrieves the height of the work area // public int getAreaHeight() { // return the height // return workAreaHeight; } // method: setWidth // // arguments: // int width: (input) width of the work area // return : none // // sets the width of the work area // public void setWidth(int width) { // set the width // workAreaWidth = width; } // method: setHeight // // arguments: // int width: (input) width of the work area // return: none // // sets the height of the work area // public void setHeight(int height) { // set the height // workAreaHeight = height; } // method: distance // // arguments: // int x1: (input) x-coordinate of the first point // int y1: (input) y-coordinate of the first point // int x2: (input) x-coordinate of the second point // int y2: (input) y-coordinate of the second point // return: distance between the points // // calculate the euclidean distance between the two points // public double distance(int x1, int y1, int x2, int y2) { double distance = 0.0; // claculate the euclidean distance // double deltaX = x2 - x1; double deltaY = y2 - y1; double sqrX = deltaX * deltaX; double sqrY = deltaY * deltaY; distance = Math.sqrt((double)sqrX + (double)sqrY); // return the distance // return distance; } // --------------------------------------------------- // // class required methods // // --------------------------------------------------- // method: paintComponent // // arguments: // Graphics g: (input) graphics object // return : none // // paint the current data points and if needed the decision regions // public void paintComponent(Graphics g) { // declare local variables // int x1 = 0; int x2 = 0; int y1 = 0; int y2 = 0; int x21 =0; int y21 =0; Vertex parent = null; Vertex child = null; Vector children = null; boolean isselfarc = false; boolean isarc = false; // invoke the super constructor // super.paintComponent(g); // highlight the current vertex focus // if (digraph.currVertexFocus != null) { g.setColor(Color.red); Rectangle bounds = digraph.currVertexFocus.getBounds(); g.drawRect(bounds.x - HIGHLIGHT_WIDTH, bounds.y - HIGHLIGHT_WIDTH, bounds.width + HIGHLIGHT_WIDTH, bounds.height + HIGHLIGHT_WIDTH); g.setColor(Color.black); } // iterate through all parents in the graph // for (int p=0; p < vertices.size(); p++) { isselfarc = false; int indexs = 0; parent = (Vertex)vertices.elementAt(p); Point loc1 = parent.getVertexLocation(); String wei = null; String epi = null; for(int k = 0; k < digraph.graphArc.from.size(); k++) { if((Vertex)digraph.graphArc.from.get(k) == parent && (Vertex)digraph.graphArc.to.get(k) == parent) { wei = (String)digraph.graphArc.weights.get(k); epi = (String)digraph.graphArc.epsilon.get(k); isselfarc = true; indexs = k; break; } } if(isselfarc == true ) { g.drawArc(loc1.x + 12, loc1.y - parent.getVertexWidth()/2 + 12, parent.getVertexWidth() - 24, parent.getVertexWidth() - 24, 0, 180); g.drawArc(loc1.x + 13, loc1.y - parent.getVertexWidth()/2 + 13, parent.getVertexWidth() - 26, parent.getVertexWidth() - 26, 0, 180); int xtip1 = loc1.x + 12; int ytip1 = loc1.y + 12 - 4; int xleft1 = loc1.x + 12 - 7/2; int yleft1 = loc1.y - 1; int xright1 = loc1.x + 12 + 7/2; int yright1 = loc1.y -1 ; int x0Pts[] = {xtip1, xright1, xleft1, xtip1}; int y0Pts[] = {ytip1, yright1, yleft1, ytip1}; // g.setColor(Color.red); g.fillPolygon(x0Pts, y0Pts, x0Pts.length); // add arc area for setting weight and epsilon // String temp = null; temp = "" + (loc1.x + 12); digraph.graphArc.xloc.set(indexs, temp); temp = "" + (loc1.y - parent.getVertexWidth()/2 + 12); digraph.graphArc.yloc.set(indexs, temp); temp = "" + (parent.getVertexWidth() - 24); digraph.graphArc.width.set(indexs, temp); temp = "" + (parent.getVertexWidth() - 24)/2; digraph.graphArc.height.set(indexs, temp); } if(epi != null) { g.setFont(arcFont); if(epi.equals("false")) { g.drawString(""+ wei, loc1.x + parent.getVertexWidth()/2,loc1.y - parent.getVertexWidth()/2 + 11 ); } else { g.drawString(""+ "epi", loc1.x + parent.getVertexWidth()/2,loc1.y - parent.getVertexWidth()/2 + 11 ); } g.setFont(newFont); } children = parent.getChildren(); // iterate through all children of the parent // for (int c=0; c < children.size(); c++) { isarc = false; child = (Vertex)children.elementAt(c); int indexd = 0; String wei1 = null; String epi1 = null; for(int k = 0; k < digraph.graphArc.from.size(); k++) { if((Vertex)digraph.graphArc.from.get(k) == parent && (Vertex)digraph.graphArc.to.get(k) == child) { wei1 = (String)digraph.graphArc.weights.get(k); epi1 = (String)digraph.graphArc.epsilon.get(k); isarc = true; indexd = k; break; } } // get the location of the parent and child // Point loc2 = child.getVertexLocation(); // determine the co-ordinates of the parent // x1 = loc1.x + (parent.getVertexWidth() / 2); y1 = loc1.y + (parent.getVertexHeight() / 2); // determine the up-down co-ordinates of the child // double delta = (double)child.getVertexWidth() / (double)(child.getInDegree() + 1); int index = child.vertexParents.indexOf(parent) + 1; x2 = loc2.x + (int)(delta * index); y2 = loc2.y + (child.getVertexHeight() / 2); // determine the four destination points on the vertex // int xpnt1 = x2; int ypnt1 = y2 - (child.getVertexHeight() / 2) - VERTEX_MARGIN; int xpnt2 = x2; int ypnt2 = y2 + (child.getVertexHeight() / 2) + VERTEX_MARGIN; // determine the left-right co-ordinates of the child // double delta1 = (double)child.getVertexHeight() / (double)(child.getInDegree() + 1); int index2 = child.vertexParents.indexOf(parent) + 1; x21 = loc2.x + (child.getVertexWidth() / 2); y21 = loc2.y + (int)(delta1 * index2); // determine the four destination points on the vertex // int xpnt3 = x21 - (child.getVertexWidth() / 2) - VERTEX_MARGIN; int ypnt3 = y21; int xpnt4 = x21 + (child.getVertexWidth() / 2) + VERTEX_MARGIN; int ypnt4 = y21; // determine the closest destination to the parent // int destVal = -1; double currMax = 0.0; double maxDist = Double.MAX_VALUE; // determine the distance to the first point // currMax = Math.abs(distance(x1, y1, xpnt1, ypnt1)); if (currMax < maxDist) { destVal = 1; maxDist = currMax; } // determine the distance to the second point // currMax = Math.abs(distance(x1, y1, xpnt2, ypnt2)); if (currMax < maxDist) { destVal = 2; maxDist = currMax; } // determine the distance to the third point // currMax = Math.abs(distance(x1, y1, xpnt3, ypnt3)); if (currMax < maxDist) { destVal = 3; maxDist = currMax; } // determine the distance to the fourth point // currMax = Math.abs(distance(x1, y1, xpnt4, ypnt4)); if (currMax < maxDist) { destVal = 4; maxDist = currMax; } // draw a directed arc form the parent to the child // if (destVal == 1) { x2 = xpnt1; y2 = ypnt1; } else if(destVal == 2){ x2 = xpnt2; y2 = ypnt2; } else if(destVal == 3){ x2 = xpnt3; y2 = ypnt3; } else { x2 = xpnt4; y2 = ypnt4; } if( isarc == true) { // draw a line connecting the parent and child // g.drawLine(x1, y1-1, x2, y2-1); g.drawLine(x1-1, y1, x2-1, y2); g.drawLine(x1, y1, x2, y2); // draw a directed arrow head from parent to child // int dy = y2 - y1; int dx = x2 - x1; int ARROW_SIZE = 6; double dist = distance(x1, y1, x2, y2); dist = dist == 0 ? 1 : dist; double medianx = x2 - dx * ARROW_SIZE / dist; double mediany = y2 - dy * ARROW_SIZE / dist; int base1x = (int)(medianx - dy * ( ARROW_SIZE / 2 ) / dist); int base1y = (int)(mediany + dx * ( ARROW_SIZE / 2 ) / dist); int base2x = (int)(medianx + dy * ( ARROW_SIZE / 2 ) / dist); int base2y = (int)(mediany - dx * ( ARROW_SIZE / 2 ) / dist); int xPts[] = {x2, base1x, base2x, x2}; int yPts[] = {y2, base1y, base2y, y2}; g.fillPolygon(xPts, yPts, xPts.length); // add arc area for setting weight and epsilon // String temp = null; temp = "" + x1; digraph.graphArc.xloc.set(indexd, temp); temp = "" + y1; digraph.graphArc.yloc.set(indexd, temp); temp = "" + x2; digraph.graphArc.width.set(indexd, temp); temp = "" + y2; digraph.graphArc.height.set(indexd, temp); } if(epi1 != null) { g.setFont(arcFont); if(epi1.equals("false")) { g.drawString(""+ wei1, (x1 + x2) /2 - 4, (y1 + y2) /2 - 3 ); } else { g.drawString(""+ "epi", (x1 + x2) /2 - 4, (y1 + y2) /2 - 3 ); } g.setFont(newFont); } } } }}//// end of file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -