2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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.api.RotatableCanvasI;
27 import jalview.datamodel.*;
28 import jalview.math.*;
36 public class RotatableCanvas extends JPanel implements MouseListener,
37 MouseMotionListener, KeyListener, RotatableCanvasI
39 RotatableMatrix idmat = new RotatableMatrix(3, 3);
41 RotatableMatrix objmat = new RotatableMatrix(3, 3);
43 RotatableMatrix rotmat = new RotatableMatrix(3, 3);
45 // RubberbandRectangle rubberband;
46 boolean drawAxes = true;
62 float[] centre = new float[3];
64 float[] width = new float[3];
66 float[] max = new float[3];
68 float[] min = new float[3];
98 float scalefactor = 1;
104 boolean showLabels = false;
106 Color bgColour = Color.black;
108 boolean applyToAllViews = false;
110 // Controller controller;
111 public RotatableCanvas(AlignmentPanel ap)
116 addMouseWheelListener(new MouseWheelListener()
118 public void mouseWheelMoved(MouseWheelEvent e)
120 if (e.getWheelRotation() > 0)
122 scale = (float) (scale * 1.1);
128 scale = (float) (scale * 0.9);
136 public void showLabels(boolean b)
142 boolean first = true;
144 public void setPoints(Vector points, int npoint)
146 this.points = points;
147 this.npoint = npoint;
150 ToolTipManager.sharedInstance().registerComponent(this);
151 ToolTipManager.sharedInstance().setInitialDelay(0);
152 ToolTipManager.sharedInstance().setDismissDelay(10000);
154 prefsize = getPreferredSize();
155 orig = new float[npoint][3];
157 for (int i = 0; i < npoint; i++)
159 SequencePoint sp = (SequencePoint) points.elementAt(i);
161 for (int j = 0; j < 3; j++)
163 orig[i][j] = sp.coord[j];
167 // Initialize the matrices to identity
168 for (int i = 0; i < 3; i++)
170 for (int j = 0; j < 3; j++)
174 idmat.addElement(i, j, 0);
175 objmat.addElement(i, j, 0);
176 rotmat.addElement(i, j, 0);
180 idmat.addElement(i, j, 0);
181 objmat.addElement(i, j, 0);
182 rotmat.addElement(i, j, 0);
187 axes = new float[3][3];
197 addMouseListener(this);
199 addMouseMotionListener(this);
204 public void initAxes()
206 for (int i = 0; i < 3; i++)
208 for (int j = 0; j < 3; j++)
225 public void findWidth()
230 max[0] = (float) -1e30;
231 max[1] = (float) -1e30;
232 max[2] = (float) -1e30;
234 min[0] = (float) 1e30;
235 min[1] = (float) 1e30;
236 min[2] = (float) 1e30;
238 for (int i = 0; i < 3; i++)
240 for (int j = 0; j < npoint; j++)
242 SequencePoint sp = (SequencePoint) points.elementAt(j);
244 if (sp.coord[i] >= max[i])
246 max[i] = sp.coord[i];
249 if (sp.coord[i] <= min[i])
251 min[i] = sp.coord[i];
256 // System.out.println("xmax " + max[0] + " min " + min[0]);
257 // System.out.println("ymax " + max[1] + " min " + min[1]);
258 // System.out.println("zmax " + max[2] + " min " + min[2]);
259 width[0] = Math.abs(max[0] - min[0]);
260 width[1] = Math.abs(max[1] - min[1]);
261 width[2] = Math.abs(max[2] - min[2]);
265 if (width[1] > width[0])
270 if (width[2] > width[1])
275 // System.out.println("Maxwidth = " + maxwidth);
281 * @return DOCUMENT ME!
283 public float findScale()
292 height = getHeight();
296 width = prefsize.width;
297 height = prefsize.height;
309 return (float) ((dim * scalefactor) / (2 * maxwidth));
315 public void findCentre()
317 // Find centre coordinate
320 centre[0] = (max[0] + min[0]) / 2;
321 centre[1] = (max[1] + min[1]) / 2;
322 centre[2] = (max[2] + min[2]) / 2;
324 // System.out.println("Centre x " + centre[0]);
325 // System.out.println("Centre y " + centre[1]);
326 // System.out.println("Centre z " + centre[2]);
332 * @return DOCUMENT ME!
334 public Dimension getPreferredSize()
336 if (prefsize != null)
342 return new Dimension(400, 400);
349 * @return DOCUMENT ME!
351 public Dimension getMinimumSize()
353 return getPreferredSize();
362 public void paintComponent(Graphics g1)
365 Graphics2D g = (Graphics2D) g1;
367 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
368 RenderingHints.VALUE_ANTIALIAS_ON);
371 g.setFont(new Font("Verdana", Font.PLAIN, 18));
372 g.drawString("Calculating PCA....", 20, getHeight() / 2);
376 // Only create the image at the beginning -
377 if ((img == null) || (prefsize.width != getWidth())
378 || (prefsize.height != getHeight()))
380 prefsize.width = getWidth();
381 prefsize.height = getHeight();
385 // System.out.println("New scale = " + scale);
386 img = createImage(getWidth(), getHeight());
387 ig = img.getGraphics();
390 drawBackground(ig, bgColour);
393 if (drawAxes == true)
398 g.drawImage(img, 0, 0, this);
408 public void drawAxes(Graphics g)
411 g.setColor(Color.yellow);
413 for (int i = 0; i < 3; i++)
415 g.drawLine(getWidth() / 2, getHeight() / 2, (int) ((axes[i][0]
416 * scale * max[0]) + (getWidth() / 2)), (int) ((axes[i][1]
417 * scale * max[1]) + (getHeight() / 2)));
429 public void drawBackground(Graphics g, Color col)
432 g.fillRect(0, 0, prefsize.width, prefsize.height);
441 public void drawScene(Graphics g1)
444 Graphics2D g = (Graphics2D) g1;
446 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
447 RenderingHints.VALUE_ANTIALIAS_ON);
449 int halfwidth = getWidth() / 2;
450 int halfheight = getHeight() / 2;
452 for (int i = 0; i < npoint; i++)
454 SequencePoint sp = (SequencePoint) points.elementAt(i);
455 int x = (int) ((float) (sp.coord[0] - centre[0]) * scale) + halfwidth;
456 int y = (int) ((float) (sp.coord[1] - centre[1]) * scale)
458 float z = sp.coord[1] - centre[2];
460 if (av.getSequenceColour(sp.sequence) == Color.black)
462 g.setColor(Color.white);
466 g.setColor(av.getSequenceColour(sp.sequence));
469 if (av.getSelectionGroup() != null)
471 if (av.getSelectionGroup().getSequences(null)
472 .contains(((SequencePoint) points.elementAt(i)).sequence))
474 g.setColor(Color.gray);
480 g.setColor(g.getColor().darker());
483 g.fillRect(x - 3, y - 3, 6, 6);
486 g.setColor(Color.red);
488 ((SequencePoint) points.elementAt(i)).sequence.getName(),
493 // //Now the rectangle
494 // if (rectx2 != -1 && recty2 != -1) {
495 // g.setColor(Color.white);
497 // g.drawRect(rectx1,recty1,rectx2-rectx1,recty2-recty1);
504 * @return DOCUMENT ME!
506 public Dimension minimumsize()
514 * @return DOCUMENT ME!
516 public Dimension preferredsize()
527 public void keyTyped(KeyEvent evt)
537 public void keyReleased(KeyEvent evt)
547 public void keyPressed(KeyEvent evt)
549 if (evt.getKeyCode() == KeyEvent.VK_UP)
551 scalefactor = (float) (scalefactor * 1.1);
554 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
556 scalefactor = (float) (scalefactor * 0.9);
559 else if (evt.getKeyChar() == 's')
561 System.err.println("DEBUG: Rectangle selection"); // log.debug
563 if ((rectx2 != -1) && (recty2 != -1))
565 rectSelect(rectx1, recty1, rectx2, recty2);
578 public void mouseClicked(MouseEvent evt)
588 public void mouseEntered(MouseEvent evt)
598 public void mouseExited(MouseEvent evt)
608 public void mouseReleased(MouseEvent evt)
618 public void mousePressed(MouseEvent evt)
638 SequenceI found = findPoint(x, y);
642 AlignmentPanel[] aps = getAssociatedPanels();
644 for (int a = 0; a < aps.length; a++)
646 if (aps[a].av.getSelectionGroup() != null)
648 aps[a].av.getSelectionGroup().addOrRemove(found, true);
652 aps[a].av.setSelectionGroup(new SequenceGroup());
653 aps[a].av.getSelectionGroup().addOrRemove(found, true);
654 aps[a].av.getSelectionGroup().setEndRes(
655 aps[a].av.getAlignment().getWidth() - 1);
658 PaintRefresher.Refresh(this, av.getSequenceSetId());
659 // canonical selection is sent to other listeners
666 // private void fireSequenceSelectionEvent(Selection sel) {
667 // controller.handleSequenceSelectionEvent(new
668 // SequenceSelectionEvent(this,sel));
670 public void mouseMoved(MouseEvent evt)
672 SequenceI found = findPoint(evt.getX(), evt.getY());
676 this.setToolTipText(found.getName());
680 this.setToolTipText(null);
690 public void mouseDragged(MouseEvent evt)
695 // Check if this is a rectangle drawing drag
696 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0)
698 // rectx2 = evt.getX();
699 // recty2 = evt.getY();
703 rotmat.setIdentity();
705 rotmat.rotate((float) (my - omy), 'x');
706 rotmat.rotate((float) (mx - omx), 'y');
708 for (int i = 0; i < npoint; i++)
710 SequencePoint sp = (SequencePoint) points.elementAt(i);
711 sp.coord[0] -= centre[0];
712 sp.coord[1] -= centre[1];
713 sp.coord[2] -= centre[2];
715 // Now apply the rotation matrix
716 sp.coord = rotmat.vectorMultiply(sp.coord);
718 // Now translate back again
719 sp.coord[0] += centre[0];
720 sp.coord[1] += centre[1];
721 sp.coord[2] += centre[2];
724 for (int i = 0; i < 3; i++)
726 axes[i] = rotmat.vectorMultiply(axes[i]);
732 paint(this.getGraphics());
748 public void rectSelect(int x1, int y1, int x2, int y2)
750 for (int i = 0; i < npoint; i++)
752 SequencePoint sp = (SequencePoint) points.elementAt(i);
753 int tmp1 = (int) (((sp.coord[0] - centre[0]) * scale) + ((float) getWidth() / 2.0));
754 int tmp2 = (int) (((sp.coord[1] - centre[1]) * scale) + ((float) getHeight() / 2.0));
756 if ((tmp1 > x1) && (tmp1 < x2) && (tmp2 > y1) && (tmp2 < y2))
760 if (!av.getSelectionGroup().getSequences(null)
761 .contains(sp.sequence))
763 av.getSelectionGroup().addSequence(sp.sequence, true);
770 // fireSequenceSelectionEvent(av.getSelection());
782 * @return DOCUMENT ME!
784 public SequenceI findPoint(int x, int y)
786 int halfwidth = getWidth() / 2;
787 int halfheight = getHeight() / 2;
791 for (int i = 0; i < npoint; i++)
793 SequencePoint sp = (SequencePoint) points.elementAt(i);
794 int px = (int) ((float) (sp.coord[0] - centre[0]) * scale)
796 int py = (int) ((float) (sp.coord[1] - centre[1]) * scale)
799 if ((Math.abs(px - x) < 3) && (Math.abs(py - y) < 3))
807 return ((SequencePoint) points.elementAt(found)).sequence;
815 AlignmentPanel[] getAssociatedPanels()
819 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
823 return new AlignmentPanel[]
830 * @return x,y,z positions of point s (index into points) under current
833 public double[] getPointPosition(int s)
835 double pts[] = new double[3];
836 float[] p = ((SequencePoint) points.elementAt(s)).coord;