How do I move a painted line in Java,using my mouse? [closed]
I hava a project which will look like similar to Paint program. Here, I'm supposed to draw lines with start and end points and then I need to be able to move these lines by simply dragging my mouse on them. I draw the lines using the drawLine method of Java. The problem is, I have no idea how to move the开发者_如何转开发se lines :(
Please help me if you have an idea on this, Thanks.
You need to add some sort of ActionListener and detect when the user clicks a line on the screen. Then you need to monitor the mouse movement; and rePaint() the line.
i think you should clear the canvas on the mouseMove event with fillRect(int x, int y, int width, int height) on the graphics objet , and within the event get the mouse location.
public void mouseMoved( MouseEvent e ) { // called during motion when no buttons are down
mx = e.getX();
my = e.getY();
repaint();
}
then in your paint method you should draw the line using mx, my coordinates aproprietly
精彩评论