//File: PlotProbingResults.java //Project: ABMT //Version: 0.2 //Designer: C. Edward Chow //Revision: // 2/9/2001 C. Edward Chow //Bug Report: send to chow@cs.uccs.edu //Description: Plotting class for Internet Network Probing Research // Class that plot line chart as soon as bandwidth data are sent // by traffic reporting program and bandwidth probing tool. // Allow adjusting of horizationtal (time) and vertical (bandwidth) // display scale. // Handle the damage redisplay when window are damaged or reopen // copyright@2001, University of Colorado at Coloraod Springs // unlimited distribution for academic research and education // for commercial usage, please check with C. Edward Chow // import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.io.*; import java.util.*; import java.net.*; import java.text.*; import java.awt.print.PrinterJob; import java.awt.print.*; class bwTimeData { double time; double bw; public bwTimeData(double t, double bandwidth) { time=t; bw=bandwidth; } } /****************************** CLASS NewCanvas *************************/ class ProbingResultCanvas extends Canvas implements Printable { int firstx = 50; // pixel start point (like 0,0) int firsty = 150; // pixel start point (like 0,0) Vector abwData; // available bandwdith vector Vector abw2Data; Vector probingData; boolean flag; double maxBW; double minBW; double linkBW; double startPlotTime; int pixelsPerSecond; PrinterJob printJob; PageFormat pf; Paper paper; public void redisplayPlot(double minbw, double maxbw, double sTime, int pixelspersecond ) { maxBW=maxbw; minBW=minbw; startPlotTime=sTime; pixelsPerSecond=pixelspersecond; repaint(); } public ProbingResultCanvas(double minbw, double maxbw, double sTime, int pixelspersecond) { maxBW=maxbw; minBW=minbw; startPlotTime=sTime; pixelsPerSecond=pixelspersecond; abwData = new Vector(50, 50); abw2Data = new Vector(50, 50); probingData = new Vector(50, 50); flag = true; } public int print(Graphics g, PageFormat pf, int pi) throws PrinterException { if (pi>=1) { return Printable.NO_SUCH_PAGE; } painting(g); return Printable.PAGE_EXISTS; } public void enablePaint() { flag = true; } public int getX(bwTimeData btd) { return 90+(int) ((btd.time - startPlotTime)*pixelsPerSecond); } public int getY(bwTimeData btd) { double bwPerPixel = (maxBW - minBW)/400; return 450 - (int)((btd.bw-minBW)/bwPerPixel); } public void updateDrawing(bwTimeData btd1, bwTimeData btd2, Color c) { Graphics g = this.getGraphics(); g.setColor(c); if (btd1 == null) { g.drawRect(90-2,450-2,4,4); g.fillRect(90-2,450-2,4,4); return; // only one point, skip drawing } // may considere draw a point if (btd1.time >= startPlotTime && btd1.time <= startPlotTime+580.0/pixelsPerSecond && minBW <= btd1.bw && btd1.bw <= maxBW && btd2.time >= startPlotTime && btd2.time <= startPlotTime+580.0/pixelsPerSecond && minBW <= btd2.bw && btd2.bw <= maxBW) { int x1 = getX(btd1); int y1 = getY(btd1); int x2 = getX(btd2); int y2 = getY(btd2); g.drawLine(x1, y1, x2, y2); g.drawRect(x2-2, y2-2, 4, 4); g.fillRect(x2-2, y2-2, 4, 4); } } public void updateABW(double time, double bw) { bwTimeData btd2 = new bwTimeData(time, bw); bwTimeData btd1; if (abwData.isEmpty()) btd1 = null; else btd1 = (bwTimeData) abwData.lastElement(); updateDrawing(btd1, btd2, Color.green); abwData.addElement(btd2); } public void updateABW2(double time, double bw) { bwTimeData btd2 = new bwTimeData(time, bw); bwTimeData btd1; if (abw2Data.isEmpty()) btd1 = null; else btd1 = (bwTimeData) abw2Data.lastElement(); updateDrawing(btd1, btd2, Color.yellow); abw2Data.addElement(btd2); } public void updateProbingData(double time, double bw) { bwTimeData btd2 = new bwTimeData(time, bw); bwTimeData btd1; if (probingData.isEmpty()) btd1 = null; else btd1 = (bwTimeData) probingData.lastElement(); updateDrawing(btd1, btd2, Color.blue); probingData.addElement(btd2); } public void paint(Graphics g){ if (flag) { painting(g); } return; } public void painting(Graphics g) { setBackground(Color.lightGray); Font fn = new Font("Helvetica", Font.PLAIN, 12); // defina a font g.setFont(fn); // and set it g.setColor(Color.yellow); g.drawRect(15,15,4,4); g.fillRect(15,15,4,4); String colorStr = "forward ABW"; g.setColor(Color.black); g.drawString(colorStr, 25, 20); g.setColor(Color.green); g.drawRect(200,15,4,4); g.fillRect(200,15,4,4); colorStr = "backward ABW"; g.setColor(Color.black); g.drawString(colorStr, 210, 20); g.setColor(Color.blue); g.drawRect(400,15,4,4); g.fillRect(400,15,4,4); colorStr = "measured ABW"; g.setColor(Color.black); g.drawString(colorStr, 410, 20); g.setColor(Color.black); // our working area is at 60,50 to 640,450 (x,y) g.drawRect(90,50,580,400); // from x,y to width,height // now put titles and axis labels on rectangle // calculate y (bw) axis labels and display bw range double bwRange = maxBW - minBW; double bwPerTick = bwRange/10; // per 40 pixels double bwPerPixel = bwRange/400; double bwScale=1000000; char bwUnit='M'; if (bwPerTick/1000000 >=1) { // use Mbps as unit g.drawString("Bandwidth in Mbps",107,40); } else { // use kbps as unit g.drawString("Bandwidth in kbps",107,40); bwScale=1000; bwUnit='k'; } for (int i=0; i<=10; i++) { double bwLabel = (minBW+i*bwPerTick)/bwScale; DecimalFormat doubleDF = new DecimalFormat("##0.0"); //The fraction part has only one digit String label= doubleDF.format(bwLabel)+bwUnit; g.drawString(label, 30, 450-i*40); } // calculate x (time) axis and display time range for (int i=0; i<=580/pixelsPerSecond; i++) { DecimalFormat doubleDF = new DecimalFormat("#0.0"); double time=startPlotTime+i; String label = doubleDF.format(time); g.drawString(label, 90+i*pixelsPerSecond, 460); } // now plot the data we read in before // bw is for bandwidth data, t is for time data // calculate offset from value to pixel for both lines we plot // plot abw and probingData lines, a = abw line, p = probingData line // // remember we draw from bottom left up to top right /* pixel x=60, y=50 x=640,y=50 -------------------------------------------------------- | | | | | | | | | | -------------------------------------------------------- pixel x=60,y=450 x=640,y=450 |Jan=x65,Feb=115,Mar=165,Apr=215,May=265, Jun=315,Jul=365,Aug=415,Sept=465,Oct=515,Nov=565,Dec=615 */ // first pixels (lines) start at 0,0 = 65,450 (bottom left) firstx = 90; // pixel value of start point (like 0,0) firsty = 450; try { for (int index = 0; index < abwData.size(); index++) { bwTimeData btd = (bwTimeData) abwData.elementAt(index); if (btd.time >= startPlotTime && btd.time <= startPlotTime+580.0/pixelsPerSecond) { // use 580 instead of 580.0 will trigger integer division get fewer points int x = getX(btd); if (minBW <= btd.bw && btd.bw <= maxBW) { int y = getY(btd); // cast values to int for drawline method g.setColor(Color.green); g.drawLine(firstx, firsty, x, y); g.drawRect(x-2, y-2, 4, 4); g.fillRect(x-2, y-2, 4, 4); firstx = x; firsty = y; // need to fix the case where some mid point out of window } } } // end for int index firstx=90; firsty=450; for (int index = 0; index < abwData.size(); index++) { bwTimeData btd = (bwTimeData) abw2Data.elementAt(index); if (btd.time >= startPlotTime && btd.time <= startPlotTime+580.0/pixelsPerSecond) { // use 580 instead of 580.0 will trigger integer division get fewer points int x = getX(btd); if (minBW <= btd.bw && btd.bw <= maxBW) { int y = getY(btd); // cast values to int for drawline method g.setColor(Color.yellow); g.drawLine(firstx, firsty, x, y); g.drawRect(x-2, y-2, 4, 4); g.fillRect(x-2, y-2, 4, 4); firstx = x; firsty = y; // need to fix the case where some mid point out of window } } } // end for int index firstx=90; firsty=450; for (int index = 0; index < probingData.size(); index++) { bwTimeData btd = (bwTimeData) probingData.elementAt(index); if (btd.time >= startPlotTime && btd.time <= startPlotTime+580.0/pixelsPerSecond) { int x = getX(btd); if (minBW <= btd.bw && btd.bw <= maxBW) { int y = getY(btd); // cast values to int for drawline method g.setColor(Color.blue); g.drawLine(firstx, firsty, x, y); g.drawRect(x-2, y-2, 4, 4); g.fillRect(x-2, y-2, 4, 4); firstx = x; firsty = y; // need to fix the case where some mid point out of window } } } // end for int index } catch (java.lang.NullPointerException n ) { //System.out.println("done plotting sales data"); } Font fb = new Font("Helvetica", Font.BOLD, 14); //def. to use later g.setFont(fb); // 14 pixels per char g.setColor(Color.red); g.drawString("Plot of Available BW and Probing Results ",225,35); // start title } } // end of class PlotCanvas public class PlotProbingResults extends Frame { BorderLayout borderLayout1 = new BorderLayout(); Label padLabel; Label nameLabel; Label maxBWLabel; TextField maxBWData; Label minBWLabel; TextField minBWData; Label startPlotTimeLabel; TextField startPlotTimeData; Label pixelsPerSecondLabel; TextField pixelsPerSecondData; Button redisplayPlot; Button print; /* GUI for testing PlotProbingResults Label abwLabel; Label probeLabel; Label abwBWLabel; Label probeBWLabel; TextField abwTime; TextField abwBWData; TextField probeTime; TextField probeBWData; Button updateABW; Button updateProbe; */ Panel p; // this is our primary panel Graphics gc; // create a graphics object to draw with ProbingResultCanvas c1; // create canvas to place graphics on //Construct the frame public PlotProbingResults(double minbw, double maxbw, double sTime, int pixelspersecond) { c1 = new ProbingResultCanvas(minbw, maxbw, sTime, pixelspersecond); //Jihui enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); } catch(Exception e) { e.printStackTrace(); } pack(); setVisible(true); while (!isShowing()) { } } //Overridden so we can exit on System Close protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if(e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } //Component initialization private void jbInit() throws Exception { this.setLayout(borderLayout1); this.setSize(680, 700); this.setTitle("Plot Probing Results"); this.setBackground(Color.green); p = new Panel(); //Jihui // create a panel in the frame // this.getContentPane().add(p); // add panel to the frame //p.setBackground(Color.green); // of menu panel GridBagLayout gbl = new GridBagLayout(); GridBagConstraints gbc = new GridBagConstraints(); p.setLayout(gbl); Font h14b = new Font("Helvetica",Font.BOLD, 14); // set font Font h12 = new Font("Helvetica",Font.PLAIN, 12); // set font nameLabel = new Label("Plot Probing Results",Label.CENTER); nameLabel.setFont(h14b); gbc.anchor = GridBagConstraints.CENTER; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(nameLabel, gbc); p.add(nameLabel); maxBWLabel = new Label("Display BW Range, maxBW=", Label.CENTER); maxBWLabel.setFont(h12); gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 1; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(maxBWLabel, gbc); p.add(maxBWLabel); String sn = ""+c1.maxBW; maxBWData = new TextField(sn, 10); maxBWData.setFont(h12); gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 2; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(maxBWData, gbc); p.add(maxBWData); minBWLabel = new Label("minBW=", Label.CENTER); minBWLabel.setFont(h12); gbc.anchor = GridBagConstraints.CENTER; gbc.gridwidth = 3; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(minBWLabel, gbc); p.add(minBWLabel); sn = ""+c1.minBW; minBWData = new TextField(sn, 10); minBWData.setFont(h12); gbc.anchor = GridBagConstraints.CENTER; gbc.gridwidth = 4;; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(minBWData, gbc); p.add(minBWData); /* padLabel = new Label(" ", Label.CENTER); gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(padLabel, gbc); p.add(padLabel); */ print = new Button("print"); print.setFont(h12); gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(print, gbc); p.add(print); startPlotTimeLabel = new Label("Display Time Range, startPlotTime=", Label.CENTER); startPlotTimeLabel.setFont(h12); gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 1; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(startPlotTimeLabel, gbc); p.add(startPlotTimeLabel); sn = ""+c1.startPlotTime; startPlotTimeData = new TextField(sn, 10); startPlotTimeData.setFont(h12); gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 2; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(startPlotTimeData, gbc); p.add(startPlotTimeData); pixelsPerSecondLabel = new Label("pixelsPerSecond=", Label.CENTER); pixelsPerSecondLabel.setFont(h12); gbc.anchor = GridBagConstraints.CENTER; gbc.gridwidth = 3; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(pixelsPerSecondLabel, gbc); p.add(pixelsPerSecondLabel); sn = ""+c1.pixelsPerSecond; pixelsPerSecondData = new TextField("10",10); pixelsPerSecondData.setFont(h12); gbc.anchor = GridBagConstraints.CENTER; gbc.gridwidth = 4; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(pixelsPerSecondData, gbc); p.add(pixelsPerSecondData); redisplayPlot = new Button("redisplayPlot"); redisplayPlot.setFont(h12); gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(redisplayPlot, gbc); p.add(redisplayPlot); /* abwLabel = new Label("Available BW Data, time=", Label.CENTER); abwLabel.setFont(h12); gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 1; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(abwLabel, gbc); p.add(abwLabel); abwTime = new TextField("4"); abwTime.setFont(h12); gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 2; gbc.insets = new Insets(10,10,5,5); gbl.setConstraints(abwTime, gbc); p.add(abwTime); abwBWLabel = new Label("BW=", Label.CENTER); abwBWLabel.setFont(h12); gbc.anchor = GridBagConstraints.CENTER; gbc.gridwidth = 3; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(abwBWLabel, gbc); p.add(abwBWLabel); abwBWData = new TextField("4E6"); abwBWData.setFont(h12); gbc.anchor = GridBagConstraints.CENTER; gbc.gridwidth = 4; gbc.insets = new Insets(10,10,5,5); gbl.setConstraints(abwBWData, gbc); p.add(abwBWData); updateABW = new Button("update ABW"); updateABW.setFont(h12); gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.insets = new Insets(0,0,5,5); gbl.setConstraints(updateABW, gbc); p.add(updateABW); probeLabel = new Label("Probing Result Data, time=", Label.CENTER); probeLabel.setFont(h12); gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 1; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(probeLabel, gbc); p.add(probeLabel); probeTime = new TextField("4"); probeTime.setFont(h12); gbc.anchor = GridBagConstraints.WEST; gbc.gridwidth = 2; gbc.insets = new Insets(10,10,5,5); gbl.setConstraints(probeTime, gbc); p.add(probeTime); probeBWLabel = new Label("BW=", Label.CENTER); probeBWLabel.setFont(h12); gbc.anchor = GridBagConstraints.CENTER; gbc.gridwidth = 3; gbc.insets = new Insets(0,0,0,0); gbl.setConstraints(probeBWLabel, gbc); p.add(probeBWLabel); probeBWData = new TextField("3.5E6"); probeBWData.setFont(h12); gbc.anchor = GridBagConstraints.CENTER; gbc.gridwidth = 4; gbc.insets = new Insets(10,10,5,5); gbl.setConstraints(probeBWData, gbc); p.add(probeBWData); updateProbe = new Button("update probing data"); updateProbe.setFont(h12); gbc.anchor = GridBagConstraints.EAST; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.insets = new Insets(0,0,5,5); gbl.setConstraints(updateProbe, gbc); p.add(updateProbe); */ c1.resize(680, 500); gbc.anchor = GridBagConstraints.CENTER; gbc.gridwidth = GridBagConstraints.REMAINDER; gbc.insets = new Insets(0,0,5,0); gbl.setConstraints(c1, gbc); p.add(c1); this.add(p); } /* public void actionPerformed(ActionEvent e) { if (e.getSource().equals("p.print") { printJob = PrinterJob.getPrinterJob(); // PageFormat pf = printJob.pageDialog(printJob.defaultPage()); /* PageFormat pf = new PageFormat(); System.out.println("Page height is"+pf.getHeight()); System.out.println("Page width is"+pf.getWidth()); System.out.println("Page Imagable height is"+pf.getImageableHeight()); System.out.println("Page imagable width is"+pf.getImageableWidth()); System.out.println("Page Image X is"+pf.getImageableX()); System.out.println("Page Image Y is"+pf.getImageableY()); */ // System.out.println("The orientation is"+pf.getOrientation()); // pf.setOrientation(PageFormat.LANDSCAPE); // System.out.println("The orientation is"+pf.getOrientation()); // PageFormat landscape = printJob.defaultPage(); // landscape.setOrientation(PageFormat.LANDSCAPE); /* printJob.setPrintable(this); if (printJob.printDialog()) { try { printJob.print(); } catch (Exception ex) { ex.printStackTrace(); } } } } */ public boolean action(Event e, Object o) { if (e.target instanceof Button && o.equals("redisplayPlot")) { String smaxBWData = maxBWData.getText(); String sminBWData = minBWData.getText(); String sstartPlotTimeData = startPlotTimeData.getText(); String spixelsPerSecondData = pixelsPerSecondData.getText(); try { Double r = Double.valueOf(smaxBWData); double maxbw = r.doubleValue(); r = Double.valueOf(sminBWData); double minbw = r.doubleValue(); r = Double.valueOf(sstartPlotTimeData); double sTime = r.doubleValue(); int pixelspersecond = Integer.parseInt(spixelsPerSecondData); c1.redisplayPlot(minbw, maxbw, sTime, pixelspersecond); } catch (NumberFormatException nfe) { System.err.println("redisplayPlot: Could not convert string to double "); } } if (e.target instanceof Button && o.equals("print")) { c1.printJob = PrinterJob.getPrinterJob(); c1.paper = new Paper(); c1.paper.setImageableArea(0, 0, 612, 792); // c1.pf = c1.printJob.pageDialog(c1.printJob.defaultPage()); c1.pf = new PageFormat(); c1.pf.setPaper(c1.paper); c1.pf.setOrientation(PageFormat.LANDSCAPE); c1.printJob.setPrintable(c1, c1.pf); if (c1.printJob.printDialog()) { try { c1.printJob.print(); } catch (Exception ex) { ex.printStackTrace(); } } } /* if (e.target instanceof Button && o.equals("update ABW")) { String sabwTime = abwTime.getText(); String sabwBWData = abwBWData.getText(); try { Double r = Double.valueOf(sabwTime); double dabwTime = r.doubleValue(); r = Double.valueOf(sabwBWData); double dabwBWData = r.doubleValue(); c1.updateABW(dabwTime, dabwBWData); } catch (NumberFormatException nfe) { System.err.println("update ABW: Could not convert string to double "); } } if (e.target instanceof Button && o.equals("update probing data")) { String sprobeTime = probeTime.getText(); String sprobeBWData = probeBWData.getText(); try { Double r = Double.valueOf(sprobeTime); double dprobeTime = r.doubleValue(); r = Double.valueOf(sprobeBWData); double dprobeBWData = r.doubleValue(); c1.updateProbingData(dprobeTime, dprobeBWData); } catch (NumberFormatException nfe) { System.err.println("update probing data: Could not convert string to double "); } } */ return true; } }