using Java2D or Swing?

Hi!

I don't know which of the two technologies - swing, Java2D - I should use.

The aim is to have an Application (made with swing-features), in which I'm able to change the positions of some Items, through moving the mouse. So the Items have to be repainted kind of very often.

I'm wondering wether to paint those Items with Java2D or take a JLabel together with some kind of LayoutManager. Probably I'd have to write my own one, wouldn't I?

Another thing I don't know: Can one use a Java2D with some kind of Layout Manager? Or is this a mixture not allowed?

Thanks for reading an *;) perhaps also answering!

Tille

[664 byte] By [Tille2a] at [2007-11-27 5:53:53]
# 1
Java2D is painting, so no it has nothing to do with LayoutManagers or Swing (except...).Swing is specific components, which use Java2D stuff to paint themselves. Which to use is entirely up to you. One is not really going to be easier than the other. Just a few differences.
bsampieria at 2007-7-12 15:47:40 > top of Java-index,Desktop,Core GUI APIs...
# 2

Ok, this is the answer I really did not want to hear, but already thought, that it will be the one I get.

Well, so, how do I decide it? I mean, the reason, I thought about using Java2D was, that the painting at defined but changing locations did not really work well with labels, so I decided so take J2D. The problem here is, at least I think it is like this, that I can't add any Listeners, can I?

Tille2a at 2007-7-12 15:47:40 > top of Java-index,Desktop,Core GUI APIs...
# 3
I haven't actually read this article, but maybe there's something for you here: http://java.sun.com/developer/technicalArticles/javase/overlaymaker/ (but maybe you have already read that...?)
rebola at 2007-7-12 15:47:41 > top of Java-index,Desktop,Core GUI APIs...
# 4

Well, you can add labels to a panel which has a null layout, in which case you control the location by calling setLocation or setBounds on the label. You can then add mouse listeners to the labels to track dragging and update the location accordingly.

If you want to use painting directly, you have to have your mouse listener on the panel, then have some other object to define the bounds of the "labels" and detect when the user has clicked and dragged within one.

It's hard to say which is better. There's a bit of overlap between the 2. But there are other considerations. For example, with the custom painting, the best performance is going to come from triggering repaints only in the areas necessary (the area vacated by the moving "label" and the new area for the label). Moving JLabels around a JPanel, this will be handled automatically.

bsampieria at 2007-7-12 15:47:41 > top of Java-index,Desktop,Core GUI APIs...