4

Im doing a personal project on UML class diagram tool in java and running in a few design/programming issues.

The very first thing is the class diagram boxes. I'm thinking if I build my own component to represent it but when it is drawn on a panel, it will be awkward to move around with mouse. Because all the layout managers in java wont let me put the box anywhere freely.

Or if I choose to draw my component as a rectangle box on the panel then it will be the simplest to implement but I can't move it around with mouse.

Any hint or pointer in how should I go on about this?

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
bili
  • 610
  • 2
  • 9
  • 20

6 Answers6

3

The Component Mover class can help you out with dragging components.

camickr
  • 321,443
  • 19
  • 166
  • 288
3

Also consider JGraph, which serves as a foundation for several diagramming tools.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

I'm thinking if I build my own component to represent it but when it is drawn on a panel, it will be awkward to move around with mouse. Because all the layout managers in java wont let me put the box anywhere freely.

If you need to absolute positioning a Component you have to possibilities:

  1. Use a null layout manager(BAD solution, but in certain limited case you can save some time)
  2. Implement your own layout manager

Or if I choose to draw my component as a rectangle box on the panel then it will be the simplest to implement but I can't move it around with mouse.

That's right. If you need mouse interaction it's better use a Component rather then drawing rectangles.

Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • Im actually reading the Oracle tutorial on creating custom layout manager, it seems short. I like to put a new class box on a panel by click on a button on menu panel and left click the mouse (anywhere) on the panel and box will be there. And later on, I can move it with the mouse. Implement my own layout manager seems more appealing. – bili Aug 21 '11 at 01:10
  • @bill: read that tutorial. If you implement your layout manager you will be free to allow absolute positioning of components and at the same time attach a mouse listener to each displayed component. – Heisenbug Aug 21 '11 at 01:13
2

Yet another graph visualization tool is the JUNG framework: comes with useful dynamic "layout managers" which can be extended easily enough. Plus has a rich user (via mouse) default interaction with a pluggable extension design.

kleopatra
  • 51,061
  • 28
  • 99
  • 211
1

Unfortunately Java Swing does not have a Canvas class, which would be ideal for this problem. However, this question and its answers demonstrate how to implement canvas-like behaviour with JPanel. You would draw the boxes manually on the JPanel as though it were a canvas.

To move the boxes around on the panel you could capture mouse events on the JPanel and handle them yourself.

Community
  • 1
  • 1
Martin Doms
  • 8,598
  • 11
  • 43
  • 60
  • I think he thought about drawing rectangles just because he thought that isn't possible to freely move a component because of layout manager. Using a canvas like component isn't a good idea from my point of view, because you can't attach mouse listener on the drawn rectangles. – Heisenbug Aug 21 '11 at 01:06
  • What do you mean Swing doesn't have a "Canvas class"? A Canvas doesn't have any special functionality built into it. You need to add all the functionality yourself. This is exactly what you would do with a JComponent or JPanel. – camickr Aug 21 '11 at 01:16
  • yes, 0verbose is absolutely right. It is the reason I can't really go on with drawing rectangle. – bili Aug 21 '11 at 01:17
  • Perhaps you use the term `Canvas class` to mean a container for graphics widgets, such as [Gtk Canvas](http://live.gnome.org/ProjectRidley/CanvasOverview), not to be confused with [`java.awt.Canvas`](http://download.oracle.com/javase/6/docs/api/java/awt/Canvas.html). – trashgod Aug 21 '11 at 02:18
0

I found this tutorial here Resizable component and it is exactly what I needed! The panel doesn't use any layout manager at all! So now I'm playing with the code a bit and gonna reorganise it to suit the class diagram.

Next thing will be implementing the observer pattern so that arrow will get update-to-date position of the boxes when moved around in panel.

Flexo
  • 87,323
  • 22
  • 191
  • 272
bili
  • 610
  • 2
  • 9
  • 20
  • Hi, welcome to stackoverflow, glad you found the answer you were looking for. This is a Q&A site - we normally use comments and votes and the "accept" function to say thanks for good answers, not more answers. I've edit your answer to remove the thanks you wrote in it. You can use one of the aforementioned methods for thanking answers you found helpful though. – Flexo Aug 22 '11 at 13:24
  • Thanks! I will get familiarise myself with the site rules. – bili Aug 23 '11 at 13:11