problem with contains in java.awt.Polygon class
in
Programming Questions
•
2 years ago
I'm using the Poly class found here:
http://wiki.processing.org/w/Using_AWT's_Polygon_class
This is a simply program that is supposed to allow you to drag squares around the screen. The problem is that the contains function is not working after I update the polygon points. Please try this and see if you know what the problem is.
Mouse over the green square and see it highlights. Now click and drag it. Note that it now highlights only when the mouse is within the square AND within the original position (purple bounding box).
Here is the code:
http://wiki.processing.org/w/Using_AWT's_Polygon_class
This is a simply program that is supposed to allow you to drag squares around the screen. The problem is that the contains function is not working after I update the polygon points. Please try this and see if you know what the problem is.
Mouse over the green square and see it highlights. Now click and drag it. Note that it now highlights only when the mouse is within the square AND within the original position (purple bounding box).
Here is the code:
- //globals
int tile_size = 50;
ArrayList tiles;
int selected_tile;
PVector press;
void setup() {
//basic setup
rectMode(CENTER);
size(600,600);
background(0);
tiles = new ArrayList();
color tile_color = color(0,255,0);
tiles.add(new Tile(1, tile_color, new PVector(200,200)));
tiles.add(new Tile(1, color(255,0,0), new PVector(400,400)));
}
//main program loop
void draw() {
background(0);
//original position
stroke(255,0,255);
noFill();
rect(200,200,100,100);
drawTiles();
selected_tile = checkMouse();
}
//draw the tiles
void drawTiles() {
for (int i = tiles.size()-1; i >= 0; i--) {
// An ArrayList doesn't know what it is storing so we have
// to cast the object coming out
Tile tile = (Tile)tiles.get(i);
tile.drawTile();
}
}
//returns the index of the tile under the mouse, -1 if none
//only will return 1 shape
int checkMouse() {
int selected = -1;
for (int i = tiles.size()-1; i >= 0; i--) {
Tile tile = (Tile)tiles.get(i);
if (tile.checkInside(mouseX, mouseY)) {
tile.selected = true;
selected = i;
}
else {
tile.selected = false;
}
}
return selected;
}
void mousePressed() {
press = new PVector(mouseX, mouseY);
}
void mouseDragged() {
if (selected_tile != -1) {
float x_dif = press.x - mouseX;
float y_dif = press.y - mouseY;
Tile tile = (Tile)tiles.get(selected_tile);
tile.move(x_dif, y_dif);
}
press = new PVector(mouseX, mouseY);
} - /*
The class inherit all the fields, constructors and functions
of the java.awt.Polygon class, including contains(), xpoint,ypoint,npoint
*/
class Poly extends java.awt.Polygon{
public Poly(){
super();
}
public Poly(int[] x,int[] y, int n){
//call the java.awt.Polygon constructor
super(x,y,n);
}
void drawMe(){
beginShape();
for(int i=0; i<npoints; i++){
vertex(xpoints[i],ypoints[i]);
}
endShape(CLOSE);
}
} - class Tile {
//public members
color tile_color;
int tile_shape;
PVector center;
boolean selected = false;
//private members
private Poly polygon = new Poly();
//constructors
Tile(int tile_shape_in, color tile_color_in, PVector center_in) {
tile_shape = tile_shape_in;
tile_color = tile_color_in;
center = center_in;
}
//methods
//draws the tile
void drawTile() {
//draw a bounding box if the shape is selected
if (selected) {
stroke(255);
}
else {
noStroke();
}
fill(tile_color);
switch (tile_shape) {
case 1://square
updatePoly();
polygon.npoints = 4;
polygon.drawMe();
break;
case 2://triangle
break;
case 3:
break;//parallelogram
}
}
//returns if the given coordinates are inside the shape
boolean checkInside(float x, float y) {
if(tile_color == color(0,255,0)) {
println("x: ");
println(polygon.xpoints);
println("y: ");
println(polygon.ypoints);
println("x: " + mouseX);
println("y: " + mouseY);
println(polygon.contains(x, y));
}
return (polygon.contains(x, y));
}
//moves the center by the given x and y values
void move(float x, float y) {
center.sub(new PVector(x,y));
}
void updatePoly() {
int[] xpoints = {
(int)center.x - tile_size, (int)center.x - tile_size, (int)center.x + tile_size, (int)center.x + tile_size
};
int[] ypoints = {
(int)center.y - tile_size, (int)center.y + tile_size, (int)center.y + tile_size, (int)center.y - tile_size
};
polygon.xpoints = xpoints;
polygon.ypoints = ypoints;
}
}
1