【问题标题】:Combining two codes with a mousePressed-event processing将两个代码与 mousePressed-event 处理相结合
【发布时间】:2014-12-24 14:31:47
【问题描述】:

我的问题如下: 当我单击一个矩形(使用 loadImage 功能)时,我编写了一个代码并设法显示图像。该矩形用作稍后我想用图像替换的按钮。 但我实际上不只是希望在单击按钮时显示图像。我想调用一个代码,将图像复制到另一个:

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),它将执行复制。但不幸的是,我不是编程天空中最亮的明星,所以有人能告诉我这部分应该是什么样子吗?当然,我对如何解决这个问题的其他建议持开放态度! 谢谢!

【问题讨论】:

    标签: java copy boolean mouseevent processing


    【解决方案1】:

    我希望这是您正在寻找的。如果你想复制一张图片,你不需要调用函数来复制一张图片,你可以简单地调用 = 符号,图片就会被复制。

    在我的示例代码中,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 整数是您正在使用的工具的计数器。根据其值,它正在执行不同的代码。

    我的代码:

    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.
      }
    }
    

    【讨论】:

    • 非常感谢您的回答,但这并不是我想要实现的目标!我的复制代码不只是复制图像,它使用鼠标作为画笔!当您在一个区域上“绘制”时,图像会以画笔的“笔触”逐个像素地显示! processing.org/reference/copy_.html我尝试逐步解释我想要实现的目标! 1. 在画布上显示按钮 2. 单击按钮时,复制图像的代码被激活,因此您现在可以在画布上“绘制”该图像
    • 我编辑了我的答案。这是你想要的吗?或者您只是想要一个图像绘制工具,而不是画笔,而是在屏幕上绘制图像。如果是这种情况,只需更改: if(toolSelected == 1 && mousePressed) image(img1, mouseX, mouseY) 并删除 img1.resize() 部分。
    猜你喜欢
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 2016-06-22
    • 2023-03-03
    相关资源
    最近更新 更多