Java ArrayList how to check if one of the rectangles contains the mouse? -
i have application place tiles. can place tiles on tiles , don't want that. know need if tile rectangle contains mouse don't place tile on it. doesn't work. @ code:
for (int = 0; < b.toarray().length; i++) { b.get(i).tick(); if (b.get(i).r.contains(comp.mx, comp.my)) { canplaceatile = false; // system.out.println("yes"); } else { canplaceatile = true; //system.out.println("no"); } if (b.get(i).remove) { b.remove(i); i--; } }
this how check if mouse inside area of 1 of tiles.
block class:
public abstract class block { public int x,id; public int y; protected image img; public boolean remove; public int rotate; public rectangle r; protected int bx, by; public block() { } public abstract void tick(); public abstract void render(graphics g); public void createcollisionrect() { r.setbounds(x - (int) play.camx, y - (int) play.camy, 20, 20); } }
an example of tile:
public class wall extends block { public wall(int x, int y, int rot) { this.x = x; this.y = y; this.rotate = rot; r = new rectangle(x - (int) play.camx, y - (int) play.camy, 20, 20); id = 0; } public void tick() { createcollisionrect(); if (comp.mr && r.contains(new point((comp.mx), (comp.my)))) { remove = true; } } public void render(graphics g) { imageicon i62 = new imageicon("res/tiles/wall.png"); img = i62.getimage(); g.drawimage(img, x - (int) play.camx, y - (int) play.camy, null); g.setcolor(color.red); // g.drawrect(x -(int)play.camx, y - play.camy, 20,20); } }
i want check if of rectangles contains mouse , set canplaceatile false if mouse inside 1 of rectangles. code above doesn't work because when print console , have mouse on 1 of tiles says:
yes no yes no yes no yes no yes no yes no yes no yes no yes no yes no yes no yes
and when mouse on rectangle without moving @ all. how can fix cannot place tiles on top of tiles. place tiles:
public void mousepressed(mouseevent e) { if (e.getbutton() == mouseevent.button1) { comp.ml = true; if (manager.isplay && play.dragging == false) { if (play.canplaceatile) { if (play.selectedid == 0) { play.b.add(new wall((comp.mx / 20) * 20, (comp.my / 20) * 20, play.selectedrot)); } } } } }
Comments
Post a Comment