Super strange behavior when mouse is over polygon
in
Programming Questions
•
3 years ago
This is directly related to a recent post of mine, which was answered in part, but one problem remains.
I have a class called Surface which extends java.awt.Polygon. These surfaces can be added to the scene through an ArrayList, which draw() then iterates through each render loop. The problem lies in that when the mouse is over a surface, a pulsating outline is displayed. So far so good, apart from that the outline is displayed over the wrong surface! Instead, if the mouse is over the first surface added, the outline appears over the second one. A strange behavior, considering the correct name is printed if I click the surface.
Any help would be greatly appreciated! Code follows:
- // Surface.pde
- public class Surface extends java.awt.Polygon {
- PFont label;
- PImage img;
- int selectionAlpha;
- int stepAlpha;
- boolean mPressed;
- boolean mDragged;
- // final int VRADIUS = 4;
- Surface() {
- label = createFont("Monaco", 12);
- 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);
- registerMouseEvent(this);
- }
- /*************************************************************
- * NAME: draw()
- * INFO: The render loop.
- *************************************************************/
- void draw() {
- beginShape();
- textureMode(IMAGE);
- texture(img);
- vertex(xpoints[0], ypoints[0], 0, 0);
- vertex(xpoints[1], ypoints[1], img.width, 0);
- vertex(xpoints[2], ypoints[2], img.width, img.height);
- vertex(xpoints[3], ypoints[3], 0, img.height);
- endShape(CLOSE);
- // go fancypants when the mouse is over the surface
- if (contains(mouseX, mouseY)) {
- strokeWeight(5);
- int r = (selectedSurface == this) ? 0 : 255;
- stroke(r, 255, 0, selectionAlpha);
- selectionAlpha += stepAlpha;
- if (selectionAlpha > 255 || selectionAlpha < 0) stepAlpha *= -1;
- }
- else {
- noStroke();
- }
- // reposition the surface when it's being dragged
- if (mDragged && selectedSurface == this) {
- translate((mouseX - pmouseX), (mouseY - pmouseY));
- }
- // surface description
- if (showLabels) {
- fill(255, 150);
- textFont(label);
- textAlign(CENTER);
- text(this.toString(), sqrt(xpoints[3] * xpoints[2]), ypoints[2]);
- }
- }
- /*************************************************************
- * NAME: mouseEvent()
- * INFO: Handles mouse events.
- *************************************************************/
- void mouseEvent(MouseEvent event) {
- if (contains(mouseX, mouseY)) {
- switch(event.getID()) {
- case MouseEvent.MOUSE_PRESSED:
- selectedSurface = this;
- mPressed = true;
- println(toString());
- break;
- case MouseEvent.MOUSE_RELEASED:
- mPressed = false;
- mDragged = false;
- break;
- case MouseEvent.MOUSE_CLICKED:
- break;
- case MouseEvent.MOUSE_DRAGGED:
- mDragged = mPressed;
- break;
- case MouseEvent.MOUSE_MOVED:
- break;
- }
- }
- }
- /*************************************************************
- * NAME: toString()
- * INFO: Override toString to show something meaningful (?)
- *************************************************************/
- public String toString() {
- return Integer.toHexString(hashCode());
- }
- }
- for (int i = 0; i < allSurfaces.size(); i++) {
- Surface s = (Surface) allSurfaces.get(i);
- s.draw();
- }
1