问题
如何获取图片宽和高
基本函数
//背景设定
size(400,400);//绘制画布,宽、高
background(color/Image);
//设定刷新频率
frameRate(int);
//绘制图形——前两个参数代表位置,后面的参数代表大小
ellipse(200,200,200,200);
line(x1,y1,x2,y2);
rect(x,y,width,height);
curveVertex(336, 364);
curveVertex(336, 364);
curveVertex(272, 76);//用很多个点绘制曲线
//初始化函数
void setup(){}
//绘制函数
void draw(){}
//交互函数
if(mousePressed){}
//辅助函数
fill(0-255); //下一个绘制的图形的填充透明度
stroke(color); //图形边缘线的颜色
map(value,a1,b1,a2,b2);//把value从[a1,b1]映射到[a2,b2]
noLoop();//停止draw()
Loop();//开始draw()
//常量
//width,height屏幕宽和高
//颜色参数的输入
stroke(0-255);
stroke(0-255,0-100);
stroke(#FF0000);
stroke(#FF0000,0-100);
// 数组声明与赋值
float[] ar;
ar = new float[int];
//自定义图像绘制
beginShape(kind);
vertex(120, 80);
vertex(340, 80);
vertex(340, 300);
vertex(120, 300);
endShape(CLOSE); //封闭形状
//转换坐标 一般用于与三角函数有关的绘制
push();
translate(x, y);
// 开始绘图,绘制复杂图
beginShape();
// 遍历啪嗒类,所有的点连成线
for(int i=0; i<splat.size(); i++){
curveVertex(splat.get(i).pos.x, splat.get(i).pos.y);
}
endShape(CLOSE);
//恢复属性
pop() ;
数据存储
ArrayList
.add(new);
.get(index);
.remove(index);
.size;
class Particle{}
ArrayList<Particle> particles = new ArrayList<Particle>();
particles.add(new Particle());
Particle part = particles.get(0);
part.display();
// The size() method returns the current number of items in the list
int total = particles.size();
println("The total number of particles is: " + total);
// You can iterate over an ArrayList in two ways.
// The first is by counting through the elements:
for (int i = 0; i < particles.size(); i++) {
Particle part = particles.get(i);
part.display();
}
// The second is using an enhanced loop:
for (Particle part : particles) {
part.display();
}
// You can delete particles from an ArrayList with remove()
particles.remove(0);
println(particles.size()); // Now one less!
// If you are modifying an ArrayList during the loop,
// then you cannot use the enhanced loop syntax.
// In addition, when deleting in order to hit all elements,
// you should loop through it backwards, as shown here:
for (int i = particles.size() - 1; i >= 0; i--) {
Particle part = particles.get(i);
if (part.finished()) {
particles.remove(i);
}
}
特效
图像
//导入外部资源
PImage img;
img = loadImage("Hokkaido.jpg");
background(img); //设置图片为背景,画布大小必须和图片大小一致(差一个像素都不行)
tint(color);//为image着色
image(img,x,y); //绘制image
image(img,x,y,width,height)//以width、height的尺寸来绘制图像
img.reSize(width,height);
color pix = ima.get(x,y);//获取x,y处的图像
渐变
把线性的数组变成渐变数组,方法
float[] coswave;
coswave = new float[width];
for (int i = 0; i < width; i++) {
float amount = map(i, 0, width, 0, PI);
coswave[i] = abs(cos(amount));
}
控制
键盘
keyPressed()函数接受键盘输入,key值接受所按下的键值。
常规接受26个字母的大小写输入
void keyPressed() {
int keyIndex = -1;
if (key >= 'A' && key <= 'Z') {
keyIndex = key - 'A';
} else if (key >= 'a' && key <= 'z') {
keyIndex = key - 'a';
}
if (keyIndex == -1) {
// If it's not a letter key, clear the screen
background(0);
} else {
// It's a letter key, fill a rectangle
fill(millis() % 255);
float x = map(keyIndex, 0, 25, 0, width - rectWidth);
rect(x, 0, rectWidth, height);
}
}
特殊键输入则key值为coded,keyCode存储按下的特殊键,具体的键值可以在Java KeyEvent里找到 (代码中的UP和DOWN只是简化版本)
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
fillVal = 255;
} else if (keyCode == DOWN) {
fillVal = 0;
}
} else {
fillVal = 126;
}
}
输出
saveFrame("fileName.png");