import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class tracar_visual extends PApplet {

//* Tra\u00e7ar Visual
//* Tiago Viana n\u00ba31844
//* Descri\u00e7\u00e3o - O meu prop\u00f3sito era criar uma fun\u00e7\u00e3o que permitisse ao programa replicar imagens escolhidas com tra\u00e7os de cor, dando a aparencia de uma reprodu\u00e7\u00e3o pincelada da imagem original, cujo resultado gr\u00e1fico varia ligeiramente sempre devido aos algoritmos aleat\u00f3rios, permitindo ao utilizador registar sempre o seu resultado e garantindo que cada resultado \u00e9 \u00fanico.
//* Funcionalidade - Os bot\u00f5es do teclado 1 a 0 selecionam as respectivas imagens, enquanto a tecla de Espa\u00e7o grava a imagem apresentada naquele instante

String[] imgNames = {"im1.jpg", "im2.jpg", "im3.jpg", "im4.jpg", "im5.jpg", "im6.jpg", "im7.jpg", "im8.jpg", "im9.jpg", "im10.jpg"};
PImage img;
PImage logo;
int imgIndex = 0;
int modo=0;


public void nextImage() {
  background(255);
  loop();
  frameCount = 0;
  
  img = loadImage(imgNames[imgIndex]);
  img.loadPixels();
  
  imgIndex += 1;
  if (imgIndex >= imgNames.length) {
    imgIndex = 0;
  }
}


public void callImage(int n) {
  background(255);
  loop();
  frameCount = 0;
  
  img = loadImage(imgNames[n]);
  img.loadPixels();
  
  imgIndex += 1;
  if (imgIndex >= imgNames.length) {
    imgIndex = 0;
  }
}


public void paintStroke(float strokeLength, int strokeColor, int strokeThickness) {
  float stepLength = strokeLength;
  
  
  float tangent1 = 0;
  float tangent2 = 0;
  
  float odds = random(1.0f);
  
  if (odds < 0.7f) {
    tangent1 = random(-strokeLength, strokeLength);
    tangent2 = random(-strokeLength, strokeLength);
  } 
  
  
  noFill();
  stroke(strokeColor);
  strokeWeight(strokeThickness);
  curve(tangent1, -stepLength*2, 0, -stepLength, 0, stepLength, tangent2, stepLength*2);
  
  int z = 1;
  
  
  for (int num = strokeThickness; num > 0; num --) {
    float offset = random(-50, 25);
    int newColor = color(red(strokeColor)+offset, green(strokeColor)+offset, blue(strokeColor)+offset, random(100, 255));
    
    stroke(newColor);
    strokeWeight((int)random(0, 3));
    curve(tangent1, -stepLength*2, z-strokeThickness/2, -stepLength*random(0.9f, 1.1f), z-strokeThickness/2, stepLength*random(0.9f, 1.1f), tangent2, stepLength*2);
    
    z += 1;
  }
}


public void setup() {
  size(1600,1000);
  logo= loadImage("logo.png");
  nextImage();
  
}


public void draw() {
  

  if (frameCount > 600){ image(logo, 1450, 900, width/12, height/12);
  }
  translate(width/2, height/2);
  
  int index = 0;
  
  for (int y = 0; y < img.height; y+=1) {
    for (int x = 0; x < img.width; x+=1) {
      int odds = (int)random(20000);
      
      if (odds < 1) {
        int pixelColor = img.pixels[index];
        pixelColor = color(red(pixelColor), green(pixelColor), blue(pixelColor), 100);
        
        pushMatrix();
        translate(x-img.width/2, y-img.height/2);
        rotate(radians(random(-90, 90)));
        
        if (frameCount < 20) {
         
          paintStroke(random(150, 250), pixelColor, (int)random(10, 25));
        } else if (frameCount < 50) {
        
          paintStroke(random(75, 125), pixelColor, (int)random(8, 12));
        } else if (frameCount < 300) {
         
          paintStroke(random(30, 60), pixelColor, (int)random(3, 7));
        } else if (frameCount < 350) {
         
          paintStroke(random(5, 20), pixelColor, (int)random(5, 15));
        } else if (frameCount < 600) {
          
          paintStroke(random(1, 10), pixelColor, (int)random(1, 7));
        }
        
        popMatrix();
      }
      
      index += 1;
    }
  }
  
  if (frameCount > 600) {
    noLoop();
  }
}


  public void keyPressed() {
  if(key=='0'){
    callImage(0);
  } 
  else if(key=='1'){
    callImage(1);
  }
  else if(key=='2'){
    callImage(2);
  }
  else if(key=='3'){
    callImage(3);
  }
  else if(key=='4'){
    callImage(4);
  }
  else if(key=='5'){
    callImage(5);
  }
  else if(key=='6'){
    callImage(6);
  }
  else if(key=='7'){
    callImage(7);
  }
  else if(key=='8'){
    callImage(8);
  }
  else if(key=='9'){
    callImage(9);
  }
  else if(key==' '){
    saveFrame();
  }
  }

  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "--full-screen", "--bgcolor=#666666", "--stop-color=#cccccc", "tracar_visual" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}
