jdk/src/share/demo/applets/MoleculeViewer/XYZApp.java
author goetz
Thu, 21 Nov 2013 18:29:34 -0800
changeset 22852 1063026e8cee
parent 20116 af846cd89e6b
child 23010 6dadb192ad81
permissions -rw-r--r--
8028471: PPC64 (part 215): opto: Extend ImplicitNullCheck optimization. Summary: Fixed Implicit NULL check optimization for AIX, where the page at address '0' is only write-protected. Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
     2
 * Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8971
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8971
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8971
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8971
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8971
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8971
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8971
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8971
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 8971
diff changeset
    40
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    41
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    42
import java.applet.Applet;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    43
import java.awt.Image;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    44
import java.awt.Graphics;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    45
import java.awt.Dimension;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    46
import java.awt.event.MouseEvent;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    47
import java.awt.event.MouseListener;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    48
import java.awt.event.MouseMotionListener;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    49
import java.net.URL;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    50
import java.awt.image.IndexColorModel;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    51
import java.awt.image.MemoryImageSource;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    52
import java.io.BufferedReader;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    53
import java.io.IOException;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    54
import java.io.InputStream;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    55
import java.io.InputStreamReader;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    56
import java.io.StreamTokenizer;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    57
import java.util.HashMap;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    58
import java.util.Map;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    59
import java.util.logging.Level;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    60
import java.util.logging.Logger;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    61
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * A set of classes to parse, represent and display Chemical compounds in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * .xyz format (see http://chem.leeds.ac.uk/Project/MIME.html)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    67
/** The representation of a Chemical .xyz model */
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    68
final class XYZChemModel {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    float vert[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    Atom atoms[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    int tvert[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    int ZsortMap[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    int nvert, maxvert;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    75
    static final Map<String, Atom> atomTable = new HashMap<String, Atom>();
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    76
    static Atom defaultAtom;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        atomTable.put("c", new Atom(0, 0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        atomTable.put("h", new Atom(210, 210, 210));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        atomTable.put("n", new Atom(0, 0, 255));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        atomTable.put("o", new Atom(255, 0, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        atomTable.put("p", new Atom(255, 0, 255));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        atomTable.put("s", new Atom(255, 255, 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        atomTable.put("hn", new Atom(150, 255, 150)); /* !!*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        defaultAtom = new Atom(255, 100, 200);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    boolean transformed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    Matrix3D mat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    float xmin, xmax, ymin, ymax, zmin, zmax;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    92
    XYZChemModel() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        mat = new Matrix3D();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        mat.xrot(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        mat.yrot(30);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    98
    /** Create a Chemical model by parsing an input stream */
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
    99
    XYZChemModel(InputStream is) throws Exception {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   100
        this();
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   101
        StreamTokenizer st = new StreamTokenizer(
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   102
                new BufferedReader(new InputStreamReader(is, "UTF-8")));
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   103
        st.eolIsSignificant(true);
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   104
        st.commentChar('#');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   106
        try {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   107
            scan:
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   108
            while (true) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   109
                switch (st.nextToken()) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   110
                    case StreamTokenizer.TT_EOF:
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   111
                        break scan;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   112
                    default:
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   113
                        break;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   114
                    case StreamTokenizer.TT_WORD:
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   115
                        String name = st.sval;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   116
                        double x = 0,
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   117
                         y = 0,
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   118
                         z = 0;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   119
                        if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   120
                            x = st.nval;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   121
                            if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   122
                                y = st.nval;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   123
                                if (st.nextToken() == StreamTokenizer.TT_NUMBER) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   124
                                    z = st.nval;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   125
                                }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   126
                            }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   127
                        }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   128
                        addVert(name, (float) x, (float) y, (float) z);
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   129
                        while (st.ttype != StreamTokenizer.TT_EOL
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   130
                                && st.ttype != StreamTokenizer.TT_EOF) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   131
                            st.nextToken();
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   132
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   134
                }   // end Switch
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   136
            }  // end while
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   138
            is.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   140
        } // end Try
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   141
        catch (IOException e) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   142
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   144
        if (st.ttype != StreamTokenizer.TT_EOF) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   145
            throw new Exception(st.toString());
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   146
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }  // end XYZChemModel()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /** Add a vertex to this model */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    int addVert(String name, float x, float y, float z) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        int i = nvert;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   153
        if (i >= maxvert) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (vert == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                maxvert = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                vert = new float[maxvert * 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                atoms = new Atom[maxvert];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                maxvert *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                float nv[] = new float[maxvert * 3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                System.arraycopy(vert, 0, nv, 0, vert.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                vert = nv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                Atom na[] = new Atom[maxvert];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                System.arraycopy(atoms, 0, na, 0, atoms.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                atoms = na;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   167
        }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   168
        Atom a = atomTable.get(name.toLowerCase());
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   169
        if (a == null) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   170
            a = defaultAtom;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   171
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        atoms[i] = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        i *= 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        vert[i] = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        vert[i + 1] = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        vert[i + 2] = z;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        return nvert++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /** Transform all the points in this model */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    void transform() {
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   182
        if (transformed || nvert <= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   184
        }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   185
        if (tvert == null || tvert.length < nvert * 3) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            tvert = new int[nvert * 3];
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   187
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        mat.transform(vert, tvert, nvert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        transformed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /** Paint this model to a graphics context.  It uses the matrix associated
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   193
    with this model to map from model space to screen space.
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   194
    The next version of the browser should have double buffering,
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   195
    which will make this *much* nicer */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    void paint(Graphics g) {
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   197
        if (vert == null || nvert <= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            return;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   199
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        transform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        int v[] = tvert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        int zs[] = ZsortMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (zs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            ZsortMap = zs = new int[nvert];
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   205
            for (int i = nvert; --i >= 0;) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                zs[i] = i * 3;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   207
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
         * I use a bubble sort since from one iteration to the next, the sort
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
         * order is pretty stable, so I just use what I had last time as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
         * "guess" of the sorted order.  With luck, this reduces O(N log N)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
         * to O(N)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        for (int i = nvert - 1; --i >= 0;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            boolean flipped = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            for (int j = 0; j <= i; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                int a = zs[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                int b = zs[j + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                if (v[a + 2] > v[b + 2]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    zs[j + 1] = a;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    zs[j] = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    flipped = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   228
            if (!flipped) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                break;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   230
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        int lim = nvert;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   234
        if (lim <= 0 || nvert <= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   236
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        for (int i = 0; i < lim; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            int j = zs[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            int grey = v[j + 2];
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   240
            if (grey < 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                grey = 0;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   242
            }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   243
            if (grey > 15) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                grey = 15;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   245
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // g.drawString(names[i], v[j], v[j+1]);
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   247
            atoms[j / 3].paint(g, v[j], v[j + 1], grey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            // g.drawImage(iBall, v[j] - (iBall.width >> 1), v[j + 1] -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            // (iBall.height >> 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /** Find the bounding box of this model */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    void findBB() {
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   255
        if (nvert <= 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            return;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   257
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        float v[] = vert;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   259
        float _xmin = v[0], _xmax = _xmin;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   260
        float _ymin = v[1], _ymax = _ymin;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   261
        float _zmin = v[2], _zmax = _zmin;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        for (int i = nvert * 3; (i -= 3) > 0;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            float x = v[i];
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   264
            if (x < _xmin) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   265
                _xmin = x;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   266
            }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   267
            if (x > _xmax) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   268
                _xmax = x;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   269
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            float y = v[i + 1];
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   271
            if (y < _ymin) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   272
                _ymin = y;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   273
            }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   274
            if (y > _ymax) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   275
                _ymax = y;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   276
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            float z = v[i + 2];
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   278
            if (z < _zmin) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   279
                _zmin = z;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   280
            }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   281
            if (z > _zmax) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   282
                _zmax = z;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   283
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   285
        this.xmax = _xmax;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   286
        this.xmin = _xmin;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   287
        this.ymax = _ymax;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   288
        this.ymin = _ymin;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   289
        this.zmax = _zmax;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   290
        this.zmin = _zmin;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   294
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
/** An applet to put a Chemical model into a page */
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   296
@SuppressWarnings("serial")
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   297
public class XYZApp extends Applet implements Runnable, MouseListener,
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   298
        MouseMotionListener {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   299
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    XYZChemModel md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    boolean painted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    float xfac;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    int prevx, prevy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    float scalefudge = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    Matrix3D amat = new Matrix3D(), tmat = new Matrix3D();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    String mdname = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    String message = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    Image backBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    Graphics backGC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    Dimension backSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    private synchronized void newBackBuffer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        backBuffer = createImage(getSize().width, getSize().height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (backGC != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            backGC.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        backGC = backBuffer.getGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        backSize = getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   321
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public void init() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        mdname = getParameter("model");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            scalefudge = Float.valueOf(getParameter("scale")).floatValue();
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   326
        } catch (Exception ignored) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   327
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        amat.yrot(20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        amat.xrot(20);
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   330
        if (mdname == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            mdname = "model.obj";
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   332
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        resize(getSize().width <= 20 ? 400 : getSize().width,
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   334
                getSize().height <= 20 ? 400 : getSize().height);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        newBackBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        addMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        addMouseMotionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   340
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    public void destroy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        removeMouseListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        removeMouseMotionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   346
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        InputStream is = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
20116
af846cd89e6b 8020060: MoleculeViewerTest demo doesn't work due to SecurityPermissions
alexsch
parents: 10292
diff changeset
   351
            is = getClass().getResourceAsStream(mdname);
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   352
            XYZChemModel m = new XYZChemModel(is);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            Atom.setApplet(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            md = m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            m.findBB();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            float xw = m.xmax - m.xmin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            float yw = m.ymax - m.ymin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            float zw = m.zmax - m.zmin;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   359
            if (yw > xw) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                xw = yw;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   361
            }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   362
            if (zw > xw) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                xw = zw;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   364
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            float f1 = getSize().width / xw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            float f2 = getSize().height / xw;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            xfac = 0.7f * (f1 < f2 ? f1 : f2) * scalefudge;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   368
        } catch (Exception e) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   369
            Logger.getLogger(XYZApp.class.getName()).log(Level.SEVERE, null, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            md = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            message = e.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        try {
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   374
            if (is != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                is.close();
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   376
            }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   377
        } catch (Exception ignored) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    }
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   381
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   382
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public void start() {
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   384
        if (md == null && message == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            new Thread(this).start();
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   386
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   388
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   389
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    public void stop() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    }
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   392
    /* event handling */
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   393
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   394
    @Override
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   395
    public void mouseClicked(MouseEvent e) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   396
    }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   397
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   398
    @Override
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   399
    public void mousePressed(MouseEvent e) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   400
        prevx = e.getX();
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   401
        prevy = e.getY();
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   402
        e.consume();
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   403
    }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   404
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   405
    @Override
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   406
    public void mouseReleased(MouseEvent e) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   407
    }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   408
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   409
    @Override
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   410
    public void mouseEntered(MouseEvent e) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   411
    }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   412
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   413
    @Override
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   414
    public void mouseExited(MouseEvent e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   417
    @Override
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   418
    public void mouseDragged(MouseEvent e) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   419
        int x = e.getX();
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   420
        int y = e.getY();
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   421
        tmat.unit();
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   422
        float xtheta = (prevy - y) * (360.0f / getSize().width);
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   423
        float ytheta = (x - prevx) * (360.0f / getSize().height);
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   424
        tmat.xrot(xtheta);
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   425
        tmat.yrot(ytheta);
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   426
        amat.mult(tmat);
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   427
        if (painted) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   428
            painted = false;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   429
            repaint();
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   430
        }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   431
        prevx = x;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   432
        prevy = y;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   433
        e.consume();
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   434
    }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   435
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   436
    @Override
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   437
    public void mouseMoved(MouseEvent e) {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   438
    }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   439
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   440
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public void update(Graphics g) {
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   442
        if (backBuffer == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            g.clearRect(0, 0, getSize().width, getSize().height);
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   444
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        paint(g);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   448
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    public void paint(Graphics g) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (md != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            md.mat.unit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            md.mat.translate(-(md.xmin + md.xmax) / 2,
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   453
                    -(md.ymin + md.ymax) / 2,
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   454
                    -(md.zmin + md.zmax) / 2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            md.mat.mult(amat);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            // md.mat.scale(xfac, -xfac, 8 * xfac / getSize().width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            md.mat.scale(xfac, -xfac, 16 * xfac / getSize().width);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            md.mat.translate(getSize().width / 2, getSize().height / 2, 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            md.transformed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            if (backBuffer != null) {
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   461
                if (!backSize.equals(getSize())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                    newBackBuffer();
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   463
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                backGC.setColor(getBackground());
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   465
                backGC.fillRect(0, 0, getSize().width, getSize().height);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                md.paint(backGC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                g.drawImage(backBuffer, 0, 0, this);
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   468
            } else {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   469
                md.paint(g);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            setPainted();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        } else if (message != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            g.drawString("Error in model:", 3, 20);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            g.drawString(message, 10, 40);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   477
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    private synchronized void setPainted() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        painted = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   483
    @Override
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   484
    public String getAppletInfo() {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   485
        return "Title: XYZApp \nAuthor: James Gosling \nAn applet to put"
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   486
                + " a Chemical model into a page.";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   489
    @Override
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   490
    public String[][] getParameterInfo() {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   491
        String[][] info = {
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   492
            { "model", "path string", "The path to the model to be displayed"
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   493
                + " in .xyz format "
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   494
                + "(see http://chem.leeds.ac.uk/Project/MIME.html)."
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   495
                + "  Default is model.obj." },
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   496
            { "scale", "float", "Scale factor.  Default is 1 (i.e. no scale)." }
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   497
        };
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   498
        return info;
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   499
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
}   // end class XYZApp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   502
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
class Atom {
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   504
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    private static Applet applet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    private static byte[] data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    private final static int R = 40;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    private final static int hx = 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    private final static int hy = 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    private final static int bgGrey = 192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    private final static int nBalls = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    private static int maxr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    private int Rl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    private int Gl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    private int Bl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    private Image balls[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        data = new byte[R * 2 * R * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        int mr = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        for (int Y = 2 * R; --Y >= 0;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            int x0 = (int) (Math.sqrt(R * R - (Y - R) * (Y - R)) + 0.5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            int p = Y * (R * 2) + R - x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            for (int X = -x0; X < x0; X++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                int x = X + hx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                int y = Y - R + hy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                int r = (int) (Math.sqrt(x * x + y * y) + 0.5);
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   528
                if (r > mr) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    mr = r;
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   530
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                data[p++] = r <= 0 ? 1 : (byte) r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        maxr = mr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
    }
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   536
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    static void setApplet(Applet app) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        applet = app;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   540
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    Atom(int Rl, int Gl, int Bl) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        this.Rl = Rl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        this.Gl = Gl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        this.Bl = Bl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    }
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   546
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   547
    private int blend(int fg, int bg, float fgfactor) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        return (int) (bg + (fg - bg) * fgfactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    }
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   550
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    private void Setup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        balls = new Image[nBalls];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        byte red[] = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        red[0] = (byte) bgGrey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        byte green[] = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        green[0] = (byte) bgGrey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        byte blue[] = new byte[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        blue[0] = (byte) bgGrey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        for (int r = 0; r < nBalls; r++) {
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   560
            float b = (float) (r + 1) / nBalls;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            for (int i = maxr; i >= 1; --i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                float d = (float) i / maxr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            IndexColorModel model = new IndexColorModel(8, maxr + 1,
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   568
                    red, green, blue, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            balls[r] = applet.createImage(
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   570
                    new MemoryImageSource(R * 2, R * 2, model, data, 0, R * 2));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    }
8971
c2519876ef49 7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents: 5506
diff changeset
   573
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    void paint(Graphics gc, int x, int y, int r) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        Image ba[] = balls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        if (ba == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            Setup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            ba = balls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        Image i = ba[r];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        int size = 10 + r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        gc.drawImage(i, x - (size >> 1), y - (size >> 1), size, size, applet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
}