Drawing outside a component's bounds
Asked Answered
A

1

7

I'm making a component (extending JComponent) which will have some decoration drawn over the top of it and partly outside its bounds.

I would like to know if there's a way of drawing outside the component using self-contained code (I don't want to have to do any drawing in the parent container, for example).

Azov answered 1/11, 2011 at 18:18 Comment(1)
Try the setClip method of GRaphics. In my attempt it worked fine.Inna
W
2

AFAIK, there is no easy solution. One way to achieve this is to draw on the container's GlassPane, as shown here. IMHO, I would just modify the Graphics object of the component to change the "apparent" bounds, which will enable you to draw stuff that's "outside the bounds" on the component itself.

Wadley answered 1/11, 2011 at 18:28 Comment(4)
You mean with setClip? I thought that could only be used to shrink the drawing area.Azov
OK, I was wrong. The reason I've only just marked this as the answer is that I've just fixed a problem that I've been stuck on for hours which has a really simple solution: If you don't repaint the parent container as well as the component, no rendering will show up on it. Obvious, but not easy to spot because I was redrawing in mouse and component events, some of which repaint the container and some of which don't.Azov
@Tharwen, Then you should answer your own question and mark that as the correct one. :DWadley
No, your answer was the solution, but that bold part was just a thing that I thought would be helpful for anyone reading this later.Azov

© 2022 - 2024 — McMap. All rights reserved.