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.
23 import jalview.api.RotatableCanvasI;
24 import jalview.datamodel.Point;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.datamodel.SequencePoint;
28 import jalview.math.RotatableMatrix;
29 import jalview.math.RotatableMatrix.Axis;
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.Graphics2D;
38 import java.awt.Image;
39 import java.awt.RenderingHints;
40 import java.awt.event.InputEvent;
41 import java.awt.event.KeyEvent;
42 import java.awt.event.KeyListener;
43 import java.awt.event.MouseEvent;
44 import java.awt.event.MouseListener;
45 import java.awt.event.MouseMotionListener;
46 import java.awt.event.MouseWheelEvent;
47 import java.awt.event.MouseWheelListener;
48 import java.util.Vector;
50 import javax.swing.JPanel;
51 import javax.swing.ToolTipManager;
54 * Models a Panel on which a set of points, and optionally x/y/z axes, can be
55 * drawn, and rotated or zoomed with the mouse
57 public class RotatableCanvas extends JPanel implements MouseListener,
58 MouseMotionListener, KeyListener, RotatableCanvasI
60 private static final int DIMS = 3;
62 // RubberbandRectangle rubberband;
63 boolean drawAxes = true;
77 float[] width = new float[DIMS];
79 float[] max = new float[DIMS];
81 float[] min = new float[DIMS];
89 Vector<SequencePoint> points;
93 Point[] axisEndPoints;
111 float scalefactor = 1;
113 AlignmentViewport av;
117 boolean showLabels = false;
119 Color bgColour = Color.black;
121 boolean applyToAllViews = false;
123 boolean first = true;
130 public RotatableCanvas(AlignmentPanel panel)
134 axisEndPoints = new Point[DIMS];
136 addMouseWheelListener(new MouseWheelListener()
139 public void mouseWheelMoved(MouseWheelEvent e)
141 double wheelRotation = e.getPreciseWheelRotation();
142 if (wheelRotation > 0)
147 scale = (float) (scale * 1.1);
150 else if (wheelRotation < 0)
155 scale = (float) (scale * 0.9);
163 public void showLabels(boolean b)
170 public void setPoints(Vector<SequencePoint> points, int npoint)
172 this.points = points;
173 this.npoint = npoint;
176 ToolTipManager.sharedInstance().registerComponent(this);
177 ToolTipManager.sharedInstance().setInitialDelay(0);
178 ToolTipManager.sharedInstance().setDismissDelay(10000);
180 prefsize = getPreferredSize();
181 orig = new Point[npoint];
183 for (int i = 0; i < npoint; i++)
185 SequencePoint sp = points.elementAt(i);
197 addMouseListener(this);
198 addMouseMotionListener(this);
204 * Resets axes to the initial state: x-axis to the right, y-axis up, z-axis to
205 * back (so obscured in a 2-D display)
207 public void resetAxes()
209 axisEndPoints[0] = new Point(1f, 0f, 0f);
210 axisEndPoints[1] = new Point(0f, 1f, 0f);
211 axisEndPoints[2] = new Point(0f, 0f, 1f);
215 * Computes and saves the maximum and minimum (x, y, z) positions of any
216 * sequence point, and also the min-max range (width) for each dimension, and
217 * the maximum width for all dimensions
219 public void findWidth()
221 max = new float[DIMS];
222 min = new float[DIMS];
224 max[0] = Float.MIN_VALUE;
225 max[1] = Float.MIN_VALUE;
226 max[2] = Float.MIN_VALUE;
228 min[0] = Float.MAX_VALUE;
229 min[1] = Float.MAX_VALUE;
230 min[2] = Float.MAX_VALUE;
232 for (SequencePoint sp : points)
234 max[0] = Math.max(max[0], sp.coord.x);
235 max[1] = Math.max(max[1], sp.coord.y);
236 max[2] = Math.max(max[2], sp.coord.z);
237 min[0] = Math.min(min[0], sp.coord.x);
238 min[1] = Math.min(min[1], sp.coord.y);
239 min[2] = Math.min(min[2], sp.coord.z);
242 width[0] = Math.abs(max[0] - min[0]);
243 width[1] = Math.abs(max[1] - min[1]);
244 width[2] = Math.abs(max[2] - min[2]);
246 maxwidth = Math.max(width[0], Math.max(width[1], width[2]));
252 * @return DOCUMENT ME!
254 public float findScale()
263 height = getHeight();
268 height = prefsize.height;
280 return (dim * scalefactor) / (2 * maxwidth);
284 * Computes and saves the position of the centre of the view
286 public void findCentre()
290 float x = (max[0] + min[0]) / 2;
291 float y = (max[1] + min[1]) / 2;
292 float z = (max[2] + min[2]) / 2;
294 centre = new Point(x, y, z);
300 * @return DOCUMENT ME!
303 public Dimension getPreferredSize()
305 if (prefsize != null)
311 return new Dimension(400, 400);
318 * @return DOCUMENT ME!
321 public Dimension getMinimumSize()
323 return getPreferredSize();
333 public void paintComponent(Graphics g1)
336 Graphics2D g = (Graphics2D) g1;
338 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
339 RenderingHints.VALUE_ANTIALIAS_ON);
342 g.setFont(new Font("Verdana", Font.PLAIN, 18));
344 MessageManager.getString("label.calculating_pca") + "....",
345 20, getHeight() / 2);
349 // Only create the image at the beginning -
350 if ((img == null) || (prefsize.width != getWidth())
351 || (prefsize.height != getHeight()))
353 prefsize.width = getWidth();
354 prefsize.height = getHeight();
358 // System.out.println("New scale = " + scale);
359 img = createImage(getWidth(), getHeight());
360 ig = img.getGraphics();
363 drawBackground(ig, bgColour);
371 g.drawImage(img, 0, 0, this);
376 * Resets the view to initial state (no rotation)
378 public void resetView()
385 * Draws lines for the x, y, z axes
389 public void drawAxes(Graphics g)
392 g.setColor(Color.yellow);
394 for (int i = 0; i < DIMS; i++)
396 g.drawLine(getWidth() / 2, getHeight() / 2,
397 (int) ((axisEndPoints[i].x * scale * max[0]) + (getWidth() / 2)),
398 (int) ((axisEndPoints[i].y * scale * max[1]) + (getHeight() / 2)));
403 * Fills the background with the specified colour
408 public void drawBackground(Graphics g, Color col)
411 g.fillRect(0, 0, prefsize.width, prefsize.height);
415 * Draws points (6x6 squares) for the sequences of the PCA, and labels
416 * (sequence names) if configured to do so. The sequence points colours are
417 * taken from the sequence ids in the alignment (converting black to white).
418 * Sequences 'at the back' (z-coordinate is negative) are shaded slightly
419 * darker to help give a 3-D sensation.
423 public void drawScene(Graphics g1)
425 Graphics2D g = (Graphics2D) g1;
427 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
428 RenderingHints.VALUE_ANTIALIAS_ON);
430 for (int i = 0; i < npoint; i++)
433 * sequence point colour as sequence id, but
434 * gray if sequence is currently selected
436 SequencePoint sp = points.elementAt(i);
437 SequenceI sequence = sp.getSequence();
438 Color sequenceColour = av.getSequenceColour(sequence);
440 sequenceColour == Color.black ? Color.white : sequenceColour);
441 if (av.getSelectionGroup() != null)
443 if (av.getSelectionGroup().getSequences(null)
446 g.setColor(Color.gray);
451 * dim sequence points 'at the back'
453 if (sp.coord.z < centre.z)
455 g.setColor(g.getColor().darker());
458 int halfwidth = getWidth() / 2;
459 int halfheight = getHeight() / 2;
460 int x = (int) ((sp.coord.x - centre.x) * scale) + halfwidth;
461 int y = (int) ((sp.coord.y - centre.y) * scale) + halfheight;
462 g.fillRect(x - 3, y - 3, 6, 6);
466 g.setColor(Color.red);
467 g.drawString(sequence.getName(), x - 3, y - 4);
471 // //Now the rectangle
472 // if (rectx2 != -1 && recty2 != -1) {
473 // g.setColor(Color.white);
475 // g.drawRect(rectx1,recty1,rectx2-rectx1,recty2-recty1);
480 public void keyTyped(KeyEvent evt)
485 public void keyReleased(KeyEvent evt)
490 * Responds to up or down arrow key by zooming in or out, respectively
495 public void keyPressed(KeyEvent evt)
497 if (evt.getKeyCode() == KeyEvent.VK_UP)
499 scalefactor = (float) (scalefactor * 1.1);
502 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
504 scalefactor = (float) (scalefactor * 0.9);
507 else if (evt.getKeyChar() == 's')
509 // Cache.log.warn("DEBUG: Rectangle selection");
510 // todo not yet enabled as rectx2, recty2 are always -1
511 // need to set them in mouseDragged
512 if ((rectx2 != -1) && (recty2 != -1))
514 rectSelect(rectx1, recty1, rectx2, recty2);
522 public void mouseClicked(MouseEvent evt)
527 public void mouseEntered(MouseEvent evt)
532 public void mouseExited(MouseEvent evt)
537 public void mouseReleased(MouseEvent evt)
542 * If the mouse press is at (within 2 pixels of) a sequence point, toggles
543 * (adds or removes) the corresponding sequence as a member of the viewport
544 * selection group. This supports configuring a group in the alignment by
545 * clicking on points in the PCA display.
548 public void mousePressed(MouseEvent evt)
565 SequenceI found = findSequenceAtPoint(x, y);
569 AlignmentPanel[] aps = getAssociatedPanels();
571 for (int a = 0; a < aps.length; a++)
573 if (aps[a].av.getSelectionGroup() != null)
575 aps[a].av.getSelectionGroup().addOrRemove(found, true);
579 aps[a].av.setSelectionGroup(new SequenceGroup());
580 aps[a].av.getSelectionGroup().addOrRemove(found, true);
581 aps[a].av.getSelectionGroup()
582 .setEndRes(aps[a].av.getAlignment().getWidth() - 1);
585 PaintRefresher.Refresh(this, av.getSequenceSetId());
586 // canonical selection is sent to other listeners
594 * Sets the tooltip to the name of the sequence within 2 pixels of the mouse
595 * position, or clears the tooltip if none found
598 public void mouseMoved(MouseEvent evt)
600 SequenceI found = findSequenceAtPoint(evt.getX(), evt.getY());
602 this.setToolTipText(found == null ? null : found.getName());
606 * Action handler for a mouse drag. Rotates the display around the X axis (for
607 * up/down mouse movement) and/or the Y axis (for left/right mouse movement).
612 public void mouseDragged(MouseEvent evt)
614 int xPos = evt.getX();
615 int yPos = evt.getY();
617 if (xPos == mouseX && yPos == mouseY)
622 // Check if this is a rectangle drawing drag
623 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0)
625 // rectx2 = evt.getX();
626 // recty2 = evt.getY();
631 * get the identity transformation...
633 RotatableMatrix rotmat = new RotatableMatrix();
636 * rotate around the X axis for change in Y
637 * (mouse movement up/down); note we are equating a
638 * number of pixels with degrees of rotation here!
642 rotmat.rotate(yPos - mouseY, Axis.X);
646 * rotate around the Y axis for change in X
647 * (mouse movement left/right)
651 rotmat.rotate(xPos - mouseX, Axis.Y);
655 * apply the composite transformation to sequence points
657 for (int i = 0; i < npoint; i++)
659 SequencePoint sp = points.elementAt(i);
660 sp.translateBack(centre);
662 // Now apply the rotation matrix
663 sp.coord = rotmat.vectorMultiply(sp.coord);
665 // Now translate back again
666 sp.translate(centre);
670 * rotate the x/y/z axis positions
672 for (int i = 0; i < DIMS; i++)
674 axisEndPoints[i] = rotmat.vectorMultiply(axisEndPoints[i]);
680 paint(this.getGraphics());
685 * Adds any sequences whose displayed points are within the given rectangle to
686 * the viewport's current selection. Intended for key 's' after dragging to
687 * select a region of the PCA.
694 public void rectSelect(int x1, int y1, int x2, int y2)
696 for (int i = 0; i < npoint; i++)
698 SequencePoint sp = points.elementAt(i);
699 int tmp1 = (int) (((sp.coord.x - centre.x) * scale)
700 + (getWidth() / 2.0));
701 int tmp2 = (int) (((sp.coord.y - centre.y) * scale)
702 + (getHeight() / 2.0));
704 if ((tmp1 > x1) && (tmp1 < x2) && (tmp2 > y1) && (tmp2 < y2))
708 SequenceI sequence = sp.getSequence();
709 if (!av.getSelectionGroup().getSequences(null)
712 av.getSelectionGroup().addSequence(sequence, true);
720 * Answers the first sequence found whose point on the display is within 2
721 * pixels of the given coordinates, or null if none is found
728 public SequenceI findSequenceAtPoint(int x, int y)
730 int halfwidth = getWidth() / 2;
731 int halfheight = getHeight() / 2;
735 for (int i = 0; i < npoint; i++)
737 SequencePoint sp = points.elementAt(i);
738 int px = (int) ((sp.coord.x - centre.x) * scale)
740 int py = (int) ((sp.coord.y - centre.y) * scale)
743 if ((Math.abs(px - x) < 3) && (Math.abs(py - y) < 3))
752 return points.elementAt(found).getSequence();
761 * Answers the panel the PCA is associated with (all panels for this alignment
762 * if 'associate with all panels' is selected).
766 AlignmentPanel[] getAssociatedPanels()
770 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
774 return new AlignmentPanel[] { ap };