// // zooming_map_old.java // /******************************************************************* Copyright (c) 1997-2003, Research Systems, Inc. All rights reserved. Unauthorized reproduction prohibited. Modified by Tzipi Sidel May 2004 ********************************************************************/ import java.awt.*; import java.applet.*; import java.io.*; import java.net.*; import com.rsi.ion.*; public class zooming_map_old extends Applet implements IONDisconnectListener,IONOutputListener,IONMouseListener { // Instance Vars IONGrConnection c_ionCon; // ION Connection object IONGrDrawable c_ionDrw; // Drawing area Dimension c_dimApp; int c_bConnected=0; // 0 => !conn, 1 => conn, -1 => conn failed IONVariable xval1; IONVariable yval1; IONVariable xval2; IONVariable yval2; float delta_lon; float delta_lat; float min_lon; float min_lat; float max_lon; float max_lat; float minLat; float maxLat; float minLon; float maxLon; float c_xval1=0; // initial x for rubber band box float c_yval1=0; // initial y for rubber band box float c_xval2=0; // final x for rubber band box float c_yval2=0; // final y for rubber band box int c_x1=0; // initial x java int c_y1=0; // initial y java int c_x2=0; // final x java int c_y2=0; // final y java // ****************************** // Init Method // ****************************** public void init(){ // Create connection and drawable objects c_ionCon = new IONGrConnection(); c_dimApp = getSize(); c_ionDrw = new IONGrDrawable(c_dimApp.width, c_dimApp.height); min_lon = -180; min_lat = -90; delta_lon = 360; delta_lat = 180; max_lon = min_lon + delta_lon; max_lat = min_lat + delta_lat; add(c_ionDrw); // add draw window to applet } /* ************************************************* * Inorder to display status messages at startup and also * to be able to disconnect when the page is not being viewed * we override the Applets start() and stop() methods ************************************************* * start() * * Purpose: * Overide the applet's start method. * Connect to IONJ if not already connected. * * Note: in pre-ION1.4 releases, this method called repaint. repaint * then would call our paint method (now deleted from this file). The * paint method was responsible for connecting. However, in some cases * our paint method would not be called and the applet would not get its * data from the server. * We are now guaranteed that we will connect to the IONJ server because * start() will always be called when the applet starts. */ public void start(){ if(c_bConnected == 0) // Not connected to ION, do so. connectToServer(); } /* ************************************************* * stop() * * Purpose: * Override the applet's stop method. This method * Is called when the page is not being viewed. We * disconnect from the server when this is the case. */ public void stop(){ if(c_bConnected == 1){ c_ionCon.removeIONDisconnectListener(this); c_ionCon.disconnect(); c_bConnected=0; } } /* ************************************************ * connectToServer() * * Purpose: * Connects to the ION server, providing feedback to user */ private void connectToServer(){ // Write Status message writeMessage("Connecting to ION Java Server..."); // Connect to the server try { c_ionCon.connect(this.getCodeBase().getHost()); } catch(UnknownHostException eUn) { System.err.println("Error: Unknown Host.") ; writeMessage("Error:Unknown Host."); c_bConnected = -1; return; } catch(IOException eIO) { System.err.println("Error: Establishing Connection. ION Java Server Down?"); writeMessage("Error: Establishing Connection. ION Java Server Down?"); c_bConnected = -1; return; } catch(IONLicenseException eLic){ System.err.println("Error: ION Java License Unavailable.") ; writeMessage("Error: ION Java License Unavailable."); c_bConnected = -1; return; } c_bConnected = 1; c_ionCon.setDecomposed(false);//use 8-bit color mode c_ionCon.addIONDisconnectListener(this); c_ionCon.addIONOutputListener(this); c_ionDrw.addIONMouseListener(this, IONMouseListener.ION_MOUSE_DOWN); c_ionCon.debugMode(true); // Add the drawable to the connection c_ionCon.addDrawable(c_ionDrw); writeMessage("Loading Map..."); // message to screen try{ // execute the plot_map.pro that plot the map String smsg; smsg = "plot_map,"+min_lat+","+max_lat+","+min_lon+","+max_lon; writeMessage("Command:"+smsg); executeCmd(smsg); //Note that it is generally faster to package multiple //IDL commands into a single .pro to call. This example //sends commands separately so that the code is easier to follow. } catch(Exception e) { String smsg; if(e instanceof IOException) smsg = "Communication error:"+e.getMessage(); else if(e instanceof IONSecurityException ) smsg = "ION Java security error"; else if(e instanceof IONIllegalCommandException ) smsg = "Illegal IDL Command detected on server."; else smsg = "Unknown error: "+e.getMessage(); System.err.println("Error: "+smsg); writeMessage("Error: "+smsg); } writeMessage("Use mouse to draw a rubber band box."); } /* /* ************************************************ * executeCmd() * * Purpose: * Try executing IDL command. */ private void executeCmd(String cmd) { try{ c_ionCon.executeIDLCommand(cmd); } catch(Exception e) { String smsg; if(e instanceof IOException) smsg = "Communication error:"+e.getMessage(); else if(e instanceof IONSecurityException ) smsg = "ION Java security error"; else if(e instanceof IONIllegalCommandException ) smsg = "Illegal IDL Command detected on server."; else smsg = "Unknown error: "+e.getMessage(); System.err.println("Error: "+smsg); writeMessage("Error: "+smsg); } } /* ************************************************ * IONDisconnection() * * Purpose: * Called when the connection is broken (can report reason). */ public void IONDisconnection(int i){ System.err.println("Server Connection Closed"); writeMessage("Server Connection Closed"); if(c_bConnected == 1) c_bConnected = 0; } /* *********************************************** * writeMessage() * * Purpose: * Utility method that is used to write a string to the * screen using Java. */ private void writeMessage(String sMsg){ showStatus(sMsg); System.out.println(sMsg); } /* ******************************************* * Mouse Listener Implementation */ public void mousePressed(com.rsi.ion.IONDrawable drawable, int X, int Y, long when, int mask) { c_x1= X; c_y1= Y; if (X < 0) {X = 0;}; if (Y < 0) {Y = 0;}; if (X > c_dimApp.width) {X = c_dimApp.width;}; if (Y > c_dimApp.height) {Y = c_dimApp.height;}; c_xval1 = X*delta_lon/c_dimApp.width+min_lon; c_yval1 = (c_dimApp.height-Y)*delta_lat/c_dimApp.height+min_lat; c_ionDrw.addIONMouseListener(this, com.rsi.ion.IONMouseListener.ION_MOUSE_ANY); return; } public void mouseMoved(com.rsi.ion.IONDrawable drawable, int X, int Y, long when, int mask) { c_x2= X; c_y2= Y; if (X < 0) {X = 0;}; if (Y < 0) {Y = 0;}; if (X > c_dimApp.width) {X = c_dimApp.width;}; if (Y > c_dimApp.height) {Y = c_dimApp.height;}; c_xval2 = X*delta_lon/c_dimApp.width+min_lon; c_yval2 = (c_dimApp.height-Y)*delta_lat/c_dimApp.height+min_lat; doBox(); c_ionDrw.addIONMouseListener(this, com.rsi.ion.IONMouseListener.ION_MOUSE_ANY); return; } public void mouseReleased(com.rsi.ion.IONDrawable drawable, int X, int Y, long when, int mask) { xval1 = new com.rsi.ion.IONVariable(c_xval1); yval1 = new com.rsi.ion.IONVariable(c_yval1); xval2 = new com.rsi.ion.IONVariable(c_xval2); yval2 = new com.rsi.ion.IONVariable(c_yval2); // Order the values for map plotting if (c_xval1 > c_xval2) { minLon = c_xval2; maxLon = c_xval1; } else { minLon = c_xval1; maxLon = c_xval2; } if (c_yval1 > c_yval2) { minLat = c_yval2; maxLat = c_yval1; } else { minLat = c_yval1; maxLat = c_yval2; } // REdefine the map frames min_lat = minLat; max_lat = maxLat; min_lon = minLon; max_lon = maxLon; delta_lon = maxLon - minLon; delta_lat = maxLat - minLat; try{ //PASS BOX VALUES CONVERTED TO DATA SPACE FOR LATER USE: //(We don't actually use them in this example) c_ionCon.setIDLVariable("lon1",xval1); c_ionCon.setIDLVariable("lat1",yval1); c_ionCon.setIDLVariable("lon2",xval2); c_ionCon.setIDLVariable("lat2",yval2); c_ionCon.executeIDLCommand("print,'Latitude: ',STRCOMPRESS(lat1,/REM),' to ',STRCOMPRESS(lat2,/REM),', Longitude: ',STRCOMPRESS(lon1,/REM),' to ',STRCOMPRESS(lon2,/REM),'.'"); } catch(Exception e) { String smsg; if(e instanceof IOException) smsg = "Communication error:"+e.getMessage(); else if(e instanceof com.rsi.ion.IONSecurityException ) smsg = "ION security error"; else if(e instanceof com.rsi.ion.IONIllegalCommandException ) smsg = "Illegal IDL Command detected on server."; else smsg = "Unknown error: "+e.getMessage(); System.err.println("Error: "+smsg); writeMessage("Error: "+smsg); return; } doBox(); try{ // execute the plot_map.pro that plot the map String smsg; writeMessage("After the Zoom"); smsg = "plot_map,"+minLat+","+maxLat+","+minLon+","+maxLon; writeMessage("Command:"+smsg); executeCmd(smsg); //Note that it is generally faster to package multiple //IDL commands into a single .pro to call. This example //sends commands separately so that the code is easier to follow. } catch(Exception e) { String smsg; if(e instanceof IOException) smsg = "Communication error:"+e.getMessage(); else if(e instanceof IONSecurityException ) smsg = "ION Java security error"; else if(e instanceof IONIllegalCommandException ) smsg = "Illegal IDL Command detected on server."; else smsg = "Unknown error: "+e.getMessage(); System.err.println("Error: "+smsg); writeMessage("Error: "+smsg); } writeMessage("Use mouse to draw a rubber band box."); c_ionDrw.addIONMouseListener(this, com.rsi.ion.IONMouseListener.ION_MOUSE_DOWN); return; } /* ******************************************* * Output Listener Implementation */ public void IONOutputText(String message) { writeMessage(message); } /* *********************** * doBox * * Purpose: Draws rubber band box with Java. */ private final void doBox(){ Graphics g = c_ionDrw.getGraphics(); c_ionDrw.update(g); g.setColor(Color.red); g.drawLine(c_x1,c_y1,c_x1,c_y2); g.drawLine(c_x1,c_y2,c_x2,c_y2); g.drawLine(c_x2,c_y2,c_x2,c_y1); g.drawLine(c_x2,c_y1,c_x1,c_y1); } }