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.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)
143 public void setPoints(Vector points, int npoint)
145 this.points = points;
146 this.npoint = npoint;
148 ToolTipManager.sharedInstance().registerComponent(this);
149 ToolTipManager.sharedInstance().setInitialDelay(0);
150 ToolTipManager.sharedInstance().setDismissDelay(10000);
152 prefsize = getPreferredSize();
153 orig = new float[npoint][3];
155 for (int i = 0; i < npoint; i++)
157 SequencePoint sp = (SequencePoint) points.elementAt(i);
159 for (int j = 0; j < 3; j++)
161 orig[i][j] = sp.coord[j];
165 // Initialize the matrices to identity
166 for (int i = 0; i < 3; i++)
168 for (int j = 0; j < 3; j++)
172 idmat.addElement(i, j, 0);
173 objmat.addElement(i, j, 0);
174 rotmat.addElement(i, j, 0);
178 idmat.addElement(i, j, 0);
179 objmat.addElement(i, j, 0);
180 rotmat.addElement(i, j, 0);
185 axes = new float[3][3];
194 addMouseListener(this);
196 addMouseMotionListener(this);
201 public void initAxes()
203 for (int i = 0; i < 3; i++)
205 for (int j = 0; j < 3; j++)
222 public void findWidth()
227 max[0] = (float) -1e30;
228 max[1] = (float) -1e30;
229 max[2] = (float) -1e30;
231 min[0] = (float) 1e30;
232 min[1] = (float) 1e30;
233 min[2] = (float) 1e30;
235 for (int i = 0; i < 3; i++)
237 for (int j = 0; j < npoint; j++)
239 SequencePoint sp = (SequencePoint) points.elementAt(j);
241 if (sp.coord[i] >= max[i])
243 max[i] = sp.coord[i];
246 if (sp.coord[i] <= min[i])
248 min[i] = sp.coord[i];
253 // System.out.println("xmax " + max[0] + " min " + min[0]);
254 // System.out.println("ymax " + max[1] + " min " + min[1]);
255 // System.out.println("zmax " + max[2] + " min " + min[2]);
256 width[0] = Math.abs(max[0] - min[0]);
257 width[1] = Math.abs(max[1] - min[1]);
258 width[2] = Math.abs(max[2] - min[2]);
262 if (width[1] > width[0])
267 if (width[2] > width[1])
272 // System.out.println("Maxwidth = " + maxwidth);
278 * @return DOCUMENT ME!
280 public float findScale()
289 height = getHeight();
293 width = prefsize.width;
294 height = prefsize.height;
306 return (float) ((dim * scalefactor) / (2 * maxwidth));
312 public void findCentre()
314 // Find centre coordinate
317 centre[0] = (max[0] + min[0]) / 2;
318 centre[1] = (max[1] + min[1]) / 2;
319 centre[2] = (max[2] + min[2]) / 2;
321 // System.out.println("Centre x " + centre[0]);
322 // System.out.println("Centre y " + centre[1]);
323 // System.out.println("Centre z " + centre[2]);
329 * @return DOCUMENT ME!
331 public Dimension getPreferredSize()
333 if (prefsize != null)
339 return new Dimension(400, 400);
346 * @return DOCUMENT ME!
348 public Dimension getMinimumSize()
350 return getPreferredSize();
359 public void paintComponent(Graphics g1)
362 Graphics2D g = (Graphics2D) g1;
364 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
365 RenderingHints.VALUE_ANTIALIAS_ON);
368 g.setFont(new Font("Verdana", Font.PLAIN, 18));
369 g.drawString("Calculating PCA....", 20, getHeight() / 2);
373 // Only create the image at the beginning -
374 if ((img == null) || (prefsize.width != getWidth())
375 || (prefsize.height != getHeight()))
377 prefsize.width = getWidth();
378 prefsize.height = getHeight();
382 // System.out.println("New scale = " + scale);
383 img = createImage(getWidth(), getHeight());
384 ig = img.getGraphics();
387 drawBackground(ig, bgColour);
390 if (drawAxes == true)
395 g.drawImage(img, 0, 0, this);
405 public void drawAxes(Graphics g)
408 g.setColor(Color.yellow);
410 for (int i = 0; i < 3; i++)
412 g.drawLine(getWidth() / 2, getHeight() / 2, (int) ((axes[i][0]
413 * scale * max[0]) + (getWidth() / 2)), (int) ((axes[i][1]
414 * scale * max[1]) + (getHeight() / 2)));
426 public void drawBackground(Graphics g, Color col)
429 g.fillRect(0, 0, prefsize.width, prefsize.height);
438 public void drawScene(Graphics g1)
441 Graphics2D g = (Graphics2D) g1;
443 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
444 RenderingHints.VALUE_ANTIALIAS_ON);
446 int halfwidth = getWidth() / 2;
447 int halfheight = getHeight() / 2;
449 for (int i = 0; i < npoint; i++)
451 SequencePoint sp = (SequencePoint) points.elementAt(i);
452 int x = (int) ((float) (sp.coord[0] - centre[0]) * scale) + halfwidth;
453 int y = (int) ((float) (sp.coord[1] - centre[1]) * scale)
455 float z = sp.coord[1] - centre[2];
457 if (av.getSequenceColour(sp.sequence) == Color.black)
459 g.setColor(Color.white);
463 g.setColor(av.getSequenceColour(sp.sequence));
466 if (av.getSelectionGroup() != null)
468 if (av.getSelectionGroup().getSequences(null)
469 .contains(((SequencePoint) points.elementAt(i)).sequence))
471 g.setColor(Color.gray);
477 g.setColor(g.getColor().darker());
480 g.fillRect(x - 3, y - 3, 6, 6);
483 g.setColor(Color.red);
485 ((SequencePoint) points.elementAt(i)).sequence.getName(),
490 // //Now the rectangle
491 // if (rectx2 != -1 && recty2 != -1) {
492 // g.setColor(Color.white);
494 // g.drawRect(rectx1,recty1,rectx2-rectx1,recty2-recty1);
501 * @return DOCUMENT ME!
503 public Dimension minimumsize()
511 * @return DOCUMENT ME!
513 public Dimension preferredsize()
524 public void keyTyped(KeyEvent evt)
534 public void keyReleased(KeyEvent evt)
544 public void keyPressed(KeyEvent evt)
546 if (evt.getKeyCode() == KeyEvent.VK_UP)
548 scalefactor = (float) (scalefactor * 1.1);
551 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
553 scalefactor = (float) (scalefactor * 0.9);
556 else if (evt.getKeyChar() == 's')
558 System.err.println("DEBUG: Rectangle selection"); // log.debug
560 if ((rectx2 != -1) && (recty2 != -1))
562 rectSelect(rectx1, recty1, rectx2, recty2);
575 public void mouseClicked(MouseEvent evt)
585 public void mouseEntered(MouseEvent evt)
595 public void mouseExited(MouseEvent evt)
605 public void mouseReleased(MouseEvent evt)
615 public void mousePressed(MouseEvent evt)
635 SequenceI found = findPoint(x, y);
639 AlignmentPanel[] aps = getAssociatedPanels();
641 for (int a = 0; a < aps.length; a++)
643 if (aps[a].av.getSelectionGroup() != null)
645 aps[a].av.getSelectionGroup().addOrRemove(found, true);
649 aps[a].av.setSelectionGroup(new SequenceGroup());
650 aps[a].av.getSelectionGroup().addOrRemove(found, true);
651 aps[a].av.getSelectionGroup().setEndRes(
652 aps[a].av.getAlignment().getWidth() - 1);
655 PaintRefresher.Refresh(this, av.getSequenceSetId());
656 // canonical selection is sent to other listeners
663 // private void fireSequenceSelectionEvent(Selection sel) {
664 // controller.handleSequenceSelectionEvent(new
665 // SequenceSelectionEvent(this,sel));
667 public void mouseMoved(MouseEvent evt)
669 SequenceI found = findPoint(evt.getX(), evt.getY());
673 this.setToolTipText(found.getName());
677 this.setToolTipText(null);
687 public void mouseDragged(MouseEvent evt)
692 // Check if this is a rectangle drawing drag
693 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0)
695 // rectx2 = evt.getX();
696 // recty2 = evt.getY();
700 rotmat.setIdentity();
702 rotmat.rotate((float) (my - omy), 'x');
703 rotmat.rotate((float) (mx - omx), 'y');
705 for (int i = 0; i < npoint; i++)
707 SequencePoint sp = (SequencePoint) points.elementAt(i);
708 sp.coord[0] -= centre[0];
709 sp.coord[1] -= centre[1];
710 sp.coord[2] -= centre[2];
712 // Now apply the rotation matrix
713 sp.coord = rotmat.vectorMultiply(sp.coord);
715 // Now translate back again
716 sp.coord[0] += centre[0];
717 sp.coord[1] += centre[1];
718 sp.coord[2] += centre[2];
721 for (int i = 0; i < 3; i++)
723 axes[i] = rotmat.vectorMultiply(axes[i]);
729 paint(this.getGraphics());
745 public void rectSelect(int x1, int y1, int x2, int y2)
747 for (int i = 0; i < npoint; i++)
749 SequencePoint sp = (SequencePoint) points.elementAt(i);
750 int tmp1 = (int) (((sp.coord[0] - centre[0]) * scale) + ((float) getWidth() / 2.0));
751 int tmp2 = (int) (((sp.coord[1] - centre[1]) * scale) + ((float) getHeight() / 2.0));
753 if ((tmp1 > x1) && (tmp1 < x2) && (tmp2 > y1) && (tmp2 < y2))
757 if (!av.getSelectionGroup().getSequences(null)
758 .contains(sp.sequence))
760 av.getSelectionGroup().addSequence(sp.sequence, true);
767 // fireSequenceSelectionEvent(av.getSelection());
779 * @return DOCUMENT ME!
781 public SequenceI findPoint(int x, int y)
783 int halfwidth = getWidth() / 2;
784 int halfheight = getHeight() / 2;
788 for (int i = 0; i < npoint; i++)
790 SequencePoint sp = (SequencePoint) points.elementAt(i);
791 int px = (int) ((float) (sp.coord[0] - centre[0]) * scale)
793 int py = (int) ((float) (sp.coord[1] - centre[1]) * scale)
796 if ((Math.abs(px - x) < 3) && (Math.abs(py - y) < 3))
804 return ((SequencePoint) points.elementAt(found)).sequence;
812 AlignmentPanel[] getAssociatedPanels()
816 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
820 return new AlignmentPanel[]
827 * @return x,y,z positions of point s (index into points) under current
830 public double[] getPointPosition(int s)
832 double pts[] = new double[3];
833 float[] p = ((SequencePoint) points.elementAt(s)).coord;