Java Swing - show chosen rectangle

snotnose

Ars Praefectus
3,454
Subscriptor
I'm learning Java by writing a Mandelbrot program. I can draw the image, then use the mouse to choose an area to magnify. Works great, except...

When I choose the area to magnify (mouse down, move mouse, mouse up) there's no outline of the rectangle on the image. Is there a variable I can set, or method I can call, to show the outline of that rectangle as I draw it?

I'm not looking for code to actually draw the rectangle, that's a YALE (Yet Another Learning Experience) if I have to write it. I just don't want to write code I don't have to.
 

Drizzt321

Ars Legatus Legionis
34,920
Subscriptor++
Oh man, it's been forever (college) since I did any Swing. Was annoying enough then, I can't imagine it's gotten better. At least it was slightly better than the AWT before it. Even so...

Might need to create a rectangle/overlay JComponent with the border set to on and keep resizing it as the mouse moves. Yeah...welcome to Java Swing.

Of course, I might be very terribly out of date at this time, and that's all from very vague recollections from 20+ years ago college work. Probably don't have that project, I lost a bunch of stuff I thought I had backed up. Although I do have some old DVDs I backed stuff up on. I should take a look through them actually.
 

ShuggyCoUk

Ars Legatus Legionis
10,387
Subscriptor++
(My java is hopelessly out of date - but that might help for Swing).

If you want to do this yourself the simplest approach is using XOR rendering to make it trivial to "undo" the previous rectangle you drew (if any) and then draw the next.
This also makes the rectangle visible no matter what the colour under it is (nice in a mandelbrot).

You can follow this libraries approach through if you like. Though it is likely way more complex/generalized than you want right now the basics of "here's the state I meed to maintain, here's how to do XOR rendering" part should be fine