2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.appletgui;
25 import java.awt.event.*;
27 import jalview.api.RotatableCanvasI;
28 import jalview.datamodel.*;
29 import jalview.math.*;
30 import jalview.util.*;
31 import jalview.viewmodel.AlignmentViewport;
33 public class RotatableCanvas extends Panel implements MouseListener,
34 MouseMotionListener, KeyListener, RotatableCanvasI
36 RotatableMatrix idmat = new RotatableMatrix(3, 3);
38 RotatableMatrix objmat = new RotatableMatrix(3, 3);
40 RotatableMatrix rotmat = new RotatableMatrix(3, 3);
46 // RubberbandRectangle rubberband;
48 boolean drawAxes = true;
64 float centre[] = new float[3];
66 float width[] = new float[3];
68 float max[] = new float[3];
70 float min[] = new float[3];
100 float scalefactor = 1;
102 AlignmentViewport av;
104 boolean showLabels = false;
106 public RotatableCanvas(AlignmentViewport av)
111 public void showLabels(boolean b)
117 public void setPoints(Vector points, int npoint)
119 this.points = points;
120 this.npoint = npoint;
121 PaintRefresher.Register(this, av.getSequenceSetId());
123 prefsize = getPreferredSize();
124 orig = new float[npoint][3];
126 for (int i = 0; i < npoint; i++)
128 SequencePoint sp = (SequencePoint) points.elementAt(i);
129 for (int j = 0; j < 3; j++)
131 orig[i][j] = sp.coord[j];
134 // Initialize the matrices to identity
136 for (int i = 0; i < 3; i++)
138 for (int j = 0; j < 3; j++)
142 idmat.addElement(i, j, 0);
143 objmat.addElement(i, j, 0);
144 rotmat.addElement(i, j, 0);
148 idmat.addElement(i, j, 0);
149 objmat.addElement(i, j, 0);
150 rotmat.addElement(i, j, 0);
155 axes = new float[3][3];
163 // System.out.println("Scale factor = " + scale);
165 addMouseListener(this);
166 addKeyListener(this);
167 // if (getParent() != null) {
168 // getParent().addKeyListener(this);
170 addMouseMotionListener(this);
173 // rubberband = new RubberbandRectangle(this);
174 // rubberband.setActive(true);
175 // rubberband.addListener(this);
179 * public boolean handleSequenceSelectionEvent(SequenceSelectionEvent evt) {
180 * redrawneeded = true; repaint(); return true; }
182 * public void removeNotify() { controller.removeListener(this);
183 * super.removeNotify(); }
186 public void initAxes()
188 for (int i = 0; i < 3; i++)
190 for (int j = 0; j < 3; j++)
204 public void findWidth()
209 max[0] = (float) -1e30;
210 max[1] = (float) -1e30;
211 max[2] = (float) -1e30;
213 min[0] = (float) 1e30;
214 min[1] = (float) 1e30;
215 min[2] = (float) 1e30;
217 for (int i = 0; i < 3; i++)
219 for (int j = 0; j < npoint; j++)
221 SequencePoint sp = (SequencePoint) points.elementAt(j);
222 if (sp.coord[i] >= max[i])
224 max[i] = sp.coord[i];
226 if (sp.coord[i] <= min[i])
228 min[i] = sp.coord[i];
233 // System.out.println("xmax " + max[0] + " min " + min[0]);
234 // System.out.println("ymax " + max[1] + " min " + min[1]);
235 // System.out.println("zmax " + max[2] + " min " + min[2]);
237 width[0] = Math.abs(max[0] - min[0]);
238 width[1] = Math.abs(max[1] - min[1]);
239 width[2] = Math.abs(max[2] - min[2]);
243 if (width[1] > width[0])
247 if (width[2] > width[1])
252 // System.out.println("Maxwidth = " + maxwidth);
255 public float findScale()
257 int dim, width, height;
258 if (getSize().width != 0)
260 width = getSize().width;
261 height = getSize().height;
265 width = prefsize.width;
266 height = prefsize.height;
278 return (float) (dim * scalefactor / (2 * maxwidth));
281 public void findCentre()
283 // Find centre coordinate
286 centre[0] = (max[0] + min[0]) / 2;
287 centre[1] = (max[1] + min[1]) / 2;
288 centre[2] = (max[2] + min[2]) / 2;
290 // System.out.println("Centre x " + centre[0]);
291 // System.out.println("Centre y " + centre[1]);
292 // System.out.println("Centre z " + centre[2]);
295 public Dimension getPreferredSize()
297 if (prefsize != null)
303 return new Dimension(400, 400);
307 public Dimension getMinimumSize()
309 return getPreferredSize();
312 public void update(Graphics g)
317 public void paint(Graphics g)
321 g.setFont(new Font("Verdana", Font.PLAIN, 18));
322 g.drawString(MessageManager.getString("label.calculating_pca")
323 + "....", 20, getSize().height / 2);
328 // Only create the image at the beginning -
329 if ((img == null) || (prefsize.width != getSize().width)
330 || (prefsize.height != getSize().height))
332 prefsize.width = getSize().width;
333 prefsize.height = getSize().height;
337 // System.out.println("New scale = " + scale);
338 img = createImage(getSize().width, getSize().height);
339 ig = img.getGraphics();
343 drawBackground(ig, Color.black);
345 if (drawAxes == true)
352 ig.setColor(Color.red);
353 ig.drawString(tooltip, toolx, tooly);
356 g.drawImage(img, 0, 0, this);
360 public void drawAxes(Graphics g)
363 g.setColor(Color.yellow);
364 for (int i = 0; i < 3; i++)
366 g.drawLine(getSize().width / 2, getSize().height / 2,
367 (int) (axes[i][0] * scale * max[0] + getSize().width / 2),
368 (int) (axes[i][1] * scale * max[1] + getSize().height / 2));
372 public void drawBackground(Graphics g, Color col)
375 g.fillRect(0, 0, prefsize.width, prefsize.height);
378 public void drawScene(Graphics g)
380 // boolean darker = false;
382 int halfwidth = getSize().width / 2;
383 int halfheight = getSize().height / 2;
385 for (int i = 0; i < npoint; i++)
387 SequencePoint sp = (SequencePoint) points.elementAt(i);
388 int x = (int) ((float) (sp.coord[0] - centre[0]) * scale) + halfwidth;
389 int y = (int) ((float) (sp.coord[1] - centre[1]) * scale)
391 float z = sp.coord[1] - centre[2];
393 if (av.getSequenceColour(sp.sequence) == Color.black)
395 g.setColor(Color.white);
399 g.setColor(av.getSequenceColour(sp.sequence));
402 if (av.getSelectionGroup() != null)
404 if (av.getSelectionGroup().getSequences(null)
405 .contains(((SequencePoint) points.elementAt(i)).sequence))
407 g.setColor(Color.gray);
412 g.setColor(g.getColor().darker());
415 g.fillRect(x - 3, y - 3, 6, 6);
418 g.setColor(Color.red);
420 ((SequencePoint) points.elementAt(i)).sequence.getName(),
426 public Dimension minimumsize()
431 public Dimension preferredsize()
436 public void keyTyped(KeyEvent evt)
440 public void keyReleased(KeyEvent evt)
444 public void keyPressed(KeyEvent evt)
446 if (evt.getKeyCode() == KeyEvent.VK_UP)
448 scalefactor = (float) (scalefactor * 1.1);
451 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
453 scalefactor = (float) (scalefactor * 0.9);
456 else if (evt.getKeyChar() == 's')
458 System.err.println("DEBUG: Rectangle selection"); // log.debug
459 if (rectx2 != -1 && recty2 != -1)
461 rectSelect(rectx1, recty1, rectx2, recty2);
468 public void printPoints()
470 for (int i = 0; i < npoint; i++)
472 SequencePoint sp = (SequencePoint) points.elementAt(i);
473 Format.print(System.out, "%5d ", i);
474 for (int j = 0; j < 3; j++)
476 Format.print(System.out, "%13.3f ", sp.coord[j]);
478 System.out.println();
482 public void mouseClicked(MouseEvent evt)
486 public void mouseEntered(MouseEvent evt)
490 public void mouseExited(MouseEvent evt)
494 public void mouseReleased(MouseEvent evt)
498 public void mousePressed(MouseEvent evt)
518 SequenceI found = findPoint(x, y);
522 // TODO: applet PCA is not associatable with multi-panels - only parent
524 if (av.getSelectionGroup() != null)
526 av.getSelectionGroup().addOrRemove(found, true);
527 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
531 av.setSelectionGroup(new SequenceGroup());
532 av.getSelectionGroup().addOrRemove(found, true);
533 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
536 PaintRefresher.Refresh(this, av.getSequenceSetId());
542 public void mouseMoved(MouseEvent evt)
544 SequenceI found = findPoint(evt.getX(), evt.getY());
551 tooltip = found.getName();
558 public void mouseDragged(MouseEvent evt)
563 rotmat.setIdentity();
565 rotmat.rotate((float) (my - omy), 'x');
566 rotmat.rotate((float) (mx - omx), 'y');
568 for (int i = 0; i < npoint; i++)
570 SequencePoint sp = (SequencePoint) points.elementAt(i);
571 sp.coord[0] -= centre[0];
572 sp.coord[1] -= centre[1];
573 sp.coord[2] -= centre[2];
575 // Now apply the rotation matrix
576 sp.coord = rotmat.vectorMultiply(sp.coord);
578 // Now translate back again
579 sp.coord[0] += centre[0];
580 sp.coord[1] += centre[1];
581 sp.coord[2] += centre[2];
584 for (int i = 0; i < 3; i++)
586 axes[i] = rotmat.vectorMultiply(axes[i]);
591 paint(this.getGraphics());
594 public void rectSelect(int x1, int y1, int x2, int y2)
596 // boolean changedSel = false;
597 for (int i = 0; i < npoint; i++)
599 SequencePoint sp = (SequencePoint) points.elementAt(i);
600 int tmp1 = (int) ((sp.coord[0] - centre[0]) * scale + (float) getSize().width / 2.0);
601 int tmp2 = (int) ((sp.coord[1] - centre[1]) * scale + (float) getSize().height / 2.0);
603 if (tmp1 > x1 && tmp1 < x2 && tmp2 > y1 && tmp2 < y2)
607 if (!av.getSelectionGroup().getSequences(null)
608 .contains(sp.sequence))
610 av.getSelectionGroup().addSequence(sp.sequence, true);
617 public SequenceI findPoint(int x, int y)
620 int halfwidth = getSize().width / 2;
621 int halfheight = getSize().height / 2;
625 for (int i = 0; i < npoint; i++)
628 SequencePoint sp = (SequencePoint) points.elementAt(i);
629 int px = (int) ((float) (sp.coord[0] - centre[0]) * scale)
631 int py = (int) ((float) (sp.coord[1] - centre[1]) * scale)
634 if (Math.abs(px - x) < 3 && Math.abs(py - y) < 3)
641 return ((SequencePoint) points.elementAt(found)).sequence;