📄 subworkarea.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.*;public class SubWorkArea extends JPanel implements Constants { // --------------------------------------------------- // // declare class member data // // --------------------------------------------------- // declare a vector of all elements in the graph // public Vector vertices = null; public int subWorkAreaWidth = 500; public int subWorkAreaHeight = 600; // declare an instance of the directed graph // SubDiGraph subDiGraph = null; // --------------------------------------------------- // // declare class constructors // // --------------------------------------------------- // method: SubWorkArea // // arguments: none // returns : none // public SubWorkArea(Vector vertices, SubDiGraph subDiGraph) { // invoke the super constructor // super(); // initialize // this.vertices = vertices; // initialize the subDiGraph // this.subDiGraph = subDiGraph; // 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 subWorkAreaWidth; } // method: getAreaHeight // // arguments: none // return: none // // retrieves the height of the work area // public int getAreaHeight() { // return the height // return subWorkAreaHeight; } // 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 // subWorkAreaWidth = 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 // subWorkAreaHeight = 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; Vertex parent = null; Vertex child = null; Vector children = null; // invoke the super constructor // super.paintComponent(g); // highlight the current vertex focus // if (subDiGraph.currVertexFocus != null) { Rectangle bounds = subDiGraph.currVertexFocus.getBounds(); g.setColor(Color.red); 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++) { parent = (Vertex)vertices.elementAt(p); children = parent.getChildren(); // iterate through all children of the parent // for (int c=0; c < children.size(); c++) { child = (Vertex)children.elementAt(c); // get the location of the parent and child // Point loc1 = parent.getVertexLocation(); 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 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 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 = 0.8 * Math.abs(distance(x1, y1, xpnt1, ypnt1)); if (currMax < maxDist) { destVal = 1; maxDist = currMax; } // determine the distance to the second point // currMax = 0.8 * Math.abs(distance(x1, y1, xpnt2, ypnt2)); if (currMax < maxDist) { destVal = 2; maxDist = currMax; } // draw a directed arc form the parent to the child // if (destVal == 1) { x2 = xpnt1; y2 = ypnt1; } else { x2 = xpnt2; y2 = ypnt2; } // 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); } } }}//// end of file
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -