📄 graphpanel.java
字号:
drawGridLines( offgraphics, numLines, xPos, yPos, xTicSpacing, yTicSpacing );
drawAxisAndTics( offgraphics, numLines, xPos, yPos, start, end, top, bottom, xTicSpacing, yTicSpacing, xColor, yColor, xOff, yOff );
//draw the highlight box if there is one.
draw_highlight(offgraphics);
//draw the input channels.
for(i = 0; i < num_channels; i ++) {
offgraphics.setColor(plotColors[i]);
if( legendActive[i] )
draw_data(offgraphics, data[i], start, end);
}
offgraphics.setColor(Color.white);
// Draw the value tester line if needed
if( valueTest ) {
double h_step_size = (double)d.width / (double)(end - start);
double v_step_size = (double)getSize().height / (double)(top-bottom);
offgraphics.drawLine( (int)(h_step_size*(valueX-start)), 0, (int)(h_step_size*(valueX-start)), d.height );
offgraphics.drawRect( (int)(h_step_size*(valueX-start))-3, (int)(v_step_size*(top-valueY))-3, 6, 6 );
offgraphics.drawString( (new Integer(valueX)).toString()+", "+(new Integer(valueY)).toString(),
35,d.height-35 );
}
drawLegend( offgraphics );
//transfer the constructed image to the screen.
g.drawImage(offscreen, 0, 0, null);
}
void drawGridLines( Graphics offgraphics, int numLines, int xPos, int yPos,
double xTicSpacing, double yTicSpacing ) {
// Draw the grid lines
offgraphics.setColor( Color.darkGray );
for( int i=-numLines;i<numLines;i++ ) {
offgraphics.drawLine( 0,(int)(yPos-i*yTicSpacing),getSize().width,(int)(yPos-i*yTicSpacing) );
offgraphics.drawLine( (int)(xPos-i*xTicSpacing),0,(int)(xPos-i*xTicSpacing),getSize().height );
}
}
void drawAxisAndTics( Graphics offgraphics, int numLines, int xPos, int yPos, int start,
int end, double top, double bottom, double xTicSpacing, double yTicSpacing,
Color xColor, Color yColor, int xOff, int yOff ) {
int i;
// Draw axis lines
offgraphics.setColor(xColor);
offgraphics.drawLine( 0, yPos, getSize().width, yPos );
offgraphics.setColor(yColor);
offgraphics.drawLine( xPos, 0, xPos, getSize().height );
//xPos = getSize().width*start/(start-end);
//yPos = (int)(((float)(getSize().height*top))/((float)(top-bottom)));
// Draw the tic marks
offgraphics.setColor(yColor);
for( i=-numLines;i<numLines;i++ )
offgraphics.drawLine( xPos-5,(int)(yPos-i*yTicSpacing),xPos+5,(int)(yPos-i*yTicSpacing) );
offgraphics.setColor(xColor);
for( i=-numLines;i<numLines;i++ )
offgraphics.drawLine( (int)(xPos-i*xTicSpacing),yPos-5,(int)(xPos-i*xTicSpacing),yPos+5 );
// Draw numbers on tics
offgraphics.setFont( new Font( "Default", Font.PLAIN, 10 ) );
offgraphics.setColor(yColor);
for( i=-numLines;i<numLines;i+=2 ) {
double curPos = i-(yOff);
offgraphics.drawString( (new Double(curPos*yScaling)).toString(),xPos-25,(int)(yPos-i*yTicSpacing)-2 );
}
offgraphics.setColor(xColor);
for( i=-numLines;i<numLines;i+=2 ) {
double curPos = -i+(xOff);
offgraphics.drawString( (new Double(curPos*xScaling)).toString(),(int)(xPos-i*xTicSpacing)-15,yPos+15 );
}
}
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.
V2 diff(Iterator a, Iterator b){
V2 vals = new V2();
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, V2 data, int start, int end){
draw_data(g,data, start, end, 1);
}
void draw_data(Graphics g, V2 data, int start, int end, int scale){
int x1=0, y1=0, x2=0, y2=0;
boolean noplot=true; // Used for line plotting //scale multiplies a signal by a constant factor.
//determine the step sizes double h_step_size = (double)getSize().width / (double)(end - start); double v_step_size = (double)getSize().height / (double)(top-bottom); if(end > data.size()) end = data.size(); int base = getSize().height; for(int i = 0; i < data.size(); i ++){ //map each point to a x,y position on the screen. if( data.get(i) != null ) { x1 = (int)((((Point2D)data.get(i)).getX() * (double)scale - start) * h_step_size); y1 = (int)((((Point2D)data.get(i)).getY() * (double)scale - bottom) * v_step_size); y1 = base - y1; if( x1 > 0 && x1 < getSize().width ) { if( connectPoints && !noplot ) g.drawLine(x2, y2, x1, y1); else if( !connectPoints ) g.drawRect(x1, y1, 1, 1); if( noplot ) noplot = false; } else { noplot = true; } } x2 = x1; y2 = y1; }
}
//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;
xScaling *= 2;
}
void zoom_out_y(){
double height = top - bottom;
bottom -= height/2;
top += height/2;
yScaling *= 2;
}
void zoom_in_x(){
int width = end - start;
start += width/4;
end -= width/4;
xScaling /= 2;
}
void zoom_in_y(){
double height = top - bottom;
bottom += height/4;
top -= height/4;
yScaling /= 2;
}
void reset(){
bottom = 00;
top = 1024.00;
start = -400; end = 5000;
xScaling = 200;
yScaling = 75;
}
void clear_data() { int i; // Reset all motes SerialForwarderStub rw = new SerialForwarderStub("localhost", 9000); byte [] packet = new byte[SerialForwarderStub.PACKET_SIZE]; short TOS_BCAST_ADDR = (short) 0xffff; // Assign packet contents packet[0] = (byte) ((TOS_BCAST_ADDR >> 8) & 0xff); packet[1] = (byte) (TOS_BCAST_ADDR & 0xff); packet[2] = 32; // Reset request packet[3] = 125; //group_id; try { rw.Open(); rw.Write( packet ); Thread.sleep(100); rw.Close(); } catch (IOException e) { System.out.println( e ); } catch (java.lang.InterruptedException e){ e.printStackTrace(); } data = new V2[10]; for( i=0;i<num_channels;i++ ) data[i] = new V2(); 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 V2();
System.out.println( ""+chanNum+" "+numSamples+"\n" );
} catch (NumberFormatException e) {
System.out.println("File is invalid." );
System.out.println(e);
}
} else {
try {
//add_point(new Point2D(lineIn), chanNum);
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(){
double h_step_size = (double)getSize().width / (double)(end - start);
double v_step_size = (double)getSize().height / (double)(top-bottom);
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(base - highlight_start.y, base - highlight_end.y);
int y_end = Math.max(base - highlight_start.y, base - highlight_end.y);
if(Math.abs(x_start - x_end) < 10) return;
if(Math.abs(y_start - y_end) < 10) return;
end = start + (int)((double)x_end / h_step_size);
start = start + (int)((double)x_start / h_step_size);
top = bottom + (double)((double)y_end / v_step_size);
bottom = bottom + (double)((double)y_start / v_step_size);
xScaling = (end-start)/25;
yScaling = (top-bottom)/13.65333;
}
boolean check_crc(byte[] packet){ int len = packet.length; packet[0] = (byte) 0xff; packet[1] = (byte) 0xff; int crc = calcrc(packet, len - 2); int check = ((packet[len - 1] * 256) & 0xff00) + (packet[len - 2] & 0xff); System.out.print(crc + " "); System.out.print(check + " "); System.out.println(len); if(check == 0) return true; return check == crc; }public int calcrc(byte[] packet, int count){ int crc=0, index=0; int i; while (count > 0) { crc = crc ^ (int) packet[index] << 8; index++; i = 8; do { if ((crc & 0x8000) == 0x8000) crc = crc << 1 ^ 0x1021; else crc = crc << 1; } while(--i != 0); count --; } return (crc & 0xffff);}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -