2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.
23 import jalview.api.RotatableCanvasI;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.datamodel.SequencePoint;
27 import jalview.math.RotatableMatrix;
28 import jalview.util.MessageManager;
29 import jalview.viewmodel.AlignmentViewport;
31 import java.awt.Color;
32 import java.awt.Dimension;
34 import java.awt.Graphics;
35 import java.awt.Graphics2D;
36 import java.awt.Image;
37 import java.awt.RenderingHints;
38 import java.awt.event.InputEvent;
39 import java.awt.event.KeyEvent;
40 import java.awt.event.KeyListener;
41 import java.awt.event.MouseEvent;
42 import java.awt.event.MouseListener;
43 import java.awt.event.MouseMotionListener;
44 import java.awt.event.MouseWheelEvent;
45 import java.awt.event.MouseWheelListener;
46 import java.util.Vector;
48 import javax.swing.JPanel;
49 import javax.swing.ToolTipManager;
57 public class RotatableCanvas extends JPanel implements MouseListener,
58 MouseMotionListener, KeyListener, RotatableCanvasI
60 RotatableMatrix idmat = new RotatableMatrix(3, 3);
62 RotatableMatrix objmat = new RotatableMatrix(3, 3);
64 RotatableMatrix rotmat = new RotatableMatrix(3, 3);
66 // RubberbandRectangle rubberband;
67 boolean drawAxes = true;
83 float[] centre = new float[3];
85 float[] width = new float[3];
87 float[] max = new float[3];
89 float[] min = new float[3];
119 float scalefactor = 1;
121 AlignmentViewport av;
125 boolean showLabels = false;
127 Color bgColour = Color.black;
129 boolean applyToAllViews = false;
131 public RotatableCanvas(AlignmentPanel ap)
136 addMouseWheelListener(new MouseWheelListener()
139 public void mouseWheelMoved(MouseWheelEvent e)
141 double wheelRotation = e.getPreciseWheelRotation();
142 if (wheelRotation > 0)
147 scale = (float) (scale * 1.1);
150 else if (wheelRotation < 0)
155 scale = (float) (scale * 0.9);
163 public void showLabels(boolean b)
169 boolean first = true;
172 public void setPoints(Vector points, int npoint)
174 this.points = points;
175 this.npoint = npoint;
178 ToolTipManager.sharedInstance().registerComponent(this);
179 ToolTipManager.sharedInstance().setInitialDelay(0);
180 ToolTipManager.sharedInstance().setDismissDelay(10000);
182 prefsize = getPreferredSize();
183 orig = new float[npoint][3];
185 for (int i = 0; i < npoint; i++)
187 SequencePoint sp = (SequencePoint) points.elementAt(i);
189 for (int j = 0; j < 3; j++)
191 orig[i][j] = sp.coord[j];
195 // Initialize the matrices to identity
196 for (int i = 0; i < 3; i++)
198 for (int j = 0; j < 3; j++)
202 idmat.addElement(i, j, 0);
203 objmat.addElement(i, j, 0);
204 rotmat.addElement(i, j, 0);
208 idmat.addElement(i, j, 0);
209 objmat.addElement(i, j, 0);
210 rotmat.addElement(i, j, 0);
215 axes = new float[3][3];
225 addMouseListener(this);
227 addMouseMotionListener(this);
232 public void initAxes()
234 for (int i = 0; i < 3; i++)
236 for (int j = 0; j < 3; j++)
253 public void findWidth()
258 max[0] = (float) -1e30;
259 max[1] = (float) -1e30;
260 max[2] = (float) -1e30;
262 min[0] = (float) 1e30;
263 min[1] = (float) 1e30;
264 min[2] = (float) 1e30;
266 for (int i = 0; i < 3; i++)
268 for (int j = 0; j < npoint; j++)
270 SequencePoint sp = (SequencePoint) points.elementAt(j);
272 if (sp.coord[i] >= max[i])
274 max[i] = sp.coord[i];
277 if (sp.coord[i] <= min[i])
279 min[i] = sp.coord[i];
284 // System.out.println("xmax " + max[0] + " min " + min[0]);
285 // System.out.println("ymax " + max[1] + " min " + min[1]);
286 // System.out.println("zmax " + max[2] + " min " + min[2]);
287 width[0] = Math.abs(max[0] - min[0]);
288 width[1] = Math.abs(max[1] - min[1]);
289 width[2] = Math.abs(max[2] - min[2]);
293 if (width[1] > width[0])
298 if (width[2] > width[1])
303 // System.out.println("Maxwidth = " + maxwidth);
309 * @return DOCUMENT ME!
311 public float findScale()
320 height = getHeight();
324 width = prefsize.width;
325 height = prefsize.height;
337 return (dim * scalefactor) / (2 * maxwidth);
343 public void findCentre()
345 // Find centre coordinate
348 centre[0] = (max[0] + min[0]) / 2;
349 centre[1] = (max[1] + min[1]) / 2;
350 centre[2] = (max[2] + min[2]) / 2;
352 // System.out.println("Centre x " + centre[0]);
353 // System.out.println("Centre y " + centre[1]);
354 // System.out.println("Centre z " + centre[2]);
360 * @return DOCUMENT ME!
363 public Dimension getPreferredSize()
365 if (prefsize != null)
371 return new Dimension(400, 400);
378 * @return DOCUMENT ME!
381 public Dimension getMinimumSize()
383 return getPreferredSize();
393 public void paintComponent(Graphics g1)
396 Graphics2D g = (Graphics2D) g1;
398 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
399 RenderingHints.VALUE_ANTIALIAS_ON);
402 g.setFont(new Font("Verdana", Font.PLAIN, 18));
404 MessageManager.getString("label.calculating_pca") + "....",
405 20, getHeight() / 2);
409 // Only create the image at the beginning -
410 if ((img == null) || (prefsize.width != getWidth())
411 || (prefsize.height != getHeight()))
413 prefsize.width = getWidth();
414 prefsize.height = getHeight();
418 // System.out.println("New scale = " + scale);
419 img = createImage(getWidth(), getHeight());
420 ig = img.getGraphics();
423 drawBackground(ig, bgColour);
426 if (drawAxes == true)
431 g.drawImage(img, 0, 0, this);
441 public void drawAxes(Graphics g)
444 g.setColor(Color.yellow);
446 for (int i = 0; i < 3; i++)
448 g.drawLine(getWidth() / 2, getHeight() / 2,
449 (int) ((axes[i][0] * scale * max[0]) + (getWidth() / 2)),
450 (int) ((axes[i][1] * scale * max[1]) + (getHeight() / 2)));
462 public void drawBackground(Graphics g, Color col)
465 g.fillRect(0, 0, prefsize.width, prefsize.height);
474 public void drawScene(Graphics g1)
477 Graphics2D g = (Graphics2D) g1;
479 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
480 RenderingHints.VALUE_ANTIALIAS_ON);
482 int halfwidth = getWidth() / 2;
483 int halfheight = getHeight() / 2;
485 for (int i = 0; i < npoint; i++)
487 SequencePoint sp = (SequencePoint) points.elementAt(i);
488 int x = (int) ((sp.coord[0] - centre[0]) * scale) + halfwidth;
489 int y = (int) ((sp.coord[1] - centre[1]) * scale)
491 float z = sp.coord[1] - centre[2];
493 if (av.getSequenceColour(sp.sequence) == Color.black)
495 g.setColor(Color.white);
499 g.setColor(av.getSequenceColour(sp.sequence));
502 if (av.getSelectionGroup() != null)
504 if (av.getSelectionGroup().getSequences(null)
505 .contains(((SequencePoint) points.elementAt(i)).sequence))
507 g.setColor(Color.gray);
513 g.setColor(g.getColor().darker());
516 g.fillRect(x - 3, y - 3, 6, 6);
519 g.setColor(Color.red);
521 ((SequencePoint) points.elementAt(i)).sequence.getName(),
526 // //Now the rectangle
527 // if (rectx2 != -1 && recty2 != -1) {
528 // g.setColor(Color.white);
530 // g.drawRect(rectx1,recty1,rectx2-rectx1,recty2-recty1);
537 * @return DOCUMENT ME!
539 public Dimension minimumsize()
547 * @return DOCUMENT ME!
549 public Dimension preferredsize()
561 public void keyTyped(KeyEvent evt)
572 public void keyReleased(KeyEvent evt)
583 public void keyPressed(KeyEvent evt)
585 if (evt.getKeyCode() == KeyEvent.VK_UP)
587 scalefactor = (float) (scalefactor * 1.1);
590 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
592 scalefactor = (float) (scalefactor * 0.9);
595 else if (evt.getKeyChar() == 's')
597 System.err.println("DEBUG: Rectangle selection"); // log.debug
599 if ((rectx2 != -1) && (recty2 != -1))
601 rectSelect(rectx1, recty1, rectx2, recty2);
615 public void mouseClicked(MouseEvent evt)
626 public void mouseEntered(MouseEvent evt)
637 public void mouseExited(MouseEvent evt)
648 public void mouseReleased(MouseEvent evt)
659 public void mousePressed(MouseEvent evt)
679 SequenceI found = findPoint(x, y);
683 AlignmentPanel[] aps = getAssociatedPanels();
685 for (int a = 0; a < aps.length; a++)
687 if (aps[a].av.getSelectionGroup() != null)
689 aps[a].av.getSelectionGroup().addOrRemove(found, true);
693 aps[a].av.setSelectionGroup(new SequenceGroup());
694 aps[a].av.getSelectionGroup().addOrRemove(found, true);
695 aps[a].av.getSelectionGroup()
696 .setEndRes(aps[a].av.getAlignment().getWidth() - 1);
699 PaintRefresher.Refresh(this, av.getSequenceSetId());
700 // canonical selection is sent to other listeners
707 // private void fireSequenceSelectionEvent(Selection sel) {
708 // controller.handleSequenceSelectionEvent(new
709 // SequenceSelectionEvent(this,sel));
712 public void mouseMoved(MouseEvent evt)
714 SequenceI found = findPoint(evt.getX(), evt.getY());
718 this.setToolTipText(found.getName());
722 this.setToolTipText(null);
733 public void mouseDragged(MouseEvent evt)
738 // Check if this is a rectangle drawing drag
739 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0)
741 // rectx2 = evt.getX();
742 // recty2 = evt.getY();
746 rotmat.setIdentity();
748 rotmat.rotate(my - omy, 'x');
749 rotmat.rotate(mx - omx, 'y');
751 for (int i = 0; i < npoint; i++)
753 SequencePoint sp = (SequencePoint) points.elementAt(i);
754 sp.coord[0] -= centre[0];
755 sp.coord[1] -= centre[1];
756 sp.coord[2] -= centre[2];
758 // Now apply the rotation matrix
759 sp.coord = rotmat.vectorMultiply(sp.coord);
761 // Now translate back again
762 sp.coord[0] += centre[0];
763 sp.coord[1] += centre[1];
764 sp.coord[2] += centre[2];
767 for (int i = 0; i < 3; i++)
769 axes[i] = rotmat.vectorMultiply(axes[i]);
775 paint(this.getGraphics());
791 public void rectSelect(int x1, int y1, int x2, int y2)
793 for (int i = 0; i < npoint; i++)
795 SequencePoint sp = (SequencePoint) points.elementAt(i);
796 int tmp1 = (int) (((sp.coord[0] - centre[0]) * scale)
797 + (getWidth() / 2.0));
798 int tmp2 = (int) (((sp.coord[1] - centre[1]) * scale)
799 + (getHeight() / 2.0));
801 if ((tmp1 > x1) && (tmp1 < x2) && (tmp2 > y1) && (tmp2 < y2))
805 if (!av.getSelectionGroup().getSequences(null)
806 .contains(sp.sequence))
808 av.getSelectionGroup().addSequence(sp.sequence, true);
815 // fireSequenceSelectionEvent(av.getSelection());
827 * @return DOCUMENT ME!
829 public SequenceI findPoint(int x, int y)
831 int halfwidth = getWidth() / 2;
832 int halfheight = getHeight() / 2;
836 for (int i = 0; i < npoint; i++)
838 SequencePoint sp = (SequencePoint) points.elementAt(i);
839 int px = (int) ((sp.coord[0] - centre[0]) * scale)
841 int py = (int) ((sp.coord[1] - centre[1]) * scale)
844 if ((Math.abs(px - x) < 3) && (Math.abs(py - y) < 3))
852 return ((SequencePoint) points.elementAt(found)).sequence;
860 AlignmentPanel[] getAssociatedPanels()
864 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
868 return new AlignmentPanel[] { ap };
874 * @return x,y,z positions of point s (index into points) under current
877 public double[] getPointPosition(int s)
879 double pts[] = new double[3];
880 float[] p = ((SequencePoint) points.elementAt(s)).coord;