In the mouse click handler just create a new AndGate() or whatever and add it to the "canvas" (an ordinary JPanel would do) at the mouse click coordinates (use absolute positioning and a null layout manager for the "canvas").
ps drag'n'drop really isn't that hard - certainly less hard than writing your own code to move things around.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
how can I make the label movable after it's been created
The mouse motion listener will help you with that. It takes some playing with to get it to move smoothly but its possible.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
any way to keep the references to these dynamically created labels?
Keep them in a list, like Vector or ArrayList
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
Here, just for fun, is runnable demo of just how easy it is to drag a JLabel around a container and log the final position. The trick is to remember that the mouse event coordinates are relative to the object being dragged, not the container, so you have to see how much the mouse has moved, and move the JLabel by the same amount.
I deliberately left out the comments to give everyone a learning opportunity ;-)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Demo implements MouseListener, MouseMotionListener {
public static void main(String[] args) {
new Demo();
}
JLabel sprite = new JLabel("drag me");
Demo() {
JFrame frame = new JFrame("drag demo");
frame.setLayout(null);
frame.setMinimumSize(new Dimension(400, 300));
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sprite.setBounds(10, 10, 80, 20);
sprite.addMouseListener(this);
sprite.addMouseMotionListener(this);
frame.add(sprite);
frame.setVisible(true);
}
int startDragX, startDragY;
boolean inDrag = false;
@Override
public void mouseEntered(MouseEvent e) {
// not interested
}
@Override
public void mouseExited(MouseEvent e) {
// not interested
}
@Override
public void mousePressed(MouseEvent e) {
startDragX = e.getX();
startDragY = e.getY();
}
@Override
public void mouseReleased(MouseEvent e) {
if (inDrag) {
System.out.println("Sprite dragged to " + sprite.getX() + ", " + sprite.getY());
inDrag = false;
}
}
@Override
public void mouseClicked(MouseEvent e) {
// not interested
}
@Override
public void mouseDragged(MouseEvent e) {
int newX = sprite.getX() + (e.getX() - startDragX);
int newY = sprite.getY() + (e.getY() - startDragY);
sprite.setLocation(newX, newY);
inDrag = true;
}
@Override
public void mouseMoved(MouseEvent arg0) {
// not interested
}
}
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
Since you are NOT using a layout manager, I don't think there is a need to validate.
Try calling repaint();
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
I am in the same situation as OP(just started learning java recently and chose this as my project). All this information is very helpful. I would also like to know how to save the circuit into a file so that it can be loaded and used later
v3ga
Junior Poster in Training
95 posts since Oct 2011
Reputation Points: 14
Solved Threads: 4
Please start your own thread for your peoblem.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656