Results 1 to 2 of 2
Thread: Drag and drop
- 02-16-2009, 06:01 AM #1Member
- Join Date
- Feb 2009
- Rep Power
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?
Last edited by thayalan; 02-16-2009 at 06:12 AM.
- 02-16-2009, 04:04 PM #2
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.
- By ysl530 in forum AWT / SwingReplies: 4Last Post: 01-22-2011, 05:58 PM
- By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 05:37 PM
- By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 05:36 PM
- By abhivenugopal in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 01-30-2008, 03:10 PM
- By RO86 in forum AWT / SwingReplies: 0Last Post: 08-14-2007, 02:22 PM