We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
alpha object (Read 583 times)
alpha object
Oct 12th, 2008, 10:46pm
 
/////////////////////////////////////////////////////////////////////

package com;

import java.lang.Long;

// calculates values from 0 to 1. an alpha object is not the transparency but an
// object that does nothing more than counting from 0 to 1, or, more technically
// spoken, is an object providing common methods for converting a time
// value into an alpha value (value from 0 to 1).
// this alpha object can do start() starts the timer, stop() stops it and resume()
// continues it after a pausing.
// the function value() returns the current position between 0 and 1.
// you can, for example, pass in value to some penner easing functions.
// parameters are a time period passed in as a value of milliseconds. if you would like to use the class,
// please consider that it uses System.nanoTime(), which is available for java 1.5 >
// so it wont work for java 1.4. for not getting any error messages for usage of
// 1.5 syntax, you also have to keep this .java at the end of the filename, otherwise
// p5 will moan..
// you will also need the Timer class. i posted it below that one here..
// Timer class needs to be instantiated like Time.getInstance(). then it has this static
// method to call from anywhere in your application. another thing required is
// a static reference to the PApplet running. i wrote this in eclipse and did not
// like to pass in the PApplet each time instantiating a class that needs a loop
// like event, so i created a static instance of it. you need to do this or add another
// constructor that can handle a passed in PApplet, and then change alle occurences
// of App.applet.registerDraw().since its beta code, its not my promise to be good to you..
// but it should work fine.
// again, same thing: dont change the file name extension (yah thats the word), otherwise
// System.nanoTime() wont work, at least i read it somewhere in the release docs of p5.
// there is, actually, something like this as part of j3d package. but its bad. i dont like
// it. its rather good for you wanting to cycle some movements, like back-and-forth-and-back
// and-so-on. for some strange reason, it did not work out too well for me, each time i tried
// to overwrite an instance of the class, it returned 1.0, even though i just overwrote the
// last instance... anyways, have fun! i guess many of you did wait some time to get such
// a great animation class. for the flash of you guys, certainly know about Tweener for flash.
// yeeeha. event handling seems to be kinda not so great in java. the nice thing about as
// is you can just listen from any kind of object to any kind of other object there. this
// does not work out too well. you may be confused by me using the PApplet.registerDraw method.
//
public class PAlpha {

private float _value; // value from 0 to 1

private float _startTime;

private float _endTime;

private float _passedTime;

private float _stopTime=0;

private float _duration;

private float _timeScale;

private boolean _finished = false;


// duration in milliseconds

public PAlpha(float duration) {  


_duration = duration;


_timeScale = 1000f / duration;


// start();

}

public float time(){


return Time.ms();

}

public float passedTime() {


return time() - _startTime - _stopTime;

}

public void draw() {


_value =  ( ( _duration - ( _duration - passedTime() ) ) / 1000f ) * _timeScale;


System.out.println( _value );


if(_value >= 1) { _value = 1; finish(); System.out.println("finish();"); }  

}

public void start() {


_finished = false;


_startTime = time();


_stopTime = 0.0f;


_endTime = _startTime + _duration;


App.applet.registerDraw(this);

}

public void stop() {


_stopTime = time();


App.applet.unregisterDraw(this);

}

public void finish() {


_finished = true;


App.applet.unregisterDraw(this);

}

public boolean resume() {


if(_finished) return false;


_stopTime = time() - _stopTime;


_endTime = passedTime();


App.applet.registerDraw(this);


return true;

}

public float value() {


return (float)_value;

}
}


/////////////////////////////////////////////////////////////////////


package com;

// serves with system time
public class Time {

 public static Time instance;
 public static long startTime;
 public static Long curTime;
 
 Time() {
   startTime = (long)System.nanoTime();
 }
 public static Time getInstance() {
   if(instance == null) { instance = new Time(); return instance; }
   else return instance;
 }
 public static Long getTime() {
   curTime = (long)System.nanoTime() - startTime;
   return curTime;
 }
 public static float floatTime() {

 return getTime().floatValue();
 }
 public static float ms() {

 return floatTime() / 1000000f;
 }
 public static float s() {

 return floatTime() / 1000000000f;
 }
 
}
Re: alpha object
Reply #1 - Oct 14th, 2008, 6:20am
 
hi christian,
just curious, is your post considered to be a suggestion for a contributed library i assume so since you have packaged your classes PAlpha and Time. if you are interested in creating a processing library, you may  take a look at the howto.txt file in the libraries folder of your processing app or at dev.processing.org/libraries. regarding the naming convention, contributed libraries and classes should not be prefixed with "P" since this should only be reserved for classes inside processing.core and other associated classes.
hope this helps, best.
andreas
Page Index Toggle Pages: 1