No radio buttons in applet
[jalview.git] / src / MCview / AppletPDBCanvas.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 package MCview;\r
20 \r
21 import jalview.analysis.AlignSeq;\r
22 \r
23 import jalview.datamodel.*;\r
24 \r
25 // JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug\r
26 import java.awt.*;\r
27 import java.awt.event.*;\r
28 \r
29 import java.io.*;\r
30 \r
31 import java.util.*;\r
32 \r
33 \r
34 \r
35 public class AppletPDBCanvas extends Panel implements MouseListener, MouseMotionListener\r
36 {\r
37   public AppletPDBCanvas()\r
38   {\r
39     try\r
40     {\r
41       jbInit();\r
42     }\r
43     catch (Exception ex)\r
44     {\r
45       ex.printStackTrace();\r
46     }\r
47   }\r
48 \r
49   MCMatrix idmat = new MCMatrix(3, 3);\r
50     MCMatrix objmat = new MCMatrix(3, 3);\r
51     boolean redrawneeded = true;\r
52     int omx = 0;\r
53     int mx = 0;\r
54     int omy = 0;\r
55     int my = 0;\r
56     public PDBfile pdb;\r
57     int bsize;\r
58     Image img;\r
59     Graphics ig;\r
60     Dimension prefsize;\r
61     float[] centre = new float[3];\r
62     float[] width = new float[3];\r
63     float maxwidth;\r
64     float scale;\r
65     String inStr;\r
66     String inType;\r
67     boolean bysequence = true;\r
68     boolean depthcue = true;\r
69     boolean wire = false;\r
70     boolean bymolecule = false;\r
71     boolean zbuffer = true;\r
72     boolean dragging;\r
73     int xstart;\r
74     int xend;\r
75     int ystart;\r
76     int yend;\r
77     int xmid;\r
78     int ymid;\r
79     Font font = new Font("Helvetica", Font.PLAIN, 10);\r
80     jalview.appletgui.SequenceRenderer sr;\r
81     jalview.appletgui.FeatureRenderer  fr;\r
82     jalview.appletgui.SeqCanvas seqcanvas;\r
83     Sequence sequence;\r
84     final StringBuffer mappingDetails = new StringBuffer();\r
85     String appletToolTip = null;\r
86     int toolx, tooly;\r
87 \r
88     public AppletPDBCanvas(jalview.appletgui.SeqCanvas seqcanvas, Sequence seq)\r
89     {\r
90       this.seqcanvas = seqcanvas;\r
91       this.sequence = seq;\r
92       sr = seqcanvas.getSequenceRenderer();\r
93       fr = seqcanvas.getFeatureRenderer();\r
94 \r
95       seqcanvas.setPDBViewer(this);\r
96       addKeyListener(new KeyAdapter()\r
97       {\r
98 \r
99         public void keyPressed(KeyEvent evt)\r
100         {\r
101           doKeyPressed(evt);\r
102         }\r
103       });\r
104 \r
105     }\r
106 \r
107   public void setPDBFile(PDBfile pdb)\r
108    {\r
109         this.sr = sr;\r
110         this.fr = fr;\r
111         int max = -10;\r
112         int maxchain = -1;\r
113         int pdbstart = 0;\r
114         int pdbend = 0;\r
115         int seqstart = 0;\r
116         int seqend = 0;\r
117 \r
118         for (int i = 0; i < pdb.chains.size(); i++)\r
119         {\r
120 \r
121           mappingDetails.append("\n\nPDB Sequence is :\nSequence = " + ((PDBChain) pdb.chains.elementAt(i)).sequence.getSequence());\r
122           mappingDetails.append("\nNo of residues = " + ((PDBChain) pdb.chains.elementAt(i)).residues.size()+"\n\n");\r
123 \r
124             // Now lets compare the sequences to get\r
125             // the start and end points.\r
126             // Align the sequence to the pdb\r
127             AlignSeq as = new AlignSeq(sequence,\r
128                     ((PDBChain) pdb.chains.elementAt(i)).sequence, "pep");\r
129             as.calcScoreMatrix();\r
130             as.traceAlignment();\r
131             PrintStream  ps = new PrintStream(System.out)\r
132            {\r
133               public void print(String x) {\r
134                    mappingDetails.append(x);\r
135                }\r
136                public void println()\r
137                {\r
138                  mappingDetails.append("\n");\r
139                }\r
140             };\r
141 \r
142             as.printAlignment(ps);\r
143 \r
144             if (as.maxscore > max) {\r
145                 max = as.maxscore;\r
146                 maxchain = i;\r
147 \r
148                 pdbstart = as.seq2start;\r
149                 pdbend = as.seq2end;\r
150                 seqstart = as.seq1start + sequence.getStart()-1;\r
151                 seqend = as.seq1end + sequence.getEnd()-1;\r
152             }\r
153 \r
154             mappingDetails.append("\nPDB start/end "  + pdbstart + " " + pdbend);\r
155             mappingDetails.append("\nSEQ start/end "+ seqstart + " " + seqend);\r
156         }\r
157 \r
158         ((PDBChain) pdb.chains.elementAt(maxchain)).pdbstart = pdbstart;\r
159         ((PDBChain) pdb.chains.elementAt(maxchain)).pdbend = pdbend;\r
160         ((PDBChain) pdb.chains.elementAt(maxchain)).seqstart = seqstart;\r
161         ((PDBChain) pdb.chains.elementAt(maxchain)).seqend = seqend;\r
162         ((PDBChain) pdb.chains.elementAt(maxchain)).isVisible = true;\r
163         ((PDBChain) pdb.chains.elementAt(maxchain)).sequence = sequence;\r
164 \r
165         this.pdb = pdb;\r
166         this.prefsize = new Dimension(getSize().width, getSize().height);\r
167 \r
168         //Initialize the matrices to identity\r
169         for (int i = 0; i < 3; i++) {\r
170             for (int j = 0; j < 3; j++) {\r
171                 if (i != j) {\r
172                     idmat.addElement(i, j, 0);\r
173                     objmat.addElement(i, j, 0);\r
174                 } else {\r
175                     idmat.addElement(i, j, 1);\r
176                     objmat.addElement(i, j, 1);\r
177                 }\r
178             }\r
179         }\r
180 \r
181         addMouseMotionListener(this);\r
182         addMouseListener(this);\r
183 \r
184       /*\r
185        SequenceGroup sg = new SequenceGroup("PDB",\r
186                                             null, true,true,false,\r
187                                             sequence.findIndex(seqstart-1),\r
188                                             sequence.findIndex(seqend-1));\r
189        sg.addSequence(sequence, false);\r
190        sg.setOutlineColour(Color.black);\r
191        seqcanvas.getViewport().getAlignment().addGroup(sg);\r
192        */\r
193 \r
194         findCentre();\r
195         findWidth();\r
196 \r
197         scale = findScale();\r
198 \r
199         updateSeqColours();\r
200     }\r
201 \r
202     public void deleteBonds() {\r
203         scale = 0;\r
204         maxwidth = 0;\r
205 \r
206         width[0] = 0;\r
207         width[1] = 0;\r
208         width[2] = 0;\r
209 \r
210         centre[0] = 0;\r
211         centre[1] = 0;\r
212         centre[2] = 0;\r
213 \r
214         for (int i = 0; i < pdb.chains.size(); i++) {\r
215             ((PDBChain) pdb.chains.elementAt(i)).bonds = null;\r
216         }\r
217     }\r
218 \r
219     public void findWidth() {\r
220         float[] max = new float[3];\r
221         float[] min = new float[3];\r
222 \r
223         max[0] = (float) -1e30;\r
224         max[1] = (float) -1e30;\r
225         max[2] = (float) -1e30;\r
226 \r
227         min[0] = (float) 1e30;\r
228         min[1] = (float) 1e30;\r
229         min[2] = (float) 1e30;\r
230 \r
231         for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
232             if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {\r
233                 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
234 \r
235                 for (int i = 0; i < bonds.size(); i++) {\r
236                     Bond tmp = (Bond) bonds.elementAt(i);\r
237 \r
238                     if (tmp.start[0] >= max[0]) {\r
239                         max[0] = tmp.start[0];\r
240                     }\r
241 \r
242                     if (tmp.start[1] >= max[1]) {\r
243                         max[1] = tmp.start[1];\r
244                     }\r
245 \r
246                     if (tmp.start[2] >= max[2]) {\r
247                         max[2] = tmp.start[2];\r
248                     }\r
249 \r
250                     if (tmp.start[0] <= min[0]) {\r
251                         min[0] = tmp.start[0];\r
252                     }\r
253 \r
254                     if (tmp.start[1] <= min[1]) {\r
255                         min[1] = tmp.start[1];\r
256                     }\r
257 \r
258                     if (tmp.start[2] <= min[2]) {\r
259                         min[2] = tmp.start[2];\r
260                     }\r
261 \r
262                     if (tmp.end[0] >= max[0]) {\r
263                         max[0] = tmp.end[0];\r
264                     }\r
265 \r
266                     if (tmp.end[1] >= max[1]) {\r
267                         max[1] = tmp.end[1];\r
268                     }\r
269 \r
270                     if (tmp.end[2] >= max[2]) {\r
271                         max[2] = tmp.end[2];\r
272                     }\r
273 \r
274                     if (tmp.end[0] <= min[0]) {\r
275                         min[0] = tmp.end[0];\r
276                     }\r
277 \r
278                     if (tmp.end[1] <= min[1]) {\r
279                         min[1] = tmp.end[1];\r
280                     }\r
281 \r
282                     if (tmp.end[2] <= min[2]) {\r
283                         min[2] = tmp.end[2];\r
284                     }\r
285                 }\r
286             }\r
287         }\r
288 \r
289         width[0] = (float) Math.abs(max[0] - min[0]);\r
290         width[1] = (float) Math.abs(max[1] - min[1]);\r
291         width[2] = (float) Math.abs(max[2] - min[2]);\r
292 \r
293         maxwidth = width[0];\r
294 \r
295         if (width[1] > width[0]) {\r
296             maxwidth = width[1];\r
297         }\r
298 \r
299         if (width[2] > width[1]) {\r
300             maxwidth = width[2];\r
301         }\r
302 \r
303        // System.out.println("Maxwidth = " + maxwidth);\r
304     }\r
305 \r
306     public float findScale() {\r
307         int dim;\r
308         int width;\r
309         int height;\r
310 \r
311         if (getSize().width != 0) {\r
312             width = getSize().width;\r
313             height = getSize().height;\r
314         } else {\r
315             width = prefsize.width;\r
316             height = prefsize.height;\r
317         }\r
318 \r
319         if (width < height) {\r
320             dim = width;\r
321         } else {\r
322             dim = height;\r
323         }\r
324 \r
325         return (float) (dim / (1.5d * maxwidth));\r
326     }\r
327 \r
328     public void findCentre() {\r
329         float xtot = 0;\r
330         float ytot = 0;\r
331         float ztot = 0;\r
332 \r
333         int bsize = 0;\r
334 \r
335         //Find centre coordinate\r
336         for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
337             if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {\r
338                 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
339 \r
340                 bsize += bonds.size();\r
341 \r
342                 for (int i = 0; i < bonds.size(); i++) {\r
343                     xtot = xtot + ((Bond) bonds.elementAt(i)).start[0] +\r
344                         ((Bond) bonds.elementAt(i)).end[0];\r
345 \r
346                     ytot = ytot + ((Bond) bonds.elementAt(i)).start[1] +\r
347                         ((Bond) bonds.elementAt(i)).end[1];\r
348 \r
349                     ztot = ztot + ((Bond) bonds.elementAt(i)).start[2] +\r
350                         ((Bond) bonds.elementAt(i)).end[2];\r
351                 }\r
352             }\r
353         }\r
354 \r
355         centre[0] = xtot / (2 * (float) bsize);\r
356         centre[1] = ytot / (2 * (float) bsize);\r
357         centre[2] = ztot / (2 * (float) bsize);\r
358     }\r
359 \r
360     public void paint(Graphics g)\r
361     {\r
362 \r
363       if(pdb==null)\r
364       {\r
365         g.setColor(Color.black);\r
366         g.setFont(new Font("Verdana", Font.BOLD, 14));\r
367         g.drawString("Error Parsing Pasted PDB data!!", 50, getSize().height/2);\r
368         return;\r
369       }\r
370 \r
371 \r
372         //Only create the image at the beginning -\r
373         //this saves much memory usage\r
374         if ((img == null) || (prefsize.width != getSize().width) ||\r
375                 (prefsize.height != getSize().height)) {\r
376             prefsize.width = getSize().width;\r
377             prefsize.height = getSize().height;\r
378 \r
379             scale = findScale();\r
380             img = createImage(prefsize.width, prefsize.height);\r
381             ig = img.getGraphics();\r
382 \r
383             redrawneeded = true;\r
384         }\r
385 \r
386 \r
387         if (redrawneeded)\r
388         {\r
389           drawAll(ig, prefsize.width, prefsize.height);\r
390           redrawneeded = false;\r
391         }\r
392         if(appletToolTip!=null)\r
393         {\r
394           ig.setColor(Color.red);\r
395           ig.drawString(appletToolTip, toolx, tooly);\r
396         }\r
397 \r
398         g.drawImage(img, 0, 0, this);\r
399 \r
400 \r
401     }\r
402 \r
403     public void drawAll(Graphics g, int width, int height)\r
404     {\r
405       g.setColor(Color.black);\r
406       g.fillRect(0, 0, width, height);\r
407       drawScene(g);\r
408       drawLabels(g);\r
409     }\r
410 \r
411 \r
412     public void updateSeqColours()\r
413     {\r
414       if(bysequence && pdb!=null)\r
415       {\r
416         for (int ii = 0; ii < pdb.chains.size(); ii++)\r
417         {\r
418           colourBySequence((PDBChain) pdb.chains.elementAt(ii));\r
419         }\r
420       }\r
421 \r
422       redrawneeded=true;\r
423       repaint();\r
424     }\r
425 \r
426     // This method has been taken out of PDBChain to allow\r
427     // Applet and Application specific sequence renderers to be used\r
428     void colourBySequence(PDBChain chain)\r
429     {\r
430       for (int i = 0; i < chain.bonds.size(); i++)\r
431       {\r
432         Bond tmp = (Bond) chain.bonds.elementAt(i);\r
433 \r
434         if ( (tmp.at1.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&\r
435             (tmp.at1.resNumber <= ( (chain.offset + chain.pdbend) - 1)))\r
436         {\r
437           int pos = chain.seqstart +\r
438               (tmp.at1.resNumber - chain.pdbstart - chain.offset);\r
439 \r
440           int index = sequence.findIndex(pos);\r
441 \r
442           tmp.startCol = sr.findSequenceColour(sequence, index);\r
443 \r
444         //  tmp.startCol = fr.findFeatureColour(tmp.startCol, sequence, index);\r
445 \r
446         }\r
447         else\r
448         {\r
449           tmp.startCol = Color.gray;\r
450         }\r
451 \r
452         if ( (tmp.at2.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) &&\r
453             (tmp.at2.resNumber <= ( (chain.pdbend + chain.offset) - 1)))\r
454         {\r
455           int pos = chain.seqstart +\r
456               (tmp.at2.resNumber - chain.pdbstart - chain.offset);\r
457           int index = sequence.findIndex(pos);\r
458 \r
459           tmp.endCol = sr.findSequenceColour( sequence, index);\r
460       //    tmp.endCol = fr.findFeatureColour(tmp.endCol, sequence, index);\r
461         }\r
462         else\r
463         {\r
464           tmp.endCol = Color.gray;\r
465         }\r
466       }\r
467     }\r
468 \r
469 \r
470     public void drawScene(Graphics g) {\r
471         // Sort the bonds by z coord\r
472         Vector bonds = new Vector();\r
473 \r
474         for (int ii = 0; ii < pdb.chains.size(); ii++)\r
475         {\r
476           if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)\r
477           {\r
478             Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;\r
479 \r
480             for (int i = 0; i < tmp.size(); i++)\r
481             {\r
482               bonds.addElement(tmp.elementAt(i));\r
483             }\r
484           }\r
485         }\r
486 \r
487         if (zbuffer) {\r
488             Zsort.Zsort(bonds);\r
489         }\r
490 \r
491         for (int i = 0; i < bonds.size(); i++) {\r
492             Bond tmpBond = (Bond) bonds.elementAt(i);\r
493 \r
494             xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) +\r
495                 (getSize().width / 2));\r
496             ystart = (int) (((tmpBond.start[1] - centre[1]) * scale) +\r
497                 (getSize().height / 2));\r
498 \r
499             xend = (int) (((tmpBond.end[0] - centre[0]) * scale) +\r
500                 (getSize().width / 2));\r
501             yend = (int) (((tmpBond.end[1] - centre[1]) * scale) +\r
502                 (getSize().height / 2));\r
503 \r
504             xmid = (xend + xstart) / 2;\r
505             ymid = (yend + ystart) / 2;\r
506 \r
507             if (depthcue && !bymolecule) {\r
508                 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {\r
509                     g.setColor(tmpBond.startCol.darker().darker());\r
510                     drawLine(g, xstart, ystart, xmid, ymid);\r
511 \r
512                     g.setColor(tmpBond.endCol.darker().darker());\r
513                     drawLine(g, xmid, ymid, xend, yend);\r
514                 } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {\r
515                     g.setColor(tmpBond.startCol.darker());\r
516                     drawLine(g, xstart, ystart, xmid, ymid);\r
517 \r
518                     g.setColor(tmpBond.endCol.darker());\r
519                     drawLine(g, xmid, ymid, xend, yend);\r
520                 } else {\r
521                     g.setColor(tmpBond.startCol);\r
522                     drawLine(g, xstart, ystart, xmid, ymid);\r
523 \r
524                     g.setColor(tmpBond.endCol);\r
525                     drawLine(g, xmid, ymid, xend, yend);\r
526                 }\r
527             } else if (depthcue && bymolecule) {\r
528                 if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {\r
529                     g.setColor(Color.green.darker().darker());\r
530                     drawLine(g, xstart, ystart, xend, yend);\r
531                 } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {\r
532                     g.setColor(Color.green.darker());\r
533                     drawLine(g, xstart, ystart, xend, yend);\r
534                 } else {\r
535                     g.setColor(Color.green);\r
536                     drawLine(g, xstart, ystart, xend, yend);\r
537                 }\r
538             } else if (!depthcue && !bymolecule) {\r
539                 g.setColor(tmpBond.startCol);\r
540                 drawLine(g, xstart, ystart, xmid, ymid);\r
541                 g.setColor(tmpBond.endCol);\r
542                 drawLine(g, xmid, ymid, xend, yend);\r
543             } else {\r
544                 drawLine(g, xstart, ystart, xend, yend);\r
545             }\r
546         }\r
547     }\r
548 \r
549     public void drawLine(Graphics g, int x1, int y1, int x2, int y2) {\r
550         if (!wire) {\r
551             if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5) {\r
552                 g.drawLine(x1, y1, x2, y2);\r
553                 g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);\r
554                 g.drawLine(x1, y1 - 1, x2, y2 - 1);\r
555             } else {\r
556                 g.setColor(g.getColor().brighter());\r
557                 g.drawLine(x1, y1, x2, y2);\r
558                 g.drawLine(x1 + 1, y1, x2 + 1, y2);\r
559                 g.drawLine(x1 - 1, y1, x2 - 1, y2);\r
560             }\r
561         } else {\r
562             g.drawLine(x1, y1, x2, y2);\r
563         }\r
564     }\r
565 \r
566     public Dimension minimumsize() {\r
567         return prefsize;\r
568     }\r
569 \r
570     public Dimension preferredsize() {\r
571         return prefsize;\r
572     }\r
573 \r
574     public void doKeyPressed(KeyEvent evt)\r
575     {\r
576       if (evt.getKeyCode() == KeyEvent.VK_UP)\r
577       {\r
578         scale = (float) (scale * 1.1);\r
579         redrawneeded = true;\r
580         repaint();\r
581       }\r
582       else if (evt.getKeyCode() == KeyEvent.VK_DOWN)\r
583       {\r
584         scale = (float) (scale * 0.9);\r
585         redrawneeded = true;\r
586         repaint();\r
587       }\r
588     }\r
589 \r
590     public void mousePressed(MouseEvent e) {\r
591         myAtom fatom = findAtom(e.getX(), e.getY());\r
592         if(fatom!=null)\r
593         {\r
594           fatom.isSelected = !fatom.isSelected;\r
595           redrawneeded = true;\r
596           repaint();\r
597         }\r
598         mx = e.getX();\r
599         my = e.getY();\r
600         omx = mx;\r
601         omy = my;\r
602         dragging = false;\r
603     }\r
604 \r
605     public void mouseMoved(MouseEvent e) {\r
606 \r
607         myAtom fatom = findAtom(e.getX(), e.getY());\r
608 \r
609         if(foundchain!=-1)\r
610         {\r
611           PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain);\r
612           int pos = chain.seqstart +\r
613               (fatom.resNumber - chain.pdbstart - chain.offset)+1;\r
614 \r
615           int index = seqcanvas.getViewport().getAlignment().findIndex(sequence);\r
616 \r
617           seqcanvas.highlightSearchResults(new int[]{index, pos, pos});\r
618         }\r
619         else\r
620           seqcanvas.highlightSearchResults(null);\r
621 \r
622         if (fatom != null) {\r
623             toolx = e.getX();\r
624             tooly = e.getY();\r
625             appletToolTip = fatom.resNumber+" "+ fatom.resName;\r
626             redrawneeded = true;\r
627             repaint();\r
628         } else {\r
629             appletToolTip = null;\r
630             redrawneeded = true;\r
631             repaint();\r
632         }\r
633     }\r
634 \r
635     public void mouseClicked(MouseEvent e) {\r
636     }\r
637 \r
638     public void mouseEntered(MouseEvent e) {\r
639     }\r
640 \r
641     public void mouseExited(MouseEvent e) {\r
642     }\r
643 \r
644     public void mouseDragged(MouseEvent evt) {\r
645         int x = evt.getX();\r
646         int y = evt.getY();\r
647         mx = x;\r
648         my = y;\r
649 \r
650         MCMatrix objmat = new MCMatrix(3, 3);\r
651         objmat.setIdentity();\r
652 \r
653         if ((evt.getModifiers() & Event.META_MASK) != 0) {\r
654             objmat.rotatez((float) ((mx - omx)));\r
655         } else {\r
656             objmat.rotatex((float) ((my - omy)));\r
657             objmat.rotatey((float) ((omx - mx)));\r
658         }\r
659 \r
660         //Alter the bonds\r
661         for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
662             Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
663 \r
664             for (int i = 0; i < bonds.size(); i++) {\r
665                 Bond tmpBond = (Bond) bonds.elementAt(i);\r
666 \r
667                 //Translate the bond so the centre is 0,0,0\r
668                 tmpBond.translate(-centre[0], -centre[1], -centre[2]);\r
669 \r
670                 //Now apply the rotation matrix\r
671                 tmpBond.start = objmat.vectorMultiply(tmpBond.start);\r
672                 tmpBond.end = objmat.vectorMultiply(tmpBond.end);\r
673 \r
674                 //Now translate back again\r
675                 tmpBond.translate(centre[0], centre[1], centre[2]);\r
676             }\r
677         }\r
678 \r
679         objmat = null;\r
680 \r
681         omx = mx;\r
682         omy = my;\r
683 \r
684         dragging = true;\r
685 \r
686         redrawneeded = true;\r
687 \r
688         repaint();\r
689     }\r
690 \r
691     public void mouseReleased(MouseEvent evt) {\r
692         dragging = false;\r
693         return;\r
694     }\r
695 \r
696     void drawLabels(Graphics g) {\r
697 \r
698         for (int ii = 0; ii < pdb.chains.size(); ii++)\r
699         {\r
700             PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);\r
701 \r
702             if (chain.isVisible)\r
703             {\r
704                 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
705 \r
706                 for (int i = 0; i < bonds.size(); i++)\r
707                 {\r
708                     Bond tmpBond = (Bond) bonds.elementAt(i);\r
709 \r
710                     if (tmpBond.at1.isSelected)\r
711                     {\r
712                         labelAtom(g, tmpBond, 1);\r
713                     }\r
714 \r
715                     if (tmpBond.at2.isSelected)\r
716                     {\r
717 \r
718                         labelAtom(g, tmpBond, 2);\r
719                     }\r
720                 }\r
721             }\r
722         }\r
723     }\r
724 \r
725     public void labelAtom(Graphics g, Bond b, int n) {\r
726         g.setFont(font);\r
727 \r
728         if (n == 1) {\r
729             int xstart = (int) (((b.start[0] - centre[0]) * scale) +\r
730                 (getSize().width / 2));\r
731             int ystart = (int) (((b.start[1] - centre[1]) * scale) +\r
732                 (getSize().height / 2));\r
733 \r
734             g.setColor(Color.red);\r
735             g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);\r
736         }\r
737 \r
738         if (n == 2) {\r
739             int xstart = (int) (((b.end[0] - centre[0]) * scale) +\r
740                 (getSize().width / 2));\r
741             int ystart = (int) (((b.end[1] - centre[1]) * scale) +\r
742                 (getSize().height / 2));\r
743 \r
744             g.setColor(Color.red);\r
745             g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);\r
746         }\r
747     }\r
748 \r
749     int foundchain = -1;\r
750     public myAtom findAtom(int x, int y) {\r
751         myAtom fatom = null;\r
752 \r
753         foundchain = -1;\r
754 \r
755         for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
756             PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);\r
757 \r
758             if (chain.isVisible) {\r
759                 Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
760 \r
761                 for (int i = 0; i < bonds.size(); i++) {\r
762                     Bond tmpBond = (Bond) bonds.elementAt(i);\r
763 \r
764                     int truex = (int) (((tmpBond.start[0] - centre[0]) * scale) +\r
765                         (getSize().width / 2));\r
766 \r
767                     if (Math.abs(truex - x) <= 2) {\r
768                         int truey = (int) (((tmpBond.start[1] - centre[1]) * scale) +\r
769                             (getSize().height / 2));\r
770 \r
771                         if (Math.abs(truey - y) <= 2)\r
772                         {\r
773                             fatom = tmpBond.at1;\r
774                             foundchain = ii;\r
775                             break;\r
776                         }\r
777                     }\r
778                 }\r
779             }\r
780 \r
781             if (fatom != null) //)&& chain.ds != null)\r
782              {\r
783                 chain = (PDBChain) pdb.chains.elementAt(foundchain);\r
784             }\r
785         }\r
786 \r
787         return fatom;\r
788     }\r
789 \r
790     public void update(Graphics g)\r
791     {\r
792       paint(g);\r
793     }\r
794 \r
795   private void jbInit()\r
796       throws Exception\r
797   {\r
798   }\r
799 }\r