Unexpected MouseEvent behavior
in
Programming Questions
•
3 years ago
I have created a custom shape class that extends the java.awt.Polygon class. It also, as the topic implies, implements mouse events. The problem I'm having is that as I add shapes to the scene, they are all treated as one entity. That is, if I click and drag on one of them, they will all be dragged. Similarly (to a degree), when I hover my mouse over a shape, the "hover effect" appears over the wrong shape (it always displays the hover effect over the shape added
after the one being hovered over).
The problem with all shapes being dragged at the same time can be solved with putting an if (over) before doing the translation, however I don't see why that should be necessary as I'm assuming each shape is being handled as an independent object.
Am I using PApplet ( app) the wrong way perhaps?
Anyway, here's the code:
EDIT:
If it helps with testing, here is the main sketch file contents. Note that an image called 0.png must be in the /data/images/landscape/ folder for it to function.
The problem with all shapes being dragged at the same time can be solved with putting an if (over) before doing the translation, however I don't see why that should be necessary as I'm assuming each shape is being handled as an independent object.
Am I using PApplet ( app) the wrong way perhaps?
Anyway, here's the code:
- public class Surface extends java.awt.Polygon {
PImage img;
int selectionAlpha;
int stepAlpha;
boolean over;
public Surface() {
app.registerDraw(this);
app.registerMouseEvent(this);
img = loadImage("images/landscape/0.png");
selectionAlpha = 0;
stepAlpha = 15;
addPoint((width / 2) - 50, (height / 2) - 50);
addPoint((width / 2) + 50, (height / 2) - 50);
addPoint((width / 2) + 50, (height / 2) + 50);
addPoint((width / 2) - 50, (height / 2) + 50);
}
/*************************************************************
* NAME: draw()
* INFO: The render loop.
*************************************************************/
void draw() {
beginShape();
textureMode(IMAGE);
texture(img);
for(int i = 0; i < npoints; i++) {
switch(i) {
case 0:
vertex(xpoints[i], ypoints[i], 0, 0);
break;
case 1:
vertex(xpoints[i], ypoints[i], img.width, 0);
break;
case 2:
vertex(xpoints[i], ypoints[i], img.width, img.height);
break;
case 3:
vertex(xpoints[i], ypoints[i], 0, img.height);
break;
}
}
endShape(CLOSE);
if (over) {
strokeWeight(2);
stroke(255, 255, 0, selectionAlpha);
selectionAlpha += stepAlpha;
if (selectionAlpha > 255 || selectionAlpha < 0) {
stepAlpha *= -1;
}
}
else {
noStroke();
}
}
/*************************************************************
* NAME: mouseEvent()
* INFO: Handles mouse events.
*************************************************************/
public void mouseEvent(MouseEvent event) {
int x = event.getX();
int y = event.getY();
switch(event.getID()) {
case MouseEvent.MOUSE_PRESSED:
if (contains(mouseX, mouseY)) {
selectedSurface = this;
}
break;
case MouseEvent.MOUSE_RELEASED:
break;
case MouseEvent.MOUSE_CLICKED:
println(this.toString());
break;
case MouseEvent.MOUSE_DRAGGED:
translate((x - pmouseX), (y - pmouseY));
break;
case MouseEvent.MOUSE_MOVED:
over = contains(mouseX, mouseY) ? true : false;
break;
}
} - }
EDIT:
If it helps with testing, here is the main sketch file contents. Note that an image called 0.png must be in the /data/images/landscape/ folder for it to function.
- PApplet app = this;
ArrayList allSurfaces; // list of all surfaces
Surface selectedSurface; // selected surface
/*************************************************************
* NAME: setup()
* INFO: Someone set us up the bomb.
*************************************************************/
void setup() {
size(1024, 768, P3D);
noStroke();
allSurfaces = new ArrayList();
}
/*************************************************************
* NAME: draw()
* INFO: The render loop.
*************************************************************/
void draw() {
background(0);
}
/*************************************************************
* NAME: keyReleased()
* INFO: Listens for key released-events.
*************************************************************/
void keyReleased() {
switch(key) {
case 'a':
allSurfaces.add(new Surface());
break;
}
}
1