//week14_1_draw_lines_void_mouseDragged_mouseButton_LEFT_RIGHT
void setup(){
size(500,500);
}
int x=-1, y=1;
void draw(){
if(x!=-1){
ellipse(x,y,1,1);
y++;
}
}
void mouseDragged(){
if(mouseButton==LEFT) line(mouseX,mouseY,pmouseX,pmouseY);
if(mouseButton==RIGHT){
x=mouseX;
y=mouseY;
}
}
想要做出酒杯倒啤酒左鍵畫畫
右鍵滴水
//week14_02_draw_lines_loadPixels_pixels_updatePixels
void setup(){
size(500,500);
background(255);
}
void draw(){
//pixels
}
void mouseDragged(){
if(mouseButton==LEFT){
line(mouseX,mouseY,pmouseX,pmouseY);
//println(pixels[0])
loadPixels();
for(int i=0; i< width*height; i++){
if(pixels[i]!=-1) pixels[i]=color(random(255),random(255),random(255));
}
updatePixels();
}
}
運用pixel
再讓他亂數
線裡面就有很多顏色
前面是在做出圖片
if(img.pixels[x+y*500]==-14) y++;
後面用for迴圈讓pixel複製
PImage img;
void setup(){
size(500,500);
img=createImage(500,500,RGB);
img.loadPixels();
for(int i=0; i<500*500; i++) img.pixels[i]=color(#FFFFF2);
img.updatePixels();
}
int x=250, y=0;
void draw(){
background(img);
ellipse(x,y,20,20);
if(img.pixels[x+y*500]==-14) y++;
}
void mouseDragged(){
background(img);
line(mouseX,mouseY,pmouseX,pmouseY);
loadPixels();
img.loadPixels();
for(int i=0; i<500*500; i++) img.pixels[i]=pixels[i];
img.updatePixels();
updatePixels();
}
結合前面3個
左鍵畫線 右鍵畫球
球碰到線會黏住
用之前教過PVector<>來用一堆球
//week14_04_draw_lines_rain_ArrayList_PVector_p_pp
PImage img;
void setup(){
size(500,500);
img = createImage(500,500,RGB);
}
//int x=250, y=0;
ArrayList<PVector> p = new ArrayList<PVector>();
void draw(){
background(img);
for(PVector pp:p){
ellipse(pp.x,pp.y,20,20);
//if(y<498 && img.pixels[x+y*500]==-16777216) y++;
if(pp.y<498 && img.pixels[int(pp.x)+int(pp.y)*500]==-16777216) pp.y++;
}
}
void mouseDragged(){
if(mouseButton==LEFT){
background(img);
stroke(random(255), random(255), random(255));
line(mouseX,mouseY,pmouseX,pmouseY);
loadPixels();
img.loadPixels();
for(int i=0; i<500*500; i++) img.pixels[i]=pixels[i];
img.updatePixels();
updatePixels();
for(PVector pp:p){
ellipse(pp.x,pp.y,20,20);
}
}else{
p.add(new PVector(mouseX,mouseY));
}
}
類似小畫家的滴管
可以填塞在一個區塊裡
案右鍵還可以隨機顏色
//week14_05_draw_lines_nyFloodFill_x_y_c1_c2_pixels_isOK
void setup(){
size(500,500);
background(#FFFFF2);
}
void draw(){
if(mousePressed && mouseButton==LEFT) line(mouseX, mouseY, pmouseX, pmouseY);
if(mousePressed && mouseButton==RIGHT){
loadPixels();
color c1 = pixels[mouseX+mouseY*500];
color c2 = color(random(255), random(255), random(255));
if(c1!=c2) myFloodFill(mouseX, mouseY, c1, c2);
updatePixels();
}
}
boolean isOK(int x, int y, color c1){
if(x<0 || y<0 || x>=500 || y>=500) return false;
if(pixels[x+y*500]!=c1) return false;
return true;
}
void myFloodFill(int x, int y, color c1, color c2){
pixels[x+y*500]=c2;
if(isOK(x+1, y, c1)) myFloodFill(x+1, y, c1, c2);
if(isOK(x-1, y, c1)) myFloodFill(x-1, y, c1, c2);
if(isOK(x, y+1, c1)) myFloodFill(x, y+1, c1, c2);
if(isOK(x, y-1, c1)) myFloodFill(x, y-1, c1, c2);
}
設定滑鼠標示
記得要用32*32
還設定一定時間可以變換
//week14_06_cute_cursor_kitty_cursor
PImage imgCute, imgKitty;
void setup(){
size(500,500);
imgCute = loadImage("cute.png");
imgKitty = loadImage("kitty.png");
cursor(imgKitty);
}
void draw(){
background(#FFFFF2);
if(frameCount%120==0) cursor(imgCute);
if(frameCount%120==60) cursor(imgKitty);
}
沒有留言:
張貼留言