Skip to content

Instantly share code, notes, and snippets.

@sfentress
Created July 6, 2010 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfentress/465910 to your computer and use it in GitHub Desktop.
Save sfentress/465910 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.net.*;
import javax.swing.*;
import org.nlogo.lite.InterfaceComponent;
/**
* This is an example which tries to load a model from a URL, using InterfaceComponent.openFromSource().
* This model will fail to work correctly because it references an image that it cannot find.
*
* The model in question
* http://ccl.northwestern.edu/netlogo/models/models/Sample%20Models/Biology/Evolution/Bug%20Hunt%20Camouflage.nlogo
* contains a reference to an image, which is located at
* http://ccl.northwestern.edu/netlogo/models/models/Sample%20Models/Biology/Evolution/poppyfield.jpg
*
* When the model tries to execute its setup() procedure, it fails to find the image and throws the error:
* error while observer running IMPORT-DRAWING in procedure CHANGE-ENVIRONMENT
* called by procedure SETUP
* import-drawing: poppyfield.jpg (No such file or directory)
* (halted running of setup)
*
* If we try to set the context of the model by specifying
* app.setPrefix(new URL("http://ccl.northwestern.edu/netlogo/models/models/Sample%20Models/Biology/Evolution/"));
* Then we get:
* error [etc]
* import-drawing: /Users/sfentress/eclipse/otrunk-nlogo41/http:/ccl.northwestern.edu/netlogo/models/models/Sample%20Models
* /Biology/Evolution/poppyfield.jpg (No such file or directory)
* i.e. it starts from within the context that we are running the application
*
* If, instead of specifying "poppyfield.jpg" as the background image of the model, we change the model so that
* we specify the entire url (http://ccl.northwestern.edu/netlogo/models/models/Sample%20Models/Biology/Evolution/poppyfield.jpg)
* we get:
* error [etc]
* import-drawing: /netlogo/models/models/Sample%20Models/Biology/Evolution/poppyfield.jpg (No such file or directory)
*
*
* @author sfentress
*
*/
public class NetLogoStandalone {
public static void main(String[] argv) {
try
{
final javax.swing.JFrame frame = new javax.swing.JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final InterfaceComponent app = new InterfaceComponent(frame);
java.awt.EventQueue.invokeAndWait
( new Runnable()
{ public void run() {
frame.setSize(1000,800);
frame.add(app);
frame.setVisible(true);
try {
String source = getStringFromUrl(new URL("http://ccl.northwestern.edu/netlogo/models/models/Sample%20Models/Biology/Evolution/Bug%20Hunt%20Camouflage.nlogo"));
// app.setPrefix(new URL("http://ccl.northwestern.edu/netlogo/models/models/Sample%20Models/Biology/Evolution/")); // this does not seem to help
app.openFromSource("Model", null, source);
}
catch(Exception ex) {
ex.printStackTrace();
}
} } ) ;
}
catch(Exception ex) {
ex.printStackTrace();
}
}
/**
* returns a string of the contents of the file at the url
*/
private static String getStringFromUrl(URL url){
try {
InputStream inputStream = url.openStream();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte [] buffer = new byte[1024];
int len;
while((len = inputStream.read(buffer)) > 0) {
bout.write(buffer, 0, len);
}
return bout.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment