2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
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
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.
25 import java.awt.event.*;
29 import jalview.api.RotatableCanvasI;
30 import jalview.datamodel.*;
31 import jalview.math.*;
32 import jalview.util.MessageManager;
40 public class RotatableCanvas extends JPanel implements MouseListener,
41 MouseMotionListener, KeyListener, RotatableCanvasI
43 RotatableMatrix idmat = new RotatableMatrix(3, 3);
45 RotatableMatrix objmat = new RotatableMatrix(3, 3);
47 RotatableMatrix rotmat = new RotatableMatrix(3, 3);
49 // RubberbandRectangle rubberband;
50 boolean drawAxes = true;
66 float[] centre = new float[3];
68 float[] width = new float[3];
70 float[] max = new float[3];
72 float[] min = new float[3];
102 float scalefactor = 1;
108 boolean showLabels = false;
110 Color bgColour = Color.black;
112 boolean applyToAllViews = false;
114 // Controller controller;
115 public RotatableCanvas(AlignmentPanel ap)
120 addMouseWheelListener(new MouseWheelListener()
122 public void mouseWheelMoved(MouseWheelEvent e)
124 if (e.getWheelRotation() > 0)
126 scale = (float) (scale * 1.1);
132 scale = (float) (scale * 0.9);
140 public void showLabels(boolean b)
146 boolean first = true;
148 public void setPoints(Vector points, int npoint)
150 this.points = points;
151 this.npoint = npoint;
154 ToolTipManager.sharedInstance().registerComponent(this);
155 ToolTipManager.sharedInstance().setInitialDelay(0);
156 ToolTipManager.sharedInstance().setDismissDelay(10000);
158 prefsize = getPreferredSize();
159 orig = new float[npoint][3];
161 for (int i = 0; i < npoint; i++)
163 SequencePoint sp = (SequencePoint) points.elementAt(i);
165 for (int j = 0; j < 3; j++)
167 orig[i][j] = sp.coord[j];
171 // Initialize the matrices to identity
172 for (int i = 0; i < 3; i++)
174 for (int j = 0; j < 3; j++)
178 idmat.addElement(i, j, 0);
179 objmat.addElement(i, j, 0);
180 rotmat.addElement(i, j, 0);
184 idmat.addElement(i, j, 0);
185 objmat.addElement(i, j, 0);
186 rotmat.addElement(i, j, 0);
191 axes = new float[3][3];
201 addMouseListener(this);
203 addMouseMotionListener(this);
208 public void initAxes()
210 for (int i = 0; i < 3; i++)
212 for (int j = 0; j < 3; j++)
229 public void findWidth()
234 max[0] = (float) -1e30;
235 max[1] = (float) -1e30;
236 max[2] = (float) -1e30;
238 min[0] = (float) 1e30;
239 min[1] = (float) 1e30;
240 min[2] = (float) 1e30;
242 for (int i = 0; i < 3; i++)
244 for (int j = 0; j < npoint; j++)
246 SequencePoint sp = (SequencePoint) points.elementAt(j);
248 if (sp.coord[i] >= max[i])
250 max[i] = sp.coord[i];
253 if (sp.coord[i] <= min[i])
255 min[i] = sp.coord[i];
260 // System.out.println("xmax " + max[0] + " min " + min[0]);
261 // System.out.println("ymax " + max[1] + " min " + min[1]);
262 // System.out.println("zmax " + max[2] + " min " + min[2]);
263 width[0] = Math.abs(max[0] - min[0]);
264 width[1] = Math.abs(max[1] - min[1]);
265 width[2] = Math.abs(max[2] - min[2]);
269 if (width[1] > width[0])
274 if (width[2] > width[1])
279 // System.out.println("Maxwidth = " + maxwidth);
285 * @return DOCUMENT ME!
287 public float findScale()
296 height = getHeight();
300 width = prefsize.width;
301 height = prefsize.height;
313 return (float) ((dim * scalefactor) / (2 * maxwidth));
319 public void findCentre()
321 // Find centre coordinate
324 centre[0] = (max[0] + min[0]) / 2;
325 centre[1] = (max[1] + min[1]) / 2;
326 centre[2] = (max[2] + min[2]) / 2;
328 // System.out.println("Centre x " + centre[0]);
329 // System.out.println("Centre y " + centre[1]);
330 // System.out.println("Centre z " + centre[2]);
336 * @return DOCUMENT ME!
338 public Dimension getPreferredSize()
340 if (prefsize != null)
346 return new Dimension(400, 400);
353 * @return DOCUMENT ME!
355 public Dimension getMinimumSize()
357 return getPreferredSize();
366 public void paintComponent(Graphics g1)
369 Graphics2D g = (Graphics2D) g1;
371 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
372 RenderingHints.VALUE_ANTIALIAS_ON);
375 g.setFont(new Font("Verdana", Font.PLAIN, 18));
376 g.drawString(MessageManager.getString("label.calculating_pca")+ "....", 20, getHeight() / 2);
380 // Only create the image at the beginning -
381 if ((img == null) || (prefsize.width != getWidth())
382 || (prefsize.height != getHeight()))
384 prefsize.width = getWidth();
385 prefsize.height = getHeight();
389 // System.out.println("New scale = " + scale);
390 img = createImage(getWidth(), getHeight());
391 ig = img.getGraphics();
394 drawBackground(ig, bgColour);
397 if (drawAxes == true)
402 g.drawImage(img, 0, 0, this);
412 public void drawAxes(Graphics g)
415 g.setColor(Color.yellow);
417 for (int i = 0; i < 3; i++)
419 g.drawLine(getWidth() / 2, getHeight() / 2, (int) ((axes[i][0]
420 * scale * max[0]) + (getWidth() / 2)), (int) ((axes[i][1]
421 * scale * max[1]) + (getHeight() / 2)));
433 public void drawBackground(Graphics g, Color col)
436 g.fillRect(0, 0, prefsize.width, prefsize.height);
445 public void drawScene(Graphics g1)
448 Graphics2D g = (Graphics2D) g1;
450 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
451 RenderingHints.VALUE_ANTIALIAS_ON);
453 int halfwidth = getWidth() / 2;
454 int halfheight = getHeight() / 2;
456 for (int i = 0; i < npoint; i++)
458 SequencePoint sp = (SequencePoint) points.elementAt(i);
459 int x = (int) ((float) (sp.coord[0] - centre[0]) * scale) + halfwidth;
460 int y = (int) ((float) (sp.coord[1] - centre[1]) * scale)
462 float z = sp.coord[1] - centre[2];
464 if (av.getSequenceColour(sp.sequence) == Color.black)
466 g.setColor(Color.white);
470 g.setColor(av.getSequenceColour(sp.sequence));
473 if (av.getSelectionGroup() != null)
475 if (av.getSelectionGroup().getSequences(null)
476 .contains(((SequencePoint) points.elementAt(i)).sequence))
478 g.setColor(Color.gray);
484 g.setColor(g.getColor().darker());
487 g.fillRect(x - 3, y - 3, 6, 6);
490 g.setColor(Color.red);
492 ((SequencePoint) points.elementAt(i)).sequence.getName(),
497 // //Now the rectangle
498 // if (rectx2 != -1 && recty2 != -1) {
499 // g.setColor(Color.white);
501 // g.drawRect(rectx1,recty1,rectx2-rectx1,recty2-recty1);
508 * @return DOCUMENT ME!
510 public Dimension minimumsize()
518 * @return DOCUMENT ME!
520 public Dimension preferredsize()
531 public void keyTyped(KeyEvent evt)
541 public void keyReleased(KeyEvent evt)
551 public void keyPressed(KeyEvent evt)
553 if (evt.getKeyCode() == KeyEvent.VK_UP)
555 scalefactor = (float) (scalefactor * 1.1);
558 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
560 scalefactor = (float) (scalefactor * 0.9);
563 else if (evt.getKeyChar() == 's')
565 System.err.println("DEBUG: Rectangle selection"); // log.debug
567 if ((rectx2 != -1) && (recty2 != -1))
569 rectSelect(rectx1, recty1, rectx2, recty2);
582 public void mouseClicked(MouseEvent evt)
592 public void mouseEntered(MouseEvent evt)
602 public void mouseExited(MouseEvent evt)
612 public void mouseReleased(MouseEvent evt)
622 public void mousePressed(MouseEvent evt)
642 SequenceI found = findPoint(x, y);
646 AlignmentPanel[] aps = getAssociatedPanels();
648 for (int a = 0; a < aps.length; a++)
650 if (aps[a].av.getSelectionGroup() != null)
652 aps[a].av.getSelectionGroup().addOrRemove(found, true);
656 aps[a].av.setSelectionGroup(new SequenceGroup());
657 aps[a].av.getSelectionGroup().addOrRemove(found, true);
658 aps[a].av.getSelectionGroup().setEndRes(
659 aps[a].av.getAlignment().getWidth() - 1);
662 PaintRefresher.Refresh(this, av.getSequenceSetId());
663 // canonical selection is sent to other listeners
670 // private void fireSequenceSelectionEvent(Selection sel) {
671 // controller.handleSequenceSelectionEvent(new
672 // SequenceSelectionEvent(this,sel));
674 public void mouseMoved(MouseEvent evt)
676 SequenceI found = findPoint(evt.getX(), evt.getY());
680 this.setToolTipText(found.getName());
684 this.setToolTipText(null);
694 public void mouseDragged(MouseEvent evt)
699 // Check if this is a rectangle drawing drag
700 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0)
702 // rectx2 = evt.getX();
703 // recty2 = evt.getY();
707 rotmat.setIdentity();
709 rotmat.rotate((float) (my - omy), 'x');
710 rotmat.rotate((float) (mx - omx), 'y');
712 for (int i = 0; i < npoint; i++)
714 SequencePoint sp = (SequencePoint) points.elementAt(i);
715 sp.coord[0] -= centre[0];
716 sp.coord[1] -= centre[1];
717 sp.coord[2] -= centre[2];
719 // Now apply the rotation matrix
720 sp.coord = rotmat.vectorMultiply(sp.coord);
722 // Now translate back again
723 sp.coord[0] += centre[0];
724 sp.coord[1] += centre[1];
725 sp.coord[2] += centre[2];
728 for (int i = 0; i < 3; i++)
730 axes[i] = rotmat.vectorMultiply(axes[i]);
736 paint(this.getGraphics());
752 public void rectSelect(int x1, int y1, int x2, int y2)
754 for (int i = 0; i < npoint; i++)
756 SequencePoint sp = (SequencePoint) points.elementAt(i);
757 int tmp1 = (int) (((sp.coord[0] - centre[0]) * scale) + ((float) getWidth() / 2.0));
758 int tmp2 = (int) (((sp.coord[1] - centre[1]) * scale) + ((float) getHeight() / 2.0));
760 if ((tmp1 > x1) && (tmp1 < x2) && (tmp2 > y1) && (tmp2 < y2))
764 if (!av.getSelectionGroup().getSequences(null)
765 .contains(sp.sequence))
767 av.getSelectionGroup().addSequence(sp.sequence, true);
774 // fireSequenceSelectionEvent(av.getSelection());
786 * @return DOCUMENT ME!
788 public SequenceI findPoint(int x, int y)
790 int halfwidth = getWidth() / 2;
791 int halfheight = getHeight() / 2;
795 for (int i = 0; i < npoint; i++)
797 SequencePoint sp = (SequencePoint) points.elementAt(i);
798 int px = (int) ((float) (sp.coord[0] - centre[0]) * scale)
800 int py = (int) ((float) (sp.coord[1] - centre[1]) * scale)
803 if ((Math.abs(px - x) < 3) && (Math.abs(py - y) < 3))
811 return ((SequencePoint) points.elementAt(found)).sequence;
819 AlignmentPanel[] getAssociatedPanels()
823 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
827 return new AlignmentPanel[]
834 * @return x,y,z positions of point s (index into points) under current
837 public double[] getPointPosition(int s)
839 double pts[] = new double[3];
840 float[] p = ((SequencePoint) points.elementAt(s)).coord;