本次对话共 4 次问答,总计 3335 个字符
用processing做一个类似美图秀秀的软件,拥有导入图片的功能,和剪切图片,并且在图片上绘画,并且可以选择画笔的颜色和粗细的功能
🌵
在Processing中实现类似美图秀秀的软件可以通过绘制界面、导入图片、剪切图片、绘画等功能来实现。以下是一个简单的示例代码,实现了导入图片、剪切图片、绘画、选择画笔颜色和粗细的功能。
PImage img;
int x, y, w, h; // 剪切图片的坐标和宽高
color brushColor = color(0); // 画笔颜色,默认为黑色
int brushSize = 5; // 画笔粗细,默认为5
void setup() {
size(800, 600);
}
void draw() {
background(255);
if (img != null) {
image(img, 0, 0, width, height);
if (mousePressed && mouseX < width && mouseY < height) {
stroke(brushColor);
strokeWeight(brushSize);
line(pmouseX, pmouseY, mouseX, mouseY);
}
}
}
void mousePressed() {
if (mouseX < width && mouseY < height) {
x = mouseX;
y = mouseY;
}
}
void mouseReleased() {
if (mouseX < width && mouseY < height) {
w = mouseX - x;
h = mouseY - y;
if (w > 0 && h > 0) {
img = get(x, y, w, h);
}
}
}
void keyPressed() {
if (key == 'r') {
brushColor = color(255, 0, 0);
} else if (key == 'g') {
brushColor = color(0, 255, 0);
} else if (key == 'b') {
brushColor = color(0, 0, 255);
} else if (key == '+') {
brushSize += 2;
} else if (key == '-') {
brushSize = max(1, brushSize - 2);
}
}
void mouseClicked() {
if (mouseX >= width && mouseY >= height) {
selectInput("Select an image to import:", "fileSelected");
}
}
void fileSelected(File selection) {
if (selection != null) {
img = loadImage(selection.getAbsolutePath());
}
}
在这个示例中,你可以拖动鼠标来绘制线条,按下鼠标并拖动来剪切图片,按下'r'、'g'、'b'键来选择红色、绿色、蓝色画笔,按下'+'、'-'键来调整画笔粗细。点击窗口右下角可以选择导入图片。
笔拖动的轨迹会留下颜色,并且创建一个橡皮擦
🌵
要实现笔拖动的轨迹留下颜色的效果,可以将鼠标轨迹保存下来,并在每一帧绘制出来。同时,创建一个橡皮擦功能也很简单,只需在特定情况下将画笔颜色设置为背景色即可。
以下是更新后的示例代码,包含了笔拖动轨迹留下颜色和橡皮擦功能:
PImage img;
int x, y, w, h; // 剪切图片的坐标和宽高
color brushColor = color(0); // 画笔颜色,默认为黑色
int brushSize = 5; // 画笔粗细,默认为5
ArrayList<PVector> path = new ArrayList<PVector>(); // 存储鼠标轨迹
void setup() {
size(800, 600);
}
void draw() {
background(255);
if (img != null) {
image(img, 0, 0, width, height);
if (mousePressed && mouseX < width && mouseY < height) {
path.add(new PVector(mouseX, mouseY));
}
noFill();
strokeWeight(brushSize);
for (int i = 0; i < path.size()-1; i++) {
PVector p1 = path.get(i);
PVector p2 = path.get(i+1);
stroke(brushColor);
line(p1.x, p1.y, p2.x, p2.y);
}
}
}
void mousePressed() {
if (mouseX < width && mouseY < height) {
x = mouseX;
y = mouseY;
}
}
void mouseReleased() {
if (mouseX < width && mouseY < height) {
w = mouseX - x;
h = mouseY - y;
if (w > 0 && h > 0) {
img = get(x, y, w, h);
}
}
}
void keyPressed() {
if (key == 'r') {
brushColor = color(255, 0, 0);
} else if (key == 'g') {
brushColor = color(0, 255, 0);
} else if (key == 'b') {
brushColor = color(0, 0, 255);
} else if (key == 'e') { // 橡皮擦功能
brushColor = color(255); // 设置为背景色
} else if (key == '+') {
brushSize += 2;
} else if (key == '-') {
brushSize = max(1, brushSize - 2);
}
}
void mouseClicked() {
if (mouseX >= width && mouseY >= height) {
selectInput("Select an image to import:", "fileSelected");
}
}
void fileSelected(File selection) {
if (selection != null) {
img = loadImage(selection.getAbsolutePath());
}
}
在这个更新后的示例中,鼠标拖动的轨迹会留下颜色,并且按下'e'键可以将画笔设置为橡皮擦功能,擦除鼠标拖动的轨迹。