📄 programcanvas.java
字号:
import java.io.*;import java.awt.*;import java.awt.image.*;import java.util.Vector;import java.util.Enumeration;import java.awt.event.*;import java.lang.Math;import javax.swing.*;/* This class is the canvas on which all drawing of Jumpshot data is done A new data structure containing a vector of StateGroup objects resides here. Each of these StateGroup objects represent a process and contain a vector 'stateVector' which is a list of all states of that process, in ascending order of their end timestamps. Double Buffering is used for smooth scrolling. For an explanation see the documents.*/public class ProgramCanvas extends JComponent implements ComponentListener { Graphics currG; public FrameDisplay parent; boolean blink = false; boolean blFlag = true; //Data Structures // Data Structures containing all displayable states StateGroupListPair all_states; Vector dtypeV; double xDensity; //pixels / second double yDensity; //pixels / process vertical thickness int _xPix; //width of each image int _yPix; //height of each image int widthCan; //width of the viewport int heightCan; //height of the viewport double maxT; //Maximum duration in time in the display double tMaxT; //end time of the last state in all tasks //relative to the FrameBegTime int maxH; //horizontal pixel limit on display //horizontal distance between the start of the viewport and the zoom lock line int zDist; //horizontal scroll postion of JViewport in the 3 images canvas int panePosX; int sbPos; int cursorPos; //Horizontal cursor position in this canvas int tbegH; //previous horizontal scrollbar value boolean fflagH, bflagH; //flags describing forward or backward // scrolling (horizontally) public double zoomH; //double value describing the zoom double zF; //zoom Factor double FrameBegTime; //the start time of the Frame double FrameEndTime; //the end time of the Frame double begTime; //time at start of the viewport double endTime; //time at the end of the viewport double currTime; //time at the current cursor position double zXTime; //time at which the zoom lock line was set double elapsedPoint; //time at which the elapsed time line was set private boolean elTLineDispStatus = true; //elapsed time line display //status private boolean zoomLkLineDispStatus = true; //zoom lock line display status // int pu = 1; int endGap = 0; //horizontal gap at the end of drawings so that //the drawings towards the right end are not //covered by the ScrollPane Container MyImage [] img; //array of 3 offscreen images MyImage currImg; //current image placed in the viewport int lineSize, rulerHt, hBWt, allDispHt, eachDispHt, eachDispIHt, fDescent; public int dpi; //number of pixels / inch on the screen public FontMetrics fm; //FontMetrics object describing the curren font // int maxLevel = 0; //maximum level of nesting double nestFactor; //value describing the height difference //between levels //nestFactor * yDensity will be the difference in height between two consecutive levels boolean message = false; //value decribing whether an arrow circle is present ?? boolean Windows = false; //catering for the extra scrollbar public boolean setupComplete; //setup status Timer timer; int totalProc; private int elTimeStrW, zLockStrW; //Constructor public ProgramCanvas() { super (); } //Setup methods ----------------------------------------------------------- void init ( FrameDisplay parent_win, double starttime, double endtime ) { setupComplete = false; parent = parent_win; all_states = parent_win.all_states; setupData ( starttime, endtime ); adjustCanvasStuff (); // setupStates (); //if (parent.dtype == CONST.TIMELINES) waitCursor(); all_states.visible.SetupNestedStates(); normalCursor(); dtypeV = new Vector (); dtypeV.addElement (new Integer (parent.dtype)); parent.optionsDlg.addDisplay (parent.dtype); //else setupMtns (); totalProc = all_states.visible.size(); tMaxT = maxT; timer = new Timer (200, new TimerListener ()); Resize (); setupEventHandlers (); debug.println( "In ProgramCanvas's Init() " ); adjustStartEndTimes (); setupComplete = true; } private void setupData( double starttime, double endtime ) { nestFactor = 0.10; zoomH = 1.0; zF = 2.0; //begTime = 0.0; FrameBegTime = starttime; FrameEndTime = endtime; begTime = FrameBegTime; endTime = FrameEndTime; zXTime = FrameBegTime; elapsedPoint = FrameBegTime; // zXTime = 0.0; elapsedPoint = 0.0; maxT = FrameEndTime - FrameBegTime; hBWt = 2; } private void adjustCanvasStuff () { Font f = parent.frameFont; setFont (f); fm = getToolkit ().getFontMetrics (f); lineSize = fm.getHeight (); rulerHt = 3 * lineSize; zLockStrW = fm.stringWidth ("Zoom Lock"); elTimeStrW = fm.stringWidth ("Elapsed Time"); fDescent = fm.getDescent (); dpi = getToolkit ().getScreenResolution (); }/* private void setupMtns () { Enumeration enum = parent.mtns.elements (); while (enum.hasMoreElements ()) { MtnInfo mtn = (MtnInfo)enum.nextElement (); if (mtn.endT > maxT) maxT = mtn.endT; } }*/ /** * Draw initial view consisting of entire data in viewport */ private void drawInitImg () { // double b = begTime, e = endTime; // double b = begTime, e = getTime (_xPix); double b = begTime, e = b + getTime(_xPix); debug.println( "drawInitImg() : " ); debug.println( "\t" + "img[0].drawRegion( " + b + ", " + e + " )" ); img [0].drawRegion (b, e); currImg = img [0]; debug.println( "currImg.begT = " + currImg.begT + ", " + "currImg.endT = " + currImg.endT ); panePosX = _xPix; repaint (); } //end of setup methods----------------------------------------------------------- public void paintComponent (Graphics g) { //We request keyboard focus out here so that we can catch events for locking zoom, etc. requestFocus (); //3 Images are painted each time. The reason we use images rather than directly drawing //onto the graphics context is that the drawings of one image can be taking place //concurrently with the other using threads into offscreen graphic contexts assoc. //with the respective images. While printing we do the opposite if (setupComplete && currImg != null){ int prevH = getPrev (currImg.imgIdH); int nextH = getNext (currImg.imgIdH); if (blFlag || !timer.isRunning ()) { img [prevH].paintStuff1 (0, g, this); currImg.paintStuff1 (_xPix, g, this); img [nextH].paintStuff1 (_xPix + _xPix, g, this); /*g.drawImage (img [prevH].img, 0, 0, this); g.drawImage (img [prevH].timeimg, 0, _yPix, this); g.drawImage (currImg.img, _xPix, 0, this); g.drawImage (currImg.timeimg, _xPix, _yPix, this); g.drawImage (img [nextH].img, _xPix + _xPix, 0, this); g.drawImage (img [nextH].timeimg, _xPix + _xPix, _yPix, this);*/ } else { img [prevH].paintStuff2 (0, g, this); currImg.paintStuff2 (_xPix, g, this); img [nextH].paintStuff2 (_xPix + _xPix, g, this); /*g.drawImage (img [prevH].img1, 0, 0, this); g.drawImage (img [prevH].timeimg, 0, _yPix, this); g.drawImage (currImg.img1, _xPix, 0, this); g.drawImage (currImg.timeimg, _xPix, _yPix, this); g.drawImage (img [nextH].img1, _xPix + _xPix, 0, this); g.drawImage (img [nextH].timeimg, _xPix + _xPix, _yPix, this);*/ } } if (zoomLkLineDispStatus && zXTime >= begTime && zXTime <= endTime) { int xcord = panePosX + getEvtXCord (zXTime - begTime); g.setColor (Color.white); g.drawLine (xcord, 0, xcord, _yPix); g.fillRect (xcord, _yPix - lineSize, zLockStrW + 2, lineSize); g.setColor (Color.black); g.drawRect (xcord, _yPix - lineSize, zLockStrW + 1, lineSize - 1); g.setColor (Color.red); g.drawString ("Zoom Lock", xcord + 1, _yPix - fDescent); } if (elTLineDispStatus && elapsedPoint >= begTime && elapsedPoint <= endTime) { int xcord = panePosX + getEvtXCord (elapsedPoint - begTime); g.setColor (Color.white); g.drawLine (xcord, 0, xcord, _yPix); g.fillRect (xcord, _yPix - lineSize, elTimeStrW + 2, lineSize); g.setColor (Color.black); g.drawRect (xcord, _yPix - lineSize, elTimeStrW + 1, lineSize - 1); g.setColor (Color.red); g.drawString ("Elapsed Time", xcord + 1, _yPix - fDescent); } } //methods for handling smooth scrolling, double buffereing------------------------ /** * Allocate memory for 3 offscreen images */ private void setupImg() { killImgs(); setValues(); parent.vcanvas1.repaint(); parent.vcanvas2.repaint(); img = new MyImage[ 3 ]; for (int i = 0; i < 3; i++) { img [i] = new MyImage( i, _xPix, _yPix, dtypeV, this ); } } protected void setValues () { int numDisp = dtypeV.size (); allDispHt = _yPix - rulerHt; eachDispHt = (int)Math.rint (allDispHt / (double)numDisp); eachDispIHt = eachDispHt - 2 * hBWt; if (eachDispHt < 1) eachDispHt = 1; //eachDispHt should be non zero calcYDensity (); } Image getOffScreenImage (int x, int y) {return createImage (x, y);} private void killImgs () { if (img != null) for (int i = 0; i < 3; i++) if (img [i] != null) { img [i].kill (); img [i] = null; } } /** * Get the appropriate display for the given scroll bar position */ void adjustImgH (int x) { // int epos1 = getEvtXCord (currImg.begT); // int epos2 = getEvtXCord (currImg.endT); int epos1 = getEvtXCord( currImg.begT - FrameBegTime ); int epos2 = getEvtXCord( currImg.endT - FrameBegTime ); int beg = x, end = x + widthCan, tendH = tbegH + widthCan; int gap1 = epos1 - beg; int gap2 = end - epos2; panePosX += (beg - tbegH); //Move the scroll position according to the increment if ((beg < epos1 && tbegH > epos1)) bflagH = true; else if (beg > epos1 && tbegH < epos1) bflagH = false; if ((end > epos2 && tendH < epos2)) fflagH = true; else if (end < epos2 && tendH > epos2) fflagH = false; int gr = getCurrGridH (x); boolean change = ( (bflagH && gap1 >= widthCan) || (fflagH && gap2 >= widthCan) || gap1 > _xPix || gap2 > _xPix ) ? true : false; if (change) { getImgH (gr); panePosX = _xPix + (x - (gr * _xPix)); bflagH = false; fflagH = false; } tbegH = beg; debug.println( "adjustImgH(" + x + ") : A) begTime = " + begTime + ", currImg.begT = " + currImg.begT ); begTime = FrameBegTime + getTime (tbegH); // begTime = getTime (tbegH); // begTime = currImg.begT + getTime (tbegH); debug.println( "adjustImgH(" + x + ") : B) tbegH = " + tbegH + ", " + "begTime = " + begTime ); adjustStartEndTimes (); // zXTime = getTime (tbegH); } private int getGridValue (int val) {return val - (getCurrGridH (val) * _xPix);} /** * Get the appropriate display for the given grid */ private void getImgH (int grid) { // double begT = getTime (grid * _xPix); // double begT = currImg.begT + getTime (grid * _xPix); double begT = FrameBegTime + getTime (grid * _xPix); double endT = begT + getTime (_xPix); MyImage the_img = searchImg (begT, endT); if ( the_img != null ) { currImg = the_img; debug.println( "getImgH(" + grid + ") : " + "currImg = Img[" + the_img.imgIdH + "]" ); } else { debug.println( "getImgH(" + grid + ") : " ); debug.println( "\t" + "currImg[" + currImg.imgIdH + "].drawRegion(" + begT + ", " + endT + ")" ); currImg.drawRegion (begT, endT); } centralizeH (); repaint (); } /** * search for a image with the given starting and ending times */ private MyImage searchImg (double begT, double endT) { for (int i = 0; i < 3; i++) { MyImage cImg = img [i]; if (cImg != null && cImg.begT == begT && cImg.endT == endT) return cImg; } return null; } /** * Return the appropriate value from the circular buffer */ private int getNext( int t ) { return (t + 1) % 3; } private int getPrev( int t ) { if ( t == 0 ) return 2; else return (t - 1) % 3; } /** * Draw the image data which is before and after the current Image */ private void centralizeH () { double t = currImg.endT - currImg.begT; debug.println( "centralizeH : " ); debug.println( "\t" + "currImg.begT = " + currImg.begT + ", " + "currImg.endT = " + currImg.endT ); // if (currImg.endT < maxT) { if ( currImg.endT < FrameBegTime + maxT ) { int nextId = getNext (currImg.imgIdH); if (img [nextId].begT != currImg.endT || img [nextId].endT != (currImg.endT + t)) renderNextH (1); debug.println( "\t" + "nextImg.begT = " + img[nextId].begT + ", " + "nextImg.endT = " + img[nextId].endT ); } // if (currImg.begT > 0.0) { if ( currImg.begT > FrameBegTime ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -