Is there a way to track the life of each flower, and no longer draw those which have "died".
This would stop the effect in your version, where all of a sudden all the flowers and stems stop being drawn.
Thanks,
Joni
public class Campo{
Flor[] flores = new Flor[100];
public int contFlores =0 ;
public int tamanio = 10;
color colorTallo;
Campo(){
println("Campo >> init");
colores.initPalette(2); //color verde
colorTallo = colores.devuelveColores(3);
println("colorTallo:"+hex(colorTallo));
initFlores();
}
void draw(){
drawFlores();
}
void drawFlores(){
for(int i=0;i<contFlores;i++) {
flores[i].go(tamanio,colores.devuelveColores());
}
}
void initFlores(){
colores.initPalette();
flores[contFlores] = new Flor(contFlores,(int) random(tamanio),colores.devuelveColores(),colorTallo);
contFlores = (contFlores + 1) % flores.length; // note the use of modulo
}
}
/* clase colores. ALba G. Corral.
Barcelona - marzo 2008
objeto gestor de colores.
Carga desde el directorio ruta diferentes im�genes en un array de colores.
*/
public class Colores {
private PImage b;
private int maxpal = 100;
private int numpal = 0;
private int numPaletas = 0;
private File dir ;
private colorUtils utils;
private String[] listado;
private boolean isDir = false; //estamos sacando las imagenes del directorio?
private color[] imageColors = new color[maxpal];
int MAXPALETAS = 100;//numero m�ximo de paletas de colores que cargamos con addPaleta
private int paletaActiva = 0;
//metodo constructor
Colores(int _maxpal){
listado = new String[MAXPALETAS];
maxpal = _maxpal;
utils = new colorUtils();
}
/*METODOS PUBLICOS-----------------------------------------------------------------*/
//a�ade una imagen al set de Colores
public void addPaleta(String _path){
listado[numPaletas] = _path;
takecolor(listado[paletaActiva]);
numPaletas++;
//println("Colores >> addPaleta: "+_path +" total paletas:"+numPaletas);
}
//a�ade las imagenes de un directorio al set de Colores
public void addPaletaDir(String _path){
isDir = true;
listado = leerDirectorio(_path);
numPaletas= listado.length;
takecolor(listado[paletaActiva]);
// println("Colores >> addPaletaDir: "+_path +" total paletas:"+numPaletas);
}
//inicia una paleta de colores aleatoria del listado.
public void initPalette(){
String rutaRandom= listado[int (random(numPaletas))];
println("random paleta:"+rutaRandom);
takecolor(rutaRandom);
}
//inicia una paleta de colores numero num del listado.
public void initPalette(int num){
paletaActiva = num;
takecolor(listado[num]);
}
//visualiza la imagen
public void Imagen(){
image(b,0,0);
}
//funcion que devuelve un color aleatorio de la paleta cargada dinamicamente
public color devuelveColores(){
return imageColors[int(random(numpal))];
}
//funcion que devuelve el color en primera posicion
public color devuelveColores(int n){
return imageColors[n];
}
//funcion que devuelve elcolor en primera posicion
public color shade(color c, float val){
return utils.shadeColor( c, val);
}
//devuelve el numero de colores de la paleta activa
public int getLength(){
return numpal;
}
/*METODOS PRIVADOS-----------------------------------------------------------------*/
private String [] leerDirectorio(String _path){
if (isDir) {
dir = new File(dataPath(_path));
}
else
{
dir = new File(_path);
}
String[] children = dir.list(new PicFilter());
if (children == null) {
//println("******* error en la lectura del directorio >>"+_path);
}
else {
//println("******* list >>"+_path +"children.length:"+children.length);
for (int i=0; i<children.length; i++) {
children[i] = _path + children[i];
println("******* paleta["+i+"] >>"+children[i]);
}
}
return children;
}
void takecolor(String fn) {
//println("Colores >> takecolor-----PALETA:"+fn);
if (isDir) {
b = loadImage(dataPath(fn));
}
else
{
b = loadImage(fn);
}
numpal=0;
for (int x=0;x<b.width;x++){
for (int y=0;y<b.height;y++) {
color c = b.get(x,y);
boolean exists = false;
for (int n=0;n<numpal;n++) {
if (c==imageColors[n]) {
exists = true;
break;
}
}
if (!exists) {
// add color to pal
if (numpal<maxpal) {
imageColors[numpal] = c;
// //println("color "+numpal+" " +hex(c));
numpal++;
}
}
}
}
//println("Colores >> -----numpal:"+numpal);
}
}
/**
* utilizades de colores
*/
class colorUtils
{
public void colorUtils(){
////println("-----init colorUtils.");
}
public color shadeColor(color c, float val)
{
return color(red(c)+val,green(c)+val,blue(c)+val, alpha(c));
}
}
/**
* filter out PICS .Changed by Alba G. Corral
*/
class PicFilter implements FilenameFilter
{
/**
* Select only *.Pic files.
*
* @param dir the directory in which the file was found.
*
* @param name the name of the file
*
* @return true if and only if the name should be
* included in the file list; false otherwise.
*/
public boolean accept ( File dir, String name )
{
if ( new File( dir, name ).isDirectory() )
{
return false;
}
name = name.toLowerCase();
return (name.endsWith( ".png" ) || name.endsWith( ".jpg" ) || name.endsWith( ".gif" )) ;
}
}
//clase que controla el fade in y el fade out
class Fade{
float fadeTime;
boolean fadeIn=false,fadeOut=false,on=false;
Fade(){
println("init Fade");
}
public void render(){
if(fadeIn) {
if(fadeTime<1) fadeTime=fadeTime+1.0/160.0;
else { // done fading in
fadeTime=1;
on=true;
fadeIn=false;
}
}
if(fadeOut) {
if(fadeTime>0) fadeTime=fadeTime-1.0/160.0;
else { // done fading in
fadeTime=0;
on=false;
fadeOut=false;
}
}
if (on==true) fade();
}
public float getFade(){
return fadeTime;
}
public void setFade(){
if(on==false) {
fadeIn=true;
on = true ;
}
else {
fadeOut=true;
on =false ;
}
}
private void fade(){
noStroke();
fill(255,fadeTime*10);
rect(0,0,width,height);
}
}
class Flor{
float x,y; // x and y location
//
int angle = 0;
int tam,id ;
color colores;
Tallo tallo;
color colorTallo;
Flor(int _id,int _tam, color _colores,color _colorTallo){
this.id = _id;
this.colores = _colores;
this.colorTallo = _colorTallo;
this.tam = _tam;
println("Flor init , id:"+id+", tamanio:"+tam);
tallo= new Tallo(colorTallo);
}
void go(int _tam, color _colores) {
this.tam = _tam;
this.colores = _colores;
if (tallo.ends){
render(tallo.getX(),tallo.getY());
}
else
{
tallo.draw();
}
}
// Dibuja la flor
void render(float x,float y) {
angle += 10;
if (angle < 90){
smooth();
fill(255,150);
ellipse(x,y,16,16);
float val = cos(radians(angle)) * (tam*2);
for (int a = 0; a < 360; a += 50) {
float xoff = cos(radians(a)) * val;
float yoff = sin(radians(a)) * val;
fill(colores);
noStroke();
ellipse(x + xoff, y + yoff, val / 2, val / 2);
}
}
}
}
class Tallo{
Motion m;
float Angle = PI, Strokeweight = 1, longitud;
public boolean ends = false;
color colorTallo ;
Tallo(color _colorTallo){
colorTallo = _colorTallo;
iniciaTallo();
println("Tallo >> init");
}
void draw(){
drawTallo();
}
private void iniciaTallo(){
m = new Motion(random(width),random((height-100),(height)));
Angle = PI;
Strokeweight = 1;
longitud = random(20,height-200);
}
private void drawTallo(){
// println("Tallo > draw");
if ((int) m.getY() > longitud){
Angle += radians(random(-3,3));
Strokeweight += random(-1,1);
m.moveDir(Angle,random(1,6));
m.move();
stroke(colorTallo);
strokeWeight(abs(Strokeweight));
line(m.getPX(),m.getPY(),m.getX(),m.getY());
}
else
{
campo.initFlores();
ends = true;
}
}
/* funciones p�blicas*/
public float getX(){
return m.getX();
}
public boolean isEnd(){
return ends;
}
public float getY(){
return m.getY();
}
}
/* montseny
created by Alba G. Corral - 2008
*/
import seltar.motion.*;
import processing.pdf.*;
import processing.video.*;
Campo campo;
Colores colores;
Fade fade;
boolean hazFade= false;
void setup()
{
size(925, 475);
background(255); //limpia la pantalla a color negro
// noCursor(); //quitamos el cursor
cursor(CROSS);
fade = new Fade();
//inicializacion de la clase colores
colores = new Colores(100);
colores.addPaleta("Gold_On_Black_1.2.png");
colores.addPaleta("Gold_On_Black_1.3.png");
colores.addPaleta("november0.png");
colores.addPaleta("painted_black_and_re.png");
campo=new Campo();
}
void draw()
{
fade.render();
smooth();
campo.draw();
}
void keyPressed() {
if(keyPressed){
println("key:"+key);
switch (key){
case 'f':
fade.setFade();
break;
case 'a':
colores.initPalette();
break;
case 'r':
campo.contFlores=0;
break;
case 'q':
//endRecord();
exit();
break;
case '+':
constrain(campo.tamanio++,0,width);
println("tamanio:"+campo.tamanio);
break;
case '-':
constrain(campo.tamanio--,0,width);
break;
}
}
}
//------------------------------------------------------------------
void mousePressed() {
campo.initFlores();
}
class Flor{
float x,y; // x and y location
//
int angle = 0;
int tam,id ;
color colores;
Tallo tallo;
color colorTallo;
Flor(int _id,int _tam, color _colores,color _colorTallo){
this.id = _id;
this.colores = _colores;
this.colorTallo = _colorTallo;
this.tam = _tam;
println("Flor init , id:"+id+", tamanio:"+tam);
tallo= new Tallo(colorTallo);
}
void go(int _tam, color _colores) {
this.tam = _tam;
this.colores = _colores;
if (tallo.ends){
render(tallo.getX(),tallo.getY());
}
else
{
tallo.draw();
}
}
// Dibuja la flor
void render(float x,float y) {
angle += 10;
if (angle < 90){
smooth();
fill(255,150);
ellipse(x,y,16,16);
float val = cos(radians(angle)) * (tam*2);
for (int a = 0; a < 360; a += 50) {
float xoff = cos(radians(a)) * val;
float yoff = sin(radians(a)) * val;
fill(colores);
noStroke();
ellipse(x + xoff, y + yoff, val / 2, val / 2);
}
}
}
}
class Tallo{
Motion m;
float Angle = PI, Strokeweight = 1, longitud;
public boolean ends = false;
color colorTallo ;
Tallo(color _colorTallo){
colorTallo = _colorTallo;
iniciaTallo();
println("Tallo >> init");
}
void draw(){
drawTallo();
}
private void iniciaTallo(){
m = new Motion(random(width),random((height-100),(height)));
Angle = PI;
Strokeweight = 1;
longitud = random(20,height-200);
}
private void drawTallo(){
// println("Tallo > draw");
if ((int) m.getY() > longitud){
Angle += radians(random(-3,3));
Strokeweight += random(-1,1);
m.moveDir(Angle,random(1,6));
m.move();
stroke(colorTallo);
strokeWeight(abs(Strokeweight));
line(m.getPX(),m.getPY(),m.getX(),m.getY());
}
else
{
campo.initFlores();
ends = true;
}
}
/* funciones p�blicas*/
public float getX(){
return m.getX();
}
public boolean isEnd(){
return ends;
}
public float getY(){
return m.getY();
}
}
Montseny | Alba G. Corral | 2008
Click mouse to add flowers.
KEYS
'a' : change flower color
'+' : increase size flower
'-' : decrease size flower
'f': fade on / off .
used lib. seltar motion