Drag and drop
I am currently working on developing a module to compute the fault tree probability. I'm planning to use the Java AWT and Swing class to develop a J-Frame or container containing the drag and drop images (AND and OR gate symbols). The images are expected to be drawn(dragged and dropped) at another container. Upon completion the software should compute the value and store them.
The question is how do I go about creating the JFrame or container with the images to be used in the drawing?
Here are a few tips:
Start with a JFrame.
Set the content pane to GridLayout; 1 row, column.
Add a JPanel. The JPanel will automatically fill the JFrame.
Set the JPanel layout manager to null. This causes absolute positioning, which allows you to put components where you want them.
Create a JComponent subclass to represent your gates. JLabel with a border and preferredSize might work quite nicely.
For each gate component, add appropriate listeners for mouse down and mouse up.
When a mouse down event occurs for a gate, start a javax.swing.Timer (*not* a java.util.Timer). Set the timer interval to 30 millis, which will give you a refresh rate of around 33 updates a second.
Every time the timer fires, find the mouse position, and move the selected gate's top/left corner to the mouse position.
Also, you can create a "snap to grid" effect by rounding off the mouse coordinates to the lower 5 or 10.
Stop the time on mouse up.
This approach saves having to monitor mouse move events while providing a smooth motion for the user.