我的问题如下: 我编写了一个代码,并在我点击一个矩形(使用loadImage-fuction)时设法显示一个图像。矩形用作我想稍后用图像替换的按钮。 但我实际上并不只是想在单击按钮时显示图像。我想调用代码,将图像复制到另一个代码:
public static int SQUARE_WIDTH = 30;
public static int SQUARE_HEIGHT = 30;
PImage img1,img2, img3;
void setup() {
size(670, 943);
img1 = loadImage("white.png");
img2 = loadImage("hase.jpg");
img3= loadImage("ohrring.jpg");
image(img1,0,0);
}
void draw() {
if(mousePressed)
copy(img2,
constrain(mouseX-SQUARE_WIDTH/2,0,width),
constrain(mouseY-SQUARE_HEIGHT/2,0,height),
SQUARE_WIDTH,SQUARE_HEIGHT,
constrain(mouseX-SQUARE_WIDTH/2,0,width),
constrain(mouseY-SQUARE_HEIGHT/2,0,height),
SQUARE_WIDTH,SQUARE_HEIGHT);
}
复制代码不是简单地复制图像,而是使用鼠标作为画笔!当您在某个区域“绘制”时,图像会以像素后的画笔像素的“笔划”显示! processing.org/reference/copy_.html
当我想将这个与我的主要代码结合起来时,我碰巧遇到了很大的问题:
int rectX, rectY;
int rectSize = 90;
boolean rectOver = false;
color rectHighlight;
color currentColor, baseColor;
color rectColor;
public static int SQUARE_WIDTH = 30;
public static int SQUARE_HEIGHT = 30;
PImage img1,img2, img3;
void setup() {
size(670, 943);
rectColor = color(0);
rectX = width/2-rectSize-10;
rectY = height/2-rectSize/2;
baseColor = color(102);
currentColor = baseColor;
img1 = loadImage("frida.jpg");
img2 = loadImage("hase.jpg");
img3 = loadImage("white.png");
background(img3);
}
void draw() {
update(mouseX, mouseY);
if (rectOver) {
fill(rectHighlight);
} else {
fill(rectColor);
}
stroke(255);
rect(rectX, rectY, rectSize, rectSize);
}
void update(int x, int y) {
if ( overRect(rectX, rectY, rectSize, rectSize) ) {
rectOver = true;
}else {
rectOver = false;
}
}
void mousePressed() {
if (rectOver) {
background(img2);
}
}
boolean overRect(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
理论上我得到了一个提示,在mousePressed()中设置一个布尔值来在draw()中进行复制操作,然后在draw()中检查这个布尔值:如果设置(true)则应该复制。但不幸的是,我不是编程天空中最耀眼的明星,所以有人能告诉我这个部分应该是什么样的吗?当然,我愿意接受其他建议如何解决这个问题! 谢谢!
答案 0 :(得分:0)
我希望这就是你要找的东西。如果要复制图像,则无需调用复制图像的功能,只需调用=符号即可复制图像。
在我的示例代码中,buttonImage是按钮上的图像。每当您不希望按钮上的图像按以下方式分配时:
buttonImage = null;
如果您想要一个图像而不是矩形,请执行以下操作:
buttonImage = yourImage;
buttonImage.resize(w, h); //fit it on the button.
我认为这是你想要实现的目标?
PImage buttonImage;
void setup()
{
}
void draw()
{
if(buttonImage == null || buttonImage.width < 2) rect(x, y, w, h);
else image(buttonImage, x, y);
}
void mouseReleased()
{
if(mouseX > x && mouseX < x + w && mouseY > y && mouseY < y + h)
{
//copy any image onto the buttonImage
buttonImage = loadImage("newPath.png"); //update / overwrite image
buttonImage.resize(w, h); //fit it on the button
}
}
x和y是按钮的位置,w和h是我示例中按钮的宽度和高度。
编辑: 好的,所以基本上你想要一个白色的背景,你想用你的工具废弃它,这样就会出现一个图像?我仍然不能100%肯定你的要求,但如果是这样,试试这个: 我使用img.get()而不是img.copy(),因为它有更少的参数来处理。我真的希望我能正确理解这一点,如果不是可能将视频链接到类似的东西?我很难理解你想要的东西。
toolSelected integer是您正在使用的工具的计数器。根据其值,它正在执行不同的代码。
我的代码:
PImage img1;
int toolSelected = 0; //Normal tool;
int widthOfBrush = 20; //Now you are drawing 20x20
int buttonX = 200;
int buttonY = 200;
int buttonW = 40;
int buttonH = 20;
void setup()
{
size(640, 480);
background(255);
img1 = loadImage("yourImg.png");
img1.resize(width, height); //Fit it on processing screen
}
void draw()
{
if(toolSelected == 0) {}//other tool
//Instead of using copy we will be using buttonImage.get(x, y, w, h) --> makes more sense
if(toolSelected == 1 && mousePressed)
{
float yourX = mouseX;
float yourY = mouseY;
yourX -= floor(widthOfBrush / 2);
yourY -= floor(widthOfBrush / 2);
//scale & copy:
PImage brushImage = img1.get((int)yourX, (int)yourY, widthOfBrush * (width / img1.width), widthOfBrush * (width / img1.width)); //Draw the image at your destination
image(brushImage, mouseX, mouseY);
}
stroke(0);
fill(255);
rect(buttonX, buttonY, buttonW, buttonH);
}
void mouseReleased()
{
if (mouseX > buttonX && mouseX < buttonX + buttonW && mouseY > buttonY && mouseY < buttonY + buttonH)
{
//copy any image onto the buttonImage
//buttonImage = loadImage("newPath.png"); //update / overwrite image
toolSelected = 1; //Our tool is the image brush now.
}
}