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.ColorUtils;
29 import jalview.util.MessageManager;
30 import jalview.viewmodel.AlignmentViewport;
32 import java.awt.Color;
33 import java.awt.Dimension;
35 import java.awt.Graphics;
36 import java.awt.Graphics2D;
37 import java.awt.Image;
38 import java.awt.RenderingHints;
39 import java.awt.event.InputEvent;
40 import java.awt.event.KeyEvent;
41 import java.awt.event.KeyListener;
42 import java.awt.event.MouseEvent;
43 import java.awt.event.MouseListener;
44 import java.awt.event.MouseMotionListener;
45 import java.awt.event.MouseWheelEvent;
46 import java.awt.event.MouseWheelListener;
47 import java.util.Vector;
49 import javax.swing.JPanel;
50 import javax.swing.ToolTipManager;
58 public class RotatableCanvas extends JPanel implements MouseListener,
59 MouseMotionListener, KeyListener, RotatableCanvasI
61 RotatableMatrix idmat = new RotatableMatrix(3, 3);
63 RotatableMatrix objmat = new RotatableMatrix(3, 3);
65 RotatableMatrix rotmat = new RotatableMatrix(3, 3);
67 // RubberbandRectangle rubberband;
68 boolean drawAxes = true;
84 float[] centre = new float[3];
86 float[] width = new float[3];
88 float[] max = new float[3];
90 float[] min = new float[3];
120 float scalefactor = 1;
122 AlignmentViewport av;
126 boolean showLabels = false;
128 Color bgColour = Color.black;
130 boolean applyToAllViews = false;
132 // Controller controller;
133 public RotatableCanvas(AlignmentPanel ap)
138 addMouseWheelListener(new MouseWheelListener()
141 public void mouseWheelMoved(MouseWheelEvent e)
143 if (e.getWheelRotation() > 0)
145 scale = (float) (scale * 1.1);
151 scale = (float) (scale * 0.9);
159 public void showLabels(boolean b)
165 boolean first = true;
168 public void setPoints(Vector points, int npoint)
170 this.points = points;
171 this.npoint = npoint;
174 ToolTipManager.sharedInstance().registerComponent(this);
175 ToolTipManager.sharedInstance().setInitialDelay(0);
176 ToolTipManager.sharedInstance().setDismissDelay(10000);
178 prefsize = getPreferredSize();
179 orig = new float[npoint][3];
181 for (int i = 0; i < npoint; i++)
183 SequencePoint sp = (SequencePoint) points.elementAt(i);
185 for (int j = 0; j < 3; j++)
187 orig[i][j] = sp.coord[j];
191 // Initialize the matrices to identity
192 for (int i = 0; i < 3; i++)
194 for (int j = 0; j < 3; j++)
198 idmat.addElement(i, j, 0);
199 objmat.addElement(i, j, 0);
200 rotmat.addElement(i, j, 0);
204 idmat.addElement(i, j, 0);
205 objmat.addElement(i, j, 0);
206 rotmat.addElement(i, j, 0);
211 axes = new float[3][3];
221 addMouseListener(this);
223 addMouseMotionListener(this);
228 public void initAxes()
230 for (int i = 0; i < 3; i++)
232 for (int j = 0; j < 3; j++)
249 public void findWidth()
254 max[0] = (float) -1e30;
255 max[1] = (float) -1e30;
256 max[2] = (float) -1e30;
258 min[0] = (float) 1e30;
259 min[1] = (float) 1e30;
260 min[2] = (float) 1e30;
262 for (int i = 0; i < 3; i++)
264 for (int j = 0; j < npoint; j++)
266 SequencePoint sp = (SequencePoint) points.elementAt(j);
268 if (sp.coord[i] >= max[i])
270 max[i] = sp.coord[i];
273 if (sp.coord[i] <= min[i])
275 min[i] = sp.coord[i];
280 // System.out.println("xmax " + max[0] + " min " + min[0]);
281 // System.out.println("ymax " + max[1] + " min " + min[1]);
282 // System.out.println("zmax " + max[2] + " min " + min[2]);
283 width[0] = Math.abs(max[0] - min[0]);
284 width[1] = Math.abs(max[1] - min[1]);
285 width[2] = Math.abs(max[2] - min[2]);
289 if (width[1] > width[0])
294 if (width[2] > width[1])
299 // System.out.println("Maxwidth = " + maxwidth);
305 * @return DOCUMENT ME!
307 public float findScale()
316 height = getHeight();
320 width = prefsize.width;
321 height = prefsize.height;
333 return (dim * scalefactor) / (2 * maxwidth);
339 public void findCentre()
341 // Find centre coordinate
344 centre[0] = (max[0] + min[0]) / 2;
345 centre[1] = (max[1] + min[1]) / 2;
346 centre[2] = (max[2] + min[2]) / 2;
348 // System.out.println("Centre x " + centre[0]);
349 // System.out.println("Centre y " + centre[1]);
350 // System.out.println("Centre z " + centre[2]);
356 * @return DOCUMENT ME!
359 public Dimension getPreferredSize()
361 if (prefsize != null)
367 return new Dimension(400, 400);
374 * @return DOCUMENT ME!
377 public Dimension getMinimumSize()
379 return getPreferredSize();
389 public void paintComponent(Graphics g1)
392 Graphics2D g = (Graphics2D) g1;
394 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
395 RenderingHints.VALUE_ANTIALIAS_ON);
398 g.setFont(new Font("Verdana", Font.PLAIN, 18));
399 g.drawString(MessageManager.getString("label.calculating_pca")
400 + "....", 20, getHeight() / 2);
404 // Only create the image at the beginning -
405 if ((img == null) || (prefsize.width != getWidth())
406 || (prefsize.height != getHeight()))
408 prefsize.width = getWidth();
409 prefsize.height = getHeight();
413 // System.out.println("New scale = " + scale);
414 img = createImage(getWidth(), getHeight());
415 ig = img.getGraphics();
418 drawBackground(ig, bgColour);
421 if (drawAxes == true)
426 g.drawImage(img, 0, 0, this);
436 public void drawAxes(Graphics g)
439 g.setColor(Color.yellow);
441 for (int i = 0; i < 3; i++)
443 g.drawLine(getWidth() / 2, getHeight() / 2, (int) ((axes[i][0]
444 * scale * max[0]) + (getWidth() / 2)), (int) ((axes[i][1]
445 * scale * max[1]) + (getHeight() / 2)));
457 public void drawBackground(Graphics g, Color col)
460 g.fillRect(0, 0, prefsize.width, prefsize.height);
469 public void drawScene(Graphics g1)
472 Graphics2D g = (Graphics2D) g1;
474 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
475 RenderingHints.VALUE_ANTIALIAS_ON);
477 int halfwidth = getWidth() / 2;
478 int halfheight = getHeight() / 2;
480 for (int i = 0; i < npoint; i++)
482 SequencePoint sp = (SequencePoint) points.elementAt(i);
483 int x = (int) ((sp.coord[0] - centre[0]) * scale) + halfwidth;
484 int y = (int) ((sp.coord[1] - centre[1]) * scale)
486 float z = sp.coord[1] - centre[2];
488 if (av.getSequenceColour(sp.sequence) == Color.black)
490 g.setColor(Color.white);
494 g.setColor(ColorUtils.getColor(av.getSequenceColour(sp.sequence)));
497 if (av.getSelectionGroup() != null)
499 if (av.getSelectionGroup().getSequences(null)
500 .contains(((SequencePoint) points.elementAt(i)).sequence))
502 g.setColor(Color.gray);
508 g.setColor(g.getColor().darker());
511 g.fillRect(x - 3, y - 3, 6, 6);
514 g.setColor(Color.red);
516 ((SequencePoint) points.elementAt(i)).sequence.getName(),
521 // //Now the rectangle
522 // if (rectx2 != -1 && recty2 != -1) {
523 // g.setColor(Color.white);
525 // g.drawRect(rectx1,recty1,rectx2-rectx1,recty2-recty1);
532 * @return DOCUMENT ME!
534 public Dimension minimumsize()
542 * @return DOCUMENT ME!
544 public Dimension preferredsize()
556 public void keyTyped(KeyEvent evt)
567 public void keyReleased(KeyEvent evt)
578 public void keyPressed(KeyEvent evt)
580 if (evt.getKeyCode() == KeyEvent.VK_UP)
582 scalefactor = (float) (scalefactor * 1.1);
585 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
587 scalefactor = (float) (scalefactor * 0.9);
590 else if (evt.getKeyChar() == 's')
592 System.err.println("DEBUG: Rectangle selection"); // log.debug
594 if ((rectx2 != -1) && (recty2 != -1))
596 rectSelect(rectx1, recty1, rectx2, recty2);
610 public void mouseClicked(MouseEvent evt)
621 public void mouseEntered(MouseEvent evt)
632 public void mouseExited(MouseEvent evt)
643 public void mouseReleased(MouseEvent evt)
654 public void mousePressed(MouseEvent evt)
674 SequenceI found = findPoint(x, y);
678 AlignmentPanel[] aps = getAssociatedPanels();
680 for (int a = 0; a < aps.length; a++)
682 if (aps[a].av.getSelectionGroup() != null)
684 aps[a].av.getSelectionGroup().addOrRemove(found, true);
688 aps[a].av.setSelectionGroup(new SequenceGroup());
689 aps[a].av.getSelectionGroup().addOrRemove(found, true);
690 aps[a].av.getSelectionGroup().setEndRes(
691 aps[a].av.getAlignment().getWidth() - 1);
694 PaintRefresher.Refresh(this, av.getSequenceSetId());
695 // canonical selection is sent to other listeners
702 // private void fireSequenceSelectionEvent(Selection sel) {
703 // controller.handleSequenceSelectionEvent(new
704 // SequenceSelectionEvent(this,sel));
707 public void mouseMoved(MouseEvent evt)
709 SequenceI found = findPoint(evt.getX(), evt.getY());
713 this.setToolTipText(found.getName());
717 this.setToolTipText(null);
728 public void mouseDragged(MouseEvent evt)
733 // Check if this is a rectangle drawing drag
734 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0)
736 // rectx2 = evt.getX();
737 // recty2 = evt.getY();
741 rotmat.setIdentity();
743 rotmat.rotate(my - omy, 'x');
744 rotmat.rotate(mx - omx, 'y');
746 for (int i = 0; i < npoint; i++)
748 SequencePoint sp = (SequencePoint) points.elementAt(i);
749 sp.coord[0] -= centre[0];
750 sp.coord[1] -= centre[1];
751 sp.coord[2] -= centre[2];
753 // Now apply the rotation matrix
754 sp.coord = rotmat.vectorMultiply(sp.coord);
756 // Now translate back again
757 sp.coord[0] += centre[0];
758 sp.coord[1] += centre[1];
759 sp.coord[2] += centre[2];
762 for (int i = 0; i < 3; i++)
764 axes[i] = rotmat.vectorMultiply(axes[i]);
770 paint(this.getGraphics());
786 public void rectSelect(int x1, int y1, int x2, int y2)
788 for (int i = 0; i < npoint; i++)
790 SequencePoint sp = (SequencePoint) points.elementAt(i);
791 int tmp1 = (int) (((sp.coord[0] - centre[0]) * scale) + (getWidth() / 2.0));
792 int tmp2 = (int) (((sp.coord[1] - centre[1]) * scale) + (getHeight() / 2.0));
794 if ((tmp1 > x1) && (tmp1 < x2) && (tmp2 > y1) && (tmp2 < y2))
798 if (!av.getSelectionGroup().getSequences(null)
799 .contains(sp.sequence))
801 av.getSelectionGroup().addSequence(sp.sequence, true);
808 // fireSequenceSelectionEvent(av.getSelection());
820 * @return DOCUMENT ME!
822 public SequenceI findPoint(int x, int y)
824 int halfwidth = getWidth() / 2;
825 int halfheight = getHeight() / 2;
829 for (int i = 0; i < npoint; i++)
831 SequencePoint sp = (SequencePoint) points.elementAt(i);
832 int px = (int) ((sp.coord[0] - centre[0]) * scale)
834 int py = (int) ((sp.coord[1] - centre[1]) * scale)
837 if ((Math.abs(px - x) < 3) && (Math.abs(py - y) < 3))
845 return ((SequencePoint) points.elementAt(found)).sequence;
853 AlignmentPanel[] getAssociatedPanels()
857 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
861 return new AlignmentPanel[] { ap };
867 * @return x,y,z positions of point s (index into points) under current
870 public double[] getPointPosition(int s)
872 double pts[] = new double[3];
873 float[] p = ((SequencePoint) points.elementAt(s)).coord;