2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
23 import java.awt.event.*;
26 import jalview.datamodel.*;
27 import jalview.math.*;
35 public class RotatableCanvas extends JPanel implements MouseListener,
36 MouseMotionListener, KeyListener
38 RotatableMatrix idmat = new RotatableMatrix(3, 3);
40 RotatableMatrix objmat = new RotatableMatrix(3, 3);
42 RotatableMatrix rotmat = new RotatableMatrix(3, 3);
44 // RubberbandRectangle rubberband;
45 boolean drawAxes = true;
61 float[] centre = new float[3];
63 float[] width = new float[3];
65 float[] max = new float[3];
67 float[] min = new float[3];
97 float scalefactor = 1;
103 boolean showLabels = false;
105 Color bgColour = Color.black;
107 boolean applyToAllViews = false;
109 // Controller controller;
110 public RotatableCanvas(AlignmentPanel ap)
115 addMouseWheelListener(new MouseWheelListener()
117 public void mouseWheelMoved(MouseWheelEvent e)
119 if (e.getWheelRotation() > 0)
121 scale = (float) (scale * 1.1);
127 scale = (float) (scale * 0.9);
135 public void showLabels(boolean b)
142 public void setPoints(Vector points, int npoint)
144 this.points = points;
145 this.npoint = npoint;
147 ToolTipManager.sharedInstance().registerComponent(this);
148 ToolTipManager.sharedInstance().setInitialDelay(0);
149 ToolTipManager.sharedInstance().setDismissDelay(10000);
151 prefsize = getPreferredSize();
152 orig = new float[npoint][3];
154 for (int i = 0; i < npoint; i++)
156 SequencePoint sp = (SequencePoint) points.elementAt(i);
158 for (int j = 0; j < 3; j++)
160 orig[i][j] = sp.coord[j];
164 // Initialize the matrices to identity
165 for (int i = 0; i < 3; i++)
167 for (int j = 0; j < 3; j++)
171 idmat.addElement(i, j, 0);
172 objmat.addElement(i, j, 0);
173 rotmat.addElement(i, j, 0);
177 idmat.addElement(i, j, 0);
178 objmat.addElement(i, j, 0);
179 rotmat.addElement(i, j, 0);
184 axes = new float[3][3];
193 addMouseListener(this);
195 addMouseMotionListener(this);
200 public void initAxes()
202 for (int i = 0; i < 3; i++)
204 for (int j = 0; j < 3; j++)
221 public void findWidth()
226 max[0] = (float) -1e30;
227 max[1] = (float) -1e30;
228 max[2] = (float) -1e30;
230 min[0] = (float) 1e30;
231 min[1] = (float) 1e30;
232 min[2] = (float) 1e30;
234 for (int i = 0; i < 3; i++)
236 for (int j = 0; j < npoint; j++)
238 SequencePoint sp = (SequencePoint) points.elementAt(j);
240 if (sp.coord[i] >= max[i])
242 max[i] = sp.coord[i];
245 if (sp.coord[i] <= min[i])
247 min[i] = sp.coord[i];
252 // System.out.println("xmax " + max[0] + " min " + min[0]);
253 // System.out.println("ymax " + max[1] + " min " + min[1]);
254 // System.out.println("zmax " + max[2] + " min " + min[2]);
255 width[0] = Math.abs(max[0] - min[0]);
256 width[1] = Math.abs(max[1] - min[1]);
257 width[2] = Math.abs(max[2] - min[2]);
261 if (width[1] > width[0])
266 if (width[2] > width[1])
271 // System.out.println("Maxwidth = " + maxwidth);
277 * @return DOCUMENT ME!
279 public float findScale()
288 height = getHeight();
292 width = prefsize.width;
293 height = prefsize.height;
305 return (float) ((dim * scalefactor) / (2 * maxwidth));
311 public void findCentre()
313 // Find centre coordinate
316 centre[0] = (max[0] + min[0]) / 2;
317 centre[1] = (max[1] + min[1]) / 2;
318 centre[2] = (max[2] + min[2]) / 2;
320 // System.out.println("Centre x " + centre[0]);
321 // System.out.println("Centre y " + centre[1]);
322 // System.out.println("Centre z " + centre[2]);
328 * @return DOCUMENT ME!
330 public Dimension getPreferredSize()
332 if (prefsize != null)
338 return new Dimension(400, 400);
345 * @return DOCUMENT ME!
347 public Dimension getMinimumSize()
349 return getPreferredSize();
358 public void paintComponent(Graphics g1)
361 Graphics2D g = (Graphics2D) g1;
363 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
364 RenderingHints.VALUE_ANTIALIAS_ON);
367 g.setFont(new Font("Verdana", Font.PLAIN, 18));
368 g.drawString("Calculating PCA....", 20, getHeight() / 2);
372 // Only create the image at the beginning -
373 if ((img == null) || (prefsize.width != getWidth())
374 || (prefsize.height != getHeight()))
376 prefsize.width = getWidth();
377 prefsize.height = getHeight();
381 // System.out.println("New scale = " + scale);
382 img = createImage(getWidth(), getHeight());
383 ig = img.getGraphics();
386 drawBackground(ig, bgColour);
389 if (drawAxes == true)
394 g.drawImage(img, 0, 0, this);
404 public void drawAxes(Graphics g)
407 g.setColor(Color.yellow);
409 for (int i = 0; i < 3; i++)
411 g.drawLine(getWidth() / 2, getHeight() / 2, (int) ((axes[i][0]
412 * scale * max[0]) + (getWidth() / 2)), (int) ((axes[i][1]
413 * scale * max[1]) + (getHeight() / 2)));
425 public void drawBackground(Graphics g, Color col)
428 g.fillRect(0, 0, prefsize.width, prefsize.height);
437 public void drawScene(Graphics g1)
440 Graphics2D g = (Graphics2D) g1;
442 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
443 RenderingHints.VALUE_ANTIALIAS_ON);
445 int halfwidth = getWidth() / 2;
446 int halfheight = getHeight() / 2;
448 for (int i = 0; i < npoint; i++)
450 SequencePoint sp = (SequencePoint) points.elementAt(i);
451 int x = (int) ((float) (sp.coord[0] - centre[0]) * scale) + halfwidth;
452 int y = (int) ((float) (sp.coord[1] - centre[1]) * scale)
454 float z = sp.coord[1] - centre[2];
456 if (av.getSequenceColour(sp.sequence) == Color.black)
458 g.setColor(Color.white);
462 g.setColor(av.getSequenceColour(sp.sequence));
465 if (av.getSelectionGroup() != null)
467 if (av.getSelectionGroup().getSequences(null)
468 .contains(((SequencePoint) points.elementAt(i)).sequence))
470 g.setColor(Color.gray);
476 g.setColor(g.getColor().darker());
479 g.fillRect(x - 3, y - 3, 6, 6);
482 g.setColor(Color.red);
484 ((SequencePoint) points.elementAt(i)).sequence.getName(),
489 // //Now the rectangle
490 // if (rectx2 != -1 && recty2 != -1) {
491 // g.setColor(Color.white);
493 // g.drawRect(rectx1,recty1,rectx2-rectx1,recty2-recty1);
500 * @return DOCUMENT ME!
502 public Dimension minimumsize()
510 * @return DOCUMENT ME!
512 public Dimension preferredsize()
523 public void keyTyped(KeyEvent evt)
533 public void keyReleased(KeyEvent evt)
543 public void keyPressed(KeyEvent evt)
545 if (evt.getKeyCode() == KeyEvent.VK_UP)
547 scalefactor = (float) (scalefactor * 1.1);
550 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
552 scalefactor = (float) (scalefactor * 0.9);
555 else if (evt.getKeyChar() == 's')
557 System.err.println("DEBUG: Rectangle selection"); // log.debug
559 if ((rectx2 != -1) && (recty2 != -1))
561 rectSelect(rectx1, recty1, rectx2, recty2);
574 public void mouseClicked(MouseEvent evt)
584 public void mouseEntered(MouseEvent evt)
594 public void mouseExited(MouseEvent evt)
604 public void mouseReleased(MouseEvent evt)
614 public void mousePressed(MouseEvent evt)
634 SequenceI found = findPoint(x, y);
638 AlignmentPanel[] aps = getAssociatedPanels();
640 for (int a = 0; a < aps.length; a++)
642 if (aps[a].av.getSelectionGroup() != null)
644 aps[a].av.getSelectionGroup().addOrRemove(found, true);
648 aps[a].av.setSelectionGroup(new SequenceGroup());
649 aps[a].av.getSelectionGroup().addOrRemove(found, true);
650 aps[a].av.getSelectionGroup().setEndRes(
651 aps[a].av.getAlignment().getWidth() - 1);
654 PaintRefresher.Refresh(this, av.getSequenceSetId());
655 // canonical selection is sent to other listeners
662 // private void fireSequenceSelectionEvent(Selection sel) {
663 // controller.handleSequenceSelectionEvent(new
664 // SequenceSelectionEvent(this,sel));
666 public void mouseMoved(MouseEvent evt)
668 SequenceI found = findPoint(evt.getX(), evt.getY());
672 this.setToolTipText(found.getName());
676 this.setToolTipText(null);
686 public void mouseDragged(MouseEvent evt)
691 // Check if this is a rectangle drawing drag
692 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0)
694 // rectx2 = evt.getX();
695 // recty2 = evt.getY();
699 rotmat.setIdentity();
701 rotmat.rotate((float) (my - omy), 'x');
702 rotmat.rotate((float) (mx - omx), 'y');
704 for (int i = 0; i < npoint; i++)
706 SequencePoint sp = (SequencePoint) points.elementAt(i);
707 sp.coord[0] -= centre[0];
708 sp.coord[1] -= centre[1];
709 sp.coord[2] -= centre[2];
711 // Now apply the rotation matrix
712 sp.coord = rotmat.vectorMultiply(sp.coord);
714 // Now translate back again
715 sp.coord[0] += centre[0];
716 sp.coord[1] += centre[1];
717 sp.coord[2] += centre[2];
720 for (int i = 0; i < 3; i++)
722 axes[i] = rotmat.vectorMultiply(axes[i]);
728 paint(this.getGraphics());
744 public void rectSelect(int x1, int y1, int x2, int y2)
746 for (int i = 0; i < npoint; i++)
748 SequencePoint sp = (SequencePoint) points.elementAt(i);
749 int tmp1 = (int) (((sp.coord[0] - centre[0]) * scale) + ((float) getWidth() / 2.0));
750 int tmp2 = (int) (((sp.coord[1] - centre[1]) * scale) + ((float) getHeight() / 2.0));
752 if ((tmp1 > x1) && (tmp1 < x2) && (tmp2 > y1) && (tmp2 < y2))
756 if (!av.getSelectionGroup().getSequences(null)
757 .contains(sp.sequence))
759 av.getSelectionGroup().addSequence(sp.sequence, true);
766 // fireSequenceSelectionEvent(av.getSelection());
778 * @return DOCUMENT ME!
780 public SequenceI findPoint(int x, int y)
782 int halfwidth = getWidth() / 2;
783 int halfheight = getHeight() / 2;
787 for (int i = 0; i < npoint; i++)
789 SequencePoint sp = (SequencePoint) points.elementAt(i);
790 int px = (int) ((float) (sp.coord[0] - centre[0]) * scale)
792 int py = (int) ((float) (sp.coord[1] - centre[1]) * scale)
795 if ((Math.abs(px - x) < 3) && (Math.abs(py - y) < 3))
803 return ((SequencePoint) points.elementAt(found)).sequence;
811 AlignmentPanel[] getAssociatedPanels()
815 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
819 return new AlignmentPanel[]
826 * @return x,y,z positions of point s (index into points) under current
829 public double[] getPointPosition(int s)
831 double pts[] = new double[3];
832 float[] p = ((SequencePoint) points.elementAt(s)).coord;