Package pthreading

Class PThread


  • public abstract class PThread
    extends java.lang.Object
    Extend this class, overriding the calc() and draw() methods with your own code, then use a PThreadManager to run the thread.

    Prefix every call to a Processing draw method with g -- for example: g.rect(10,10,10,10);

    Refer to any PApplet variable by prefixing it with p -- for example: p.mousePressed.

    Author:
    micycle1
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected processing.core.PGraphics g
      The PGraphics object the thread draws into.
      protected processing.core.PApplet p
      Exposed so that any subclasses can access PApplet variables (such as mouse coords).
    • Constructor Summary

      Constructors 
      Constructor Description
      PThread​(processing.core.PApplet p)
      Constructs a thread.
    • Method Summary

      Modifier and Type Method Description
      protected void calc()
      An optional override (you can do calculation-related code in draw(), but putting it here may make more sense).
      void disableTiming()
      Disables the collection of timing information (draw and calc time).
      protected abstract void draw()
      The heart of a PThread.
      void enableTiming()
      Enables the collection of timing information (draw and calc time).
      float getCalcFPS()
      Returns time taken for the thread's calc() loop to execute.
      float getDrawFPS()
      Returns time taken for the thread's draw() loop to execute.
      protected void setup()
      An optional override.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • g

        protected processing.core.PGraphics g
        The PGraphics object the thread draws into.
      • p

        protected final processing.core.PApplet p
        Exposed so that any subclasses can access PApplet variables (such as mouse coords).
    • Constructor Detail

      • PThread

        public PThread​(processing.core.PApplet p)
        Constructs a thread.

        NOTE: Merely instantiating a thread will not run it. Add it to a PThreadManager for it to execute.

        Parameters:
        p - Parent PApplet
    • Method Detail

      • setup

        protected void setup()
        An optional override. Called at instantiation. Use this method set PGraphics' settings (e.g. g.colorMode(PApplet.HSB, 360, 100, 100); ), etc.
      • calc

        protected void calc()
        An optional override (you can do calculation-related code in draw(), but putting it here may make more sense). This code will be executed in a thread.

        Putting calculation-related code here, rather than in draw(), is useful when the 'unthread drawing' flag is true, so that draw time and calc time can be compared.

        Internally, this method is called before draw().

        See Also:
        draw()
      • draw

        protected abstract void draw()
        The heart of a PThread. Override this method with code that should be executed in a thread. Prefix calls to processing draw functions with g. (eg. g.ellipse(50, 50, 50, 50).

        Internally, this method is called after calc().

        See Also:
        calc()
      • getDrawFPS

        public final float getDrawFPS()
        Returns time taken for the thread's draw() loop to execute. Can be used with getCalcFPS() to determine if a thread is calculation bound or draw-call bound.
        Returns:
        draw() execution time (milliseconds)
      • getCalcFPS

        public final float getCalcFPS()
        Returns time taken for the thread's calc() loop to execute. Can be used with getDrawFPS() to determine if a thread is calculation bound or draw-call bound.
        Returns:
        calc() execution time (milliseconds)
      • enableTiming

        public final void enableTiming()
        Enables the collection of timing information (draw and calc time). May incur a slight overhead. By default, timing information is not enabled.
        See Also:
        disableTiming(), getDrawFPS(), getCalcFPS()
      • disableTiming

        public final void disableTiming()
        Disables the collection of timing information (draw and calc time). By default, timing information is not enabled.
        See Also:
        enableTiming()