Skip to main content

Problem with layers

2 replies [Last post]
Joined: 2008-05-16


I'm creating simple shape editor.

I have a little problem with calling repaint() on the whole JPanel, because i show also grid in the background.

For example when I'm doing multiple selection, just because I want to show current selection boundary I have to repaint everything ... grid, geometry, selection box.

So my question is:
Is it possible to create more ovelaping layers and each one renders only itself ? (analogy to glasspane)

I'd like to have:
1st layer ... grid
2nd layer ... geometry
3rd layer ... guides, select boxes .. a.s.o.

Anyone can point me out to solution? Or a pages? Thanks a lot lot !!!


Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2008-08-11

I've come across this from a Sun book published in 1999, "Java 2D API Graphics"

What i would like to know is, 9 years on is this the best way to do it? Is there an alternate framwork, method or pattern?

Joined: 2003-06-13

The basic concepts of Java2D are based on the basic concepts of computer graphics, and they have been explored since the 1960's. So a lot of the core concepts are as relevant today as they were in 1999. The painter's model is the key abstraction. When you paint, paint the objects in the background before the objects in the foreground, because foreground objects obscure background objects.

The latest modern design strategy centers around the concept of "Painters." This is just a delegate object that does the actual painting. So instead of having a single paint() method in a component that does all the work, that component now calls out to its Painter delegate to do the actual painting. This allows for a more flexible design since you can swap out painters as needed to produce different effects, and you can combine painters to do combination of effects.

The Filthy Rich Clients book discusses this concept in great detail, and the SwingX project in SwingLabs uses this model extensively in its components.