📄 netbeamschart.java
字号:
// else if we've resized the window, we need to resize buffer } else if (pts.length != graphW) { float[] tmp = new float[graphW]; if (ptNum < graphW) { // window is wider than data System.arraycopy(pts, 0, tmp, 0, ptNum); } else { // more data than fits window System.arraycopy(pts, ptNum - graphW, tmp, 0, graphW - 1); ptNum = graphW - 1; } pts = tmp; } // go fetch next value of data pts[ptNum] = getCurrentValue(); //System.out.println("currentValue = " +pts[ptNum]); if (fullDrawNeeded) ptNum++; // find the min/max within valid data if (ptNum > 0) { minval = pts[ptNum - 1]; maxval = pts[ptNum - 1]; } for (int i = 0; i < ptNum; i++) { minval = Math.min(minval, pts[i]); maxval = Math.max(maxval, pts[i]); } //System.out.println("minval = " + minval +", maxval = "+maxval); // determine the min/max chart range make it sticky and stable // to prevent replotting the chart lines all the time // also, give some head and tail room float diff = 1.5f * (maxval - minval) / NROWS; int incr = 1; while (diff > 10.0f) { diff /= 10.0f; incr *= 10; } if (diff > 5.0f) { incr *= 10; } else if (diff > 2.0f) { incr *= 5; } else if (diff > 1.0f) { incr *= 2; } int totalspan = incr * NROWS; int ymin = (int) ((maxval + minval - totalspan) / 2); ymin = ((int) (ymin / incr)) * incr; int ymax = ymin + totalspan; big.setBackground(getBackground()); big.clearRect(0, 0, w, h); // .. Draw axis strings .. big.setColor(Color.green); float currVal0 = ((int) (currVal * 10)) / 10.0f; big.drawString(units + " [current val = " + currVal0 + "]", 35, ascent + 1); big.drawString("Time: " + (new Date()), 40, h - descent); // .. Draw History Graph .. big.setColor(graphColor); graphOutlineRect.setRect(graphX, graphY, graphW, graphH); big.draw(graphOutlineRect); int graphRow = graphH / NROWS; // .. Draw rows and unit scale .. int yval = ymax; for (int j = graphY; j <= graphH + graphY; j += graphRow) { graphLine.setLine(graphX, j, graphX + graphW, j); big.setColor(graphColor); big.draw(graphLine); big.setColor(Color.green); String yvalstr = Integer.toString(yval); if (fm == null) fm = g.getFontMetrics(); int swidth = fm.stringWidth(yvalstr); big.drawString(yvalstr, 33 - swidth, j + 4); yval -= incr; } // .. Draw animated column movement .. int graphColumn = graphW / NCOLS; big.setColor(graphColor); if (columnInc == 0) { columnInc = graphColumn; } for (int j = graphX + columnInc; j < graphW + graphX; j += graphColumn) { graphLine.setLine(j, graphY, j, graphY + graphH); big.draw(graphLine); } if (fullDrawNeeded) --columnInc; // paint the chart big.setColor(Color.yellow); for (int j = graphX + graphW - ptNum, k = 0; k < ptNum; k++, j++) { if (k != 0) { if (pts[k] != pts[k - 1]) { big.drawLine(j - 1, (int) (graphY + graphH - graphH * (pts[k - 1] - ymin) / totalspan), j, (int) (graphY + graphH - graphH * (pts[k] - ymin) / totalspan)); } else { big.fillRect(j, (int) (graphY + graphH - graphH * (pts[k] - ymin) / totalspan), 1, 1); } } } if (ptNum + 2 >= pts.length) { // throw out oldest point for (int j = 1; j < ptNum; j++) { pts[j - 1] = pts[j]; } --ptNum; } g.drawImage(bimg, 0, 0, this); fullDrawNeeded = false; } public void start() { thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.setName("NetBEAMS-" + sid); thread.start(); } public synchronized void stop() { thread = null; notify(); } public void run() { Thread me = Thread.currentThread(); while (thread == me && !isShowing() || getSize().width == 0) { try { Thread.sleep(600); } catch (InterruptedException e) { return; } } while (thread == me && isShowing()) { Dimension d = getSize(); if (d.width != w || d.height != h) { w = d.width; h = d.height; bimg = (BufferedImage) createImage(w, h); big = bimg.createGraphics(); big.setFont(font); fm = big.getFontMetrics(font); ascent = (int) fm.getAscent(); descent = (int) fm.getDescent(); } repaint(); try { Thread.sleep(sleepAmount); // retrieve the URL data String xmldoc = DataManager.retrieveURLFile(url); String path = System.getenv("HOME"); path += "/public_html/" + gid + "/" + sid + "/" + System.currentTimeMillis() + ".xml"; DataManager.prepDirectory(path); long currtimemillis = System.currentTimeMillis(); long elapsed = currtimemillis - lastwritetime; if (elapsed > writeInterval) { DataManager.saveToFile(xmldoc, path); lastwritetime = currtimemillis; String value = NRSSDataExtractor.getValueFromURL(url, vxpath); //System.out.println("curr value: " + value); try { currVal = scalefactor * Float.parseFloat(value); } catch (Exception exc) { exc.printStackTrace(); } } } catch (InterruptedException e) { break; } } thread = null; } } public static void main(String s[]) throws Exception { Properties p = new Properties(); p.setProperty("url", "http://198.144.203.235/~jcliu/netbeams.xml"); p.setProperty("sid", "00001"); p.setProperty("type", "temperature"); p.setProperty("units", "Celsius"); p.setProperty("desc", "Generic Temperature Sensor"); p.setProperty("scalefactor", "1.0"); p.setProperty("gpsx", "-122.0000"); p.setProperty("gpsy", "37.0000"); p.setProperty("vxpath", "/rss/channel/item/description/nrss:data/nrss:content/nrss:argArray[@n='current']/nrss:arg[@n='value']"); final NetBeamsChart demo = new NetBeamsChart(p); WindowListener l = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } public void windowDeiconified(WindowEvent e) { demo.surf.start(); } public void windowIconified(WindowEvent e) { demo.surf.stop(); } }; JFrame f = new JFrame("NetBeams Chart"); f.addWindowListener(l); f.getContentPane().add("Center", demo); f.pack(); f.setSize(new Dimension(500, 200)); f.setVisible(true); demo.surf.start(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -