Java 将两个代码与鼠标按下的事件处理相结合
我的问题如下: 我编写了一个代码,并在单击矩形时显示了一个图像(带有loadImage功能)。矩形用作按钮,我希望稍后用图像替换该按钮。 但我实际上并不只是想在点击按钮时显示图像。我想调用一个代码,将一个图像复制到另一个图像上:Java 将两个代码与鼠标按下的事件处理相结合,java,copy,boolean,mouseevent,processing,Java,Copy,Boolean,Mouseevent,Processing,我的问题如下: 我编写了一个代码,并在单击矩形时显示了一个图像(带有loadImage功能)。矩形用作按钮,我希望稍后用图像替换该按钮。 但我实际上并不只是想在点击按钮时显示图像。我想调用一个代码,将一个图像复制到另一个图像上: public static int SQUARE_WIDTH = 30; public static int SQUARE_HEIGHT = 30; PImage img1,img2, img3; void setup() { size(670, 943); i
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);
}
复制代码不仅仅是复制图像,它使用鼠标作为画笔!当你在一个区域上“画画”时,画笔的“笔划”一个像素接一个像素地显示图像!
当我想将此代码与我的主代码相结合时,我碰巧遇到了巨大的问题:
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;
}
}
intrectx,rectY;
int rectSize=90;
布尔rectOver=false;
色彩突出;
颜色当前颜色,基色;
颜色;
公共静态整数平方_宽度=30;
公共静态平方英尺高度=30;
PImage img1、img2、img3;
无效设置(){
规模(670943);
rectColor=color(0);
rectX=宽度/2-rectSize-10;
rectY=高度/2-rectSize/2;
基色=颜色(102);
currentColor=基色;
img1=loadImage(“frida.jpg”);
img2=loadImage(“hase.jpg”);
img3=加载图像(“white.png”);
背景(img3);
}
作废提款(){
更新(mouseX,mouseY);
if(rectOver){
填充(突出显示);
}否则{
填充(彩色);
}
中风(255);
rect(rectX,rectY,rectSize,rectSize);
}
无效更新(整数x,整数y){
if(过度选择(rectX、rectY、rectSize、rectSize)){
rectOver=true;
}否则{
rectOver=false;
}
}
void mousePressed(){
if(rectOver){
背景(img2);
}
}
布尔覆盖(整数x,整数y,整数宽度,整数高度){
如果(mouseX>=x&&mouseX=y&&mouseY我希望这就是您要寻找的。如果您要复制图像,不需要调用函数来复制图像,只需调用=符号,图像就会被复制
在我的示例中,代码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
}
}
PImage按钮图像;
无效设置()
{
}
作废提款()
{
if(buttonImage==null | | buttonImage.width<2)rect(x,y,w,h);
else图像(按钮图像,x,y);
}
void mouseereleased()
{
if(mouseX>x&&mouseXy&&mouseY
在我的示例中,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.
}
}
PImage img1;
int-toolSelected=0;//普通刀具;
int widthOfBrush=20;//现在绘制的是20x20
int buttonX=200;
int buttonY=200;
int=40;
int=20;
无效设置()
{
尺寸(640480);
背景(255);
img1=loadImage(“yourImg.png”);
img1.调整大小(宽度、高度);//将其安装在处理屏幕上
}
作废提款()
{
如果(toolSelected==0){}//其他工具
//我们将使用buttonImage.get(x,y,w,h)-->而不是使用copy
if(选定工具==1&&mousePressed)
{
float yourX=mouseX;
浮动yourY=mouseY;
yourX-=地板(刷宽度/2);
yourY-=地板(刷子宽度/2);
//缩放和复制:
PImage brushImage=img1.get((int)yourX,(int)yourY,widthOfBrush*(width/img1.width),widthOfBrush*(width/img1.width));//在目的地绘制图像
图像(brushImage、mouseX、mouseY);
}
冲程(0);
填充(255);
rect(按钮X、按钮Y、按钮NW、按钮NH);
}
void mouseereleased()
{
if(mouseX>buttonX&&mouseXbuttonY&&mouseY
非常感谢您的回答,但这并不是我想要实现的目标!我的复制代码不仅仅是复制图像,它使用鼠标作为画笔!当您在某个区域“绘制”时,图像会显示为“笔划”一个像素接着一个像素的画笔!我试图解释我想在步骤中实现什么!1.在画布上显示按钮2.单击按钮时,复制图像的代码将被激活,以便您现在可以“绘制”画布上的图像我编辑了我的答案。这是你想要的吗?或者你只是想要一个图像绘制工具,在屏幕上绘制图像而不是画笔。如果是这样,只需更改:If(toolSelected==1&&mousePressed)image(img1,mouseX,mouseY)并删除img1.resize()部分。