Java 当一个矩形';它在上空盘旋?
当单个矩形悬停在上方时,如何对其着色?下面使用的具体方法并没有给我任何关于如何解决这个问题的想法。它使用单个矩形在窗口中生成网格。如何在不中断此代码的情况下,监听Java 当一个矩形';它在上空盘旋?,java,processing,onhover,Java,Processing,Onhover,当单个矩形悬停在上方时,如何对其着色?下面使用的具体方法并没有给我任何关于如何解决这个问题的想法。它使用单个矩形在窗口中生成网格。如何在不中断此代码的情况下,监听mouseX和mouseY并为一个矩形上色?谢谢 int cols,rows; int scl = 20; int gridsize = 0; void setup(){ size(400,400); int w = 400; int h = 400; cols = w / scl; rows = h / scl; } void
mouseX
和mouseY
并为一个矩形上色?谢谢
int cols,rows;
int scl = 20;
int gridsize = 0;
void setup(){
size(400,400);
int w = 400;
int h = 400;
cols = w / scl;
rows = h / scl;
}
void draw() {
//mouseX, mouseY
background(r,g,b);
for (int x = 0; x < cols; x++){
for (int y = 0; y < rows; y++){
stroke(55);
//noFill();
fill(50,50,50);
rect(x*scl,y*scl,scl,scl);
}
}
}
int列、行;
int scl=20;
int gridsize=0;
无效设置(){
尺寸(400400);
int w=400;
int h=400;
cols=w/scl;
rows=h/scl;
}
作废提款(){
//摩丝,摩丝
背景(r、g、b);
对于(int x=0;x
作为参考,我使用的是Processing 3 For Java。您可以随时检查鼠标是否在矩形范围内:
- 你知道mouseX,mouseY值
- 你知道每个盒子的x、y和大小
- 如果mouseX在x和x+大小范围内,mouseY在y和y+大小范围内,那么您就在一个盒子上方
int列、行;
int scl=20;
int gridsize=0;
无效设置(){
尺寸(400400);
int w=400;
int h=400;
cols=w/scl;
rows=h/scl;
}
作废提款(){
//摩丝,摩丝
背景(255);
对于(int x=0;x (mouseX>=xpos&&mouseX=ypos&&mouseY您始终可以检查鼠标是否在矩形的边界内:
- 你知道mouseX,mouseY值
- 你知道每个盒子的x、y和大小
- 如果mouseX在x和x+大小范围内,mouseY在y和y+大小范围内,那么您就在一个盒子上方
以下是应用于您的代码的上述内容(如果条件格式化便于查看,请随意重新格式化):
int列、行;
int scl=20;
int gridsize=0;
无效设置(){
尺寸(400400);
int w=400;
int h=400;
cols=w/scl;
rows=h/scl;
}
作废提款(){
//摩丝,摩丝
背景(255);
对于(int x=0;x=xpos&&mouseX=ypos&&mouseY非常适合这种情况,但是如果您想在这里使用面向对象的方法,还有另一种更复杂的方法。对于这个小例子,您可以使用一个Grid
类来保存和管理Cell
对象数组。或者您可以跳过Grid
类s并管理主草图中的单元格
。您可以给单元格
类一个函数来渲染自身,也可以给每个单元格一个颜色和大小,这完全取决于您。此外,它还可以有一个函数告诉您鼠标是否在其上,以及一个函数来更改其颜色。骨架如下所示:
class Cell {
float x,y;
float length, breadth;
color col;
Cell(float x, float y) {
this.x = x;
this.y = y;
length = 10;
breadth = 10;
col = color(0);
}
void render() {
fill(col);
rect(x, y, length, breadth);
}
void setColor(color col) {
this.col = col;
}
boolean mouseOver() {
if(mouseX > x && mouseX < x+length) {
if(mouseY > y && mouseY < y+breadth) {
return true;
}
}
return false;
}
int cols;
int rows;
int scl = 20;
void setup() {
size(400, 400);
cols = width / scl;
rows = height / scl;
}
void draw() {
background(100);
for (int x = 0; x < cols; x++) {
for (int y = 0; y < rows; y++) {
stroke(55);
fill(50, 50, 50);
rect(x*scl, y*scl, scl, scl);
}
}
int hoveredRectColX = int(mouseX / scl);
int hoveredRectRowY = int(mouseY / scl);
float rectX = hoveredRectColX * scl;
float rectY = hoveredRectRowY * scl;
fill(255, 0, 0);
rect(rectX, rectY, scl, scl);
}
类单元{
浮动x,y;
浮动长度、宽度;
颜色颜色;
单元(浮动x,浮动y){
这个.x=x;
这个。y=y;
长度=10;
宽度=10;
col=颜色(0);
}
void render(){
填充(col);
rect(x,y,长度,宽度);
}
无效设置颜色(颜色列){
this.col=col;
}
布尔mouseOver(){
if(mouseX>x&&mouseXy&&mouseY
现在,您可以在主草图中使用该类及其方法来查找单元格,并在其上调用setColor
,以更改其颜色。适用于此场景,但如果您想在此处使用面向对象的方法,还有另一种更复杂的方法。对于这个小示例,您可以使用de>Grid
类,用于保存和管理一组单元格
对象。或者您可以跳过Grid
类,在主草图中管理单元格
。您可以为单元格
类提供一个渲染自身的函数,也可以为每个单元格指定颜色和大小,这完全取决于您。此外,它还可以d有一个函数告诉你鼠标是否在它上面,还有一个函数改变它的颜色。骨架如下所示:
class Cell {
float x,y;
float length, breadth;
color col;
Cell(float x, float y) {
this.x = x;
this.y = y;
length = 10;
breadth = 10;
col = color(0);
}
void render() {
fill(col);
rect(x, y, length, breadth);
}
void setColor(color col) {
this.col = col;
}
boolean mouseOver() {
if(mouseX > x && mouseX < x+length) {
if(mouseY > y && mouseY < y+breadth) {
return true;
}
}
return false;
}
int cols;
int rows;
int scl = 20;
void setup() {
size(400, 400);
cols = width / scl;
rows = height / scl;
}
void draw() {
background(100);
for (int x = 0; x < cols; x++) {
for (int y = 0; y < rows; y++) {
stroke(55);
fill(50, 50, 50);
rect(x*scl, y*scl, scl, scl);
}
}
int hoveredRectColX = int(mouseX / scl);
int hoveredRectRowY = int(mouseY / scl);
float rectX = hoveredRectColX * scl;
float rectY = hoveredRectRowY * scl;
fill(255, 0, 0);
rect(rectX, rectY, scl, scl);
}
类单元{
浮动x,y;
浮动长度、宽度;
颜色颜色;
单元(浮动x,浮动y){
这个.x=x;
这个。y=y;
长度=10;
宽度=10;
col=颜色(0);
}
void render(){
填充(col);
rect(x,y,长度,宽度);
}
无效设置颜色(颜色列){
this.col=col;
}
布尔mouseOver(){
if(mouseX>x&&mouseXy&&mouseY
现在,您可以在主草图中使用该类及其方法来查找单元格,并在其上调用setColor
,以更改其颜色。乔治的答案是正确的。我投了更高的票,我相信您应该将其标记为正确答案。玉石的答案基本上就是乔治的答案,移动到一个类中
它们都使用点-矩形碰撞检测,检查点是否在矩形内。您只需对照点检查每个矩形(在本例中为鼠标位置),这允许您确定鼠标所在的矩形。即使您有一组不同形状的矩形,也可以使用此功能,甚至可以使用重叠的矩形
另一种方法是使用基于网格的碰撞检测,它利用了一个事实,即你有一堆间距均匀的矩形,没有重叠。你只需使用除法来找出鼠标所在的单元格,然后将该单元格转换为坐标,然后使用这些坐标来绘制re克坦格尔,那是我