home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / Molecule.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  5.3 KB  |  243 lines

  1. /**
  2.  * A bean to parse, represent and display Chemical compounds in
  3.  * .xyz format (see http://chem.leeds.ac.uk/Project/MIME.html)
  4.  */
  5.  
  6. package sunw.demo.molecule;
  7.  
  8. import java.io.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11.  
  12. /* A Bean to display a Chemical model */
  13.  
  14. public class Molecule extends java.awt.Panel implements Serializable,
  15.                 MouseListener, MouseMotionListener {
  16.  
  17.     private static int ourVersion = 3;
  18.     XYZChemModel md;
  19.     float xfac;
  20.     int prevx;
  21.     int prevy;
  22.     float xtheta;
  23.     float ytheta;
  24.     float scalefudge;
  25.     Matrix3D amat;
  26.     Matrix3D tmat;
  27.     String message = null;
  28.     Image backBuffer;
  29.     Graphics backGC;
  30.     int width;
  31.     int height;
  32.  
  33.     public Molecule() {
  34.     reset();
  35.     amat.yrot(20);
  36.     amat.xrot(20);
  37.     setBackground(java.awt.Color.white);
  38.     addMouseListener(this);
  39.     addMouseMotionListener(this);
  40.     }
  41.  
  42.     public java.awt.Dimension getPreferredSize() {
  43.     return new java.awt.Dimension(150,150);
  44.     }
  45.  
  46.     /**
  47.      * @deprecated provided for backward compatibility with old layout managers.
  48.      */
  49.     public Dimension preferredSize() {
  50.     return getPreferredSize();
  51.     }
  52.  
  53.     public synchronized void initialize() {
  54.  
  55.     InputStream is = null;
  56.  
  57.     try {
  58.         width = getSize().width;
  59.         height = getSize().height;
  60.  
  61.         is = this.getClass().getResourceAsStream(moleculeName + ".xyz");
  62.  
  63.         XYZChemModel m = new XYZChemModel(is);
  64.         Atom.setPanel(this);
  65.         md = m;
  66.         m.findBB();
  67.         float xw = m.xmax - m.xmin;
  68.         float yw = m.ymax - m.ymin;
  69.         float zw = m.zmax - m.zmin;
  70.         if (yw > xw) {
  71.         xw = yw;
  72.         }
  73.         if (zw > xw) {
  74.         xw = zw;
  75.         }
  76.         float f1 = width / xw;
  77.         float f2 = height / xw;
  78.         xfac = 0.7f * (f1 < f2 ? f1 : f2) * scalefudge;
  79.         backBuffer = createImage(width, height);
  80.         backGC = backBuffer.getGraphics();
  81.     } catch(Exception e) {
  82.         e.printStackTrace();
  83.         md = null;
  84.         message = e.toString();
  85.     }
  86.     try {
  87.         if (is != null) {
  88.         is.close();
  89.         }
  90.     } catch(Exception e) {
  91.     }
  92.     }
  93.  
  94.     private synchronized void rotate(int x, int y) {
  95.     tmat.unit();
  96.     float xtheta = (prevy - y) * (360.0f / width);
  97.     float ytheta = (x - prevx) * (360.0f / height);
  98.     tmat.xrot(xtheta);
  99.     tmat.yrot(ytheta);
  100.     amat.mult(tmat);
  101.         repaint();
  102.     prevx = x;
  103.     prevy = y;
  104.     }
  105.  
  106.     //----------------------------------------------------------------------
  107.  
  108.     // Mouse listener methods.
  109.  
  110.     public void mouseClicked(MouseEvent evt) {
  111.     }
  112.  
  113.     public synchronized void mousePressed(MouseEvent evt) {
  114.     prevx = evt.getX();
  115.     prevy = evt.getY();
  116.     }
  117.  
  118.     public void mouseReleased(MouseEvent evt) {
  119.     }
  120.  
  121.     public void mouseEntered(MouseEvent evt) {
  122.     }
  123.  
  124.     public void mouseExited(MouseEvent evt) {
  125.     }
  126.  
  127.     public void mouseDragged(MouseEvent evt) { 
  128.         rotate(evt.getX(), evt.getY());
  129.     }
  130.  
  131.     public void mouseMoved(MouseEvent evt) {
  132.     }
  133.  
  134.     //----------------------------------------------------------------------
  135.  
  136.     public synchronized void rotateOnY() {
  137.     int x = prevx;
  138.     int y = (prevy + 10)%height;
  139.     rotate(x, y);
  140.     }
  141.  
  142.     public synchronized void rotateY(ActionEvent b) {
  143.     rotateOnY();
  144.     }
  145.  
  146.     public synchronized void rotateOnX() {
  147.     int x = (prevx + 10)%width;
  148.     int y = prevy;
  149.     rotate(x, y);
  150.     }
  151.  
  152.     public synchronized void rotateX(ActionEvent b) {
  153.  
  154.          rotateOnX();
  155.     }
  156.  
  157.     public synchronized void update(Graphics g) {
  158.     if (backBuffer == null) {
  159.         g.clearRect(0, 0, getSize().width, getSize().height);
  160.     }
  161.     paint(g);
  162.     }
  163.  
  164.     public synchronized void paint(Graphics g) {
  165.  
  166.     if (backBuffer == null || getSize().width != width 
  167.                    || getSize().height != height) {
  168.         initialize();
  169.     }
  170.  
  171.     if (md != null) {
  172.         md.mat.unit();
  173.         md.mat.translate(-(md.xmin + md.xmax) / 2,
  174.                  -(md.ymin + md.ymax) / 2,
  175.                  -(md.zmin + md.zmax) / 2);
  176.         md.mat.mult(amat);
  177.         // md.mat.scale(xfac, -xfac, 8 * xfac / getSize().width);
  178.         md.mat.scale(xfac, -xfac, 16 * xfac / getSize().width);
  179.         md.mat.translate(getSize().width / 2, getSize().height / 2, 8);
  180.         md.transformed = false;
  181.  
  182.         backGC.setColor(getBackground());
  183.         backGC.fillRect(0,0,getSize().width, getSize().height);
  184.         md.paint(backGC);
  185.         g.drawImage(backBuffer, 0, 0, this);
  186.  
  187.     } else if (message != null) {
  188.         g.drawString("Error in model:", 3, 20);
  189.         g.drawString(message, 10, 40);
  190.     }
  191.     }
  192.  
  193.     // Support for serialization.  KGH 6/2/96
  194.  
  195.     private void writeObject(java.io.ObjectOutputStream s)
  196.                 throws java.io.IOException {
  197.     s.writeInt(ourVersion);
  198.     s.writeObject(moleculeName);
  199.     }
  200.  
  201.     private void readObject(java.io.ObjectInputStream s)
  202.                 throws java.lang.ClassNotFoundException,
  203.                    java.io.IOException {
  204.     // Compensate for missing constructor.
  205.     reset();
  206.     if (s.readInt() != ourVersion) {
  207.         throw new IOException("Molecule.readObject: version mismatch");
  208.     }
  209.     moleculeName = (String) s.readObject();
  210.     }
  211.  
  212.     private synchronized void reset() {
  213.     md = null;
  214.     xfac = (float)0.0;
  215.     prevx = 0;
  216.     prevy = 0;
  217.     xtheta = (float)0.0;
  218.     ytheta = (float)0.0;
  219.     scalefudge = 1;
  220.         amat = new Matrix3D();
  221.         tmat = new Matrix3D();
  222.     message = null;
  223.     backBuffer = null;
  224.     backGC = null;
  225.     }
  226.  
  227.     //-----------------------------------------------------------------
  228.     // Beans properties.
  229.  
  230.     public String getMoleculeName() {
  231.     return moleculeName;
  232.     }
  233.  
  234.     public void setMoleculeName(String name) {
  235.     moleculeName = name;
  236.     reset();
  237.     repaint();
  238.     }
  239.  
  240.     private String moleculeName = "HyaluronicAcid";
  241.  
  242. }
  243.