author | goetz |
Thu, 21 Nov 2013 18:29:34 -0800 | |
changeset 22852 | 1063026e8cee |
parent 20116 | af846cd89e6b |
child 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* |
4 |
* Redistribution and use in source and binary forms, with or without |
|
5 |
* modification, are permitted provided that the following conditions |
|
6 |
* are met: |
|
7 |
* |
|
8 |
* - Redistributions of source code must retain the above copyright |
|
9 |
* notice, this list of conditions and the following disclaimer. |
|
10 |
* |
|
11 |
* - Redistributions in binary form must reproduce the above copyright |
|
12 |
* notice, this list of conditions and the following disclaimer in the |
|
13 |
* documentation and/or other materials provided with the distribution. |
|
14 |
* |
|
5506 | 15 |
* - Neither the name of Oracle nor the names of its |
2 | 16 |
* contributors may be used to endorse or promote products derived |
17 |
* from this software without specific prior written permission. |
|
18 |
* |
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30 |
*/ |
|
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 | 62 |
|
63 |
/* |
|
64 |
* A set of classes to parse, represent and display Chemical compounds in |
|
65 |
* .xyz format (see http://chem.leeds.ac.uk/Project/MIME.html) |
|
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 | 69 |
|
70 |
float vert[]; |
|
71 |
Atom atoms[]; |
|
72 |
int tvert[]; |
|
73 |
int ZsortMap[]; |
|
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 | 77 |
|
78 |
static { |
|
79 |
atomTable.put("c", new Atom(0, 0, 0)); |
|
80 |
atomTable.put("h", new Atom(210, 210, 210)); |
|
81 |
atomTable.put("n", new Atom(0, 0, 255)); |
|
82 |
atomTable.put("o", new Atom(255, 0, 0)); |
|
83 |
atomTable.put("p", new Atom(255, 0, 255)); |
|
84 |
atomTable.put("s", new Atom(255, 255, 0)); |
|
85 |
atomTable.put("hn", new Atom(150, 255, 150)); /* !!*/ |
|
86 |
defaultAtom = new Atom(255, 100, 200); |
|
87 |
} |
|
88 |
boolean transformed; |
|
89 |
Matrix3D mat; |
|
90 |
float xmin, xmax, ymin, ymax, zmin, zmax; |
|
91 |
||
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
92 |
XYZChemModel() { |
2 | 93 |
mat = new Matrix3D(); |
94 |
mat.xrot(20); |
|
95 |
mat.yrot(30); |
|
96 |
} |
|
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 | 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 | 133 |
|
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
134 |
} // end Switch |
2 | 135 |
|
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
136 |
} // end while |
2 | 137 |
|
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
138 |
is.close(); |
2 | 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 | 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 | 147 |
|
148 |
} // end XYZChemModel() |
|
149 |
||
150 |
/** Add a vertex to this model */ |
|
151 |
int addVert(String name, float x, float y, float z) { |
|
152 |
int i = nvert; |
|
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
153 |
if (i >= maxvert) { |
2 | 154 |
if (vert == null) { |
155 |
maxvert = 100; |
|
156 |
vert = new float[maxvert * 3]; |
|
157 |
atoms = new Atom[maxvert]; |
|
158 |
} else { |
|
159 |
maxvert *= 2; |
|
160 |
float nv[] = new float[maxvert * 3]; |
|
161 |
System.arraycopy(vert, 0, nv, 0, vert.length); |
|
162 |
vert = nv; |
|
163 |
Atom na[] = new Atom[maxvert]; |
|
164 |
System.arraycopy(atoms, 0, na, 0, atoms.length); |
|
165 |
atoms = na; |
|
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 | 172 |
atoms[i] = a; |
173 |
i *= 3; |
|
174 |
vert[i] = x; |
|
175 |
vert[i + 1] = y; |
|
176 |
vert[i + 2] = z; |
|
177 |
return nvert++; |
|
178 |
} |
|
179 |
||
180 |
/** Transform all the points in this model */ |
|
181 |
void transform() { |
|
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
182 |
if (transformed || nvert <= 0) { |
2 | 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 | 186 |
tvert = new int[nvert * 3]; |
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
187 |
} |
2 | 188 |
mat.transform(vert, tvert, nvert); |
189 |
transformed = true; |
|
190 |
} |
|
191 |
||
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 | 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 | 198 |
return; |
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
199 |
} |
2 | 200 |
transform(); |
201 |
int v[] = tvert; |
|
202 |
int zs[] = ZsortMap; |
|
203 |
if (zs == null) { |
|
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 | 206 |
zs[i] = i * 3; |
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
207 |
} |
2 | 208 |
} |
209 |
||
210 |
/* |
|
211 |
* I use a bubble sort since from one iteration to the next, the sort |
|
212 |
* order is pretty stable, so I just use what I had last time as a |
|
213 |
* "guess" of the sorted order. With luck, this reduces O(N log N) |
|
214 |
* to O(N) |
|
215 |
*/ |
|
216 |
||
217 |
for (int i = nvert - 1; --i >= 0;) { |
|
218 |
boolean flipped = false; |
|
219 |
for (int j = 0; j <= i; j++) { |
|
220 |
int a = zs[j]; |
|
221 |
int b = zs[j + 1]; |
|
222 |
if (v[a + 2] > v[b + 2]) { |
|
223 |
zs[j + 1] = a; |
|
224 |
zs[j] = b; |
|
225 |
flipped = true; |
|
226 |
} |
|
227 |
} |
|
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
228 |
if (!flipped) { |
2 | 229 |
break; |
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
230 |
} |
2 | 231 |
} |
232 |
||
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 | 235 |
return; |
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
236 |
} |
2 | 237 |
for (int i = 0; i < lim; i++) { |
238 |
int j = zs[i]; |
|
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 | 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 | 244 |
grey = 15; |
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
245 |
} |
2 | 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 | 248 |
// g.drawImage(iBall, v[j] - (iBall.width >> 1), v[j + 1] - |
249 |
// (iBall.height >> 1)); |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
/** Find the bounding box of this model */ |
|
254 |
void findBB() { |
|
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
255 |
if (nvert <= 0) { |
2 | 256 |
return; |
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
257 |
} |
2 | 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 | 262 |
for (int i = nvert * 3; (i -= 3) > 0;) { |
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 | 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 | 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 | 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 | 291 |
} |
292 |
} |
|
293 |
||
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
294 |
|
2 | 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 | 300 |
XYZChemModel md; |
301 |
boolean painted = true; |
|
302 |
float xfac; |
|
303 |
int prevx, prevy; |
|
304 |
float scalefudge = 1; |
|
305 |
Matrix3D amat = new Matrix3D(), tmat = new Matrix3D(); |
|
306 |
String mdname = null; |
|
307 |
String message = null; |
|
308 |
Image backBuffer; |
|
309 |
Graphics backGC; |
|
310 |
Dimension backSize; |
|
311 |
||
312 |
private synchronized void newBackBuffer() { |
|
313 |
backBuffer = createImage(getSize().width, getSize().height); |
|
314 |
if (backGC != null) { |
|
315 |
backGC.dispose(); |
|
316 |
} |
|
317 |
backGC = backBuffer.getGraphics(); |
|
318 |
backSize = getSize(); |
|
319 |
} |
|
320 |
||
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
321 |
@Override |
2 | 322 |
public void init() { |
323 |
mdname = getParameter("model"); |
|
324 |
try { |
|
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 | 328 |
amat.yrot(20); |
329 |
amat.xrot(20); |
|
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
330 |
if (mdname == null) { |
2 | 331 |
mdname = "model.obj"; |
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
332 |
} |
2 | 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 | 335 |
newBackBuffer(); |
336 |
addMouseListener(this); |
|
337 |
addMouseMotionListener(this); |
|
338 |
} |
|
339 |
||
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
340 |
@Override |
2 | 341 |
public void destroy() { |
342 |
removeMouseListener(this); |
|
343 |
removeMouseMotionListener(this); |
|
344 |
} |
|
345 |
||
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
346 |
@Override |
2 | 347 |
public void run() { |
348 |
InputStream is = null; |
|
349 |
try { |
|
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 | 353 |
Atom.setApplet(this); |
354 |
md = m; |
|
355 |
m.findBB(); |
|
356 |
float xw = m.xmax - m.xmin; |
|
357 |
float yw = m.ymax - m.ymin; |
|
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 | 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 | 363 |
xw = zw; |
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
364 |
} |
2 | 365 |
float f1 = getSize().width / xw; |
366 |
float f2 = getSize().height / xw; |
|
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 | 370 |
md = null; |
371 |
message = e.toString(); |
|
372 |
} |
|
373 |
try { |
|
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
374 |
if (is != null) { |
2 | 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 | 378 |
} |
379 |
repaint(); |
|
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 | 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 | 385 |
new Thread(this).start(); |
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
386 |
} |
2 | 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 | 390 |
public void stop() { |
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 | 415 |
} |
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 | 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 | 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 | 445 |
paint(g); |
446 |
} |
|
447 |
||
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
448 |
@Override |
2 | 449 |
public void paint(Graphics g) { |
450 |
if (md != null) { |
|
451 |
md.mat.unit(); |
|
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 | 455 |
md.mat.mult(amat); |
456 |
// md.mat.scale(xfac, -xfac, 8 * xfac / getSize().width); |
|
457 |
md.mat.scale(xfac, -xfac, 16 * xfac / getSize().width); |
|
458 |
md.mat.translate(getSize().width / 2, getSize().height / 2, 8); |
|
459 |
md.transformed = false; |
|
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 | 462 |
newBackBuffer(); |
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
463 |
} |
2 | 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 | 466 |
md.paint(backGC); |
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 | 470 |
} |
471 |
setPainted(); |
|
472 |
} else if (message != null) { |
|
473 |
g.drawString("Error in model:", 3, 20); |
|
474 |
g.drawString(message, 10, 40); |
|
475 |
} |
|
476 |
} |
|
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
477 |
|
2 | 478 |
private synchronized void setPainted() { |
479 |
painted = true; |
|
480 |
notifyAll(); |
|
481 |
} |
|
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 | 487 |
} |
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 | 500 |
} // end class XYZApp |
501 |
||
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
502 |
|
2 | 503 |
class Atom { |
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
504 |
|
2 | 505 |
private static Applet applet; |
506 |
private static byte[] data; |
|
507 |
private final static int R = 40; |
|
508 |
private final static int hx = 15; |
|
509 |
private final static int hy = 15; |
|
510 |
private final static int bgGrey = 192; |
|
511 |
private final static int nBalls = 16; |
|
512 |
private static int maxr; |
|
513 |
private int Rl; |
|
514 |
private int Gl; |
|
515 |
private int Bl; |
|
516 |
private Image balls[]; |
|
517 |
||
518 |
static { |
|
519 |
data = new byte[R * 2 * R * 2]; |
|
520 |
int mr = 0; |
|
521 |
for (int Y = 2 * R; --Y >= 0;) { |
|
522 |
int x0 = (int) (Math.sqrt(R * R - (Y - R) * (Y - R)) + 0.5); |
|
523 |
int p = Y * (R * 2) + R - x0; |
|
524 |
for (int X = -x0; X < x0; X++) { |
|
525 |
int x = X + hx; |
|
526 |
int y = Y - R + hy; |
|
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 | 529 |
mr = r; |
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
530 |
} |
2 | 531 |
data[p++] = r <= 0 ? 1 : (byte) r; |
532 |
} |
|
533 |
} |
|
534 |
maxr = mr; |
|
535 |
} |
|
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
536 |
|
2 | 537 |
static void setApplet(Applet app) { |
538 |
applet = app; |
|
539 |
} |
|
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
540 |
|
2 | 541 |
Atom(int Rl, int Gl, int Bl) { |
542 |
this.Rl = Rl; |
|
543 |
this.Gl = Gl; |
|
544 |
this.Bl = Bl; |
|
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 | 548 |
return (int) (bg + (fg - bg) * fgfactor); |
549 |
} |
|
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
550 |
|
2 | 551 |
private void Setup() { |
552 |
balls = new Image[nBalls]; |
|
553 |
byte red[] = new byte[256]; |
|
554 |
red[0] = (byte) bgGrey; |
|
555 |
byte green[] = new byte[256]; |
|
556 |
green[0] = (byte) bgGrey; |
|
557 |
byte blue[] = new byte[256]; |
|
558 |
blue[0] = (byte) bgGrey; |
|
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 | 561 |
for (int i = maxr; i >= 1; --i) { |
562 |
float d = (float) i / maxr; |
|
563 |
red[i] = (byte) blend(blend(Rl, 255, d), bgGrey, b); |
|
564 |
green[i] = (byte) blend(blend(Gl, 255, d), bgGrey, b); |
|
565 |
blue[i] = (byte) blend(blend(Bl, 255, d), bgGrey, b); |
|
566 |
} |
|
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 | 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 | 571 |
} |
572 |
} |
|
8971
c2519876ef49
7027686: /applets/MoleculeViewer demo needs to be improved
mrkam
parents:
5506
diff
changeset
|
573 |
|
2 | 574 |
void paint(Graphics gc, int x, int y, int r) { |
575 |
Image ba[] = balls; |
|
576 |
if (ba == null) { |
|
577 |
Setup(); |
|
578 |
ba = balls; |
|
579 |
} |
|
580 |
Image i = ba[r]; |
|
581 |
int size = 10 + r; |
|
582 |
gc.drawImage(i, x - (size >> 1), y - (size >> 1), size, size, applet); |
|
583 |
} |
|
584 |
} |