📄 graphpanel.java
字号:
virt = new Point2D(origin.x, origin.y); screen = virtualToScreen(virt); while (screen.x >= 0) { offgraphics.drawLine((int)screen.x, 0, (int)screen.x, getSize().height); virt.x -= xTicSpacing; screen = virtualToScreen(virt); } virt = new Point2D(origin.x, origin.y); screen = virtualToScreen(virt); while (screen.y < getSize().height) { offgraphics.drawLine(0, (int)screen.y, getSize().width, (int)screen.y); virt.y -= yTicSpacing; screen = virtualToScreen(virt); } virt = new Point2D(origin.x, origin.y); screen = virtualToScreen(virt); while (screen.y >= 0) { offgraphics.drawLine(0, (int)screen.y, getSize().width, (int)screen.y); virt.y += yTicSpacing; screen = virtualToScreen(virt); } } void drawAxisAndTics(Graphics offgraphics, Point2D origin, int start, int end, double top, double bottom, double xTicSpacing, double yTicSpacing, Color xColor, Color yColor) { int i; // Draw axis lines Point2D origin_screen = virtualToScreen(origin); offgraphics.setColor(xColor); offgraphics.drawLine(0, (int)origin_screen.y, getSize().width, (int)origin_screen.y); offgraphics.setColor(yColor); offgraphics.drawLine((int)origin_screen.x, 0, (int)origin_screen.x, getSize().height); // Draw the tic marks and numbers offgraphics.setFont(new Font("Default", Font.PLAIN, 10)); offgraphics.setColor(yColor); Point2D virt, screen; boolean label; // Y axis label = true; virt = new Point2D(origin.x, origin.y); screen = virtualToScreen(virt); while (screen.y < getSize().height) { offgraphics.drawLine((int)screen.x - 5, (int)screen.y, (int)screen.x + 5, (int)screen.y); if (label) { String tickstr; int xsub; if (yaxishex) { int tmp = (int)(virt.y); tickstr = "0x"+Integer.toHexString(tmp); xsub = 40; } else { tickstr = new Double(virt.y).toString(); xsub = 25; } offgraphics.drawString(tickstr, (int)screen.x-xsub, (int)screen.y-2); label = false; } else { label = true; } virt.y -= yTicSpacing; screen = virtualToScreen(virt); } label = false; virt = new Point2D(origin.x, origin.y + yTicSpacing); screen = virtualToScreen(virt); while (screen.y >= 0) { offgraphics.drawLine((int)screen.x - 5, (int)screen.y, (int)screen.x + 5, (int)screen.y); if (label) { String tickstr; int xsub; if (yaxishex) { int tmp = (int)(virt.y); tickstr = "0x"+Integer.toHexString(tmp); xsub = 40; } else { tickstr = new Double(virt.y).toString(); xsub = 25; } offgraphics.drawString(tickstr, (int)screen.x-xsub, (int)screen.y-2); label = false; } else { label = true; } virt.y += yTicSpacing; screen = virtualToScreen(virt); } // X axis label = true; virt = new Point2D(origin.x, origin.y); screen = virtualToScreen(virt); while (screen.x < getSize().width) { offgraphics.drawLine((int)screen.x, (int)screen.y - 5, (int)screen.x, (int)screen.y + 5); if (label) { String tickstr = new Double(virt.x).toString(); offgraphics.drawString(tickstr, (int)screen.x-15, (int)screen.y-15); label = false; } else { label = true; } virt.x += xTicSpacing; screen = virtualToScreen(virt); } label = false; virt = new Point2D(origin.x - xTicSpacing, origin.y); screen = virtualToScreen(virt); while (screen.x >= 0) { offgraphics.drawLine((int)screen.x, (int)screen.y - 5, (int)screen.x, (int)screen.y + 5); if (label) { String tickstr = new Double(virt.x).toString(); offgraphics.drawString(tickstr, (int)screen.x-15, (int)screen.y-15); label = false; } else { label = true; } virt.x -= xTicSpacing; screen = virtualToScreen(virt); } } void drawLegend( Graphics offgraphics ) { int i; // Draw the legend if( legend ) { int activeChannels=0,curChan=0; for( i=0;i<NUM_CHANNELS;i++ ) if( legendActive[i] ) activeChannels++; if( activeChannels == 0 ) return; offgraphics.setColor(Color.black); offgraphics.fillRect( getSize().width-20-130, getSize().height-20-20*activeChannels, 130, 20*activeChannels ); offgraphics.setColor(Color.white); offgraphics.drawRect( getSize().width-20-130, getSize().height-20-20*activeChannels, 130, 20*activeChannels ); for( i=NUM_CHANNELS-1;i>=0;i-- ) { if( legendActive[i] ) { offgraphics.setColor(Color.white); offgraphics.drawString( dataLegend[i], getSize().width-20-100, getSize().height-30-17*curChan ); offgraphics.setColor(plotColors[i]); offgraphics.fillRect( getSize().width-20-120, getSize().height-38-17*curChan, 10, 10 ); curChan++; } } } } //return the difference between the two input vectors. Vector diff(Iterator a, Iterator b){ Vector vals = new Vector(); while(a.hasNext() && b.hasNext()){ vals.add(new Double((((Double)b.next()).doubleValue() - ((Double)a.next()).doubleValue()))); } return vals; } //draw the highlight box. void draw_highlight(Graphics g){ if(highlight_start == null) return; int x, y, h, l; x = Math.min(highlight_start.x, highlight_end.x); y = Math.min(highlight_start.y, highlight_end.y); l = Math.abs(highlight_start.x - highlight_end.x); h = Math.abs(highlight_start.y - highlight_end.y); g.setColor(Color.white); g.fillRect(x,y,l,h); } void draw_data(Graphics g, Vector data, int start, int end){ draw_data(g,data, start, end, 1); } //scale multiplies a signal by a constant factor. void draw_data(Graphics g, Vector data, int start, int end, int scale){ Point2D screen = null, screen2 = null; boolean noplot=true; // Used for line plotting for(int i = 0; i < data.size(); i ++){ Point2D virt; //map each point to a x,y position on the screen. if((virt = (Point2D)data.get(i)) != null) { screen = virtualToScreen(virt); if (screen.x >= 0 && screen.x < getSize().width) { if(connectPoints && !noplot) g.drawLine((int)screen2.x, (int)screen2.y, (int)screen.x, (int)screen.y); else if( !connectPoints ) g.drawRect((int)screen.x, (int)screen.y, 1, 1); if (noplot) noplot = false; } else { noplot = true; } } screen2 = screen; } } //functions for controlling zooming. void move_up(){ double height = top - bottom; bottom += height/4; top += height/4; } void move_down(){ double height = top - bottom; bottom -= height/4; top -= height/4; } void move_right(){ int width = end - start; start += width/4; end += width/4; } void move_left(){ int width = end - start; start -= width/4; end -= width/4; } void zoom_out_x(){ int width = end - start; start -= width/2; end += width/2; } void zoom_out_y(){ double height = top - bottom; bottom -= height/2; top += height/2; } void zoom_in_x(){ int width = end - start; start += width/4; end -= width/4; } void zoom_in_y(){ double height = top - bottom; bottom += height/4; top -= height/4; } void reset(){ bottom = DEFAULT_BOTTOM; top = DEFAULT_TOP; start = DEFAULT_START; end = DEFAULT_END; } void clear_data() { // Reset all motes if (oscilloscope.group_id != -1) { try { System.err.println("SENDING OscopeResetmsg\n"); mote.send(MoteIF.TOS_BCAST_ADDR, new OscopeResetMsg()); } catch (IOException ioe) { System.err.println("Warning: Got IOException sending reset message: "+ioe); ioe.printStackTrace(); } } int i; data = new Vector2[NUM_CHANNELS]; for( i=0;i<NUM_CHANNELS;i++ ) data[i] = new Vector2(); for( i=0;i<NUM_CHANNELS;i++ ) dataLegend[i] = ""; for( i=0;i<NUM_CHANNELS;i++ ) legendActive[i] = false; for( i=0;i<NUM_CHANNELS;i++ ) lastPacketNum[i] = -1; for( i=0;i<NUM_CHANNELS;i++ ) streamNum[i] = -1; for( i=0;i<NUM_CHANNELS;i++ ) moteNum[i] = -1; } // Currently non-functional b/c of switch to 2D point data void load_data(){ JFileChooser file_chooser = new JFileChooser(); File loadedFile; FileReader dataIn; String lineIn; int retval,chanNum,numSamples; boolean keepReading; retval = file_chooser.showOpenDialog(null); if( retval == JFileChooser.APPROVE_OPTION ) { try { loadedFile = file_chooser.getSelectedFile(); System.out.println( "Opened file: "+loadedFile.getName() ); dataIn = new FileReader( loadedFile ); keepReading = true; chanNum = numSamples = -1; while( keepReading ) { lineIn = read_line( dataIn ); if( lineIn == null ) keepReading = false; else if( !lineIn.startsWith( "#" ) ) { if( chanNum == -1 ) { try { chanNum = Integer.parseInt( lineIn.substring(0,lineIn.indexOf(" ")) ); numSamples = Integer.parseInt( lineIn.substring(lineIn.indexOf(" ")+1,lineIn.length()) ); data[chanNum] = new Vector2(); System.out.println( ""+chanNum+" "+numSamples+"\n" ); } catch (NumberFormatException e) { System.out.println("File is invalid." ); System.out.println(e); } } else { try { numSamples--; if( numSamples <= 0 ) numSamples = chanNum = -1; } catch (NumberFormatException e) { System.out.println("File is invalid." ); System.out.println(e); } } } } dataIn.close(); } catch( IOException e ) { System.out.println( e ); } } } String read_line( FileReader dataIn ) { StringBuffer lineIn = new StringBuffer(); int c,readOne; try { while( true ) { c = dataIn.read(); if( c == -1 || c == '\n' ) { if( lineIn.toString().length() > 0 ) return lineIn.toString(); else return null; } else lineIn.append((char)c); } } catch ( IOException e ) { } return lineIn.toString(); } void save_data(){ JFileChooser file_chooser = new JFileChooser(); File savedFile; FileWriter dataOut; int retval,i,n; retval = file_chooser.showSaveDialog(null); if( retval == JFileChooser.APPROVE_OPTION ) { try { savedFile = file_chooser.getSelectedFile(); System.out.println( "Saved file: "+savedFile.getName() ); dataOut = new FileWriter( savedFile ); dataOut.write( "# Test Data File\n" ); dataOut.write( "# "+(new Date())+"\n" ); for( i=0;i<10;i++ ) { if( data[i].size() > 0 ) { dataOut.write( "# Channel "+i+"\n" ); dataOut.write( "# "+data[i].size()+" samples\n" ); dataOut.write( ""+i+" "+data[i].size()+"\n" ); for( n=0;n<data[i].size();n++ ) { dataOut.write( ((Double)data[i].get(n)).toString() ); dataOut.write( "\n" ); } } } dataOut.close(); } catch( IOException e ) { System.out.println( e ); } } } void set_zoom(){ int base = getSize().height; int x_start = Math.min(highlight_start.x, highlight_end.x); int x_end = Math.max(highlight_start.x, highlight_end.x); int y_start = Math.min(highlight_start.y, highlight_end.y); int y_end = Math.max(highlight_start.y, highlight_end.y); if(Math.abs(x_start - x_end) < 10) return; if(Math.abs(y_start - y_end) < 10) return; Point2D topleft = screenToVirtual(new Point2D(x_start, y_start)); Point2D botright = screenToVirtual(new Point2D(x_end, y_end)); start = (int)topleft.x; end = (int)botright.x; top = topleft.y; bottom = botright.y; } /** Convert from virtual coordinates to screen coordinates. */ Point2D virtualToScreen(Point2D virt) { double xoff = virt.getX() - start; double xpos = xoff / (end*1.0 - start*1.0); double screen_xpos = xpos * getSize().width; double yoff = virt.getY() - bottom; double ypos = yoff / (top*1.0 - bottom*1.0); double screen_ypos = getSize().height - (ypos * getSize().height); return new Point2D(screen_xpos, screen_ypos); } /** Convert from screen coordinates to virtual coordinates. */ Point2D screenToVirtual(Point2D screen) { double xoff = screen.getX(); double xpos = xoff / (getSize().width * 1.0); double virt_xpos = start + (xpos * (end*1.0 - start*1.0)); double yoff = screen.getY(); double ypos = yoff / (getSize().height * 1.0); double virt_ypos = top - (ypos * (top*1.0 - bottom*1.0)); return new Point2D(virt_xpos, virt_ypos); } /** Find nearest point in 'data' to x-coordinate of given point. */ Point2D findNearestX(Vector2 data, Point2D test) { try { float xval = Math.round(test.x); for (int i = 0; i < data.size(); i++) { Point2D pt = (Point2D)data.get(i); if (pt == null) continue; if (Math.round(pt.x) == xval) { return pt; } } return null; } catch (Exception e) { return null; } } /** A simple inner class representing a 2D point. */ class Point2D { double x, y; Point2D(double newX, double newY) { x = newX; y = newY; } double getX() { return x; } double getY() { return y; } public String toString() { return x+","+y; } } /** An extension to Vector supporting chop(). */ class Vector2 extends java.util.Vector { void chop(int index) { removeRange(0, index); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -