The problem is. on the next click/drag, the old line disappears when the paint method is called again
You need to save old lines in a list that the paint method can access and use to draw the old lines as well as the current line.
There is no magic. If you want old lines, you must save them somehow.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
5 vectors could solve this problem (though there probably is a more efficient way).
Make one for the start x and one for the start y. Repeat for the end positions.
make one for the color
use an advanced for loop to set the color and then print the lines that the user drew.
sirlink99
Practically a Master Poster
661 posts since Oct 2010
Reputation Points: 45
Solved Threads: 19
Create a new class for a line. It would contain the x,y of the end points, the color and the width and any other property you want for the line. It would have a drawMe method that takes a Graphics object and does the drawing.
Objects of this class could be saved in a list that would be iterated over by the paintComponent method.
@efxee Override paintComponent for Swing components. See line 34
@sirlink99 Your design isn't OOP. 5 Vectors???
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
what do I extend to make this class
You don't extend any class. The new class contains the line's properties and a drawMe method.
The paintComponent method for panel that the lines are being drawn on would call the drawMe(graphics) method for each of the saved lines. The drawMe method would draw the line on the panel referenced by the graphics object.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
@efxee
I was answering the OPs question about this:
The problem is. on the next click/drag, the old line disappears when the paint method is called again.
I did not look at your code.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656