2

I have a line that I want to be drawn from the player x and y towards the mouse x and y but I want it to just go in the direction of the mouse cursor (I don't want it to be drawn from the player to the mouse but from the player towards the mouse and the go beyond the mouse). I get the mouse position by using the code below.

PointerInfo mouse = Mouse.getPointerInfo();
Point point = new Point(mouse.getLocation());

I the draw the line with this code.

g2d.draw(new Line2D.Double(player.getX() + 32, player.getY() + 32, 
    point.getX(), point.getY()));

This only make it so that the line is drawn between the player and the mouse but how would I make it go beyond the mouse so that it will travel out of the screen?

Blaze7
  • 98
  • 4

1 Answers1

1

If its not important how far the line needs to go, just add width and height for it to go off the screen like so:

g2d.draw(new Line2D.Double(player.getX() + 32, player.getY() + 32, 
    point.getX()+width, point.getY()+height));

Where 'width' and 'height' are defined as the size of the screen.

Nico
  • 3,471
  • 2
  • 29
  • 40
  • 1
    You need to add both width and height of the screen. You can always do the math and calculate line slope to draw it as well :) – Jesus Ramos May 06 '13 at 20:41
  • Added the part about the height. I was assuming the OP wanted it to go off the screen along the x axis. :-) Agree about the math. My answer is a very crude example of course. – Nico May 06 '13 at 20:43