Processing – Asynchronous load web image

How to load an image from an URL asynchronously from the web?

When you use the regular loadImage() function, sometimes it ‘hangs’ while fetching an image from the web.
The following class has a timeout parameter, so it will give up after an x-amount of seconds.

Add to your sketch:

// ASYNC IMAGE LOADER
WebImageLoader webImageLoader;

void setup() {
(...)
webImageLoader = new WebImageLoader(this);
(...)
}

The loader class:

/**************************************************************************
 *
 *  ASYNC IMAGE LOADER
 *
 **************************************************************************/
boolean loaderDebug = false;
boolean done;

/**************************************************************************
 *
 *  LOADER CLASS
 *
 **************************************************************************/
class Loader extends Thread {
  PImage p;
  PApplet parent;
  String f;

  /************************************************************************
   *
   *  CONSTRUCTOR
   *
   ************************************************************************/
  Loader(PApplet parent) {
    this.parent = parent;
    done = true;
  } // Loader()


  /************************************************************************
   *
   *  LOAD
   *
   ************************************************************************/
  void load(String file) {
    if (done == true) {
      this.f = file;
      done   = false;
    } // if (this.done == true)
  } // load()


  /************************************************************************
   *
   *  RUN
   *
   ************************************************************************/
  public void run() {
    if (done == false) {
      if (loaderDebug) println("Loader loading: " + this.f);
      try {
        this.p = this.parent.loadImage(this.f);
      } 
      catch(Exception e) {
        e.printStackTrace();
      };

      if (this.p != null) {
        if (loaderDebug) println(this.p.width + " " + this.p.height);
      }
      done = true;
      if (loaderDebug) println("Loader done");
    } // if (this.done == false)
  } // run()
} // Loader


/**************************************************************************
 *
 *  ASYNC LOAD IMAGE CLASS
 *
 **************************************************************************/
public class WebImageLoader {
  PApplet parent;

  /************************************************************************
   *
   *  CONSTRUCTOR
   *
   ************************************************************************/
  WebImageLoader(PApplet _parent) {
    parent = _parent;
  } // WebImageLoader()


  /************************************************************************
   *
   *  LOAD THE IMAGE
   *
   ************************************************************************/
  PImage loadWebImage(String file, int timeout) {
    Loader ldr = new Loader(parent);
    ldr.start();
    ldr.load(file);
    long load = System.currentTimeMillis();

    while (true) {
      if (loaderDebug) println("Checking: "+ done);

      if (done == true) {
        if (loaderDebug) println("Returning image");
        return ldr.p;
      } // if (ldr.done == true)

      if ((System.currentTimeMillis() - load)  > timeout) {
        //if (loaderDebug) println("Timeout");
        return null;
      } // if ((System.currentTimeMillis() - load)  > timeout)

      try {
        parent.delay(50); // 500
      } 
      catch(Exception e) {
      } // try
    } // while (true)
  } // loadWebImage()
} // WebImageLoader

Load an image:

PImage img = webImageLoader.loadWebImage(url, 5000);
with url is the url of the image; 5000 is the timeout in millis

Adapted from:
https://forum.processing.org/one/topic/loadimage-timeout.html


Processing – Validate a date

How to validate a date in Processing?

boolean isValidDate(String _date) {
  try {
    DateFormat df = new SimpleDateFormat("dd-M-yyyy");
    df.setLenient(false);
    df.parse(_date);
    return true;
  } 
  catch (Exception e) {
    return false;
  } // try
} // isValidDate()





Processing – Disable console messages

How to suppress all console messages in a Processing sketch?

Add the following code to your sketch:

// FOR SUPPRESSING CONSOLE MESSAGES
import java.io.PrintStream;
import java.io.OutputStream;

void setup() {
 // Suppress Error Messages (the RED messages)
 System.setErr(new PrintStream(new OutputStream() {
 public void write(int b) {
 }
 }));
 // Suppress Warnings and Regular Messages (the GRAY messages)
 System.setOut(new PrintStream(new OutputStream() {
 public void write(int b) {
 }
 }));
 [... the rest of your code ...]
 } // setup()

Resource:
http://stackoverflow.com/questions/16722462/processing-hide-console-warning

Tested up to: Processing v3.3.3



Processing – Present Mode Background Color

How to change the background color while running in Present Mode?

Add to setup() (NOTE: doesn’t work for Processing 3+!)

((javax.swing.JFrame) frame).getContentPane().setBackground(new java.awt.Color(0, 0, 0));

Alternative method (also Processing 3+!):

  • close Processing!
  • edit <user name>/Appdata/Roaming/Processing/preference.txt
  • make sure the run.present.bgcolor line looks like this:
    run.present.bgcolor=#000000

Processing Android – Playing Sounds

How to play sounds on Android, using Processing Android.

The minim library doesn’t work on Android, so use APWidgets instead.

Example:

import apwidgets.*;
APMediaPlayer player;

void setup()
{
  player = new APMediaPlayer(this);

  // SOUND FILE IS LOCATED IN THE '/data' FOLDER
  player.setMediaFile("sound.mp3");
}

void keyPressed()
{
  if (key == 's') player.start();
}

Resources:
http://forum.processing.org/one/topic/audio-for-android.html
https://code.google.com/p/apwidgets/