diff --git a/android/core/src/processing/core/PApplet.java b/android/core/src/processing/core/PApplet.java index 3bf5fa318..dc0ca2a6a 100644 --- a/android/core/src/processing/core/PApplet.java +++ b/android/core/src/processing/core/PApplet.java @@ -24,34 +24,57 @@ package processing.core; -import android.content.*; -import android.content.res.AssetManager; -import android.graphics.*; - -import java.io.*; -import java.lang.reflect.*; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.Array; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.net.MalformedURLException; import java.net.URL; import java.text.NumberFormat; -import java.util.*; -import java.util.regex.*; -import java.util.zip.*; - +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Random; +import java.util.StringTokenizer; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; import android.app.Activity; +import android.content.Context; +import android.content.res.AssetManager; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.Typeface; import android.opengl.GLSurfaceView; -import android.view.SurfaceView; -import android.view.WindowManager; import android.os.Bundle; -import android.view.*; - +import android.view.KeyEvent; +import android.view.MotionEvent; +import android.view.SurfaceHolder; +import android.view.SurfaceView; +import android.view.Window; +import android.view.WindowManager; public class PApplet extends Activity implements PConstants, Runnable { /** The PGraphics renderer associated with this PApplet */ public PGraphics g; /** The frame containing this applet (if any) */ -// public Frame frame; - + // public Frame frame; /** * The screen size when the applet was started. *
@@ -70,10 +93,9 @@ public class PApplet extends Activity implements PConstants, Runnable { * This variable is not static, because future releases need to be better * at handling multiple displays. */ -// public Dimension screen = -// Toolkit.getDefaultToolkit().getScreenSize(); -// int screenWidth, screenHeight; - + // public Dimension screen = + // Toolkit.getDefaultToolkit().getScreenSize(); + // int screenWidth, screenHeight; /** * Command line options passed in from main(). *
@@ -85,12 +107,10 @@ public class PApplet extends Activity implements PConstants, Runnable { public String sketchPath; //folder; /** When debugging headaches */ -// static final boolean THREAD_DEBUG = false; - + // static final boolean THREAD_DEBUG = false; /** Default width and height for applet when not specified */ -// static public final int DEFAULT_WIDTH = 100; -// static public final int DEFAULT_HEIGHT = 100; - + // static public final int DEFAULT_WIDTH = 100; + // static public final int DEFAULT_HEIGHT = 100; /** * Minimum dimensions for the window holding an applet. * This varies between platforms, Mac OS X 10.3 can do any height @@ -98,9 +118,8 @@ public class PApplet extends Activity implements PConstants, Runnable { * set of limitations. And for all I know, Linux probably lets you * make windows with negative sizes. */ -// static public final int MIN_WINDOW_WIDTH = 128; -// static public final int MIN_WINDOW_HEIGHT = 128; - + // static public final int MIN_WINDOW_WIDTH = 128; + // static public final int MIN_WINDOW_HEIGHT = 128; /** * Exception thrown when size() is called the first time. *
@@ -108,8 +127,7 @@ public class PApplet extends Activity implements PConstants, Runnable { * when the renderer is changed. This is the only way for us to handle * invoking the new renderer while also in the midst of rendering. */ -// static public class RendererChangeException extends RuntimeException { } - + // static public class RendererChangeException extends RuntimeException { } /** * true if no size() command has been executed. This is used to wait until * a size has been set before placing in the window and showing it. @@ -190,12 +208,11 @@ public class PApplet extends Activity implements PConstants, Runnable { */ public boolean firstMotion; -// public int mouseButton; + // public int mouseButton; public boolean mousePressed; -// public MotionEvent motionEvent; - + // public MotionEvent motionEvent; /** * Last key pressed. @@ -222,8 +239,7 @@ public class PApplet extends Activity implements PConstants, Runnable { /** * the last KeyEvent object passed into a mouse function. */ -// public KeyEvent keyEvent; - + // public KeyEvent keyEvent; /** * Gets set to true/false as the applet gains/loses focus. */ @@ -238,8 +254,7 @@ public class PApplet extends Activity implements PConstants, Runnable { * This can be used to test how the applet should behave * since online situations are different (no file writing, etc). */ -// public boolean online = false; - + // public boolean online = false; /** * Time in milliseconds when the applet was started. *
@@ -356,75 +371,68 @@ public class PApplet extends Activity implements PConstants, Runnable { /** true if this sketch is being run by the PDE */ boolean external = false; - - static final String ERROR_MIN_MAX = - "Cannot use min() or max() on an empty array."; - + static final String ERROR_MIN_MAX = "Cannot use min() or max() on an empty array."; ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// + // static final private int BACK_ID = Menu.FIRST; + // static final private int CLEAR_ID = Menu.FIRST + 1; + // private EditText mEditor; -// static final private int BACK_ID = Menu.FIRST; -// static final private int CLEAR_ID = Menu.FIRST + 1; -// private EditText mEditor; - - -// public BagelDroid() { -// } - -// SurfaceView surfaceView; + // public BagelDroid() { + // } + // SurfaceView surfaceView; /** Called with the activity is first created. */ @Override - public void onCreate(Bundle savedInstanceState) { + public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); - Window window = getWindow(); - + final Window window = getWindow(); + // Take up as much area as possible requestWindowFeature(Window.FEATURE_NO_TITLE); window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, - WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); + WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN); // This does the actual full screen work window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, - WindowManager.LayoutParams.FLAG_FULLSCREEN); - + WindowManager.LayoutParams.FLAG_FULLSCREEN); + // window size is -1 at this point, so no sense in using attrs -// final WindowManager.LayoutParams attrs = window.getAttributes(); -// println("window width = " + attrs.width); -// println("window height = " + attrs.height); - + // final WindowManager.LayoutParams attrs = window.getAttributes(); + // println("window width = " + attrs.width); + // println("window height = " + attrs.height); + if (sketchRenderer().equals(A2D)) { surfaceView = new SketchSurfaceView2D(this); - } else if (sketchRenderer().equals(A3D)) { + } else if (sketchRenderer().equals(A3D)) { surfaceView = new SketchSurfaceView3D(this); } - - window.setContentView(surfaceView); // attempt to fix full-screen - + window.setContentView(surfaceView); // attempt to fix full-screen + // code below here formerly from init() - + millisOffset = System.currentTimeMillis(); finished = false; // just for clarity // this will be cleared by draw() if it is not overridden looping = true; - redraw = true; // draw this guy once + redraw = true; // draw this guy once firstMotion = true; - + // TODO is there a better way to set the sketch path? -// try { -// if (sketchPath == null) { -// sketchPath = System.getProperty("user.dir"); -// } -// } catch (Exception e) { } // may be a security problem - Context context = getApplicationContext(); + // try { + // if (sketchPath == null) { + // sketchPath = System.getProperty("user.dir"); + // } + // } catch (Exception e) { } // may be a security problem + final Context context = getApplicationContext(); sketchPath = context.getFilesDir().getAbsolutePath(); /* @@ -455,32 +463,31 @@ public class PApplet extends Activity implements PConstants, Runnable { start(); } - + @Override protected void onResume() { // TODO need to bring back app state here! -// surfaceView.onResume(); + // surfaceView.onResume(); System.out.println("PApplet.onResume() called"); paused = false; - start(); // kick the thread back on + start(); // kick the thread back on resume(); super.onResume(); -// surfaceView.onResume(); + // surfaceView.onResume(); } - + @Override protected void onPause() { // TODO need to save all application state here! -// System.out.println("PApplet.onPause() called"); + // System.out.println("PApplet.onPause() called"); paused = true; - pause(); // handler for others to write -// synchronized (this) { -// paused = true; -//} + pause(); // handler for others to write + // synchronized (this) { + // paused = true; + //} super.onPause(); -// surfaceView.onPause(); + // surfaceView.onPause(); } - /** * Developers can override here to save state. The 'paused' variable will be * set before this function is called. @@ -488,7 +495,6 @@ public class PApplet extends Activity implements PConstants, Runnable { public void pause() { } - /** * Developers can override here to restore state. The 'paused' variable * will be cleared before this function is called. @@ -496,84 +502,80 @@ public class PApplet extends Activity implements PConstants, Runnable { public void resume() { } - + @Override public void onDestroy() { System.out.println("PApplet.onDestroy() called"); super.onDestroy(); } - - ////////////////////////////////////////////////////////////// // ANDROID SURFACE VIEW - SurfaceView surfaceView; SurfaceHolder surfaceHolder; - public SurfaceHolder getSurfaceHolder() { //return surfaceView.getHolder(); return surfaceHolder; } -// public class SketchSurfaceView { -// -// SketchSurfaceView(Context context) { -// if (sketchRenderer().equals(A2D)) { -// surfaceA2D = new SketchSurfaceView2D(context); -// surfaceA3D = null; -// } -// else if (sketchRenderer().equals(A3D)) { -// surfaceA2D = null; -// surfaceA3D = new SketchSurfaceView3D(context); -// } -// else { -// // Should throw exception here, since the renderer in unknown. -// } -// } -// -// void requestDraw() { -// if (surfaceA2D != null) handleDraw(); -// else surfaceA3D.requestRender(); -// } -// -// void onPause() { -// if (surfaceA2D != null) surfaceA2D.onPause(); -// else surfaceA3D.onPause(); -// } -// -// void onResume() { -// if (surfaceA2D != null) surfaceA2D.onResume(); -// else surfaceA3D.onResume(); -// } -// -// SurfaceView getSurface() { -// if (surfaceA2D != null) return surfaceA2D; -// else return surfaceA3D; -// } -// -// SketchSurfaceView2D surfaceA2D; -// SketchSurfaceView3D surfaceA3D; -// } + // public class SketchSurfaceView { + // + // SketchSurfaceView(Context context) { + // if (sketchRenderer().equals(A2D)) { + // surfaceA2D = new SketchSurfaceView2D(context); + // surfaceA3D = null; + // } + // else if (sketchRenderer().equals(A3D)) { + // surfaceA2D = null; + // surfaceA3D = new SketchSurfaceView3D(context); + // } + // else { + // // Should throw exception here, since the renderer in unknown. + // } + // } + // + // void requestDraw() { + // if (surfaceA2D != null) handleDraw(); + // else surfaceA3D.requestRender(); + // } + // + // void onPause() { + // if (surfaceA2D != null) surfaceA2D.onPause(); + // else surfaceA3D.onPause(); + // } + // + // void onResume() { + // if (surfaceA2D != null) surfaceA2D.onResume(); + // else surfaceA3D.onResume(); + // } + // + // SurfaceView getSurface() { + // if (surfaceA2D != null) return surfaceA2D; + // else return surfaceA3D; + // } + // + // SketchSurfaceView2D surfaceA2D; + // SketchSurfaceView3D surfaceA3D; + // } public class SketchSurfaceView3D extends GLSurfaceView { - public SketchSurfaceView3D(Context context) { + public SketchSurfaceView3D(final Context context) { super(context); surfaceHolder = getHolder(); // are these two needed? surfaceHolder.addCallback(this); //surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU); -// System.out.println("Creating PGraphicsAndroid3D " + width + " " + height); + // System.out.println("Creating PGraphicsAndroid3D " + width + " " + height); // The PGraphics object needs to be created here so the renderer is not // null. This is required because PApplet.onResume events (which call // this.onResume() and thus require a valid renderer) are triggered // before surfaceChanged() is ever called. - PGraphics newGraphics = new PGraphicsAndroid3D(); + final PGraphics newGraphics = new PGraphicsAndroid3D(); // Set arbitrary size; will be set properly when surfaceChanged() called newGraphics.setSize(100, 100); newGraphics.setParent(PApplet.this); @@ -581,40 +583,40 @@ public class PApplet extends Activity implements PConstants, Runnable { g = newGraphics; // The renderer can be set only once. - setRenderer(((PGraphicsAndroid3D)g).getRenderer()); + setRenderer(((PGraphicsAndroid3D) g).getRenderer()); setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); - + setFocusable(true); setFocusableInTouchMode(true); requestFocus(); } - // part of SurfaceHolder.Callback - public void surfaceCreated(SurfaceHolder holder) { + @Override + public void surfaceCreated(final SurfaceHolder holder) { super.surfaceCreated(holder); System.out.println("surfaceCreated()"); } - // part of SurfaceHolder.Callback - public void surfaceDestroyed(SurfaceHolder holder) { + @Override + public void surfaceDestroyed(final SurfaceHolder holder) { super.surfaceDestroyed(holder); System.out.println("surfaceDestroyed()"); // I don't think we need this: //g.dispose(); } - @Override - public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { + public void surfaceChanged(final SurfaceHolder holder, final int format, + final int w, final int h) { super.surfaceChanged(holder, format, w, h); //System.out.println("surfaceChanged() " + w + " " + h); width = w; height = h; - + g.setSize(w, h); // No need to call g.setSize(width, height) b/c super.surfaceChanged() @@ -622,34 +624,36 @@ public class PApplet extends Activity implements PConstants, Runnable { // -- apparently not true? (100110) } - /** * Inform the view that the window focus has changed. */ @Override - public void onWindowFocusChanged(boolean hasFocus) { + public void onWindowFocusChanged(final boolean hasFocus) { super.onWindowFocusChanged(hasFocus); focused = hasFocus; if (focused) { -// println("got focus"); + // println("got focus"); focusGained(); } else { -// println("lost focus"); + // println("lost focus"); focusLost(); } } - public boolean onTouchEvent(MotionEvent event) { + @Override + public boolean onTouchEvent(final MotionEvent event) { checkMotionEvent(event); return true; } - public boolean onKeyDown(int code, KeyEvent event) { + @Override + public boolean onKeyDown(final int code, final KeyEvent event) { checkKeyEvent(event); return super.onKeyDown(code, event); } - public boolean onKeyUp(int code, KeyEvent event) { + @Override + public boolean onKeyUp(final int code, final KeyEvent event) { checkKeyEvent(event); return super.onKeyDown(code, event); } @@ -661,80 +665,73 @@ public class PApplet extends Activity implements PConstants, Runnable { } } - - public class SketchSurfaceView2D extends SurfaceView implements SurfaceHolder.Callback { + public class SketchSurfaceView2D extends SurfaceView implements + SurfaceHolder.Callback { - public SketchSurfaceView2D(Context context) { + public SketchSurfaceView2D(final Context context) { super(context); - + println("surface holder"); // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed surfaceHolder = getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU); - + println("creating graphics"); - PGraphics newGraphics = new PGraphicsAndroid2D(); + final PGraphics newGraphics = new PGraphicsAndroid2D(); // Set arbitrary size; will be set properly when surfaceChanged() called newGraphics.setSize(100, 100); -// newGraphics.setSize(getWidth(), getHeight()); + // newGraphics.setSize(getWidth(), getHeight()); newGraphics.setParent(PApplet.this); newGraphics.setPrimary(true); // Set the value for 'g' once everything is ready (otherwise rendering // may attempt before setSize(), setParent() etc) g = newGraphics; - + println("setting focusable, requesting focus"); setFocusable(true); setFocusableInTouchMode(true); requestFocus(); - + println("done making surface view"); } - // part of SurfaceHolder.Callback - public void surfaceCreated(SurfaceHolder holder) { + public void surfaceCreated(final SurfaceHolder holder) { } - // part of SurfaceHolder.Callback - public void surfaceDestroyed(SurfaceHolder holder) { + public void surfaceDestroyed(final SurfaceHolder holder) { g.dispose(); } - // part of SurfaceHolder.Callback - public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { + public void surfaceChanged(final SurfaceHolder holder, final int format, + final int w, final int h) { System.out.println("surfaceChanged() " + w + " " + h); width = w; height = h; - + g.setSize(w, h); } - /** * Inform the view that the activity is paused. */ -// public void onPause() { -// } - - + // public void onPause() { + // } /** * Inform the view that the activity is resumed. */ -// public void onResume() { -// } - - + // public void onResume() { + // } /** * Inform the view that the window focus has changed. */ @Override - public void onWindowFocusChanged(boolean hasFocus) { + public void onWindowFocusChanged(final boolean hasFocus) { super.onWindowFocusChanged(hasFocus); focused = hasFocus; if (focused) { @@ -744,32 +741,31 @@ public class PApplet extends Activity implements PConstants, Runnable { } } - /** * If you override this function without calling super.onTouchEvent(), * then motionX, motionY, motionPressed, and motionEvent will not be set. */ - public boolean onTouchEvent(MotionEvent event) { + @Override + public boolean onTouchEvent(final MotionEvent event) { checkMotionEvent(event); //return super.onTouchEvent(event); return true; } - - public boolean onKeyDown(int code, KeyEvent event) { -// System.out.println("got onKeyDown for " + code + " " + event); + @Override + public boolean onKeyDown(final int code, final KeyEvent event) { + // System.out.println("got onKeyDown for " + code + " " + event); checkKeyEvent(event); return super.onKeyDown(code, event); } - - public boolean onKeyUp(int code, KeyEvent event) { -// System.out.println("got onKeyUp for " + code + " " + event); + @Override + public boolean onKeyUp(final int code, final KeyEvent event) { + // System.out.println("got onKeyUp for " + code + " " + event); checkKeyEvent(event); return super.onKeyDown(code, event); } - @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); @@ -777,13 +773,10 @@ public class PApplet extends Activity implements PConstants, Runnable { } } - - ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////// - /* public int sketchWidth() { return DEFAULT_WIDTH; @@ -795,12 +788,10 @@ public class PApplet extends Activity implements PConstants, Runnable { } */ - public String sketchRenderer() { return A2D; } - /** * Called by the browser or applet viewer to inform this applet that it * should start its execution. It is called after the init method and @@ -821,7 +812,6 @@ public class PApplet extends Activity implements PConstants, Runnable { } } - /** * Called by the browser or applet viewer to inform * this applet that it should stop its execution. @@ -832,17 +822,20 @@ public class PApplet extends Activity implements PConstants, Runnable { */ public void stop() { // bringing this back for 0111, hoping it'll help opengl shutdown - finished = true; // why did i comment this out? + finished = true; // why did i comment this out? // don't run stop and disposers twice - if (thread == null) return; + if (thread == null) { + return; + } thread = null; // call to shut down renderer, in case it needs it (pdf does) - if (g != null) g.dispose(); + if (g != null) { + g.dispose(); + } } - /** * Called by the browser or applet viewer to inform this applet * that it is being reclaimed and that it should destroy @@ -858,47 +851,37 @@ public class PApplet extends Activity implements PConstants, Runnable { * when moving between pages), though. */ public void destroy() { - ((PApplet)this).stop(); + (this).stop(); } - /** * This returns the last width and height specified by the user * via the size() command. */ -// public Dimension getPreferredSize() { -// return new Dimension(width, height); -// } - - -// public void addNotify() { -// super.addNotify(); -// println("addNotify()"); -// } - - + // public Dimension getPreferredSize() { + // return new Dimension(width, height); + // } + // public void addNotify() { + // super.addNotify(); + // println("addNotify()"); + // } ////////////////////////////////////////////////////////////// - - public void setup() { } - public void draw() { // if no draw method, then shut things down //System.out.println("no draw method, goodbye"); finished = true; } - ////////////////////////////////////////////////////////////// - /* protected void resizeRenderer(int iwidth, int iheight) { -// println("resizeRenderer request for " + iwidth + " " + iheight); + // println("resizeRenderer request for " + iwidth + " " + iheight); if (width != iwidth || height != iheight) { -// println(" former size was " + width + " " + height); + // println(" former size was " + width + " " + height); g.setSize(iwidth, iheight); width = iwidth; height = iheight; @@ -906,7 +889,6 @@ public class PApplet extends Activity implements PConstants, Runnable { } */ - /** * Starts up and creates a two-dimensional drawing surface, * or resizes the current drawing surface. @@ -922,16 +904,14 @@ public class PApplet extends Activity implements PConstants, Runnable { * If called once a renderer has already been set, this will * use the previous renderer and simply resize it. */ - public void size(int iwidth, int iheight) { + public void size(final int iwidth, final int iheight) { //size(iwidth, iheight, A2D, null); } - - public void size(int iwidth, int iheight, String irenderer) { + public void size(final int iwidth, final int iheight, final String irenderer) { //size(iwidth, iheight, irenderer, null); } - /** * Creates a new PGraphics object and sets it to the specified size. * @@ -974,7 +954,7 @@ public class PApplet extends Activity implements PConstants, Runnable { height = iheight; // fire resize event to make sure the applet is the proper size -// setSize(iwidth, iheight); + // setSize(iwidth, iheight); // this is the function that will run if the user does their own // size() command inside setup, so set defaultSize to false. defaultSize = false; @@ -988,7 +968,6 @@ public class PApplet extends Activity implements PConstants, Runnable { } */ - /** * Create an offscreen PGraphics object for drawing. This can be used * for bitmap or vector images drawing or rendering. @@ -1039,30 +1018,28 @@ public class PApplet extends Activity implements PConstants, Runnable { * PImage.save(). * */ - public PGraphics createGraphics(int iwidth, int iheight, - String irenderer) { - PGraphics pg = makeGraphics(iwidth, iheight, irenderer, null, false); + public PGraphics createGraphics(final int iwidth, final int iheight, + final String irenderer) { + final PGraphics pg = makeGraphics(iwidth, iheight, irenderer, null, false); //pg.parent = this; // make save() work return pg; } - /** * Create an offscreen graphics surface for drawing, in this case * for a renderer that writes to a file (such as PDF or DXF). * @param ipath can be an absolute or relative path */ - public PGraphics createGraphics(int iwidth, int iheight, - String irenderer, String ipath) { + public PGraphics createGraphics(final int iwidth, final int iheight, + final String irenderer, String ipath) { if (ipath != null) { ipath = savePath(ipath); } - PGraphics pg = makeGraphics(iwidth, iheight, irenderer, ipath, false); - pg.parent = this; // make save() work + final PGraphics pg = makeGraphics(iwidth, iheight, irenderer, ipath, false); + pg.parent = this; // make save() work return pg; } - /** * Version of createGraphics() used internally. * @@ -1070,58 +1047,57 @@ public class PApplet extends Activity implements PConstants, Runnable { * @oaram applet the parent applet object, this should only be non-null * in cases where this is the main drawing surface object. */ - protected PGraphics makeGraphics(int iwidth, int iheight, - String irenderer, String ipath, - boolean iprimary) { + protected PGraphics makeGraphics(final int iwidth, final int iheight, + final String irenderer, final String ipath, + final boolean iprimary) { try { - Class> rendererClass = - Thread.currentThread().getContextClassLoader().loadClass(irenderer); + final Class> rendererClass = Thread.currentThread() + .getContextClassLoader().loadClass(irenderer); - Constructor> constructor = rendererClass.getConstructor(new Class[] { }); - PGraphics pg = (PGraphics) constructor.newInstance(); + final Constructor> constructor = rendererClass + .getConstructor(new Class[] {}); + final PGraphics pg = (PGraphics) constructor.newInstance(); pg.setParent(this); pg.setPrimary(iprimary); - if (ipath != null) pg.setPath(ipath); + if (ipath != null) { + pg.setPath(ipath); + } pg.setSize(iwidth, iheight); // everything worked, return it return pg; - } catch (InvocationTargetException ite) { + } catch (final InvocationTargetException ite) { ite.getTargetException().printStackTrace(); - Throwable target = ite.getTargetException(); + final Throwable target = ite.getTargetException(); throw new RuntimeException(target.getMessage()); - } catch (ClassNotFoundException cnfe) { - throw new RuntimeException("You need to use \"Import Library\" " + - "to add " + irenderer + " to your sketch."); - } catch (Exception e) { + } catch (final ClassNotFoundException cnfe) { + throw new RuntimeException("You need to use \"Import Library\" " + + "to add " + irenderer + " to your sketch."); + } catch (final Exception e) { throw new RuntimeException(e.getMessage()); } } - /** * Preferred method of creating new PImage objects, ensures that a * reference to the parent PApplet is included, which makes save() work * without needing an absolute path. */ - public PImage createImage(int wide, int high, int format) { - PImage image = new PImage(wide, high, format); - image.parent = this; // make save() work + public PImage createImage(final int wide, final int high, final int format) { + final PImage image = new PImage(wide, high, format); + image.parent = this; // make save() work return image; } - // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . - // not necessary, ja? -// public void update(Graphics screen) { -// paint(screen); -// } - + // public void update(Graphics screen) { + // paint(screen); + // } /* //synchronized public void paint(Graphics screen) { // shutting off for 0146 @@ -1129,7 +1105,7 @@ public class PApplet extends Activity implements PConstants, Runnable { // ignore the very first call to paint, since it's coming // from the o.s., and the applet will soon update itself anyway. if (frameCount == 0) { -// println("Skipping frame"); + // println("Skipping frame"); // paint() may be called more than once before things // are finally painted to the screen and the thread gets going return; @@ -1148,13 +1124,12 @@ public class PApplet extends Activity implements PConstants, Runnable { // make sure the screen is visible and usable // (also prevents over-drawing when using PGraphicsOpenGL) if ((g != null) && (g.image != null)) { -// println("inside paint(), screen.drawImage()"); + // println("inside paint(), screen.drawImage()"); screen.drawImage(g.image, 0, 0, null); } } */ - // active paint method /* protected void paint() { @@ -1178,16 +1153,13 @@ public class PApplet extends Activity implements PConstants, Runnable { } */ - ////////////////////////////////////////////////////////////// - - /** * Main method for the primary animation thread. * * Painting in AWT and Swing */ - public void run() { // not good to make this synchronized, locks things up + public void run() { // not good to make this synchronized, locks things up long beforeTime = System.nanoTime(); long overSleepTime = 0L; @@ -1199,52 +1171,55 @@ public class PApplet extends Activity implements PConstants, Runnable { while ((Thread.currentThread() == thread) && !finished) { // Don't resize the renderer from the EDT (i.e. from a ComponentEvent), // otherwise it may attempt a resize mid-render. -// if (resizeRequest) { -// resizeRenderer(resizeWidth, resizeHeight); -// resizeRequest = false; -// } + // if (resizeRequest) { + // resizeRenderer(resizeWidth, resizeHeight); + // resizeRequest = false; + // } // render a single frame - if (g != null) g.requestDraw(); -// g.requestDraw(); -// surfaceView.requestDraw(); + if (g != null) { + g.requestDraw(); + // g.requestDraw(); + // surfaceView.requestDraw(); + } // removed in android -// if (frameCount == 1) { -// // Call the request focus event once the image is sure to be on -// // screen and the component is valid. The OpenGL renderer will -// // request focus for its canvas inside beginDraw(). -// // http://java.sun.com/j2se/1.4.2/docs/api/java/awt/doc-files/FocusSpec.html -// //println("requesting focus"); -// requestFocus(); -// } + // if (frameCount == 1) { + // // Call the request focus event once the image is sure to be on + // // screen and the component is valid. The OpenGL renderer will + // // request focus for its canvas inside beginDraw(). + // // http://java.sun.com/j2se/1.4.2/docs/api/java/awt/doc-files/FocusSpec.html + // //println("requesting focus"); + // requestFocus(); + // } // wait for update & paint to happen before drawing next frame // this is necessary since the drawing is sometimes in a // separate thread, meaning that the next frame will start // before the update/paint is completed - long afterTime = System.nanoTime(); - long timeDiff = afterTime - beforeTime; + final long afterTime = System.nanoTime(); + final long timeDiff = afterTime - beforeTime; //System.out.println("time diff is " + timeDiff); - long sleepTime = (frameRatePeriod - timeDiff) - overSleepTime; + final long sleepTime = (frameRatePeriod - timeDiff) - overSleepTime; - if (sleepTime > 0) { // some time left in this cycle + if (sleepTime > 0) { // some time left in this cycle try { -// Thread.sleep(sleepTime / 1000000L); // nanoseconds -> milliseconds + // Thread.sleep(sleepTime / 1000000L); // nanoseconds -> milliseconds Thread.sleep(sleepTime / 1000000L, (int) (sleepTime % 1000000L)); - noDelays = 0; // Got some sleep, not delaying anymore - } catch (InterruptedException ex) { } + noDelays = 0; // Got some sleep, not delaying anymore + } catch (final InterruptedException ex) { + } overSleepTime = (System.nanoTime() - afterTime) - sleepTime; //System.out.println(" oversleep is " + overSleepTime); - } else { // sleepTime <= 0; the frame took longer than the period -// excess -= sleepTime; // store excess time value + } else { // sleepTime <= 0; the frame took longer than the period + // excess -= sleepTime; // store excess time value overSleepTime = 0L; if (noDelays > NO_DELAYS_PER_YIELD) { - Thread.yield(); // give another thread a chance to run + Thread.yield(); // give another thread a chance to run noDelays = 0; } } @@ -1254,7 +1229,7 @@ public class PApplet extends Activity implements PConstants, Runnable { // if this isn't just a pause, shut it all down if (!paused) { - stop(); // call to shutdown libs? + stop(); // call to shutdown libs? // If the user called the exit() function, the window should close, // rather than the sketch just halting. @@ -1264,7 +1239,6 @@ public class PApplet extends Activity implements PConstants, Runnable { } } - public void handleDraw() { if (g != null && !paused && (looping || redraw)) { if (!g.canDraw()) { @@ -1275,24 +1249,24 @@ public class PApplet extends Activity implements PConstants, Runnable { g.beginDraw(); - long now = System.nanoTime(); + final long now = System.nanoTime(); if (frameCount == 0) { -// try { - //println("Calling setup()"); + // try { + //println("Calling setup()"); setup(); - //println("Done with setup()"); + //println("Done with setup()"); -// } catch (RendererChangeException e) { -// // Give up, instead set the new renderer and re-attempt setup() -// return; -// } + // } catch (RendererChangeException e) { + // // Give up, instead set the new renderer and re-attempt setup() + // return; + // } this.defaultSize = false; - } else { // frameCount > 0, meaning an actual draw() + } else { // frameCount > 0, meaning an actual draw() // update the current frameRate - double rate = 1000000.0 / ((now - frameRateLastNanos) / 1000000.0); - float instantaneousRate = (float) rate / 1000.0f; + final double rate = 1000000.0 / ((now - frameRateLastNanos) / 1000000.0); + final float instantaneousRate = (float) rate / 1000.0f; frameRate = (frameRate * 0.9f) + (instantaneousRate * 0.1f); // use dmouseX/Y as previous mouse pos, since this is the @@ -1319,7 +1293,7 @@ public class PApplet extends Activity implements PConstants, Runnable { dequeueMotionEvents(); dequeueKeyEvents(); - redraw = false; // unset 'redraw' flag in case it was set + redraw = false; // unset 'redraw' flag in case it was set // (only do this once draw() has run, not just setup()) } @@ -1331,73 +1305,64 @@ public class PApplet extends Activity implements PConstants, Runnable { } } - ////////////////////////////////////////////////////////////// - - synchronized public void redraw() { if (!looping) { redraw = true; -// if (thread != null) { -// // wake from sleep (necessary otherwise it'll be -// // up to 10 seconds before update) -// if (CRUSTY_THREADS) { -// thread.interrupt(); -// } else { -// synchronized (blocker) { -// blocker.notifyAll(); -// } -// } -// } + // if (thread != null) { + // // wake from sleep (necessary otherwise it'll be + // // up to 10 seconds before update) + // if (CRUSTY_THREADS) { + // thread.interrupt(); + // } else { + // synchronized (blocker) { + // blocker.notifyAll(); + // } + // } + // } } } - synchronized public void loop() { if (!looping) { looping = true; } } - synchronized public void noLoop() { if (looping) { looping = false; } } - ////////////////////////////////////////////////////////////// - // all these are handled in SurfaceView, which is a listener for all of em -// public void addListeners() { -// addMouseListener(this); -// addMouseMotionListener(this); -// addKeyListener(this); -// addFocusListener(this); -// -// addComponentListener(new ComponentAdapter() { -// public void componentResized(ComponentEvent e) { -// Component c = e.getComponent(); -// //System.out.println("componentResized() " + c); -// Rectangle bounds = c.getBounds(); -// resizeRequest = true; -// resizeWidth = bounds.width; -// resizeHeight = bounds.height; -// } -// }); -// } - + // public void addListeners() { + // addMouseListener(this); + // addMouseMotionListener(this); + // addKeyListener(this); + // addFocusListener(this); + // + // addComponentListener(new ComponentAdapter() { + // public void componentResized(ComponentEvent e) { + // Component c = e.getComponent(); + // //System.out.println("componentResized() " + c); + // Rectangle bounds = c.getBounds(); + // resizeRequest = true; + // resizeWidth = bounds.width; + // resizeHeight = bounds.height; + // } + // }); + // } ////////////////////////////////////////////////////////////// - class PMotionEvent { - int action; - float motionX, motionY; - float motionPressure; + int action; + float motionX, motionY; + float motionPressure; int mouseX, mouseY; } @@ -1405,7 +1370,7 @@ public class PApplet extends Activity implements PConstants, Runnable { PMotionEvent[] motionEventQueue; int motionEventCount; - protected void enqueueMotionEvent(MotionEvent event) { + protected void enqueueMotionEvent(final MotionEvent event) { synchronized (motionLock) { // on first run, allocate array for motion events if (motionEventQueue == null) { @@ -1416,31 +1381,31 @@ public class PApplet extends Activity implements PConstants, Runnable { } // allocate more PMotionEvent objects if we're out if (motionEventCount + event.getHistorySize() >= motionEventQueue.length) { - PMotionEvent[] temp = new PMotionEvent[motionEventCount << 1]; + final PMotionEvent[] temp = new PMotionEvent[motionEventCount << 1]; System.arraycopy(motionEventQueue, 0, temp, 0, motionEventCount); motionEventQueue = temp; for (int i = motionEventCount; i < motionEventCount << 1; i++) { motionEventQueue[i] = new PMotionEvent(); } } - int historyCount = event.getHistorySize(); + final int historyCount = event.getHistorySize(); // this will be the last event in the list - PMotionEvent pme = motionEventQueue[motionEventCount + historyCount]; + final PMotionEvent pme = motionEventQueue[motionEventCount + historyCount]; pme.action = event.getAction(); pme.motionX = event.getX(); pme.motionY = event.getY(); - pme.motionPressure = event.getPressure(); // should this be constrained? + pme.motionPressure = event.getPressure(); // should this be constrained? pme.mouseX = (int) event.getRawX(); pme.mouseY = (int) event.getRawY(); // historical events happen before the 'current' values if (pme.action == MotionEvent.ACTION_MOVE && historyCount > 0) { - float rawOffsetX = pme.mouseX - pme.motionX; - float rawOffsetY = pme.mouseY - pme.motionY; + final float rawOffsetX = pme.mouseX - pme.motionX; + final float rawOffsetY = pme.mouseY - pme.motionY; for (int i = 0; i < historyCount; i++) { - PMotionEvent hist = motionEventQueue[motionEventCount++]; + final PMotionEvent hist = motionEventQueue[motionEventCount++]; hist.action = pme.action; hist.motionX = event.getHistoricalX(i); hist.motionY = event.getHistoricalY(i); @@ -1465,7 +1430,6 @@ public class PApplet extends Activity implements PConstants, Runnable { } } - /** * Take action based on a motion event. * Internally updates mouseX, mouseY, mousePressed, and mouseEvent. @@ -1473,7 +1437,7 @@ public class PApplet extends Activity implements PConstants, Runnable { * i.e. mousePressed() or mouseReleased() that the user may have * overloaded to do something more useful. */ - protected void handleMotionEvent(PMotionEvent pme) { + protected void handleMotionEvent(final PMotionEvent pme) { pmotionX = emotionX; pmotionY = emotionY; motionX = pme.motionX; @@ -1491,7 +1455,7 @@ public class PApplet extends Activity implements PConstants, Runnable { if (firstMotion) { pmouseX = mouseX; pmouseY = mouseY; - dmouseX = mouseX; // set it as the first value to be used inside draw() too + dmouseX = mouseX; // set it as the first value to be used inside draw() too dmouseY = mouseY; pmotionX = motionX; @@ -1528,13 +1492,12 @@ public class PApplet extends Activity implements PConstants, Runnable { } } - /** * Figure out how to process a mouse event. When loop() has been * called, the events will be queued up until drawing is complete. * If noLoop() has been called, then events will happen immediately. */ - protected void checkMotionEvent(MotionEvent event) { + protected void checkMotionEvent(final MotionEvent event) { enqueueMotionEvent(event); // if not looping, then remove from the queue immediately @@ -1545,26 +1508,26 @@ public class PApplet extends Activity implements PConstants, Runnable { } } + public void mousePressed() { + } - public void mousePressed() { } + public void mouseReleased() { + } - public void mouseReleased() { } - -// public void mouseClicked() { } - - public void mouseDragged() { } - - public void mouseMoved() { } + // public void mouseClicked() { } + public void mouseDragged() { + } + public void mouseMoved() { + } ////////////////////////////////////////////////////////////// - KeyEvent[] keyEventQueue = new KeyEvent[10]; int keyEventCount; - protected void enqueueKeyEvent(KeyEvent e) { + protected void enqueueKeyEvent(final KeyEvent e) { synchronized (keyEventQueue) { if (keyEventCount == keyEventQueue.length) { keyEventQueue = (KeyEvent[]) expand(keyEventQueue); @@ -1582,22 +1545,21 @@ public class PApplet extends Activity implements PConstants, Runnable { } } - - protected void handleKeyEvent(KeyEvent event) { + protected void handleKeyEvent(final KeyEvent event) { // event.isPrintingKey() returns false for whitespace and others, // which is a problem if the space bar or tab key are used. key = (char) event.getUnicodeChar(); // if not mappable to a unicode character, instead mark as coded key if (key == 0) { -// System.out.println(" key is coded"); + // System.out.println(" key is coded"); key = CODED; } else { -// System.out.println(" key is unicode"); + // System.out.println(" key is unicode"); } keyCode = event.getKeyCode(); - int action = event.getAction(); + final int action = event.getAction(); if (action == KeyEvent.ACTION_DOWN) { keyPressed = true; keyPressed(); @@ -1608,24 +1570,23 @@ public class PApplet extends Activity implements PConstants, Runnable { // if someone else wants to intercept the key, they should // set key to zero (or something besides the ESC). -// if (event.getID() == KeyEvent.KEY_PRESSED) { -// if (key == KeyEvent.VK_ESCAPE) { -// exit(); -// } - // When running tethered to the Processing application, respond to - // Ctrl-W (or Cmd-W) events by closing the sketch. Disable this behavior - // when running independently, because this sketch may be one component - // embedded inside an application that has its own close behavior. -// if (external && -// event.getModifiers() == MENU_SHORTCUT && -// event.getKeyCode() == 'W') { -// exit(); -// } -// } + // if (event.getID() == KeyEvent.KEY_PRESSED) { + // if (key == KeyEvent.VK_ESCAPE) { + // exit(); + // } + // When running tethered to the Processing application, respond to + // Ctrl-W (or Cmd-W) events by closing the sketch. Disable this behavior + // when running independently, because this sketch may be one component + // embedded inside an application that has its own close behavior. + // if (external && + // event.getModifiers() == MENU_SHORTCUT && + // event.getKeyCode() == 'W') { + // exit(); + // } + // } } - - protected void checkKeyEvent(KeyEvent event) { + protected void checkKeyEvent(final KeyEvent event) { if (looping) { enqueueKeyEvent(event); } else { @@ -1633,7 +1594,6 @@ public class PApplet extends Activity implements PConstants, Runnable { } } - /** * Overriding keyXxxxx(KeyEvent e) functions will cause the 'key', * 'keyCode', and 'keyEvent' variables to no longer work; @@ -1641,11 +1601,9 @@ public class PApplet extends Activity implements PConstants, Runnable { * and the keyPressed(), keyReleased() and keyTyped() methods * will no longer be called. */ -// public void keyPressed(KeyEvent e) { checkKeyEvent(e); } -// public void keyReleased(KeyEvent e) { checkKeyEvent(e); } -// public void keyTyped(KeyEvent e) { checkKeyEvent(e); } - - + // public void keyPressed(KeyEvent e) { checkKeyEvent(e); } + // public void keyReleased(KeyEvent e) { checkKeyEvent(e); } + // public void keyTyped(KeyEvent e) { checkKeyEvent(e); } /** * Called each time a single key on the keyboard is pressed. * Because of how operating systems handle key repeats, holding @@ -1693,46 +1651,44 @@ public class PApplet extends Activity implements PConstants, Runnable { * Not tested on other platforms or for 1.3. * */ - public void keyPressed() { } - + public void keyPressed() { + } /** * See keyPressed(). */ - public void keyReleased() { } - + public void keyReleased() { + } /** * Only called for "regular" keys like letters, * see keyPressed() for full documentation. */ - public void keyTyped() { } - + public void keyTyped() { + } ////////////////////////////////////////////////////////////// + public void focusGained() { + } - public void focusGained() { } + // public void focusGained(FocusEvent e) { + // focused = true; + // focusGained(); + // } -// public void focusGained(FocusEvent e) { -// focused = true; -// focusGained(); -// } - - - public void focusLost() { } - -// public void focusLost(FocusEvent e) { -// focused = false; -// focusLost(); -// } + public void focusLost() { + } + // public void focusLost(FocusEvent e) { + // focused = false; + // focusLost(); + // } ////////////////////////////////////////////////////////////// // getting the time - /** * Get the number of milliseconds since the applet started. *
@@ -1789,12 +1745,10 @@ public class PApplet extends Activity implements PConstants, Runnable { return Calendar.getInstance().get(Calendar.YEAR); } - ////////////////////////////////////////////////////////////// // controlling time (playing god) - /** * The delay() function causes the program to halt for a specified time. * Delay times are specified in thousandths of a second. For example, @@ -1808,17 +1762,17 @@ public class PApplet extends Activity implements PConstants, Runnable { * updated before or after the delay, meaning which means it just * makes the app lock up temporarily. */ - public void delay(int napTime) { + public void delay(final int napTime) { if (frameCount != 0) { if (napTime > 0) { try { Thread.sleep(napTime); - } catch (InterruptedException e) { } + } catch (final InterruptedException e) { + } } } } - /** * Set a target frameRate. This will cause delay() to be called * after each frame so that the sketch synchronizes to a particular speed. @@ -1827,50 +1781,43 @@ public class PApplet extends Activity implements PConstants, Runnable { * setting, and will attempt to use maximum processor power to achieve * maximum speed. */ - public void frameRate(float newRateTarget) { + public void frameRate(final float newRateTarget) { frameRateTarget = newRateTarget; frameRatePeriod = (long) (1000000000.0 / frameRateTarget); } - ////////////////////////////////////////////////////////////// - /** * Get a param from the web page, or (eventually) * from a properties file. */ -// public String param(String what) { -// if (online) { -// return getParameter(what); -// -// } else { -// System.err.println("param() only works inside a web browser"); -// } -// return null; -// } - - + // public String param(String what) { + // if (online) { + // return getParameter(what); + // + // } else { + // System.err.println("param() only works inside a web browser"); + // } + // return null; + // } /** * Show status in the status bar of a web browser, or in the * System.out console. Eventually this might show status in the * p5 environment itself, rather than relying on the console. */ -// public void status(String what) { -// if (online) { -// showStatus(what); -// -// } else { -// System.out.println(what); // something more interesting? -// } -// } - - - public void link(String here) { + // public void status(String what) { + // if (online) { + // showStatus(what); + // + // } else { + // System.out.println(what); // something more interesting? + // } + // } + public void link(final String here) { link(here, null); } - /** * Link to an external page without all the muss. *
@@ -1882,61 +1829,56 @@ public class PApplet extends Activity implements PConstants, Runnable { * or whatever you want as your browser, since Linux doesn't * yet have a standard method for launching URLs. */ - public void link(String url, String frameTitle) { + public void link(final String url, final String frameTitle) { } - /** * Attempt to open a file using the platform's shell. */ - static public void open(String filename) { + static public void open(final String filename) { open(new String[] { filename }); } - /** * Launch a process using a platforms shell. This version uses an array * to make it easier to deal with spaces in the individual elements. * (This avoids the situation of trying to put single or double quotes * around different bits). */ - static public Process open(String argv[]) { + static public Process open(final String argv[]) { return exec(argv); } - - static public Process exec(String[] argv) { + static public Process exec(final String[] argv) { try { return Runtime.getRuntime().exec(argv); - } catch (Exception e) { + } catch (final Exception e) { e.printStackTrace(); throw new RuntimeException("Could not open " + join(argv, ' ')); } } - ////////////////////////////////////////////////////////////// - /** * Function for an applet/application to kill itself and * display an error. Mostly this is here to be improved later. */ - public void die(String what) { + public void die(final String what) { stop(); throw new RuntimeException(what); } - /** * Same as above but with an exception. Also needs work. */ - public void die(String what, Exception e) { - if (e != null) e.printStackTrace(); + public void die(final String what, final Exception e) { + if (e != null) { + e.printStackTrace(); + } die(what); } - /** * Call to safely exit the sketch when finished. For instance, * to render a single frame, save it, and quit. @@ -1946,7 +1888,6 @@ public class PApplet extends Activity implements PConstants, Runnable { // exit immediately, stop() has already been called, // meaning that the main thread has long since exited exit2(); - } else if (looping) { // stop() will be called as the thread exits finished = true; @@ -1964,31 +1905,27 @@ public class PApplet extends Activity implements PConstants, Runnable { } } - void exit2() { try { System.exit(0); - } catch (SecurityException e) { + } catch (final SecurityException e) { // don't care about applet security exceptions } } - ////////////////////////////////////////////////////////////// // SCREEN GRABASS - /** * Intercepts any relative paths to make them absolute (relative * to the sketch folder) before passing to save() in PImage. * (Changed in 0100) */ - public void save(String filename) { + public void save(final String filename) { g.save(savePath(filename)); } - /** * Grab an image of what's currently in the drawing area and save it * as a .tif or .tga file. @@ -2000,13 +1937,12 @@ public class PApplet extends Activity implements PConstants, Runnable { public void saveFrame() { try { g.save(savePath("screen-" + nf(frameCount, 4) + ".tif")); - } catch (SecurityException se) { - System.err.println("Can't use saveFrame() when running in a browser, " + - "unless using a signed applet."); + } catch (final SecurityException se) { + System.err.println("Can't use saveFrame() when running in a browser, " + + "unless using a signed applet."); } } - /** * Save the current frame as a .tif or .tga image. *
@@ -2017,16 +1953,15 @@ public class PApplet extends Activity implements PConstants, Runnable { * // saves a numbered tiff image, replacing the * // #### signs with zeros and the frame number */ - public void saveFrame(String what) { + public void saveFrame(final String what) { try { g.save(savePath(insertFrame(what))); - } catch (SecurityException se) { - System.err.println("Can't use saveFrame() when running in a browser, " + - "unless using a signed applet."); + } catch (final SecurityException se) { + System.err.println("Can't use saveFrame() when running in a browser, " + + "unless using a signed applet."); } } - /** * Check a string for #### signs to see if the frame number should be * inserted. Used for functions like saveFrame() and beginRecord() to @@ -2034,52 +1969,44 @@ public class PApplet extends Activity implements PConstants, Runnable { * it will be ignored, under the assumption that it's probably not * intended to be the frame number. */ - protected String insertFrame(String what) { - int first = what.indexOf('#'); - int last = what.lastIndexOf('#'); + protected String insertFrame(final String what) { + final int first = what.indexOf('#'); + final int last = what.lastIndexOf('#'); if ((first != -1) && (last - first > 0)) { - String prefix = what.substring(0, first); - int count = last - first + 1; - String suffix = what.substring(last + 1); + final String prefix = what.substring(0, first); + final int count = last - first + 1; + final String suffix = what.substring(last + 1); return prefix + nf(frameCount, count) + suffix; } - return what; // no change + return what; // no change } - - ////////////////////////////////////////////////////////////// // CURSOR // Removed, this doesn't make sense in a touch interface. - -// int cursorType = ARROW; // cursor type -// boolean cursorVisible = true; // cursor visibility flag -// PImage invisibleCursor; - + // int cursorType = ARROW; // cursor type + // boolean cursorVisible = true; // cursor visibility flag + // PImage invisibleCursor; /** * Set the cursor type */ -// public void cursor(int cursorType) { -// setCursor(Cursor.getPredefinedCursor(cursorType)); -// cursorVisible = true; -// this.cursorType = cursorType; -// } - - + // public void cursor(int cursorType) { + // setCursor(Cursor.getPredefinedCursor(cursorType)); + // cursorVisible = true; + // this.cursorType = cursorType; + // } /** * Replace the cursor with the specified PImage. The x- and y- * coordinate of the center will be the center of the image. */ -// public void cursor(PImage image) { -// cursor(image, image.width/2, image.height/2); -// } - - + // public void cursor(PImage image) { + // cursor(image, image.width/2, image.height/2); + // } /** * Set a custom cursor to an image with a specific hotspot. * Only works with JDK 1.2 and later. @@ -2089,135 +2016,123 @@ public class PApplet extends Activity implements PConstants, Runnable { * code to handle Java versions via reflection by Jonathan Feinberg. * Reflection removed for release 0128 and later. */ -// public void cursor(PImage image, int hotspotX, int hotspotY) { -// // don't set this as cursor type, instead use cursor_type -// // to save the last cursor used in case cursor() is called -// //cursor_type = Cursor.CUSTOM_CURSOR; -// Image jimage = -// createImage(new MemoryImageSource(image.width, image.height, -// image.pixels, 0, image.width)); -// Point hotspot = new Point(hotspotX, hotspotY); -// Toolkit tk = Toolkit.getDefaultToolkit(); -// Cursor cursor = tk.createCustomCursor(jimage, hotspot, "Custom Cursor"); -// setCursor(cursor); -// cursorVisible = true; -// } - - + // public void cursor(PImage image, int hotspotX, int hotspotY) { + // // don't set this as cursor type, instead use cursor_type + // // to save the last cursor used in case cursor() is called + // //cursor_type = Cursor.CUSTOM_CURSOR; + // Image jimage = + // createImage(new MemoryImageSource(image.width, image.height, + // image.pixels, 0, image.width)); + // Point hotspot = new Point(hotspotX, hotspotY); + // Toolkit tk = Toolkit.getDefaultToolkit(); + // Cursor cursor = tk.createCustomCursor(jimage, hotspot, "Custom Cursor"); + // setCursor(cursor); + // cursorVisible = true; + // } /** * Show the cursor after noCursor() was called. * Notice that the program remembers the last set cursor type */ -// public void cursor() { -// // maybe should always set here? seems dangerous, since -// // it's likely that java will set the cursor to something -// // else on its own, and the applet will be stuck b/c bagel -// // thinks that the cursor is set to one particular thing -// if (!cursorVisible) { -// cursorVisible = true; -// setCursor(Cursor.getPredefinedCursor(cursorType)); -// } -// } - - + // public void cursor() { + // // maybe should always set here? seems dangerous, since + // // it's likely that java will set the cursor to something + // // else on its own, and the applet will be stuck b/c bagel + // // thinks that the cursor is set to one particular thing + // if (!cursorVisible) { + // cursorVisible = true; + // setCursor(Cursor.getPredefinedCursor(cursorType)); + // } + // } /** * Hide the cursor by creating a transparent image * and using it as a custom cursor. */ -// public void noCursor() { -// if (!cursorVisible) return; // don't hide if already hidden. -// -// if (invisibleCursor == null) { -// invisibleCursor = new PImage(16, 16, ARGB); -// } -// // was formerly 16x16, but the 0x0 was added by jdf as a fix -// // for macosx, which wasn't honoring the invisible cursor -// cursor(invisibleCursor, 8, 8); -// cursorVisible = false; -// } - - - ////////////////////////////////////////////////////////////// - - - static public void print(byte what) { - System.out.print(what); - System.out.flush(); - } - - static public void print(boolean what) { - System.out.print(what); - System.out.flush(); - } - - static public void print(char what) { - System.out.print(what); - System.out.flush(); - } - - static public void print(int what) { - System.out.print(what); - System.out.flush(); - } - - static public void print(float what) { - System.out.print(what); - System.out.flush(); - } - - static public void print(String what) { - System.out.print(what); - System.out.flush(); - } - - static public void print(Object what) { - if (what == null) { - // special case since this does fuggly things on > 1.1 - System.out.print("null"); - } else { - System.out.println(what.toString()); - } - } - + // public void noCursor() { + // if (!cursorVisible) return; // don't hide if already hidden. // + // if (invisibleCursor == null) { + // invisibleCursor = new PImage(16, 16, ARGB); + // } + // // was formerly 16x16, but the 0x0 was added by jdf as a fix + // // for macosx, which wasn't honoring the invisible cursor + // cursor(invisibleCursor, 8, 8); + // cursorVisible = false; + // } + ////////////////////////////////////////////////////////////// + static public void print(final byte what) { + System.out.print(what); + System.out.flush(); + } + + static public void print(final boolean what) { + System.out.print(what); + System.out.flush(); + } + + static public void print(final char what) { + System.out.print(what); + System.out.flush(); + } + + static public void print(final int what) { + System.out.print(what); + System.out.flush(); + } + + static public void print(final float what) { + System.out.print(what); + System.out.flush(); + } + + static public void print(final String what) { + System.out.print(what); + System.out.flush(); + } + + static public void print(final Object what) { + print(String.valueOf(what)); + } static public void println() { System.out.println(); } - // - - static public void println(byte what) { - print(what); System.out.println(); + static public void println(final byte what) { + print(what); + System.out.println(); } - static public void println(boolean what) { - print(what); System.out.println(); + static public void println(final boolean what) { + print(what); + System.out.println(); } - static public void println(char what) { - print(what); System.out.println(); + static public void println(final char what) { + print(what); + System.out.println(); } - static public void println(int what) { - print(what); System.out.println(); + static public void println(final int what) { + print(what); + System.out.println(); } - static public void println(float what) { - print(what); System.out.println(); + static public void println(final float what) { + print(what); + System.out.println(); } - static public void println(String what) { - print(what); System.out.println(); + static public void println(final String what) { + print(what); + System.out.println(); } - static public void println(Object what) { + static public void println(final Object what) { if (what == null) { // special case since this does fuggly things on > 1.1 System.out.println("null"); - } else { - String name = what.getClass().getName(); + final String name = what.getClass().getName(); if (name.charAt(0) == '[') { switch (name.charAt(1)) { case '[': @@ -2228,7 +2143,7 @@ public class PApplet extends Activity implements PConstants, Runnable { case 'L': // print a 1D array of objects as individual elements - Object poo[] = (Object[]) what; + final Object poo[] = (Object[]) what; for (int i = 0; i < poo.length; i++) { if (poo[i] instanceof String) { System.out.println("[" + i + "] \"" + poo[i] + "\""); @@ -2238,54 +2153,54 @@ public class PApplet extends Activity implements PConstants, Runnable { } break; - case 'Z': // boolean - boolean zz[] = (boolean[]) what; + case 'Z': // boolean + final boolean zz[] = (boolean[]) what; for (int i = 0; i < zz.length; i++) { System.out.println("[" + i + "] " + zz[i]); } break; - case 'B': // byte - byte bb[] = (byte[]) what; + case 'B': // byte + final byte bb[] = (byte[]) what; for (int i = 0; i < bb.length; i++) { System.out.println("[" + i + "] " + bb[i]); } break; - case 'C': // char - char cc[] = (char[]) what; + case 'C': // char + final char cc[] = (char[]) what; for (int i = 0; i < cc.length; i++) { System.out.println("[" + i + "] '" + cc[i] + "'"); } break; - case 'I': // int - int ii[] = (int[]) what; + case 'I': // int + final int ii[] = (int[]) what; for (int i = 0; i < ii.length; i++) { System.out.println("[" + i + "] " + ii[i]); } break; - case 'F': // float - float ff[] = (float[]) what; + case 'F': // float + final float ff[] = (float[]) what; for (int i = 0; i < ff.length; i++) { System.out.println("[" + i + "] " + ff[i]); } break; - /* + /* case 'D': // double - double dd[] = (double[]) what; - for (int i = 0; i < dd.length; i++) { - System.out.println("[" + i + "] " + dd[i]); - } - break; - */ + double dd[] = (double[]) what; + for (int i = 0; i < dd.length; i++) { + System.out.println("[" + i + "] " + dd[i]); + } + break; + */ default: System.out.println(what); } - } else { // not an array + } else { System.out.println(what); } } @@ -2306,76 +2221,70 @@ public class PApplet extends Activity implements PConstants, Runnable { } */ - ////////////////////////////////////////////////////////////// - // MATH - // lots of convenience methods for math with floats. // doubles are overkill for processing applets, and casting // things all the time is annoying, thus the functions below. - - - static public final float abs(float n) { + static public final float abs(final float n) { return (n < 0) ? -n : n; } - static public final int abs(int n) { + static public final int abs(final int n) { return (n < 0) ? -n : n; } - static public final float sq(float a) { - return a*a; + static public final float sq(final float a) { + return a * a; } - static public final float sqrt(float a) { - return (float)Math.sqrt(a); + static public final float sqrt(final float a) { + return (float) Math.sqrt(a); } - static public final float log(float a) { - return (float)Math.log(a); + static public final float log(final float a) { + return (float) Math.log(a); } - static public final float exp(float a) { - return (float)Math.exp(a); + static public final float exp(final float a) { + return (float) Math.exp(a); } - static public final float pow(float a, float b) { - return (float)Math.pow(a, b); + static public final float pow(final float a, final float b) { + return (float) Math.pow(a, b); } - - static public final int max(int a, int b) { + static public final int max(final int a, final int b) { return (a > b) ? a : b; } - static public final float max(float a, float b) { + static public final float max(final float a, final float b) { return (a > b) ? a : b; } - - static public final int max(int a, int b, int c) { + static public final int max(final int a, final int b, final int c) { return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c); } - static public final float max(float a, float b, float c) { + static public final float max(final float a, final float b, final float c) { return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c); } - /** * Find the maximum value in an array. * Throws an ArrayIndexOutOfBoundsException if the array is length 0. * @param list the source array * @return The maximum value */ - static public final int max(int[] list) { + static public final int max(final int[] list) { if (list.length == 0) { throw new ArrayIndexOutOfBoundsException(ERROR_MIN_MAX); } int max = list[0]; for (int i = 1; i < list.length; i++) { - if (list[i] > max) max = list[i]; + if (list[i] > max) { + max = list[i]; + } } return max; } @@ -2386,152 +2295,152 @@ public class PApplet extends Activity implements PConstants, Runnable { * @param list the source array * @return The maximum value */ - static public final float max(float[] list) { + static public final float max(final float[] list) { if (list.length == 0) { throw new ArrayIndexOutOfBoundsException(ERROR_MIN_MAX); } float max = list[0]; for (int i = 1; i < list.length; i++) { - if (list[i] > max) max = list[i]; + if (list[i] > max) { + max = list[i]; + } } return max; } - - static public final int min(int a, int b) { + static public final int min(final int a, final int b) { return (a < b) ? a : b; } - static public final float min(float a, float b) { + static public final float min(final float a, final float b) { return (a < b) ? a : b; } - - static public final int min(int a, int b, int c) { + static public final int min(final int a, final int b, final int c) { return (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c); } - static public final float min(float a, float b, float c) { + static public final float min(final float a, final float b, final float c) { return (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c); } - /** * Find the minimum value in an array. * Throws an ArrayIndexOutOfBoundsException if the array is length 0. * @param list the source array * @return The minimum value */ - static public final int min(int[] list) { + static public final int min(final int[] list) { if (list.length == 0) { throw new ArrayIndexOutOfBoundsException(ERROR_MIN_MAX); } int min = list[0]; for (int i = 1; i < list.length; i++) { - if (list[i] < min) min = list[i]; + if (list[i] < min) { + min = list[i]; + } } return min; } + /** * Find the minimum value in an array. * Throws an ArrayIndexOutOfBoundsException if the array is length 0. * @param list the source array * @return The minimum value */ - static public final float min(float[] list) { + static public final float min(final float[] list) { if (list.length == 0) { throw new ArrayIndexOutOfBoundsException(ERROR_MIN_MAX); } float min = list[0]; for (int i = 1; i < list.length; i++) { - if (list[i] < min) min = list[i]; + if (list[i] < min) { + min = list[i]; + } } return min; } - - static public final int constrain(int amt, int low, int high) { + static public final int constrain(final int amt, final int low, final int high) { return (amt < low) ? low : ((amt > high) ? high : amt); } - static public final float constrain(float amt, float low, float high) { + static public final float constrain(final float amt, final float low, + final float high) { return (amt < low) ? low : ((amt > high) ? high : amt); } - - static public final float sin(float angle) { - return (float)Math.sin(angle); + static public final float sin(final float angle) { + return (float) Math.sin(angle); } - static public final float cos(float angle) { - return (float)Math.cos(angle); + static public final float cos(final float angle) { + return (float) Math.cos(angle); } - static public final float tan(float angle) { - return (float)Math.tan(angle); + static public final float tan(final float angle) { + return (float) Math.tan(angle); } - - static public final float asin(float value) { - return (float)Math.asin(value); + static public final float asin(final float value) { + return (float) Math.asin(value); } - static public final float acos(float value) { - return (float)Math.acos(value); + static public final float acos(final float value) { + return (float) Math.acos(value); } - static public final float atan(float value) { - return (float)Math.atan(value); + static public final float atan(final float value) { + return (float) Math.atan(value); } - static public final float atan2(float a, float b) { - return (float)Math.atan2(a, b); + static public final float atan2(final float a, final float b) { + return (float) Math.atan2(a, b); } - - static public final float degrees(float radians) { + static public final float degrees(final float radians) { return radians * RAD_TO_DEG; } - static public final float radians(float degrees) { + static public final float radians(final float degrees) { return degrees * DEG_TO_RAD; } - - static public final int ceil(float what) { + static public final int ceil(final float what) { return (int) Math.ceil(what); } - static public final int floor(float what) { + static public final int floor(final float what) { return (int) Math.floor(what); } - static public final int round(float what) { - return (int) Math.round(what); + static public final int round(final float what) { + return Math.round(what); } - - static public final float mag(float a, float b) { - return (float)Math.sqrt(a*a + b*b); + static public final float mag(final float a, final float b) { + return (float) Math.sqrt(a * a + b * b); } - static public final float mag(float a, float b, float c) { - return (float)Math.sqrt(a*a + b*b + c*c); + static public final float mag(final float a, final float b, final float c) { + return (float) Math.sqrt(a * a + b * b + c * c); } - - static public final float dist(float x1, float y1, float x2, float y2) { - return sqrt(sq(x2-x1) + sq(y2-y1)); + static public final float dist(final float x1, final float y1, + final float x2, final float y2) { + return sqrt(sq(x2 - x1) + sq(y2 - y1)); } - static public final float dist(float x1, float y1, float z1, - float x2, float y2, float z2) { - return sqrt(sq(x2-x1) + sq(y2-y1) + sq(z2-z1)); + static public final float dist(final float x1, final float y1, + final float z1, final float x2, + final float y2, final float z2) { + return sqrt(sq(x2 - x1) + sq(y2 - y1) + sq(z2 - z1)); } - - static public final float lerp(float start, float stop, float amt) { - return start + (stop-start) * amt; + static public final float lerp(final float start, final float stop, + final float amt) { + return start + (stop - start) * amt; } /** @@ -2539,7 +2448,8 @@ public class PApplet extends Activity implements PConstants, Runnable { * Mathematically the opposite of lerp(), figures out what proportion * a particular value is relative to start and stop coordinates. */ - static public final float norm(float value, float start, float stop) { + static public final float norm(final float value, final float start, + final float stop) { return (value - start) / (stop - start); } @@ -2547,19 +2457,16 @@ public class PApplet extends Activity implements PConstants, Runnable { * Convenience function to map a variable from one coordinate space * to another. Equivalent to unlerp() followed by lerp(). */ - static public final float map(float value, - float istart, float istop, - float ostart, float ostop) { + static public final float map(final float value, final float istart, + final float istop, final float ostart, + final float ostop) { return ostart + (ostop - ostart) * ((value - istart) / (istop - istart)); } - - ////////////////////////////////////////////////////////////// // RANDOM NUMBERS - Random internalRandom; /** @@ -2568,16 +2475,20 @@ public class PApplet extends Activity implements PConstants, Runnable { * The number returned will range from zero up to * (but not including) 'howbig'. */ - public final float random(float howbig) { + public final float random(final float howbig) { // for some reason (rounding error?) Math.random() * 3 // can sometimes return '3' (once in ~30 million tries) // so a check was added to avoid the inclusion of 'howbig' // avoid an infinite loop - if (howbig == 0) return 0; + if (howbig == 0) { + return 0; + } // internal random number object - if (internalRandom == null) internalRandom = new Random(); + if (internalRandom == null) { + internalRandom = new Random(); + } float value = 0; do { @@ -2587,7 +2498,6 @@ public class PApplet extends Activity implements PConstants, Runnable { return value; } - /** * Return a random number in the range [howsmall, howbig). *
@@ -2598,21 +2508,22 @@ public class PApplet extends Activity implements PConstants, Runnable {
* meaning that random(5, 5) will return 5 (useful)
* and random(7, 4) will return 7 (not useful.. better idea?)
*/
- public final float random(float howsmall, float howbig) {
- if (howsmall >= howbig) return howsmall;
- float diff = howbig - howsmall;
+ public final float random(final float howsmall, final float howbig) {
+ if (howsmall >= howbig) {
+ return howsmall;
+ }
+ final float diff = howbig - howsmall;
return random(diff) + howsmall;
}
-
- public final void randomSeed(long what) {
+ public final void randomSeed(final long what) {
// internal random number object
- if (internalRandom == null) internalRandom = new Random();
+ if (internalRandom == null) {
+ internalRandom = new Random();
+ }
internalRandom.setSeed(what);
}
-
-
//////////////////////////////////////////////////////////////
// PERLIN NOISE
@@ -2629,9 +2540,9 @@ public class PApplet extends Activity implements PConstants, Runnable {
// as used in their demo "art": http://www.farb-rausch.de/fr010src.zip
static final int PERLIN_YWRAPB = 4;
- static final int PERLIN_YWRAP = 1<
@@ -3351,12 +3263,12 @@ public class PApplet extends Activity implements PConstants, Runnable {
*
@@ -3576,29 +3487,32 @@ public class PApplet extends Activity implements PConstants, Runnable {
* in a "scripting" fashion. If you want to handle exceptions,
* use Java methods for I/O.
*/
- public String[] loadStrings(String filename) {
- InputStream is = createInput(filename);
- if (is != null) return loadStrings(is);
+ public String[] loadStrings(final String filename) {
+ final InputStream is = createInput(filename);
+ if (is != null) {
+ return loadStrings(is);
+ }
- System.err.println("The file \"" + filename + "\" " +
- "is missing or inaccessible, make sure " +
- "the URL is valid or that the file has been " +
- "added to your sketch and is readable.");
+ System.err.println("The file \"" + filename + "\" "
+ + "is missing or inaccessible, make sure "
+ + "the URL is valid or that the file has been "
+ + "added to your sketch and is readable.");
return null;
}
-
- static public String[] loadStrings(InputStream input) {
+ static public String[] loadStrings(final InputStream input) {
try {
- BufferedReader reader =
- new BufferedReader(new InputStreamReader(input, "UTF-8"));
+ final BufferedReader reader = new BufferedReader(
+ new InputStreamReader(
+ input,
+ "UTF-8"));
String lines[] = new String[100];
int lineCount = 0;
String line = null;
while ((line = reader.readLine()) != null) {
if (lineCount == lines.length) {
- String temp[] = new String[lineCount << 1];
+ final String temp[] = new String[lineCount << 1];
System.arraycopy(lines, 0, temp, 0, lineCount);
lines = temp;
}
@@ -3611,24 +3525,21 @@ public class PApplet extends Activity implements PConstants, Runnable {
}
// resize array to appropriate amount for these lines
- String output[] = new String[lineCount];
+ final String output[] = new String[lineCount];
System.arraycopy(lines, 0, output, 0, lineCount);
return output;
- } catch (IOException e) {
+ } catch (final IOException e) {
e.printStackTrace();
//throw new RuntimeException("Error inside loadStrings()");
}
return null;
}
-
-
//////////////////////////////////////////////////////////////
// FILE OUTPUT
-
/**
* Similar to createInput() (formerly openStream), this creates a Java
* OutputStream for a given filename or path. The file will be created in
@@ -3643,67 +3554,66 @@ public class PApplet extends Activity implements PConstants, Runnable {
* little too clever (and then we'd have to add the same features to the
* other file functions like createWriter). Who you callin' bloated?
*/
- public OutputStream createOutput(String filename) {
+ public OutputStream createOutput(final String filename) {
try {
//return createOutput(saveFile(filename));
- Context context = getApplicationContext();
+ final Context context = getApplicationContext();
// MODE_PRIVATE is default, should we use that instead?
return context.openFileOutput(filename, MODE_WORLD_READABLE);
- } catch (Exception e) {
+ } catch (final Exception e) {
e.printStackTrace();
return null;
}
}
-
-// static public OutputStream createOutput(File file) {
-// try {
-// FileOutputStream fos = new FileOutputStream(file);
-// if (file.getName().toLowerCase().endsWith(".gz")) {
-// return new GZIPOutputStream(fos);
-// }
-// return fos;
-//
-// } catch (IOException e) {
-// e.printStackTrace();
-// }
-// return null;
-// }
-
+ // static public OutputStream createOutput(File file) {
+ // try {
+ // FileOutputStream fos = new FileOutputStream(file);
+ // if (file.getName().toLowerCase().endsWith(".gz")) {
+ // return new GZIPOutputStream(fos);
+ // }
+ // return fos;
+ //
+ // } catch (IOException e) {
+ // e.printStackTrace();
+ // }
+ // return null;
+ // }
/**
* Save the contents of a stream to a file in the sketch folder.
* This is basically saveBytes(blah, loadBytes()), but done
* more efficiently (and with less confusing syntax).
*/
- public void saveStream(String targetFilename, String sourceLocation) {
+ public void saveStream(final String targetFilename,
+ final String sourceLocation) {
saveStream(saveFile(targetFilename), sourceLocation);
}
-
/**
* Identical to the other saveStream(), but writes to a File
* object, for greater control over the file location.
* Note that unlike other api methods, this will not automatically
* compress or uncompress gzip files.
*/
- public void saveStream(File targetFile, String sourceLocation) {
+ public void saveStream(final File targetFile, final String sourceLocation) {
saveStream(targetFile, createInputRaw(sourceLocation));
}
-
- static public void saveStream(File targetFile, InputStream sourceStream) {
+ static public void saveStream(final File targetFile,
+ final InputStream sourceStream) {
File tempFile = null;
try {
- File parentDir = targetFile.getParentFile();
+ final File parentDir = targetFile.getParentFile();
tempFile = File.createTempFile(targetFile.getName(), null, parentDir);
- BufferedInputStream bis = new BufferedInputStream(sourceStream, 16384);
- FileOutputStream fos = new FileOutputStream(tempFile);
+ final BufferedInputStream bis = new BufferedInputStream(sourceStream,
+ 16384);
+ final FileOutputStream fos = new FileOutputStream(tempFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
- byte[] buffer = new byte[8192];
+ final byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, bytesRead);
@@ -3714,10 +3624,10 @@ public class PApplet extends Activity implements PConstants, Runnable {
bos = null;
if (!tempFile.renameTo(targetFile)) {
- System.err.println("Could not rename temporary file " +
- tempFile.getAbsolutePath());
+ System.err.println("Could not rename temporary file "
+ + tempFile.getAbsolutePath());
}
- } catch (IOException e) {
+ } catch (final IOException e) {
if (tempFile != null) {
tempFile.delete();
}
@@ -3725,7 +3635,6 @@ public class PApplet extends Activity implements PConstants, Runnable {
}
}
-
/**
* Saves bytes to a file to inside the sketch folder.
* The filename can be a relative path, i.e. "poo/bytefun.txt"
@@ -3733,17 +3642,16 @@ public class PApplet extends Activity implements PConstants, Runnable {
* called 'poo' inside the sketch folder. If the in-between
* subfolders don't exist, they'll be created.
*/
- public void saveBytes(String filename, byte buffer[]) {
+ public void saveBytes(final String filename, final byte buffer[]) {
saveBytes(saveFile(filename), buffer);
}
-
/**
* Saves bytes to a specific File location specified by the user.
*/
- static public void saveBytes(File file, byte buffer[]) {
+ static public void saveBytes(final File file, final byte buffer[]) {
try {
- String filename = file.getAbsolutePath();
+ final String filename = file.getAbsolutePath();
createPath(filename);
OutputStream output = new FileOutputStream(file);
if (file.getName().toLowerCase().endsWith(".gz")) {
@@ -3752,36 +3660,34 @@ public class PApplet extends Activity implements PConstants, Runnable {
saveBytes(output, buffer);
output.close();
- } catch (IOException e) {
+ } catch (final IOException e) {
System.err.println("error saving bytes to " + file);
e.printStackTrace();
}
}
-
/**
* Spews a buffer of bytes to an OutputStream.
*/
- static public void saveBytes(OutputStream output, byte buffer[]) {
+ static public void saveBytes(final OutputStream output, final byte buffer[]) {
try {
output.write(buffer);
output.flush();
- } catch (IOException e) {
+ } catch (final IOException e) {
e.printStackTrace();
}
}
//
- public void saveStrings(String filename, String strings[]) {
+ public void saveStrings(final String filename, final String strings[]) {
saveStrings(saveFile(filename), strings);
}
-
- static public void saveStrings(File file, String strings[]) {
+ static public void saveStrings(final File file, final String strings[]) {
try {
- String location = file.getAbsolutePath();
+ final String location = file.getAbsolutePath();
createPath(location);
OutputStream output = new FileOutputStream(location);
if (file.getName().toLowerCase().endsWith(".gz")) {
@@ -3790,27 +3696,26 @@ public class PApplet extends Activity implements PConstants, Runnable {
saveStrings(output, strings);
output.close();
- } catch (IOException e) {
+ } catch (final IOException e) {
e.printStackTrace();
}
}
-
- static public void saveStrings(OutputStream output, String strings[]) {
+ static public void saveStrings(final OutputStream output,
+ final String strings[]) {
try {
- OutputStreamWriter osw = new OutputStreamWriter(output, "UTF-8");
- PrintWriter writer = new PrintWriter(osw);
- for (int i = 0; i < strings.length; i++) {
- writer.println(strings[i]);
+ final OutputStreamWriter osw = new OutputStreamWriter(output, "UTF-8");
+ final PrintWriter writer = new PrintWriter(osw);
+ for (final String string : strings) {
+ writer.println(string);
}
writer.flush();
- } catch (UnsupportedEncodingException e) { } // will not happen
+ } catch (final UnsupportedEncodingException e) {
+ } // will not happen
}
-
//////////////////////////////////////////////////////////////
-
/**
* Prepend the sketch folder path to the filename (or path) that is
* passed in. External libraries should use this function to save to
@@ -3825,31 +3730,32 @@ public class PApplet extends Activity implements PConstants, Runnable {
* my some other main() or by other code. For proper use of init(),
* see the examples in the main description text for PApplet.
*/
- public String sketchPath(String where) {
+ public String sketchPath(final String where) {
if (sketchPath == null) {
return where;
-// throw new RuntimeException("The applet was not inited properly, " +
-// "or security restrictions prevented " +
-// "it from determining its path.");
+ // throw new RuntimeException("The applet was not inited properly, " +
+ // "or security restrictions prevented " +
+ // "it from determining its path.");
}
-
+
// isAbsolute() could throw an access exception, but so will writing
// to the local disk using the sketch path, so this is safe here.
// for 0120, added a try/catch anyways.
try {
- if (new File(where).isAbsolute()) return where;
- } catch (Exception e) { }
+ if (new File(where).isAbsolute()) {
+ return where;
+ }
+ } catch (final Exception e) {
+ }
- Context context = getApplicationContext();
+ final Context context = getApplicationContext();
return context.getFileStreamPath(where).getAbsolutePath();
}
-
- public File sketchFile(String where) {
+ public File sketchFile(final String where) {
return new File(sketchPath(where));
}
-
/**
* Returns a path inside the applet folder to save to. Like sketchPath(),
* but creates any in-between folders so that things save properly.
@@ -3863,24 +3769,24 @@ public class PApplet extends Activity implements PConstants, Runnable {
* If you know you're running locally, and want to save to the data folder,
* use saveXxxx("data/blah.dat").
*/
- public String savePath(String where) {
- if (where == null) return null;
+ public String savePath(final String where) {
+ if (where == null) {
+ return null;
+ }
System.out.println("filename before sketchpath is " + where);
- String filename = sketchPath(where);
+ final String filename = sketchPath(where);
System.out.println("filename after sketchpath is " + filename);
createPath(filename);
return filename;
}
-
/**
* Identical to savePath(), but returns a File object.
*/
- public File saveFile(String where) {
+ public File saveFile(final String where) {
return new File(savePath(where));
}
-
/**
* Return a full path to an item in the data folder.
*
@@ -3892,239 +3798,218 @@ public class PApplet extends Activity implements PConstants, Runnable {
* and it's not possible to read/write from the jar file in a generic way.
* If you need to read data from the jar file, you should use createInput().
*/
- public String dataPath(String where) {
+ public String dataPath(final String where) {
// isAbsolute() could throw an access exception, but so will writing
// to the local disk using the sketch path, so this is safe here.
- if (new File(where).isAbsolute()) return where;
+ if (new File(where).isAbsolute()) {
+ return where;
+ }
return sketchPath + File.separator + "data" + File.separator + where;
}
-
/**
* Return a full path to an item in the data folder as a File object.
* See the dataPath() method for more information.
*/
- public File dataFile(String where) {
+ public File dataFile(final String where) {
return new File(dataPath(where));
}
-
/**
* Takes a path and creates any in-between folders if they don't
* already exist. Useful when trying to save to a subfolder that
* may not actually exist.
*/
- static public void createPath(String path) {
+ static public void createPath(final String path) {
try {
- File file = new File(path);
- String parent = file.getParent();
+ final File file = new File(path);
+ final String parent = file.getParent();
if (parent != null) {
- File unit = new File(parent);
- if (!unit.exists()) unit.mkdirs();
+ final File unit = new File(parent);
+ if (!unit.exists()) {
+ unit.mkdirs();
+ }
}
- } catch (SecurityException se) {
+ } catch (final SecurityException se) {
System.err.println("You don't have permissions to create " + path);
}
}
-
-
//////////////////////////////////////////////////////////////
// SORT
-
- static public byte[] sort(byte what[]) {
+ static public byte[] sort(final byte what[]) {
return sort(what, what.length);
}
-
- static public byte[] sort(byte[] what, int count) {
- byte[] outgoing = new byte[what.length];
+ static public byte[] sort(final byte[] what, final int count) {
+ final byte[] outgoing = new byte[what.length];
System.arraycopy(what, 0, outgoing, 0, what.length);
Arrays.sort(outgoing, 0, count);
return outgoing;
}
-
- static public char[] sort(char what[]) {
+ static public char[] sort(final char what[]) {
return sort(what, what.length);
}
-
- static public char[] sort(char[] what, int count) {
- char[] outgoing = new char[what.length];
+ static public char[] sort(final char[] what, final int count) {
+ final char[] outgoing = new char[what.length];
System.arraycopy(what, 0, outgoing, 0, what.length);
Arrays.sort(outgoing, 0, count);
return outgoing;
}
-
- static public int[] sort(int what[]) {
+ static public int[] sort(final int what[]) {
return sort(what, what.length);
}
-
- static public int[] sort(int[] what, int count) {
- int[] outgoing = new int[what.length];
+ static public int[] sort(final int[] what, final int count) {
+ final int[] outgoing = new int[what.length];
System.arraycopy(what, 0, outgoing, 0, what.length);
Arrays.sort(outgoing, 0, count);
return outgoing;
}
-
- static public float[] sort(float what[]) {
+ static public float[] sort(final float what[]) {
return sort(what, what.length);
}
-
- static public float[] sort(float[] what, int count) {
- float[] outgoing = new float[what.length];
+ static public float[] sort(final float[] what, final int count) {
+ final float[] outgoing = new float[what.length];
System.arraycopy(what, 0, outgoing, 0, what.length);
Arrays.sort(outgoing, 0, count);
return outgoing;
}
-
- static public String[] sort(String what[]) {
+ static public String[] sort(final String what[]) {
return sort(what, what.length);
}
-
- static public String[] sort(String[] what, int count) {
- String[] outgoing = new String[what.length];
+ static public String[] sort(final String[] what, final int count) {
+ final String[] outgoing = new String[what.length];
System.arraycopy(what, 0, outgoing, 0, what.length);
Arrays.sort(outgoing, 0, count);
return outgoing;
}
-
-
//////////////////////////////////////////////////////////////
// ARRAY UTILITIES
-
/**
* Calls System.arraycopy(), included here so that we can
* avoid people needing to learn about the System object
* before they can just copy an array.
*/
- static public void arrayCopy(Object src, int srcPosition,
- Object dst, int dstPosition,
- int length) {
+ static public void arrayCopy(final Object src, final int srcPosition,
+ final Object dst, final int dstPosition,
+ final int length) {
System.arraycopy(src, srcPosition, dst, dstPosition, length);
}
-
/**
* Convenience method for arraycopy().
* Identical to The preprocessor will convert boolean(what) to parseBoolean(what).arraycopy(src, 0, dst, 0, length);
*/
- static public void arrayCopy(Object src, Object dst, int length) {
+ static public void arrayCopy(final Object src, final Object dst,
+ final int length) {
System.arraycopy(src, 0, dst, 0, length);
}
-
/**
* Shortcut to copy the entire contents of
* the source into the destination array.
* Identical to arraycopy(src, 0, dst, 0, src.length);
*/
- static public void arrayCopy(Object src, Object dst) {
+ static public void arrayCopy(final Object src, final Object dst) {
System.arraycopy(src, 0, dst, 0, Array.getLength(src));
}
//
- static public boolean[] expand(boolean list[]) {
+ static public boolean[] expand(final boolean list[]) {
return expand(list, list.length << 1);
}
- static public boolean[] expand(boolean list[], int newSize) {
- boolean temp[] = new boolean[newSize];
+ static public boolean[] expand(final boolean list[], final int newSize) {
+ final boolean temp[] = new boolean[newSize];
System.arraycopy(list, 0, temp, 0, Math.min(newSize, list.length));
return temp;
}
-
- static public byte[] expand(byte list[]) {
+ static public byte[] expand(final byte list[]) {
return expand(list, list.length << 1);
}
- static public byte[] expand(byte list[], int newSize) {
- byte temp[] = new byte[newSize];
+ static public byte[] expand(final byte list[], final int newSize) {
+ final byte temp[] = new byte[newSize];
System.arraycopy(list, 0, temp, 0, Math.min(newSize, list.length));
return temp;
}
-
- static public char[] expand(char list[]) {
+ static public char[] expand(final char list[]) {
return expand(list, list.length << 1);
}
- static public char[] expand(char list[], int newSize) {
- char temp[] = new char[newSize];
+ static public char[] expand(final char list[], final int newSize) {
+ final char temp[] = new char[newSize];
System.arraycopy(list, 0, temp, 0, Math.min(newSize, list.length));
return temp;
}
-
- static public int[] expand(int list[]) {
+ static public int[] expand(final int list[]) {
return expand(list, list.length << 1);
}
- static public int[] expand(int list[], int newSize) {
- int temp[] = new int[newSize];
+ static public int[] expand(final int list[], final int newSize) {
+ final int temp[] = new int[newSize];
System.arraycopy(list, 0, temp, 0, Math.min(newSize, list.length));
return temp;
}
-
- static public PImage[] expand(PImage list[]) {
+ static public PImage[] expand(final PImage list[]) {
return expand(list, list.length << 1);
}
- static public PImage[] expand(PImage list[], int newSize) {
- PImage temp[] = new PImage[newSize];
- System.arraycopy(list, 0, temp, 0, Math.min(newSize, list.length));
- return temp;
- }
-
-
- static public float[] expand(float list[]) {
- return expand(list, list.length << 1);
- }
-
- static public float[] expand(float list[], int newSize) {
- float temp[] = new float[newSize];
+ static public PImage[] expand(final PImage list[], final int newSize) {
+ final PImage temp[] = new PImage[newSize];
System.arraycopy(list, 0, temp, 0, Math.min(newSize, list.length));
return temp;
}
-
- static public String[] expand(String list[]) {
+ static public float[] expand(final float list[]) {
return expand(list, list.length << 1);
}
- static public String[] expand(String list[], int newSize) {
- String temp[] = new String[newSize];
+ static public float[] expand(final float list[], final int newSize) {
+ final float temp[] = new float[newSize];
+ System.arraycopy(list, 0, temp, 0, Math.min(newSize, list.length));
+ return temp;
+ }
+
+ static public String[] expand(final String list[]) {
+ return expand(list, list.length << 1);
+ }
+
+ static public String[] expand(final String list[], final int newSize) {
+ final String temp[] = new String[newSize];
// in case the new size is smaller than list.length
System.arraycopy(list, 0, temp, 0, Math.min(newSize, list.length));
return temp;
}
-
- static public Object expand(Object array) {
+ static public Object expand(final Object array) {
return expand(array, Array.getLength(array) << 1);
}
- static public Object expand(Object list, int newSize) {
- Class> type = list.getClass().getComponentType();
- Object temp = Array.newInstance(type, newSize);
- System.arraycopy(list, 0, temp, 0,
- Math.min(Array.getLength(list), newSize));
+ static public Object expand(final Object list, final int newSize) {
+ final Class> type = list.getClass().getComponentType();
+ final Object temp = Array.newInstance(type, newSize);
+ System
+ .arraycopy(list, 0, temp, 0, Math.min(Array.getLength(list), newSize));
return temp;
}
@@ -4135,38 +4020,38 @@ public class PApplet extends Activity implements PConstants, Runnable {
//
- static public byte[] append(byte b[], byte value) {
+ static public byte[] append(byte b[], final byte value) {
b = expand(b, b.length + 1);
- b[b.length-1] = value;
+ b[b.length - 1] = value;
return b;
}
- static public char[] append(char b[], char value) {
+ static public char[] append(char b[], final char value) {
b = expand(b, b.length + 1);
- b[b.length-1] = value;
+ b[b.length - 1] = value;
return b;
}
- static public int[] append(int b[], int value) {
+ static public int[] append(int b[], final int value) {
b = expand(b, b.length + 1);
- b[b.length-1] = value;
+ b[b.length - 1] = value;
return b;
}
- static public float[] append(float b[], float value) {
+ static public float[] append(float b[], final float value) {
b = expand(b, b.length + 1);
- b[b.length-1] = value;
+ b[b.length - 1] = value;
return b;
}
- static public String[] append(String b[], String value) {
+ static public String[] append(String b[], final String value) {
b = expand(b, b.length + 1);
- b[b.length-1] = value;
+ b[b.length - 1] = value;
return b;
}
- static public Object append(Object b, Object value) {
- int length = Array.getLength(b);
+ static public Object append(Object b, final Object value) {
+ final int length = Array.getLength(b);
b = expand(b, length + 1);
Array.set(b, length, value);
return b;
@@ -4174,170 +4059,159 @@ public class PApplet extends Activity implements PConstants, Runnable {
//
- static public boolean[] shorten(boolean list[]) {
- return subset(list, 0, list.length-1);
+ static public boolean[] shorten(final boolean list[]) {
+ return subset(list, 0, list.length - 1);
}
- static public byte[] shorten(byte list[]) {
- return subset(list, 0, list.length-1);
+ static public byte[] shorten(final byte list[]) {
+ return subset(list, 0, list.length - 1);
}
- static public char[] shorten(char list[]) {
- return subset(list, 0, list.length-1);
+ static public char[] shorten(final char list[]) {
+ return subset(list, 0, list.length - 1);
}
- static public int[] shorten(int list[]) {
- return subset(list, 0, list.length-1);
+ static public int[] shorten(final int list[]) {
+ return subset(list, 0, list.length - 1);
}
- static public float[] shorten(float list[]) {
- return subset(list, 0, list.length-1);
+ static public float[] shorten(final float list[]) {
+ return subset(list, 0, list.length - 1);
}
- static public String[] shorten(String list[]) {
- return subset(list, 0, list.length-1);
+ static public String[] shorten(final String list[]) {
+ return subset(list, 0, list.length - 1);
}
- static public Object shorten(Object list) {
- int length = Array.getLength(list);
+ static public Object shorten(final Object list) {
+ final int length = Array.getLength(list);
return subset(list, 0, length - 1);
}
//
- static final public boolean[] splice(boolean list[],
- boolean v, int index) {
- boolean outgoing[] = new boolean[list.length + 1];
+ static final public boolean[] splice(final boolean list[], final boolean v,
+ final int index) {
+ final boolean outgoing[] = new boolean[list.length + 1];
System.arraycopy(list, 0, outgoing, 0, index);
outgoing[index] = v;
- System.arraycopy(list, index, outgoing, index + 1,
- list.length - index);
+ System.arraycopy(list, index, outgoing, index + 1, list.length - index);
return outgoing;
}
- static final public boolean[] splice(boolean list[],
- boolean v[], int index) {
- boolean outgoing[] = new boolean[list.length + v.length];
+ static final public boolean[] splice(final boolean list[], final boolean v[],
+ final int index) {
+ final boolean outgoing[] = new boolean[list.length + v.length];
System.arraycopy(list, 0, outgoing, 0, index);
System.arraycopy(v, 0, outgoing, index, v.length);
- System.arraycopy(list, index, outgoing, index + v.length,
- list.length - index);
+ System.arraycopy(list, index, outgoing, index + v.length, list.length
+ - index);
return outgoing;
}
-
- static final public byte[] splice(byte list[],
- byte v, int index) {
- byte outgoing[] = new byte[list.length + 1];
+ static final public byte[] splice(final byte list[], final byte v,
+ final int index) {
+ final byte outgoing[] = new byte[list.length + 1];
System.arraycopy(list, 0, outgoing, 0, index);
outgoing[index] = v;
- System.arraycopy(list, index, outgoing, index + 1,
- list.length - index);
+ System.arraycopy(list, index, outgoing, index + 1, list.length - index);
return outgoing;
}
- static final public byte[] splice(byte list[],
- byte v[], int index) {
- byte outgoing[] = new byte[list.length + v.length];
+ static final public byte[] splice(final byte list[], final byte v[],
+ final int index) {
+ final byte outgoing[] = new byte[list.length + v.length];
System.arraycopy(list, 0, outgoing, 0, index);
System.arraycopy(v, 0, outgoing, index, v.length);
- System.arraycopy(list, index, outgoing, index + v.length,
- list.length - index);
+ System.arraycopy(list, index, outgoing, index + v.length, list.length
+ - index);
return outgoing;
}
-
- static final public char[] splice(char list[],
- char v, int index) {
- char outgoing[] = new char[list.length + 1];
+ static final public char[] splice(final char list[], final char v,
+ final int index) {
+ final char outgoing[] = new char[list.length + 1];
System.arraycopy(list, 0, outgoing, 0, index);
outgoing[index] = v;
- System.arraycopy(list, index, outgoing, index + 1,
- list.length - index);
+ System.arraycopy(list, index, outgoing, index + 1, list.length - index);
return outgoing;
}
- static final public char[] splice(char list[],
- char v[], int index) {
- char outgoing[] = new char[list.length + v.length];
+ static final public char[] splice(final char list[], final char v[],
+ final int index) {
+ final char outgoing[] = new char[list.length + v.length];
System.arraycopy(list, 0, outgoing, 0, index);
System.arraycopy(v, 0, outgoing, index, v.length);
- System.arraycopy(list, index, outgoing, index + v.length,
- list.length - index);
+ System.arraycopy(list, index, outgoing, index + v.length, list.length
+ - index);
return outgoing;
}
-
- static final public int[] splice(int list[],
- int v, int index) {
- int outgoing[] = new int[list.length + 1];
+ static final public int[] splice(final int list[], final int v,
+ final int index) {
+ final int outgoing[] = new int[list.length + 1];
System.arraycopy(list, 0, outgoing, 0, index);
outgoing[index] = v;
- System.arraycopy(list, index, outgoing, index + 1,
- list.length - index);
+ System.arraycopy(list, index, outgoing, index + 1, list.length - index);
return outgoing;
}
- static final public int[] splice(int list[],
- int v[], int index) {
- int outgoing[] = new int[list.length + v.length];
+ static final public int[] splice(final int list[], final int v[],
+ final int index) {
+ final int outgoing[] = new int[list.length + v.length];
System.arraycopy(list, 0, outgoing, 0, index);
System.arraycopy(v, 0, outgoing, index, v.length);
- System.arraycopy(list, index, outgoing, index + v.length,
- list.length - index);
+ System.arraycopy(list, index, outgoing, index + v.length, list.length
+ - index);
return outgoing;
}
-
- static final public float[] splice(float list[],
- float v, int index) {
- float outgoing[] = new float[list.length + 1];
+ static final public float[] splice(final float list[], final float v,
+ final int index) {
+ final float outgoing[] = new float[list.length + 1];
System.arraycopy(list, 0, outgoing, 0, index);
outgoing[index] = v;
- System.arraycopy(list, index, outgoing, index + 1,
- list.length - index);
+ System.arraycopy(list, index, outgoing, index + 1, list.length - index);
return outgoing;
}
- static final public float[] splice(float list[],
- float v[], int index) {
- float outgoing[] = new float[list.length + v.length];
+ static final public float[] splice(final float list[], final float v[],
+ final int index) {
+ final float outgoing[] = new float[list.length + v.length];
System.arraycopy(list, 0, outgoing, 0, index);
System.arraycopy(v, 0, outgoing, index, v.length);
- System.arraycopy(list, index, outgoing, index + v.length,
- list.length - index);
+ System.arraycopy(list, index, outgoing, index + v.length, list.length
+ - index);
return outgoing;
}
-
- static final public String[] splice(String list[],
- String v, int index) {
- String outgoing[] = new String[list.length + 1];
+ static final public String[] splice(final String list[], final String v,
+ final int index) {
+ final String outgoing[] = new String[list.length + 1];
System.arraycopy(list, 0, outgoing, 0, index);
outgoing[index] = v;
- System.arraycopy(list, index, outgoing, index + 1,
- list.length - index);
+ System.arraycopy(list, index, outgoing, index + 1, list.length - index);
return outgoing;
}
- static final public String[] splice(String list[],
- String v[], int index) {
- String outgoing[] = new String[list.length + v.length];
+ static final public String[] splice(final String list[], final String v[],
+ final int index) {
+ final String outgoing[] = new String[list.length + v.length];
System.arraycopy(list, 0, outgoing, 0, index);
System.arraycopy(v, 0, outgoing, index, v.length);
- System.arraycopy(list, index, outgoing, index + v.length,
- list.length - index);
+ System.arraycopy(list, index, outgoing, index + v.length, list.length
+ - index);
return outgoing;
}
-
- static final public Object splice(Object list, Object v, int index) {
+ static final public Object splice(final Object list, final Object v,
+ final int index) {
Object[] outgoing = null;
- int length = Array.getLength(list);
+ final int length = Array.getLength(list);
// check whether item being spliced in is an array
if (v.getClass().getName().charAt(0) == '[') {
- int vlength = Array.getLength(v);
+ final int vlength = Array.getLength(v);
outgoing = new Object[length + vlength];
System.arraycopy(list, 0, outgoing, 0, index);
System.arraycopy(v, 0, outgoing, index, vlength);
@@ -4354,133 +4228,133 @@ public class PApplet extends Activity implements PConstants, Runnable {
//
- static public boolean[] subset(boolean list[], int start) {
+ static public boolean[] subset(final boolean list[], final int start) {
return subset(list, start, list.length - start);
}
- static public boolean[] subset(boolean list[], int start, int count) {
- boolean output[] = new boolean[count];
+ static public boolean[] subset(final boolean list[], final int start,
+ final int count) {
+ final boolean output[] = new boolean[count];
System.arraycopy(list, start, output, 0, count);
return output;
}
-
- static public byte[] subset(byte list[], int start) {
+ static public byte[] subset(final byte list[], final int start) {
return subset(list, start, list.length - start);
}
- static public byte[] subset(byte list[], int start, int count) {
- byte output[] = new byte[count];
+ static public byte[] subset(final byte list[], final int start,
+ final int count) {
+ final byte output[] = new byte[count];
System.arraycopy(list, start, output, 0, count);
return output;
}
-
- static public char[] subset(char list[], int start) {
+ static public char[] subset(final char list[], final int start) {
return subset(list, start, list.length - start);
}
- static public char[] subset(char list[], int start, int count) {
- char output[] = new char[count];
+ static public char[] subset(final char list[], final int start,
+ final int count) {
+ final char output[] = new char[count];
System.arraycopy(list, start, output, 0, count);
return output;
}
-
- static public int[] subset(int list[], int start) {
+ static public int[] subset(final int list[], final int start) {
return subset(list, start, list.length - start);
}
- static public int[] subset(int list[], int start, int count) {
- int output[] = new int[count];
+ static public int[] subset(final int list[], final int start, final int count) {
+ final int output[] = new int[count];
System.arraycopy(list, start, output, 0, count);
return output;
}
-
- static public float[] subset(float list[], int start) {
+ static public float[] subset(final float list[], final int start) {
return subset(list, start, list.length - start);
}
- static public float[] subset(float list[], int start, int count) {
- float output[] = new float[count];
+ static public float[] subset(final float list[], final int start,
+ final int count) {
+ final float output[] = new float[count];
System.arraycopy(list, start, output, 0, count);
return output;
}
-
- static public String[] subset(String list[], int start) {
+ static public String[] subset(final String list[], final int start) {
return subset(list, start, list.length - start);
}
- static public String[] subset(String list[], int start, int count) {
- String output[] = new String[count];
+ static public String[] subset(final String list[], final int start,
+ final int count) {
+ final String output[] = new String[count];
System.arraycopy(list, start, output, 0, count);
return output;
}
-
- static public Object subset(Object list, int start) {
- int length = Array.getLength(list);
+ static public Object subset(final Object list, final int start) {
+ final int length = Array.getLength(list);
return subset(list, start, length - start);
}
- static public Object subset(Object list, int start, int count) {
- Class> type = list.getClass().getComponentType();
- Object outgoing = Array.newInstance(type, count);
+ static public Object subset(final Object list, final int start,
+ final int count) {
+ final Class> type = list.getClass().getComponentType();
+ final Object outgoing = Array.newInstance(type, count);
System.arraycopy(list, start, outgoing, 0, count);
return outgoing;
}
//
- static public boolean[] concat(boolean a[], boolean b[]) {
- boolean c[] = new boolean[a.length + b.length];
+ static public boolean[] concat(final boolean a[], final boolean b[]) {
+ final boolean c[] = new boolean[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
return c;
}
- static public byte[] concat(byte a[], byte b[]) {
- byte c[] = new byte[a.length + b.length];
+ static public byte[] concat(final byte a[], final byte b[]) {
+ final byte c[] = new byte[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
return c;
}
- static public char[] concat(char a[], char b[]) {
- char c[] = new char[a.length + b.length];
+ static public char[] concat(final char a[], final char b[]) {
+ final char c[] = new char[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
return c;
}
- static public int[] concat(int a[], int b[]) {
- int c[] = new int[a.length + b.length];
+ static public int[] concat(final int a[], final int b[]) {
+ final int c[] = new int[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
return c;
}
- static public float[] concat(float a[], float b[]) {
- float c[] = new float[a.length + b.length];
+ static public float[] concat(final float a[], final float b[]) {
+ final float c[] = new float[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
return c;
}
- static public String[] concat(String a[], String b[]) {
- String c[] = new String[a.length + b.length];
+ static public String[] concat(final String a[], final String b[]) {
+ final String c[] = new String[a.length + b.length];
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
return c;
}
- static public Object concat(Object a, Object b) {
- Class> type = a.getClass().getComponentType();
- int alength = Array.getLength(a);
- int blength = Array.getLength(b);
- Object outgoing = Array.newInstance(type, alength + blength);
+ static public Object concat(final Object a, final Object b) {
+ final Class> type = a.getClass().getComponentType();
+ final int alength = Array.getLength(a);
+ final int blength = Array.getLength(b);
+ final Object outgoing = Array.newInstance(type, alength + blength);
System.arraycopy(a, 0, outgoing, 0, alength);
System.arraycopy(b, 0, outgoing, alength, blength);
return outgoing;
@@ -4488,109 +4362,103 @@ public class PApplet extends Activity implements PConstants, Runnable {
//
- static public boolean[] reverse(boolean list[]) {
- boolean outgoing[] = new boolean[list.length];
- int length1 = list.length - 1;
+ static public boolean[] reverse(final boolean list[]) {
+ final boolean outgoing[] = new boolean[list.length];
+ final int length1 = list.length - 1;
for (int i = 0; i < list.length; i++) {
outgoing[i] = list[length1 - i];
}
return outgoing;
}
- static public byte[] reverse(byte list[]) {
- byte outgoing[] = new byte[list.length];
- int length1 = list.length - 1;
+ static public byte[] reverse(final byte list[]) {
+ final byte outgoing[] = new byte[list.length];
+ final int length1 = list.length - 1;
for (int i = 0; i < list.length; i++) {
outgoing[i] = list[length1 - i];
}
return outgoing;
}
- static public char[] reverse(char list[]) {
- char outgoing[] = new char[list.length];
- int length1 = list.length - 1;
+ static public char[] reverse(final char list[]) {
+ final char outgoing[] = new char[list.length];
+ final int length1 = list.length - 1;
for (int i = 0; i < list.length; i++) {
outgoing[i] = list[length1 - i];
}
return outgoing;
}
- static public int[] reverse(int list[]) {
- int outgoing[] = new int[list.length];
- int length1 = list.length - 1;
+ static public int[] reverse(final int list[]) {
+ final int outgoing[] = new int[list.length];
+ final int length1 = list.length - 1;
for (int i = 0; i < list.length; i++) {
outgoing[i] = list[length1 - i];
}
return outgoing;
}
- static public float[] reverse(float list[]) {
- float outgoing[] = new float[list.length];
- int length1 = list.length - 1;
+ static public float[] reverse(final float list[]) {
+ final float outgoing[] = new float[list.length];
+ final int length1 = list.length - 1;
for (int i = 0; i < list.length; i++) {
outgoing[i] = list[length1 - i];
}
return outgoing;
}
- static public String[] reverse(String list[]) {
- String outgoing[] = new String[list.length];
- int length1 = list.length - 1;
+ static public String[] reverse(final String list[]) {
+ final String outgoing[] = new String[list.length];
+ final int length1 = list.length - 1;
for (int i = 0; i < list.length; i++) {
outgoing[i] = list[length1 - i];
}
return outgoing;
}
- static public Object reverse(Object list) {
- Class> type = list.getClass().getComponentType();
- int length = Array.getLength(list);
- Object outgoing = Array.newInstance(type, length);
+ static public Object reverse(final Object list) {
+ final Class> type = list.getClass().getComponentType();
+ final int length = Array.getLength(list);
+ final Object outgoing = Array.newInstance(type, length);
for (int i = 0; i < length; i++) {
Array.set(outgoing, i, Array.get(list, (length - 1) - i));
}
return outgoing;
}
-
-
//////////////////////////////////////////////////////////////
// STRINGS
-
/**
* Remove whitespace characters from the beginning and ending
* of a String. Works like String.trim() but includes the
* unicode nbsp character as well.
*/
- static public String trim(String str) {
+ static public String trim(final String str) {
return str.replace('\u00A0', ' ').trim();
}
-
/**
* Trim the whitespace from a String array. This returns a new
* array and does not affect the passed-in array.
*/
- static public String[] trim(String[] array) {
- String[] outgoing = new String[array.length];
+ static public String[] trim(final String[] array) {
+ final String[] outgoing = new String[array.length];
for (int i = 0; i < array.length; i++) {
outgoing[i] = array[i].replace('\u00A0', ' ').trim();
}
return outgoing;
}
-
/**
* Join an array of Strings together as a single String,
* separated by the whatever's passed in for the separator.
*/
- static public String join(String str[], char separator) {
+ static public String join(final String str[], final char separator) {
return join(str, String.valueOf(separator));
}
-
/**
* Join an array of Strings together as a single String,
* separated by the whatever's passed in for the separator.
@@ -4602,16 +4470,17 @@ public class PApplet extends Activity implements PConstants, Runnable {
* String list = join(stuff, ", ");
* // list is now "apple, bear, cat"
*/
- static public String join(String str[], String separator) {
- StringBuffer buffer = new StringBuffer();
+ static public String join(final String str[], final String separator) {
+ final StringBuffer buffer = new StringBuffer();
for (int i = 0; i < str.length; i++) {
- if (i != 0) buffer.append(separator);
+ if (i != 0) {
+ buffer.append(separator);
+ }
buffer.append(str[i]);
}
return buffer.toString();
}
-
/**
* Split the provided String at wherever whitespace occurs.
* Multiple whitespace (extra spaces or tabs or whatever)
@@ -4627,11 +4496,10 @@ public class PApplet extends Activity implements PConstants, Runnable {
* splitTokens("a\tb") -> { "a", "b" }
* splitTokens("a \t b ") -> { "a", "b" }
*/
- static public String[] splitTokens(String what) {
+ static public String[] splitTokens(final String what) {
return splitTokens(what, WHITESPACE);
}
-
/**
* Splits a string into pieces, using any of the chars in the
* String 'delim' as separator characters. For instance,
@@ -4646,9 +4514,9 @@ public class PApplet extends Activity implements PConstants, Runnable {
*
* i.e. splitTokens("a | b", WHITESPACE + "|"); -> { "a", "b" }
*/
- static public String[] splitTokens(String what, String delim) {
- StringTokenizer toker = new StringTokenizer(what, delim);
- String pieces[] = new String[toker.countTokens()];
+ static public String[] splitTokens(final String what, final String delim) {
+ final StringTokenizer toker = new StringTokenizer(what, delim);
+ final String pieces[] = new String[toker.countTokens()];
int index = 0;
while (toker.hasMoreTokens()) {
@@ -4657,7 +4525,6 @@ public class PApplet extends Activity implements PConstants, Runnable {
return pieces;
}
-
/**
* Split a string into pieces along a specific character.
* Most commonly used to break up a String along a space or a tab
@@ -4669,55 +4536,57 @@ public class PApplet extends Activity implements PConstants, Runnable {
* one can split on tab characters, but maintain the column
* alignments (of say an excel file) where there are empty columns.
*/
- static public String[] split(String what, char delim) {
+ static public String[] split(final String what, final char delim) {
// do this so that the exception occurs inside the user's
// program, rather than appearing to be a bug inside split()
- if (what == null) return null;
- //return split(what, String.valueOf(delim)); // huh
+ if (what == null) {
+ return null;
+ //return split(what, String.valueOf(delim)); // huh
+ }
- char chars[] = what.toCharArray();
+ final char chars[] = what.toCharArray();
int splitCount = 0; //1;
- for (int i = 0; i < chars.length; i++) {
- if (chars[i] == delim) splitCount++;
+ for (final char c : chars) {
+ if (c == delim) {
+ splitCount++;
+ }
}
// make sure that there is something in the input string
//if (chars.length > 0) {
- // if the last char is a delimeter, get rid of it..
- //if (chars[chars.length-1] == delim) splitCount--;
- // on second thought, i don't agree with this, will disable
+ // if the last char is a delimeter, get rid of it..
+ //if (chars[chars.length-1] == delim) splitCount--;
+ // on second thought, i don't agree with this, will disable
//}
if (splitCount == 0) {
- String splits[] = new String[1];
+ final String splits[] = new String[1];
splits[0] = new String(what);
return splits;
}
//int pieceCount = splitCount + 1;
- String splits[] = new String[splitCount + 1];
+ final String splits[] = new String[splitCount + 1];
int splitIndex = 0;
int startIndex = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == delim) {
- splits[splitIndex++] =
- new String(chars, startIndex, i-startIndex);
+ splits[splitIndex++] = new String(chars, startIndex, i - startIndex);
startIndex = i + 1;
}
}
//if (startIndex != chars.length) {
- splits[splitIndex] =
- new String(chars, startIndex, chars.length-startIndex);
+ splits[splitIndex] = new String(chars, startIndex, chars.length
+ - startIndex);
//}
return splits;
}
-
/**
* Split a String on a specific delimiter. Unlike Java's String.split()
* method, this does not parse the delimiter as a regexp because it's more
* confusing than necessary, and String.split() is always available for
* those who want regexp.
*/
- static public String[] split(String what, String delim) {
- ArrayList