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;
23 import jalview.api.RotatableCanvasI;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.datamodel.SequencePoint;
27 import jalview.math.RotatableMatrix;
28 import jalview.util.ColorUtils;
29 import jalview.util.Format;
30 import jalview.util.MessageManager;
31 import jalview.viewmodel.AlignmentViewport;
33 import java.awt.Color;
34 import java.awt.Dimension;
36 import java.awt.Graphics;
37 import java.awt.Image;
38 import java.awt.Panel;
39 import java.awt.event.KeyEvent;
40 import java.awt.event.KeyListener;
41 import java.awt.event.MouseEvent;
42 import java.awt.event.MouseListener;
43 import java.awt.event.MouseMotionListener;
44 import java.util.Vector;
46 public class RotatableCanvas extends Panel implements MouseListener,
47 MouseMotionListener, KeyListener, RotatableCanvasI
49 RotatableMatrix idmat = new RotatableMatrix(3, 3);
51 RotatableMatrix objmat = new RotatableMatrix(3, 3);
53 RotatableMatrix rotmat = new RotatableMatrix(3, 3);
59 // RubberbandRectangle rubberband;
61 boolean drawAxes = true;
77 float centre[] = new float[3];
79 float width[] = new float[3];
81 float max[] = new float[3];
83 float min[] = new float[3];
113 float scalefactor = 1;
115 AlignmentViewport av;
117 boolean showLabels = false;
119 public RotatableCanvas(AlignmentViewport av)
124 public void showLabels(boolean b)
131 public void setPoints(Vector points, int npoint)
133 this.points = points;
134 this.npoint = npoint;
135 PaintRefresher.Register(this, av.getSequenceSetId());
137 prefsize = getPreferredSize();
138 orig = new float[npoint][3];
140 for (int i = 0; i < npoint; i++)
142 SequencePoint sp = (SequencePoint) points.elementAt(i);
143 for (int j = 0; j < 3; j++)
145 orig[i][j] = sp.coord[j];
148 // Initialize the matrices to identity
150 for (int i = 0; i < 3; i++)
152 for (int j = 0; j < 3; j++)
156 idmat.addElement(i, j, 0);
157 objmat.addElement(i, j, 0);
158 rotmat.addElement(i, j, 0);
162 idmat.addElement(i, j, 0);
163 objmat.addElement(i, j, 0);
164 rotmat.addElement(i, j, 0);
169 axes = new float[3][3];
177 // System.out.println("Scale factor = " + scale);
179 addMouseListener(this);
180 addKeyListener(this);
181 // if (getParent() != null) {
182 // getParent().addKeyListener(this);
184 addMouseMotionListener(this);
187 // rubberband = new RubberbandRectangle(this);
188 // rubberband.setActive(true);
189 // rubberband.addListener(this);
193 * public boolean handleSequenceSelectionEvent(SequenceSelectionEvent evt) {
194 * redrawneeded = true; repaint(); return true; }
196 * public void removeNotify() { controller.removeListener(this);
197 * super.removeNotify(); }
200 public void initAxes()
202 for (int i = 0; i < 3; i++)
204 for (int j = 0; j < 3; j++)
218 public void findWidth()
223 max[0] = (float) -1e30;
224 max[1] = (float) -1e30;
225 max[2] = (float) -1e30;
227 min[0] = (float) 1e30;
228 min[1] = (float) 1e30;
229 min[2] = (float) 1e30;
231 for (int i = 0; i < 3; i++)
233 for (int j = 0; j < npoint; j++)
235 SequencePoint sp = (SequencePoint) points.elementAt(j);
236 if (sp.coord[i] >= max[i])
238 max[i] = sp.coord[i];
240 if (sp.coord[i] <= min[i])
242 min[i] = sp.coord[i];
247 // System.out.println("xmax " + max[0] + " min " + min[0]);
248 // System.out.println("ymax " + max[1] + " min " + min[1]);
249 // System.out.println("zmax " + max[2] + " min " + min[2]);
251 width[0] = Math.abs(max[0] - min[0]);
252 width[1] = Math.abs(max[1] - min[1]);
253 width[2] = Math.abs(max[2] - min[2]);
257 if (width[1] > width[0])
261 if (width[2] > width[1])
266 // System.out.println("Maxwidth = " + maxwidth);
269 public float findScale()
271 int dim, width, height;
272 if (getSize().width != 0)
274 width = getSize().width;
275 height = getSize().height;
279 width = prefsize.width;
280 height = prefsize.height;
292 return dim * scalefactor / (2 * maxwidth);
295 public void findCentre()
297 // Find centre coordinate
300 centre[0] = (max[0] + min[0]) / 2;
301 centre[1] = (max[1] + min[1]) / 2;
302 centre[2] = (max[2] + min[2]) / 2;
304 // System.out.println("Centre x " + centre[0]);
305 // System.out.println("Centre y " + centre[1]);
306 // System.out.println("Centre z " + centre[2]);
310 public Dimension getPreferredSize()
312 if (prefsize != null)
318 return new Dimension(400, 400);
323 public Dimension getMinimumSize()
325 return getPreferredSize();
329 public void update(Graphics g)
335 public void paint(Graphics g)
339 g.setFont(new Font("Verdana", Font.PLAIN, 18));
340 g.drawString(MessageManager.getString("label.calculating_pca")
341 + "....", 20, getSize().height / 2);
346 // Only create the image at the beginning -
347 if ((img == null) || (prefsize.width != getSize().width)
348 || (prefsize.height != getSize().height))
350 prefsize.width = getSize().width;
351 prefsize.height = getSize().height;
355 // System.out.println("New scale = " + scale);
356 img = createImage(getSize().width, getSize().height);
357 ig = img.getGraphics();
361 drawBackground(ig, Color.black);
363 if (drawAxes == true)
370 ig.setColor(Color.red);
371 ig.drawString(tooltip, toolx, tooly);
374 g.drawImage(img, 0, 0, this);
378 public void drawAxes(Graphics g)
381 g.setColor(Color.yellow);
382 for (int i = 0; i < 3; i++)
384 g.drawLine(getSize().width / 2, getSize().height / 2,
385 (int) (axes[i][0] * scale * max[0] + getSize().width / 2),
386 (int) (axes[i][1] * scale * max[1] + getSize().height / 2));
390 public void drawBackground(Graphics g, Color col)
393 g.fillRect(0, 0, prefsize.width, prefsize.height);
396 public void drawScene(Graphics g)
398 // boolean darker = false;
400 int halfwidth = getSize().width / 2;
401 int halfheight = getSize().height / 2;
403 for (int i = 0; i < npoint; i++)
405 SequencePoint sp = (SequencePoint) points.elementAt(i);
406 int x = (int) ((sp.coord[0] - centre[0]) * scale) + halfwidth;
407 int y = (int) ((sp.coord[1] - centre[1]) * scale)
409 float z = sp.coord[1] - centre[2];
411 if (av.getSequenceColour(sp.sequence) == Color.black)
413 g.setColor(Color.white);
417 g.setColor(ColorUtils.getColor(av.getSequenceColour(sp.sequence)));
420 if (av.getSelectionGroup() != null)
422 if (av.getSelectionGroup().getSequences(null)
423 .contains(((SequencePoint) points.elementAt(i)).sequence))
425 g.setColor(Color.gray);
430 g.setColor(g.getColor().darker());
433 g.fillRect(x - 3, y - 3, 6, 6);
436 g.setColor(Color.red);
438 ((SequencePoint) points.elementAt(i)).sequence.getName(),
444 public Dimension minimumsize()
449 public Dimension preferredsize()
455 public void keyTyped(KeyEvent evt)
460 public void keyReleased(KeyEvent evt)
465 public void keyPressed(KeyEvent evt)
467 if (evt.getKeyCode() == KeyEvent.VK_UP)
469 scalefactor = (float) (scalefactor * 1.1);
472 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
474 scalefactor = (float) (scalefactor * 0.9);
477 else if (evt.getKeyChar() == 's')
479 System.err.println("DEBUG: Rectangle selection"); // log.debug
480 if (rectx2 != -1 && recty2 != -1)
482 rectSelect(rectx1, recty1, rectx2, recty2);
489 public void printPoints()
491 for (int i = 0; i < npoint; i++)
493 SequencePoint sp = (SequencePoint) points.elementAt(i);
494 Format.print(System.out, "%5d ", i);
495 for (int j = 0; j < 3; j++)
497 Format.print(System.out, "%13.3f ", sp.coord[j]);
499 System.out.println();
504 public void mouseClicked(MouseEvent evt)
509 public void mouseEntered(MouseEvent evt)
514 public void mouseExited(MouseEvent evt)
519 public void mouseReleased(MouseEvent evt)
524 public void mousePressed(MouseEvent evt)
544 SequenceI found = findPoint(x, y);
548 // TODO: applet PCA is not associatable with multi-panels - only parent
550 if (av.getSelectionGroup() != null)
552 av.getSelectionGroup().addOrRemove(found, true);
553 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
557 av.setSelectionGroup(new SequenceGroup());
558 av.getSelectionGroup().addOrRemove(found, true);
559 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
562 PaintRefresher.Refresh(this, av.getSequenceSetId());
569 public void mouseMoved(MouseEvent evt)
571 SequenceI found = findPoint(evt.getX(), evt.getY());
578 tooltip = found.getName();
586 public void mouseDragged(MouseEvent evt)
591 rotmat.setIdentity();
593 rotmat.rotate(my - omy, 'x');
594 rotmat.rotate(mx - omx, 'y');
596 for (int i = 0; i < npoint; i++)
598 SequencePoint sp = (SequencePoint) points.elementAt(i);
599 sp.coord[0] -= centre[0];
600 sp.coord[1] -= centre[1];
601 sp.coord[2] -= centre[2];
603 // Now apply the rotation matrix
604 sp.coord = rotmat.vectorMultiply(sp.coord);
606 // Now translate back again
607 sp.coord[0] += centre[0];
608 sp.coord[1] += centre[1];
609 sp.coord[2] += centre[2];
612 for (int i = 0; i < 3; i++)
614 axes[i] = rotmat.vectorMultiply(axes[i]);
619 paint(this.getGraphics());
622 public void rectSelect(int x1, int y1, int x2, int y2)
624 // boolean changedSel = false;
625 for (int i = 0; i < npoint; i++)
627 SequencePoint sp = (SequencePoint) points.elementAt(i);
628 int tmp1 = (int) ((sp.coord[0] - centre[0]) * scale + getSize().width / 2.0);
629 int tmp2 = (int) ((sp.coord[1] - centre[1]) * scale + getSize().height / 2.0);
631 if (tmp1 > x1 && tmp1 < x2 && tmp2 > y1 && tmp2 < y2)
635 if (!av.getSelectionGroup().getSequences(null)
636 .contains(sp.sequence))
638 av.getSelectionGroup().addSequence(sp.sequence, true);
645 public SequenceI findPoint(int x, int y)
648 int halfwidth = getSize().width / 2;
649 int halfheight = getSize().height / 2;
653 for (int i = 0; i < npoint; i++)
656 SequencePoint sp = (SequencePoint) points.elementAt(i);
657 int px = (int) ((sp.coord[0] - centre[0]) * scale)
659 int py = (int) ((sp.coord[1] - centre[1]) * scale)
662 if (Math.abs(px - x) < 3 && Math.abs(py - y) < 3)
669 return ((SequencePoint) points.elementAt(found)).sequence;