FileDocCategorySizeDatePackage
Molecule.javaAPI DocExample5389Sat Apr 23 22:35:42 BST 2005magicbeans.sunw.demo.molecule

Molecule

public class Molecule extends JComponent implements Serializable, MouseListener, MouseMotionListener
A bean to parse, represent and display Chemical compounds in .xyz format (see http://chem.leeds.ac.uk/Project/MIME.html)

Fields Summary
private static int
ourVersion
XYZChemModel
md
float
xfac
int
prevx
int
prevy
float
xtheta
float
ytheta
float
scalefudge
Matrix3D
amat
Matrix3D
tmat
String
message
Image
backBuffer
Graphics
backGC
int
width
int
height
private String
moleculeName
Constructors Summary
public Molecule()


      
	reset();
	amat.yrot(20);
	amat.xrot(20);
	setBackground(java.awt.Color.white);
	addMouseListener(this);
	addMouseMotionListener(this);
    
Methods Summary
public java.lang.StringgetMoleculeName()

	return moleculeName;
    
public java.awt.DimensiongetPreferredSize()

	return new java.awt.Dimension(150,150);
    
public synchronized voidinitialize()


	InputStream is = null;

	try {
	    width = getSize().width;
	    height = getSize().height;

	    is = this.getClass().getResourceAsStream(moleculeName + ".xyz");

	    XYZChemModel m = new XYZChemModel(is);
	    Atom.setComponent(this);
	    md = m;
	    m.findBB();
	    float xw = m.xmax - m.xmin;
	    float yw = m.ymax - m.ymin;
	    float zw = m.zmax - m.zmin;
	    if (yw > xw) {
		xw = yw;
	    }
	    if (zw > xw) {
		xw = zw;
	    }
	    float f1 = width / xw;
	    float f2 = height / xw;
	    xfac = 0.7f * (f1 < f2 ? f1 : f2) * scalefudge;
	    backBuffer = createImage(width, height);
	    backGC = backBuffer.getGraphics();
	} catch(Exception e) {
	    e.printStackTrace();
	    md = null;
	    message = e.toString();
	}
	try {
	    if (is != null) {
		is.close();
	    }
	} catch(Exception e) {
	}
    
public voidmouseClicked(java.awt.event.MouseEvent evt)

    
public voidmouseDragged(java.awt.event.MouseEvent evt)

 
        rotate(evt.getX(), evt.getY());
    
public voidmouseEntered(java.awt.event.MouseEvent evt)

    
public voidmouseExited(java.awt.event.MouseEvent evt)

    
public voidmouseMoved(java.awt.event.MouseEvent evt)

    
public synchronized voidmousePressed(java.awt.event.MouseEvent evt)

	prevx = evt.getX();
	prevy = evt.getY();
    
public voidmouseReleased(java.awt.event.MouseEvent evt)

    
public synchronized voidpaint(java.awt.Graphics g)


	if (backBuffer == null || getSize().width != width 
				   || getSize().height != height) {
	    initialize();
	}

	if (md != null) {
	    md.mat.unit();
	    md.mat.translate(-(md.xmin + md.xmax) / 2,
			     -(md.ymin + md.ymax) / 2,
			     -(md.zmin + md.zmax) / 2);
	    md.mat.mult(amat);
	    // md.mat.scale(xfac, -xfac, 8 * xfac / getSize().width);
	    md.mat.scale(xfac, -xfac, 16 * xfac / getSize().width);
	    md.mat.translate(getSize().width / 2, getSize().height / 2, 8);
	    md.transformed = false;

	    backGC.setColor(getBackground());
	    backGC.fillRect(0,0,getSize().width, getSize().height);
	    md.paint(backGC);
	    g.drawImage(backBuffer, 0, 0, this);

	} else if (message != null) {
	    g.drawString("Error in model:", 3, 20);
	    g.drawString(message, 10, 40);
	}
    
private voidreadObject(java.io.ObjectInputStream s)

	// Compensate for missing constructor.
	reset();
	if (s.readInt() != ourVersion) {
	    throw new IOException("Molecule.readObject: version mismatch");
	}
	moleculeName = (String) s.readObject();
    
private synchronized voidreset()

	md = null;
	xfac = (float)0.0;
	prevx = 0;
	prevy = 0;
	xtheta = (float)0.0;
	ytheta = (float)0.0;
	scalefudge = 1;
        amat = new Matrix3D();
        tmat = new Matrix3D();
	message = null;
	backBuffer = null;
	backGC = null;
    
private synchronized voidrotate(int x, int y)

	tmat.unit();
	float xtheta = (prevy - y) * (360.0f / width);
	float ytheta = (x - prevx) * (360.0f / height);
	tmat.xrot(xtheta);
	tmat.yrot(ytheta);
	amat.mult(tmat);
        repaint();
	prevx = x;
	prevy = y;
    
public synchronized voidrotateOnX()

	int x = (prevx + 10)%width;
	int y = prevy;
	rotate(x, y);
    
public synchronized voidrotateOnY()

	int x = prevx;
	int y = (prevy + 10)%height;
	rotate(x, y);
    
public synchronized voidrotateX(java.awt.event.ActionEvent b)


         rotateOnX();
    
public synchronized voidrotateY(java.awt.event.ActionEvent b)

	rotateOnY();
    
public voidsetMoleculeName(java.lang.String name)

	moleculeName = name;
	reset();
	repaint();
    
public synchronized voidupdate(java.awt.Graphics g)

	if (backBuffer == null) {
	    g.clearRect(0, 0, getSize().width, getSize().height);
	}
	paint(g);
    
private voidwriteObject(java.io.ObjectOutputStream s)

	s.writeInt(ourVersion);
	s.writeObject(moleculeName);