@Override public void mousePressed(MouseEvent e) { elist.setSelected(null); if (e.getButton() == 1) { leftPressed = true; rightPressed = false; MoleculeComponent clickedOn = elist.getClickedComponent(e.getX(), e.getY()); if (clickedOn != null) { elist.setSelected(clickedOn); } mmc.view.displayAttributes(clickedOn); } if (e.getButton() == 3) { rightPressed = true; leftPressed = false; } }
// ***************** MouseListener ***************** public void mouseReleased(MouseEvent e) { currentX = getGraphCoordinateX(e.getX()); currentY = getGraphCoordinateY(e.getY()); MoleculeComponent clickedOn = elist.getClickedComponent(e.getX(), e.getY()); if (drawArrowLine || drawBondLine) // if connection is being made { if (clickedOn != null && elist.getSelected() != null) // if released on valid component { if (clickedOn.getClass() == Element.class && (rightPressed && !leftPressed)) { // if it's a bond elist.add(new Bond(elist.getSelected(), clickedOn)); } else if (clickedOn.getClass() == Element.class && (leftPressed && !rightPressed)) { elist.add(new Arrow(elist.getSelected(), clickedOn)); } } drawArrowLine = false; drawBondLine = false; repaint(); } else // connection is not being made { if (clickedOn != null) // if released on valid component { } else // Didn't click on valid component { if (leftPressed && !rightPressed) // If mouse button clicked was left { elist.add(new Element(currentX, currentY)); } } } }
@Override public void mouseDragged(MouseEvent e) { currentX = getGraphCoordinateX(e.getX()); currentY = getGraphCoordinateY(e.getY()); if (leftPressed && !rightPressed) // Left click is for arrows { /* If left click and drag: * 1. If clicked on nothing, don't draw anything * 2. If clicked on bond, draw line from bond (bond checked first because its * points aren't on the grid system) * 2. If clicked on element, draw line from element */ drawBondLine = false; if (drawArrowLine == true) { repaint(); return; } MoleculeComponent component = elist.getClickedComponent(e.getX(), e.getY()); if (component != null) { elist.setSelected(component); drawArrowLine = true; } // Bond bArrowStart = null; //Attempt to find a bond at this point to drag from // for(Bond b : elist.getBonds()) // { // if (b.contains(e.getX(), e.getY())) // { // System.out.println("YES!"); // bArrowStart = b; // break; // } // else // { // System.out.println("NO :("); // } // } // // Element eArrowStart = elist.getElementAt(roundX, roundY);//Attempt to see if there is an // element. // if(eArrowStart == null) //If there is no element at this point... // { // if(bArrowStart != null)//Check to see if the bond is null. // { // System.out.println("Did it reach here?"); // elist.setSelected(bArrowStart); // drawArrowLine = true; // } // } // else // { // elist.setSelected(elist.getElementAt(eArrowStart.getKey()));//Start the arrow here // drawArrowLine = true; // } } if (rightPressed && !leftPressed) // Right click is for bonding { drawArrowLine = false; /* Cases to consider: * 1. If user is already dragging (this should come first!) * 1. Clicked on element * 2. Clicked no empty grid space */ if (drawBondLine == true) // if the element is already in the process of being bonded { repaint(); return; // No need for further steps. } currentX = getGraphCoordinateX(e.getX()); currentY = getGraphCoordinateY(e.getY()); Element bondStart = elist.getElementAt(currentX, currentY); // If this is the initial movement for bonding: if (bondStart != null) // if bonding began at element { elist.setSelected( elist.getElementAt(bondStart.getKey())); // keep track of the bonding element drawBondLine = true; // indicate that an arrow should start to be drawn } else // if nothing is being dragged { } } // */ }