i want my line 2d to stop being drawn when it hits the edge of the rectangle 2d (code below)
my declairations:
Point pointStart = new Point(posX, posY); Point pointEnd = new Point(0,0); Rectangle2D example; public static boolean shooting = false; Line2D line = new Line2D.Double(); int x = 0; int y = 0;
the mouse listener:
the paint comptent: (i am repainting at end so it is a loop)addMouseMotionListener(new MouseMotionAdapter() { public void mouseMoved(MouseEvent e) { pointEnd = e.getPoint(); linex = pointEnd.x + 10; liney = pointEnd.y + 10; } });
int x = (this.getWidth() - bkgrndImage.getWidth()) / 2; int y = (this.getHeight() - bkgrndImage.getHeight()) / 2; line = new Line2D.Double(x, y, linex, liney); example = new Rectangle2D.Double(mob1x, mob1y,50,50); g2.setStroke(new BasicStroke(3f)); g2.setPaint(Color.RED); if (shooting){ g2.draw(line); if(line.intersects(example)) { System.out.println("line collided with rect"); pointEnd.x = (int) example.getCenterX(); pointEnd.y = (int) example.getCenterY(); //line = new Line2D.Double(x, y, pointEnd.x, pointEnd.y); // g2.draw(line); } } else { pointEnd.x = (int) x; pointEnd.y = (int) y; //repaint(); }
i dont think i have missed anything
NOTE: it does work overall but i want it so instead of jumping to the middle, it stops at the edge.
--Thanks in advanced!
--Bc1151