Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Simple Line Drawing Program

Tags:

java

line

drawing

I want to create a simple java application for drawing only lines.

My program is like that now;

User can draw everything by draggin his mouse, but by the time he release his finger, I deleted everything from the screen and I draw a line withrespect to first mouse coordinates and the last mouse coordinates.

However, because everytime I cleared the screen, user can only draw one line. If I dont clean the screen, there are lines but also curves and etc which are created while user dragging his mouse.

How should I find a solution for that problem ?

Thanks.

like image 481
CanCeylan Avatar asked Feb 21 '26 04:02

CanCeylan


1 Answers

One straightforward way to solve your problem is to retain state in the program. Every time a line is drawn, store it in an ArrayList of point-pairs. When the user successsfully draws one line, store the start point and end point for that line into the ArrayList. Each time the user draws another line, add that pair of points to the ArrayList. Then, when it is time to draw "all the lines", clear the screen and then use a loop, and draw one line for each stored pair of points.

Somewhere in your program there is a class that has a lifetime that is "as long as a drawing", or "as long as the application runs." That's a good place to keep state.

like image 85
Cheeso Avatar answered Feb 23 '26 18:02

Cheeso



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!