Draggable rectangles in Java 2D [duplicate]

混江龙づ霸主 提交于 2019-12-17 10:06:43

问题


Possible Duplicate:
how to drag object

I need to draw some UML components (classes, packages etc) using Java 2D and then be able to drag them around. Is there a way to do this? I mean, to make a shape "draggable"?


回答1:


JHotDraw was designed as "a Java GUI framework for technical and structured Graphics." The linked JHotDraw Pattern Language: JHotDraw Domain Overview illustrates how to customize drawing editors. The sample org.jhotdraw.samples.draw.Main is a reasonable starting point, and JModeller is a simple UML editor built using the framework.




回答2:


Are you forced to swing?

If not you might have look at draw2d which is a java library that works on a SWT canvas. You can find some examples of draw2d here.




回答3:


You can only add MouseListener to a (J)Component. All Java2D stuff is painted on the component. If you manage all shapes in a List you can search for the correct shape under the mouse courser, move it and repaint the component.



来源:https://stackoverflow.com/questions/13997296/draggable-rectangles-in-java-2d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!