2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import java.awt.event.*;
27 import jalview.api.RotatableCanvasI;
28 import jalview.datamodel.*;
29 import jalview.math.*;
30 import jalview.util.MessageManager;
38 public class RotatableCanvas extends JPanel implements MouseListener,
39 MouseMotionListener, KeyListener, RotatableCanvasI
41 RotatableMatrix idmat = new RotatableMatrix(3, 3);
43 RotatableMatrix objmat = new RotatableMatrix(3, 3);
45 RotatableMatrix rotmat = new RotatableMatrix(3, 3);
47 // RubberbandRectangle rubberband;
48 boolean drawAxes = true;
64 float[] centre = new float[3];
66 float[] width = new float[3];
68 float[] max = new float[3];
70 float[] min = new float[3];
100 float scalefactor = 1;
106 boolean showLabels = false;
108 Color bgColour = Color.black;
110 boolean applyToAllViews = false;
112 // Controller controller;
113 public RotatableCanvas(AlignmentPanel ap)
118 addMouseWheelListener(new MouseWheelListener()
120 public void mouseWheelMoved(MouseWheelEvent e)
122 if (e.getWheelRotation() > 0)
124 scale = (float) (scale * 1.1);
130 scale = (float) (scale * 0.9);
138 public void showLabels(boolean b)
144 boolean first = true;
146 public void setPoints(Vector points, int npoint)
148 this.points = points;
149 this.npoint = npoint;
152 ToolTipManager.sharedInstance().registerComponent(this);
153 ToolTipManager.sharedInstance().setInitialDelay(0);
154 ToolTipManager.sharedInstance().setDismissDelay(10000);
156 prefsize = getPreferredSize();
157 orig = new float[npoint][3];
159 for (int i = 0; i < npoint; i++)
161 SequencePoint sp = (SequencePoint) points.elementAt(i);
163 for (int j = 0; j < 3; j++)
165 orig[i][j] = sp.coord[j];
169 // Initialize the matrices to identity
170 for (int i = 0; i < 3; i++)
172 for (int j = 0; j < 3; j++)
176 idmat.addElement(i, j, 0);
177 objmat.addElement(i, j, 0);
178 rotmat.addElement(i, j, 0);
182 idmat.addElement(i, j, 0);
183 objmat.addElement(i, j, 0);
184 rotmat.addElement(i, j, 0);
189 axes = new float[3][3];
199 addMouseListener(this);
201 addMouseMotionListener(this);
206 public void initAxes()
208 for (int i = 0; i < 3; i++)
210 for (int j = 0; j < 3; j++)
227 public void findWidth()
232 max[0] = (float) -1e30;
233 max[1] = (float) -1e30;
234 max[2] = (float) -1e30;
236 min[0] = (float) 1e30;
237 min[1] = (float) 1e30;
238 min[2] = (float) 1e30;
240 for (int i = 0; i < 3; i++)
242 for (int j = 0; j < npoint; j++)
244 SequencePoint sp = (SequencePoint) points.elementAt(j);
246 if (sp.coord[i] >= max[i])
248 max[i] = sp.coord[i];
251 if (sp.coord[i] <= min[i])
253 min[i] = sp.coord[i];
258 // System.out.println("xmax " + max[0] + " min " + min[0]);
259 // System.out.println("ymax " + max[1] + " min " + min[1]);
260 // System.out.println("zmax " + max[2] + " min " + min[2]);
261 width[0] = Math.abs(max[0] - min[0]);
262 width[1] = Math.abs(max[1] - min[1]);
263 width[2] = Math.abs(max[2] - min[2]);
267 if (width[1] > width[0])
272 if (width[2] > width[1])
277 // System.out.println("Maxwidth = " + maxwidth);
283 * @return DOCUMENT ME!
285 public float findScale()
294 height = getHeight();
298 width = prefsize.width;
299 height = prefsize.height;
311 return (float) ((dim * scalefactor) / (2 * maxwidth));
317 public void findCentre()
319 // Find centre coordinate
322 centre[0] = (max[0] + min[0]) / 2;
323 centre[1] = (max[1] + min[1]) / 2;
324 centre[2] = (max[2] + min[2]) / 2;
326 // System.out.println("Centre x " + centre[0]);
327 // System.out.println("Centre y " + centre[1]);
328 // System.out.println("Centre z " + centre[2]);
334 * @return DOCUMENT ME!
336 public Dimension getPreferredSize()
338 if (prefsize != null)
344 return new Dimension(400, 400);
351 * @return DOCUMENT ME!
353 public Dimension getMinimumSize()
355 return getPreferredSize();
364 public void paintComponent(Graphics g1)
367 Graphics2D g = (Graphics2D) g1;
369 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
370 RenderingHints.VALUE_ANTIALIAS_ON);
373 g.setFont(new Font("Verdana", Font.PLAIN, 18));
374 g.drawString(MessageManager.getString("label.calculating_pca")+ "....", 20, getHeight() / 2);
378 // Only create the image at the beginning -
379 if ((img == null) || (prefsize.width != getWidth())
380 || (prefsize.height != getHeight()))
382 prefsize.width = getWidth();
383 prefsize.height = getHeight();
387 // System.out.println("New scale = " + scale);
388 img = createImage(getWidth(), getHeight());
389 ig = img.getGraphics();
392 drawBackground(ig, bgColour);
395 if (drawAxes == true)
400 g.drawImage(img, 0, 0, this);
410 public void drawAxes(Graphics g)
413 g.setColor(Color.yellow);
415 for (int i = 0; i < 3; i++)
417 g.drawLine(getWidth() / 2, getHeight() / 2, (int) ((axes[i][0]
418 * scale * max[0]) + (getWidth() / 2)), (int) ((axes[i][1]
419 * scale * max[1]) + (getHeight() / 2)));
431 public void drawBackground(Graphics g, Color col)
434 g.fillRect(0, 0, prefsize.width, prefsize.height);
443 public void drawScene(Graphics g1)
446 Graphics2D g = (Graphics2D) g1;
448 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
449 RenderingHints.VALUE_ANTIALIAS_ON);
451 int halfwidth = getWidth() / 2;
452 int halfheight = getHeight() / 2;
454 for (int i = 0; i < npoint; i++)
456 SequencePoint sp = (SequencePoint) points.elementAt(i);
457 int x = (int) ((float) (sp.coord[0] - centre[0]) * scale) + halfwidth;
458 int y = (int) ((float) (sp.coord[1] - centre[1]) * scale)
460 float z = sp.coord[1] - centre[2];
462 if (av.getSequenceColour(sp.sequence) == Color.black)
464 g.setColor(Color.white);
468 g.setColor(av.getSequenceColour(sp.sequence));
471 if (av.getSelectionGroup() != null)
473 if (av.getSelectionGroup().getSequences(null)
474 .contains(((SequencePoint) points.elementAt(i)).sequence))
476 g.setColor(Color.gray);
482 g.setColor(g.getColor().darker());
485 g.fillRect(x - 3, y - 3, 6, 6);
488 g.setColor(Color.red);
490 ((SequencePoint) points.elementAt(i)).sequence.getName(),
495 // //Now the rectangle
496 // if (rectx2 != -1 && recty2 != -1) {
497 // g.setColor(Color.white);
499 // g.drawRect(rectx1,recty1,rectx2-rectx1,recty2-recty1);
506 * @return DOCUMENT ME!
508 public Dimension minimumsize()
516 * @return DOCUMENT ME!
518 public Dimension preferredsize()
529 public void keyTyped(KeyEvent evt)
539 public void keyReleased(KeyEvent evt)
549 public void keyPressed(KeyEvent evt)
551 if (evt.getKeyCode() == KeyEvent.VK_UP)
553 scalefactor = (float) (scalefactor * 1.1);
556 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
558 scalefactor = (float) (scalefactor * 0.9);
561 else if (evt.getKeyChar() == 's')
563 System.err.println("DEBUG: Rectangle selection"); // log.debug
565 if ((rectx2 != -1) && (recty2 != -1))
567 rectSelect(rectx1, recty1, rectx2, recty2);
580 public void mouseClicked(MouseEvent evt)
590 public void mouseEntered(MouseEvent evt)
600 public void mouseExited(MouseEvent evt)
610 public void mouseReleased(MouseEvent evt)
620 public void mousePressed(MouseEvent evt)
640 SequenceI found = findPoint(x, y);
644 AlignmentPanel[] aps = getAssociatedPanels();
646 for (int a = 0; a < aps.length; a++)
648 if (aps[a].av.getSelectionGroup() != null)
650 aps[a].av.getSelectionGroup().addOrRemove(found, true);
654 aps[a].av.setSelectionGroup(new SequenceGroup());
655 aps[a].av.getSelectionGroup().addOrRemove(found, true);
656 aps[a].av.getSelectionGroup().setEndRes(
657 aps[a].av.getAlignment().getWidth() - 1);
660 PaintRefresher.Refresh(this, av.getSequenceSetId());
661 // canonical selection is sent to other listeners
668 // private void fireSequenceSelectionEvent(Selection sel) {
669 // controller.handleSequenceSelectionEvent(new
670 // SequenceSelectionEvent(this,sel));
672 public void mouseMoved(MouseEvent evt)
674 SequenceI found = findPoint(evt.getX(), evt.getY());
678 this.setToolTipText(found.getName());
682 this.setToolTipText(null);
692 public void mouseDragged(MouseEvent evt)
697 // Check if this is a rectangle drawing drag
698 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0)
700 // rectx2 = evt.getX();
701 // recty2 = evt.getY();
705 rotmat.setIdentity();
707 rotmat.rotate((float) (my - omy), 'x');
708 rotmat.rotate((float) (mx - omx), 'y');
710 for (int i = 0; i < npoint; i++)
712 SequencePoint sp = (SequencePoint) points.elementAt(i);
713 sp.coord[0] -= centre[0];
714 sp.coord[1] -= centre[1];
715 sp.coord[2] -= centre[2];
717 // Now apply the rotation matrix
718 sp.coord = rotmat.vectorMultiply(sp.coord);
720 // Now translate back again
721 sp.coord[0] += centre[0];
722 sp.coord[1] += centre[1];
723 sp.coord[2] += centre[2];
726 for (int i = 0; i < 3; i++)
728 axes[i] = rotmat.vectorMultiply(axes[i]);
734 paint(this.getGraphics());
750 public void rectSelect(int x1, int y1, int x2, int y2)
752 for (int i = 0; i < npoint; i++)
754 SequencePoint sp = (SequencePoint) points.elementAt(i);
755 int tmp1 = (int) (((sp.coord[0] - centre[0]) * scale) + ((float) getWidth() / 2.0));
756 int tmp2 = (int) (((sp.coord[1] - centre[1]) * scale) + ((float) getHeight() / 2.0));
758 if ((tmp1 > x1) && (tmp1 < x2) && (tmp2 > y1) && (tmp2 < y2))
762 if (!av.getSelectionGroup().getSequences(null)
763 .contains(sp.sequence))
765 av.getSelectionGroup().addSequence(sp.sequence, true);
772 // fireSequenceSelectionEvent(av.getSelection());
784 * @return DOCUMENT ME!
786 public SequenceI findPoint(int x, int y)
788 int halfwidth = getWidth() / 2;
789 int halfheight = getHeight() / 2;
793 for (int i = 0; i < npoint; i++)
795 SequencePoint sp = (SequencePoint) points.elementAt(i);
796 int px = (int) ((float) (sp.coord[0] - centre[0]) * scale)
798 int py = (int) ((float) (sp.coord[1] - centre[1]) * scale)
801 if ((Math.abs(px - x) < 3) && (Math.abs(py - y) < 3))
809 return ((SequencePoint) points.elementAt(found)).sequence;
817 AlignmentPanel[] getAssociatedPanels()
821 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
825 return new AlignmentPanel[]
832 * @return x,y,z positions of point s (index into points) under current
835 public double[] getPointPosition(int s)
837 double pts[] = new double[3];
838 float[] p = ((SequencePoint) points.elementAt(s)).coord;