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 int halfwidth = getWidth() / 2;
431 int halfheight = getHeight() / 2;
433 for (int i = 0; i < npoint; i++)
435 SequencePoint sp = points.elementAt(i);
436 int x = (int) ((sp.coord.x - centre.x) * scale) + halfwidth;
437 int y = (int) ((sp.coord.y - centre.y) * scale)
439 float z = sp.coord.y - centre.z; // todo sp.coord.z JAL-2963
441 SequenceI sequence = sp.getSequence();
442 if (av.getSequenceColour(sequence) == Color.black)
444 g.setColor(Color.white);
448 g.setColor(av.getSequenceColour(sequence));
451 if (av.getSelectionGroup() != null)
453 if (av.getSelectionGroup().getSequences(null)
456 g.setColor(Color.gray);
462 g.setColor(g.getColor().darker());
465 g.fillRect(x - 3, y - 3, 6, 6);
468 g.setColor(Color.red);
469 g.drawString(sequence.getName(), x - 3, y - 4);
473 // //Now the rectangle
474 // if (rectx2 != -1 && recty2 != -1) {
475 // g.setColor(Color.white);
477 // g.drawRect(rectx1,recty1,rectx2-rectx1,recty2-recty1);
482 public void keyTyped(KeyEvent evt)
487 public void keyReleased(KeyEvent evt)
492 * Responds to up or down arrow key by zooming in or out, respectively
497 public void keyPressed(KeyEvent evt)
499 if (evt.getKeyCode() == KeyEvent.VK_UP)
501 scalefactor = (float) (scalefactor * 1.1);
504 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
506 scalefactor = (float) (scalefactor * 0.9);
509 else if (evt.getKeyChar() == 's')
511 // Cache.log.warn("DEBUG: Rectangle selection");
512 // todo not yet enabled as rectx2, recty2 are always -1
513 // need to set them in mouseDragged
514 if ((rectx2 != -1) && (recty2 != -1))
516 rectSelect(rectx1, recty1, rectx2, recty2);
524 public void mouseClicked(MouseEvent evt)
529 public void mouseEntered(MouseEvent evt)
534 public void mouseExited(MouseEvent evt)
539 public void mouseReleased(MouseEvent evt)
544 * If the mouse press is at (within 2 pixels of) a sequence point, toggles
545 * (adds or removes) the corresponding sequence as a member of the viewport
546 * selection group. This supports configuring a group in the alignment by
547 * clicking on points in the PCA display.
550 public void mousePressed(MouseEvent evt)
567 SequenceI found = findSequenceAtPoint(x, y);
571 AlignmentPanel[] aps = getAssociatedPanels();
573 for (int a = 0; a < aps.length; a++)
575 if (aps[a].av.getSelectionGroup() != null)
577 aps[a].av.getSelectionGroup().addOrRemove(found, true);
581 aps[a].av.setSelectionGroup(new SequenceGroup());
582 aps[a].av.getSelectionGroup().addOrRemove(found, true);
583 aps[a].av.getSelectionGroup()
584 .setEndRes(aps[a].av.getAlignment().getWidth() - 1);
587 PaintRefresher.Refresh(this, av.getSequenceSetId());
588 // canonical selection is sent to other listeners
596 * Sets the tooltip to the name of the sequence within 2 pixels of the mouse
597 * position, or clears the tooltip if none found
600 public void mouseMoved(MouseEvent evt)
602 SequenceI found = findSequenceAtPoint(evt.getX(), evt.getY());
604 this.setToolTipText(found == null ? null : found.getName());
608 * Action handler for a mouse drag. Rotates the display around the X axis (for
609 * up/down mouse movement) and/or the Y axis (for left/right mouse movement).
614 public void mouseDragged(MouseEvent evt)
616 int xPos = evt.getX();
617 int yPos = evt.getY();
619 if (xPos == mouseX && yPos == mouseY)
624 // Check if this is a rectangle drawing drag
625 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0)
627 // rectx2 = evt.getX();
628 // recty2 = evt.getY();
633 * get the identity transformation...
635 RotatableMatrix rotmat = new RotatableMatrix();
638 * rotate around the X axis for change in Y
639 * (mouse movement up/down); note we are equating a
640 * number of pixels with degrees of rotation here!
644 rotmat.rotate(yPos - mouseY, Axis.X);
648 * rotate around the Y axis for change in X
649 * (mouse movement left/right)
653 rotmat.rotate(xPos - mouseX, Axis.Y);
657 * apply the composite transformation to sequence points
659 for (int i = 0; i < npoint; i++)
661 SequencePoint sp = points.elementAt(i);
662 sp.translateBack(centre);
664 // Now apply the rotation matrix
665 sp.coord = rotmat.vectorMultiply(sp.coord);
667 // Now translate back again
668 sp.translate(centre);
672 * rotate the x/y/z axis positions
674 for (int i = 0; i < DIMS; i++)
676 axisEndPoints[i] = rotmat.vectorMultiply(axisEndPoints[i]);
682 paint(this.getGraphics());
687 * Adds any sequences whose displayed points are within the given rectangle to
688 * the viewport's current selection. Intended for key 's' after dragging to
689 * select a region of the PCA.
696 public void rectSelect(int x1, int y1, int x2, int y2)
698 for (int i = 0; i < npoint; i++)
700 SequencePoint sp = points.elementAt(i);
701 int tmp1 = (int) (((sp.coord.x - centre.x) * scale)
702 + (getWidth() / 2.0));
703 int tmp2 = (int) (((sp.coord.y - centre.y) * scale)
704 + (getHeight() / 2.0));
706 if ((tmp1 > x1) && (tmp1 < x2) && (tmp2 > y1) && (tmp2 < y2))
710 SequenceI sequence = sp.getSequence();
711 if (!av.getSelectionGroup().getSequences(null)
714 av.getSelectionGroup().addSequence(sequence, true);
722 * Answers the first sequence found whose point on the display is within 2
723 * pixels of the given coordinates, or null if none is found
730 public SequenceI findSequenceAtPoint(int x, int y)
732 int halfwidth = getWidth() / 2;
733 int halfheight = getHeight() / 2;
737 for (int i = 0; i < npoint; i++)
739 SequencePoint sp = points.elementAt(i);
740 int px = (int) ((sp.coord.x - centre.x) * scale)
742 int py = (int) ((sp.coord.y - centre.y) * scale)
745 if ((Math.abs(px - x) < 3) && (Math.abs(py - y) < 3))
754 return points.elementAt(found).getSequence();
763 * Answers the panel the PCA is associated with (all panels for this alignment
764 * if 'associate with all panels' is selected).
768 AlignmentPanel[] getAssociatedPanels()
772 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
776 return new AlignmentPanel[] { ap };