Hi all,

I'm having a bit of trouble with atom indexes. I'm using mouse over 
events to get the atom under the mouse cursor and then trying to select 
the same atom in Jmol. In essence it's this:

int atom = findNearestAtomIndex(e.getPoint()); // e is of type MouseEvent
evalString("select atomno=" + atom + ";");

However, the selected atom is not the same as under the mouse. Here's a 
demo app so you can see what I mean (please excuse the messy use of 
reflection). You will need Jmol and the 2 PDB files in the same 
directory as the compiled class file to run it.

Thanks in advance for any help.

Cheers,
Chris Foley.








package JmolAtomDemo;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.lang.reflect.Method;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.jmol.adapter.smarter.SmarterJmolAdapter;
import org.jmol.viewer.Viewer;

public class JmolAtomDemo extends JPanel {
    private static final long serialVersionUID = 1L;
   
   
   
    // Oh so messy!
    private static Method findNearestAtomIndex;
    static {
        Class<?> c = Viewer.class;
        Class<?>[] findNearestAtomIndexParams = {int.class, int.class};
        try {findNearestAtomIndex = 
c.getDeclaredMethod("findNearestAtomIndex", findNearestAtomIndexParams);
        } catch (Exception e) {e.printStackTrace();}
        findNearestAtomIndex.setAccessible(true);
    }
   
   
   
    private Viewer viewer;
   
    public JmolAtomDemo(String pdb) {
        viewer = new Viewer(this, new SmarterJmolAdapter() );
        viewer.setAppletContext("",null,null,"");
        viewer.openFile(pdb);
        viewer.evalString("selectionHalos ON;");
       
        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                int atomIndex = findNearestAtomIndex(e.getPoint());
                String script = "select atomno=" + atomIndex + ";";
                System.out.println(script);
                viewer.evalString(script);
            }
        });
    } // constructor
   
    private final Dimension currentSize = new Dimension();
    private final Rectangle rectClip = new Rectangle();

    @Override
    public void paint(Graphics g) {
        getSize(currentSize);
        g.getClipBounds(rectClip);
        viewer.renderScreenImage(g, currentSize, rectClip);
    } // paint




    public int findNearestAtomIndex(Point mousePosition) {
        Integer rv = -1;
        try {
            rv = (Integer)findNearestAtomIndex.invoke(
                    viewer, mousePosition.x, mousePosition.y);
        } catch (Exception e) {
            e.printStackTrace();
        } // try
        return rv;
    } // findNearestAtomIndex
   
   
   
    public static void main(String[] args) {
       
       
        JFrame frame1 = new JFrame("1NCP");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.getContentPane().add(new JmolAtomDemo("1NCP.pdb"));
        frame1.setSize(300, 300);
        frame1.setVisible(true);
       
        JFrame frame2 = new JFrame("1CDH");
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.getContentPane().add(new JmolAtomDemo("1cdh.pdb"));
        frame2.setLocation(300, 0);
        frame2.setSize(300, 300);
        frame2.setVisible(true);
       
       
    } // main
   
   
   
   
}


------------------------------------------------------------------------------
Open Source Business Conference (OSBC), March 24-25, 2009, San Francisco, CA
-OSBC tackles the biggest issue in open source: Open Sourcing the Enterprise
-Strategies to boost innovation and cut costs with open source participation
-Receive a $600 discount off the registration fee with the source code: SFAD
http://p.sf.net/sfu/XcvMzF8H
_______________________________________________
Jmol-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jmol-users

Reply via email to