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")
377 + "....", 20, getHeight() / 2);
381 // Only create the image at the beginning -
382 if ((img == null) || (prefsize.width != getWidth())
383 || (prefsize.height != getHeight()))
385 prefsize.width = getWidth();
386 prefsize.height = getHeight();
390 // System.out.println("New scale = " + scale);
391 img = createImage(getWidth(), getHeight());
392 ig = img.getGraphics();
395 drawBackground(ig, bgColour);
398 if (drawAxes == true)
403 g.drawImage(img, 0, 0, this);
413 public void drawAxes(Graphics g)
416 g.setColor(Color.yellow);
418 for (int i = 0; i < 3; i++)
420 g.drawLine(getWidth() / 2, getHeight() / 2, (int) ((axes[i][0]
421 * scale * max[0]) + (getWidth() / 2)), (int) ((axes[i][1]
422 * scale * max[1]) + (getHeight() / 2)));
434 public void drawBackground(Graphics g, Color col)
437 g.fillRect(0, 0, prefsize.width, prefsize.height);
446 public void drawScene(Graphics g1)
449 Graphics2D g = (Graphics2D) g1;
451 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
452 RenderingHints.VALUE_ANTIALIAS_ON);
454 int halfwidth = getWidth() / 2;
455 int halfheight = getHeight() / 2;
457 for (int i = 0; i < npoint; i++)
459 SequencePoint sp = (SequencePoint) points.elementAt(i);
460 int x = (int) ((float) (sp.coord[0] - centre[0]) * scale) + halfwidth;
461 int y = (int) ((float) (sp.coord[1] - centre[1]) * scale)
463 float z = sp.coord[1] - centre[2];
465 if (av.getSequenceColour(sp.sequence) == Color.black)
467 g.setColor(Color.white);
471 g.setColor(av.getSequenceColour(sp.sequence));
474 if (av.getSelectionGroup() != null)
476 if (av.getSelectionGroup().getSequences(null)
477 .contains(((SequencePoint) points.elementAt(i)).sequence))
479 g.setColor(Color.gray);
485 g.setColor(g.getColor().darker());
488 g.fillRect(x - 3, y - 3, 6, 6);
491 g.setColor(Color.red);
493 ((SequencePoint) points.elementAt(i)).sequence.getName(),
498 // //Now the rectangle
499 // if (rectx2 != -1 && recty2 != -1) {
500 // g.setColor(Color.white);
502 // g.drawRect(rectx1,recty1,rectx2-rectx1,recty2-recty1);
509 * @return DOCUMENT ME!
511 public Dimension minimumsize()
519 * @return DOCUMENT ME!
521 public Dimension preferredsize()
532 public void keyTyped(KeyEvent evt)
542 public void keyReleased(KeyEvent evt)
552 public void keyPressed(KeyEvent evt)
554 if (evt.getKeyCode() == KeyEvent.VK_UP)
556 scalefactor = (float) (scalefactor * 1.1);
559 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
561 scalefactor = (float) (scalefactor * 0.9);
564 else if (evt.getKeyChar() == 's')
566 System.err.println("DEBUG: Rectangle selection"); // log.debug
568 if ((rectx2 != -1) && (recty2 != -1))
570 rectSelect(rectx1, recty1, rectx2, recty2);
583 public void mouseClicked(MouseEvent evt)
593 public void mouseEntered(MouseEvent evt)
603 public void mouseExited(MouseEvent evt)
613 public void mouseReleased(MouseEvent evt)
623 public void mousePressed(MouseEvent evt)
643 SequenceI found = findPoint(x, y);
647 AlignmentPanel[] aps = getAssociatedPanels();
649 for (int a = 0; a < aps.length; a++)
651 if (aps[a].av.getSelectionGroup() != null)
653 aps[a].av.getSelectionGroup().addOrRemove(found, true);
657 aps[a].av.setSelectionGroup(new SequenceGroup());
658 aps[a].av.getSelectionGroup().addOrRemove(found, true);
659 aps[a].av.getSelectionGroup().setEndRes(
660 aps[a].av.getAlignment().getWidth() - 1);
663 PaintRefresher.Refresh(this, av.getSequenceSetId());
664 // canonical selection is sent to other listeners
671 // private void fireSequenceSelectionEvent(Selection sel) {
672 // controller.handleSequenceSelectionEvent(new
673 // SequenceSelectionEvent(this,sel));
675 public void mouseMoved(MouseEvent evt)
677 SequenceI found = findPoint(evt.getX(), evt.getY());
681 this.setToolTipText(found.getName());
685 this.setToolTipText(null);
695 public void mouseDragged(MouseEvent evt)
700 // Check if this is a rectangle drawing drag
701 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0)
703 // rectx2 = evt.getX();
704 // recty2 = evt.getY();
708 rotmat.setIdentity();
710 rotmat.rotate((float) (my - omy), 'x');
711 rotmat.rotate((float) (mx - omx), 'y');
713 for (int i = 0; i < npoint; i++)
715 SequencePoint sp = (SequencePoint) points.elementAt(i);
716 sp.coord[0] -= centre[0];
717 sp.coord[1] -= centre[1];
718 sp.coord[2] -= centre[2];
720 // Now apply the rotation matrix
721 sp.coord = rotmat.vectorMultiply(sp.coord);
723 // Now translate back again
724 sp.coord[0] += centre[0];
725 sp.coord[1] += centre[1];
726 sp.coord[2] += centre[2];
729 for (int i = 0; i < 3; i++)
731 axes[i] = rotmat.vectorMultiply(axes[i]);
737 paint(this.getGraphics());
753 public void rectSelect(int x1, int y1, int x2, int y2)
755 for (int i = 0; i < npoint; i++)
757 SequencePoint sp = (SequencePoint) points.elementAt(i);
758 int tmp1 = (int) (((sp.coord[0] - centre[0]) * scale) + ((float) getWidth() / 2.0));
759 int tmp2 = (int) (((sp.coord[1] - centre[1]) * scale) + ((float) getHeight() / 2.0));
761 if ((tmp1 > x1) && (tmp1 < x2) && (tmp2 > y1) && (tmp2 < y2))
765 if (!av.getSelectionGroup().getSequences(null)
766 .contains(sp.sequence))
768 av.getSelectionGroup().addSequence(sp.sequence, true);
775 // fireSequenceSelectionEvent(av.getSelection());
787 * @return DOCUMENT ME!
789 public SequenceI findPoint(int x, int y)
791 int halfwidth = getWidth() / 2;
792 int halfheight = getHeight() / 2;
796 for (int i = 0; i < npoint; i++)
798 SequencePoint sp = (SequencePoint) points.elementAt(i);
799 int px = (int) ((float) (sp.coord[0] - centre[0]) * scale)
801 int py = (int) ((float) (sp.coord[1] - centre[1]) * scale)
804 if ((Math.abs(px - x) < 3) && (Math.abs(py - y) < 3))
812 return ((SequencePoint) points.elementAt(found)).sequence;
820 AlignmentPanel[] getAssociatedPanels()
824 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
828 return new AlignmentPanel[]
835 * @return x,y,z positions of point s (index into points) under current
838 public double[] getPointPosition(int s)
840 double pts[] = new double[3];
841 float[] p = ((SequencePoint) points.elementAt(s)).coord;