Is there a way to mark only a specific JPanel region as opaque?

I mainly draw a lot of transparent JPanels; profiling shows that most of the time is spent in Component.paint (). This could be optimized quite radically, since in most cases the real opaque area on JPanel is quite small, for example, along the edges.

As of now, repainting () a component will cause a repaint of all its parents, since the RepaintManager cannot know that the dirty parent area is actually opaque and will move up the component hierarchy. I thought about calling markCompletelyClean() for all parents whenever the panel is invalid and itself controls dirty areas with addDirtyRegion() .

However, is there a cleaner approach for marking only certain JPanel rectangles as opaque (or transparent, doesn't matter)?

+4
source share
3 answers

Even if you are doing something unusual with markCompletelyClean () and addDirtyRegion (), I doubt that you will get a big performance benefit. When the swing goes to redraw, it matches all the dirty areas to draw, and starts the redraw with the minimum bounding box of all the dirty areas. Thus, if you mark the perimeter of the JPanel as dirty, the bounding box of the JPanel will be the same as the whole JPanel, which means that you will repaint the whole thing in any way.

+2
source

Consider using JLabel (opaque by default) instead of JPanel , then you can (before JLabel ) add any JComponent the same as in JPanel , but you have to install LayoutManager , you forgot about Opacity/Transparency and Region details (s )

+2
source

Why can't you define your own myRepaint() method calling multiple

 public void repaint(long tm, int x, int y, int width, int height) 

for all borders?

Also try playing with clipBounds in your Graphics to redraw only the parts you need. You can set any custom Shape as a clip.

+1
source

Source: https://habr.com/ru/post/1381815/


All Articles