Saturday, October 25, 2014

JAVA self portrait project code (processing)

import processing.core.*; 

import java.util.Random;


public class SelfPort extends PApplet { //declare the class extending PApplet (imp)

Random randy= new Random(); //create a random object (imp)
PImage photoUL, photoUR, photoDL, photoDR; // PImage (imp)
int level1 = 4; // create an integer for smmoth() level (opt)
public void setup(){ // setup (imp)
size(1000,1000); // setup the size (mid)
photoUL = loadImage("321.jpg"); // load image - put the name of the image (imp)
photoUL.filter(GRAY); // filter the image (opt)
photoUR = loadImage("321.jpg"); //*
photoUR.filter(POSTERIZE, 4); //*
photoDL = loadImage("321.jpg"); //*
photoDL.filter(THRESHOLD); //*
photoDR = loadImage("321.jpg"); //*
photoDR.filter(ERODE); //*

background(255); // background color (mid)
noStroke(); // no stroke - check out the processing.org (opt)
noLoop(); // no loop - check out the processing.org (opt)
smooth(level1); // anti-aliasing, I put the level value that I set by my self (opt)
}
public void draw(){ // draw (imp)
int max = 255; // range value that I'm going to use later (opt)
int min = 100; // range value that I'm going to use later (opt)
for (int k = 0; k < 60000; k++) { //for loop, repeating 60000 times to make ellipses to pointillize the image (imp)
int random1 = randy.nextInt(8); // random(8) to randomize the ellipse size(opt)
int random2 = randy.nextInt(max); // random value  
int x = PApplet.parseInt(random(photoUL.width)); //random x coordinate(imp)
int y = PApplet.parseInt(random(photoUL.height)); //random y coordinate(imp)
int pixx = photoUL.get(x, y); // get the color value(imp)
fill(pixx,random2); //fill the ellipse with certain color(imp)
ellipse(x+500, y+500, random1, random1); //create an ellipse at (500,500)~(potowidth+500,photoheight+500) location(imp)
}
for (int k = 0; k < 150000; k++) { // for loop, repeating 150000 times to make rectangles to mosaic the image (imp)
int random = randy.nextInt(max-min+1)+min; // random value (opt)
int x = PApplet.parseInt(random(photoDR.width)); //random x coordinate (imp)
int y = PApplet.parseInt(random(photoDR.height)); //random y coordinate (imp)
int pixx = photoDR.get(x, y); //get the color value (imp)
fill(pixx,random); //fill the rectangle with certain color (imp)
rect(x,y+500,15,15); //create a rectangle at the (0,500)~(photowidth,photoheight+500) location (imp)
}
image(photoUR,500,0); // put the image at (500,0)(imp)
image(photoDL,0,0); // put the image at (0,0) (imp)
}
static public void main(String[] args) { //main (imp)
PApplet.main(new String[] { "Myface" }); //run the program, the name of the portrait is Myface (imp)
}
}




Notes

1. Copy and paste to your compiler

2. You might have to modify some file names and file sizes that are already assigned in the program ( I set the portrait size as 1000*1000, and put 500*500 image for 4 times)(you might want to convert your image size if it's irregular, such as 342*213...)

3. Check out processing.org for some methods

4. You need core API to run this program

5. (imp)-important, (mid) - mid important, (opt) - options