Java I
Jonah Warrenjonah@parsons.edu
http://a.parsons.edu/~java2004
Lab 7
Code from class...
SIN FUNCTION CONTROL YPOSITION
float counter;
void setup()
{
}
void loop()
{
background(255);
counter+=0.1;
println(sin(counter));
rectMode(CENTER_DIAMETER);
rect(50, sin(counter)*50+50, 5, 5);
}
|
SIN AND COS FUNCTION CONTROL POSITION
float counter;
void setup()
{
}
void loop()
{
background(255);
counter+=0.1;
println(sin(counter));
rectMode(CENTER_DIAMETER);
rect(cos(counter)*50+50, sin(counter)*50+50, 5, 5);
}
|
SIN FOR STROBEBLOCK
strobeBlock sBlock;
void setup() {
size(200, 200);
sBlock = new strobeBlock(75,75,10);
}
void loop() {
background(255);
sBlock.update();
sBlock.draw();
}
class strobeBlock {
float xPos;
float yPos;
float speed;
float col;
float counter;
strobeBlock(float xp, float yp, float sp) {
xPos = xp;
yPos = yp;
speed = sp;
col = 0;
counter=0;
}
void draw() {
noStroke();
fill(col);
rect(xPos, yPos, 50, 50);
}
void update() {
float osc;
counter += 0.01;
osc = (sin(counter))+1;
osc = osc / 2;
col = osc * 255;
}
}
|
DRAWING TOOL... CONNECT THE DOTS
float prevX;
float prevY;
void setup() {
size(200, 200);
background(255);
}
void loop() {
noFill();
rect(0,0,width-1,height-1);
if (mousePressed) {
if (prevX != 0) {
line(prevX, prevY, mouseX, mouseY);
}
prevX = mouseX;
prevY = mouseY;
}
}
|
DRAWING TOOL... FUNNY RECTS
float prevX;
float prevY;
void setup() {
size(200, 200);
background(255);
}
void loop() {
noFill();
rect(0,0,width-1,height-1);
if (mousePressed) {
if (prevX != 0) {
rect(prevX, prevY, abs(prevX-mouseX), abs(prevY-mouseY));
}
prevX = mouseX;
prevY = mouseY;
}
}
|
DRAWING TOOL... BARBED WIRE
float prevX;
float prevY;
void setup() {
size(200, 200);
background(255);
}
void loop() {
float rand1 = random(10);
float rand2 = random(10);
noFill();
rect(0,0,width-1,height-1);
if (mousePressed) {
if (prevX != 0) {
line(prevX, prevY, mouseX + rand1, mouseY + rand2);
}
prevX = mouseX + rand1;
prevY = mouseY + rand2;
}
}
|
DRAWING TOOL... CURLIES
float prevX;
float prevY;
float counter;
void setup()
{
size(300,300);
background(255);
}
void loop()
{
noFill();
rect(0,0,width-1,height-1);
if (mousePressed) {
counter+=0.8;
if (prevX != 0) {
line(prevX, prevY, cos(counter)*10 + mouseX, sin(counter)*10 + mouseY);
}
prevX=cos(counter)*10 + mouseX;
prevY=sin(counter)*10 + mouseY;
}
}
|
DRAWING TOOL... ADDING VALUES TO AN ARRAY
float [] mousePtsX;
float [] mousePtsY;
int counter;
void setup(){
size(300,300);
mousePtsX = new float[100];
mousePtsY = new float[100];
counter = 0;
background(255);
}
void loop(){
noFill();
rect(0,0,width-1,height-1);
// drawing the line
for (int i = 0; i < counter-1; i++){
line(mousePtsX[i], mousePtsY[i], mousePtsX[i+1], mousePtsY[i+1]);
}
if(mousePressed) {
if (counter < 100){
mousePtsX[counter] = mouseX;
mousePtsY[counter] = mouseY;
counter++;
}
}
}
|
DRAWING TOOL... CONTINUALLY MULTIPLYING ARRAY VALUES
float [] mousePtsX;
float [] mousePtsY;
int counter;
void setup(){
size(300,300);
mousePtsX = new float[1000];
mousePtsY = new float[1000];
counter = 0;
background(255);
}
void loop(){
background(255);
noFill();
rect(0,0,width-1,height-1);
// drawing the line
for (int i = 0; i < counter-1; i++){
line(mousePtsX[i], mousePtsY[i], mousePtsX[i+1], mousePtsY[i+1]);
}
if(mousePressed) {
if (counter < 1000){
mousePtsX[counter] = mouseX;
mousePtsY[counter] = mouseY;
counter++;
}
for (int i = 0; i < counter-1; i++){
mousePtsX[i] = mousePtsX[i]*1.001;
mousePtsY[i] = mousePtsY[i]*1.001;
mousePtsX[i+1] = mousePtsX[i+1]*1.001;
mousePtsY[i+1] = mousePtsY[i+1]*1.001;
}
}
}
|
3D ROTATION... USING PUSH AND POP
float counter;
void setup()
{
size(200,200);
}
void loop()
{
background(255);
counter+=0.01;
fill(155,155,0);
push();
translate(100,100,0);
rotateZ(counter);
translate(50,0,0);
box(20,20,20);
pop();
fill(200,155,0);
push();
translate(100,100,0);
rotateY(counter);
translate(50,0,0);
box(20,20,20);
pop();
}
|
IMAGE MANIPULATION... PIXELIZATION
color[][] output = new color[100][100];
int gridWidth;
int numGrid;
void setup() {
size(250,250);
background(0);
gridWidth = 50;
numGrid = 200/gridWidth;
}
void loop() {
BImage b;
b = loadImage("utah.jpg");
image(b, 25, 25);
for(int x=0;x<gridWidth;x++) {
for(int y=0;y<gridWidth;y++) {
output[x][y] = get(x*numGrid+25,y*numGrid+25);
}
}
for(int x=0;x<gridWidth;x++) {
for(int y=0;y<gridWidth;y++) {
fill(output[x][y]);
rect(25+x*numGrid,25+y*numGrid,numGrid,numGrid);
}
}
}
|
Homework 7
(Please email me if you have any questions or problems)- Finish this assigment: This is a two week assignment that will be due Nov 1st. Create 3 Halloween e-cards. Think: spooky, scary, bizarre. Each card must
be significantly different from the others. Be simple + creative.
- You must use objects and classes in at least one of your cards.
- Use text in at least one of your cards. Here is a link to how to use fonts in Processing.
- At least two of them must be dynamic (they need to move).
- There will be a crit of the cards Nov 1. This will count as 2 HW assignments.