hey,

i have drawn few 2d lines and i would like to apply a button for each of them so once the button is clicked that line is removed once clicked again it would reappear. the rest of the lines stay where they are unless their buttons are clicked.

how could this be done??


thank you very much

Recommended Answers

All 5 Replies

With an object to "record" the coordinates of the line and then either drawing, or drawing over, the line on button click using an actionlistner and swingutilities.

Thanks a lot for your reply,
I have actuly read about this , that you mentioned, but I am still very uncler how the code should look like.
The most important thing for me is removing the line , I think I could figure out how to draw the line on my own.
It would be great if you could show me some example that would delete the line when the button is clicked.

Thank you very much for your help, it is greatly appreciated

Drawing over a line to delete it doesn't work too well because you will end up with gaps in the other lines where the deleted line overlapped them.
Each time a button is clicked update a list of which lines should be visible, then clear the whole drawing area and re-draw all the lines that should be visible.

i understand the theory of it but i tried putting the code in to practise, it doesnt work...
i might be missing something does anyone know any examples of it ?
i been stuck for ages now and would appreciate the help


thank you

Create a simple class to hold your lines - instance vars: int fromX, fromY, toX, toY and boolean visible (maybe color as well?).
Create a "global" ArrayList to hold instances of that class. Add all your lines to it, and keep the "visible" boolean up to date as the buttons are clicked.
Whenever you change anything about any line force a re-draw of your window. In the paintComponent method loop thru the list drawing any line whose visibility is "true".
Share your best version here and we'll have a look.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.