2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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/>.
22 import java.awt.event.*;
26 import jalview.api.RotatableCanvasI;
27 import jalview.datamodel.*;
28 import jalview.math.*;
29 import jalview.util.MessageManager;
37 public class RotatableCanvas extends JPanel implements MouseListener,
38 MouseMotionListener, KeyListener, RotatableCanvasI
40 RotatableMatrix idmat = new RotatableMatrix(3, 3);
42 RotatableMatrix objmat = new RotatableMatrix(3, 3);
44 RotatableMatrix rotmat = new RotatableMatrix(3, 3);
46 // RubberbandRectangle rubberband;
47 boolean drawAxes = true;
63 float[] centre = new float[3];
65 float[] width = new float[3];
67 float[] max = new float[3];
69 float[] min = new float[3];
99 float scalefactor = 1;
105 boolean showLabels = false;
107 Color bgColour = Color.black;
109 boolean applyToAllViews = false;
111 // Controller controller;
112 public RotatableCanvas(AlignmentPanel ap)
117 addMouseWheelListener(new MouseWheelListener()
119 public void mouseWheelMoved(MouseWheelEvent e)
121 if (e.getWheelRotation() > 0)
123 scale = (float) (scale * 1.1);
129 scale = (float) (scale * 0.9);
137 public void showLabels(boolean b)
143 boolean first = true;
145 public void setPoints(Vector points, int npoint)
147 this.points = points;
148 this.npoint = npoint;
151 ToolTipManager.sharedInstance().registerComponent(this);
152 ToolTipManager.sharedInstance().setInitialDelay(0);
153 ToolTipManager.sharedInstance().setDismissDelay(10000);
155 prefsize = getPreferredSize();
156 orig = new float[npoint][3];
158 for (int i = 0; i < npoint; i++)
160 SequencePoint sp = (SequencePoint) points.elementAt(i);
162 for (int j = 0; j < 3; j++)
164 orig[i][j] = sp.coord[j];
168 // Initialize the matrices to identity
169 for (int i = 0; i < 3; i++)
171 for (int j = 0; j < 3; j++)
175 idmat.addElement(i, j, 0);
176 objmat.addElement(i, j, 0);
177 rotmat.addElement(i, j, 0);
181 idmat.addElement(i, j, 0);
182 objmat.addElement(i, j, 0);
183 rotmat.addElement(i, j, 0);
188 axes = new float[3][3];
198 addMouseListener(this);
200 addMouseMotionListener(this);
205 public void initAxes()
207 for (int i = 0; i < 3; i++)
209 for (int j = 0; j < 3; j++)
226 public void findWidth()
231 max[0] = (float) -1e30;
232 max[1] = (float) -1e30;
233 max[2] = (float) -1e30;
235 min[0] = (float) 1e30;
236 min[1] = (float) 1e30;
237 min[2] = (float) 1e30;
239 for (int i = 0; i < 3; i++)
241 for (int j = 0; j < npoint; j++)
243 SequencePoint sp = (SequencePoint) points.elementAt(j);
245 if (sp.coord[i] >= max[i])
247 max[i] = sp.coord[i];
250 if (sp.coord[i] <= min[i])
252 min[i] = sp.coord[i];
257 // System.out.println("xmax " + max[0] + " min " + min[0]);
258 // System.out.println("ymax " + max[1] + " min " + min[1]);
259 // System.out.println("zmax " + max[2] + " min " + min[2]);
260 width[0] = Math.abs(max[0] - min[0]);
261 width[1] = Math.abs(max[1] - min[1]);
262 width[2] = Math.abs(max[2] - min[2]);
266 if (width[1] > width[0])
271 if (width[2] > width[1])
276 // System.out.println("Maxwidth = " + maxwidth);
282 * @return DOCUMENT ME!
284 public float findScale()
293 height = getHeight();
297 width = prefsize.width;
298 height = prefsize.height;
310 return (float) ((dim * scalefactor) / (2 * maxwidth));
316 public void findCentre()
318 // Find centre coordinate
321 centre[0] = (max[0] + min[0]) / 2;
322 centre[1] = (max[1] + min[1]) / 2;
323 centre[2] = (max[2] + min[2]) / 2;
325 // System.out.println("Centre x " + centre[0]);
326 // System.out.println("Centre y " + centre[1]);
327 // System.out.println("Centre z " + centre[2]);
333 * @return DOCUMENT ME!
335 public Dimension getPreferredSize()
337 if (prefsize != null)
343 return new Dimension(400, 400);
350 * @return DOCUMENT ME!
352 public Dimension getMinimumSize()
354 return getPreferredSize();
363 public void paintComponent(Graphics g1)
366 Graphics2D g = (Graphics2D) g1;
368 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
369 RenderingHints.VALUE_ANTIALIAS_ON);
372 g.setFont(new Font("Verdana", Font.PLAIN, 18));
373 g.drawString(MessageManager.getString("label.calculating_pca")+ "....", 20, getHeight() / 2);
377 // Only create the image at the beginning -
378 if ((img == null) || (prefsize.width != getWidth())
379 || (prefsize.height != getHeight()))
381 prefsize.width = getWidth();
382 prefsize.height = getHeight();
386 // System.out.println("New scale = " + scale);
387 img = createImage(getWidth(), getHeight());
388 ig = img.getGraphics();
391 drawBackground(ig, bgColour);
394 if (drawAxes == true)
399 g.drawImage(img, 0, 0, this);
409 public void drawAxes(Graphics g)
412 g.setColor(Color.yellow);
414 for (int i = 0; i < 3; i++)
416 g.drawLine(getWidth() / 2, getHeight() / 2, (int) ((axes[i][0]
417 * scale * max[0]) + (getWidth() / 2)), (int) ((axes[i][1]
418 * scale * max[1]) + (getHeight() / 2)));
430 public void drawBackground(Graphics g, Color col)
433 g.fillRect(0, 0, prefsize.width, prefsize.height);
442 public void drawScene(Graphics g1)
445 Graphics2D g = (Graphics2D) g1;
447 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
448 RenderingHints.VALUE_ANTIALIAS_ON);
450 int halfwidth = getWidth() / 2;
451 int halfheight = getHeight() / 2;
453 for (int i = 0; i < npoint; i++)
455 SequencePoint sp = (SequencePoint) points.elementAt(i);
456 int x = (int) ((float) (sp.coord[0] - centre[0]) * scale) + halfwidth;
457 int y = (int) ((float) (sp.coord[1] - centre[1]) * scale)
459 float z = sp.coord[1] - centre[2];
461 if (av.getSequenceColour(sp.sequence) == Color.black)
463 g.setColor(Color.white);
467 g.setColor(av.getSequenceColour(sp.sequence));
470 if (av.getSelectionGroup() != null)
472 if (av.getSelectionGroup().getSequences(null)
473 .contains(((SequencePoint) points.elementAt(i)).sequence))
475 g.setColor(Color.gray);
481 g.setColor(g.getColor().darker());
484 g.fillRect(x - 3, y - 3, 6, 6);
487 g.setColor(Color.red);
489 ((SequencePoint) points.elementAt(i)).sequence.getName(),
494 // //Now the rectangle
495 // if (rectx2 != -1 && recty2 != -1) {
496 // g.setColor(Color.white);
498 // g.drawRect(rectx1,recty1,rectx2-rectx1,recty2-recty1);
505 * @return DOCUMENT ME!
507 public Dimension minimumsize()
515 * @return DOCUMENT ME!
517 public Dimension preferredsize()
528 public void keyTyped(KeyEvent evt)
538 public void keyReleased(KeyEvent evt)
548 public void keyPressed(KeyEvent evt)
550 if (evt.getKeyCode() == KeyEvent.VK_UP)
552 scalefactor = (float) (scalefactor * 1.1);
555 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
557 scalefactor = (float) (scalefactor * 0.9);
560 else if (evt.getKeyChar() == 's')
562 System.err.println("DEBUG: Rectangle selection"); // log.debug
564 if ((rectx2 != -1) && (recty2 != -1))
566 rectSelect(rectx1, recty1, rectx2, recty2);
579 public void mouseClicked(MouseEvent evt)
589 public void mouseEntered(MouseEvent evt)
599 public void mouseExited(MouseEvent evt)
609 public void mouseReleased(MouseEvent evt)
619 public void mousePressed(MouseEvent evt)
639 SequenceI found = findPoint(x, y);
643 AlignmentPanel[] aps = getAssociatedPanels();
645 for (int a = 0; a < aps.length; a++)
647 if (aps[a].av.getSelectionGroup() != null)
649 aps[a].av.getSelectionGroup().addOrRemove(found, true);
653 aps[a].av.setSelectionGroup(new SequenceGroup());
654 aps[a].av.getSelectionGroup().addOrRemove(found, true);
655 aps[a].av.getSelectionGroup().setEndRes(
656 aps[a].av.getAlignment().getWidth() - 1);
659 PaintRefresher.Refresh(this, av.getSequenceSetId());
660 // canonical selection is sent to other listeners
667 // private void fireSequenceSelectionEvent(Selection sel) {
668 // controller.handleSequenceSelectionEvent(new
669 // SequenceSelectionEvent(this,sel));
671 public void mouseMoved(MouseEvent evt)
673 SequenceI found = findPoint(evt.getX(), evt.getY());
677 this.setToolTipText(found.getName());
681 this.setToolTipText(null);
691 public void mouseDragged(MouseEvent evt)
696 // Check if this is a rectangle drawing drag
697 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0)
699 // rectx2 = evt.getX();
700 // recty2 = evt.getY();
704 rotmat.setIdentity();
706 rotmat.rotate((float) (my - omy), 'x');
707 rotmat.rotate((float) (mx - omx), 'y');
709 for (int i = 0; i < npoint; i++)
711 SequencePoint sp = (SequencePoint) points.elementAt(i);
712 sp.coord[0] -= centre[0];
713 sp.coord[1] -= centre[1];
714 sp.coord[2] -= centre[2];
716 // Now apply the rotation matrix
717 sp.coord = rotmat.vectorMultiply(sp.coord);
719 // Now translate back again
720 sp.coord[0] += centre[0];
721 sp.coord[1] += centre[1];
722 sp.coord[2] += centre[2];
725 for (int i = 0; i < 3; i++)
727 axes[i] = rotmat.vectorMultiply(axes[i]);
733 paint(this.getGraphics());
749 public void rectSelect(int x1, int y1, int x2, int y2)
751 for (int i = 0; i < npoint; i++)
753 SequencePoint sp = (SequencePoint) points.elementAt(i);
754 int tmp1 = (int) (((sp.coord[0] - centre[0]) * scale) + ((float) getWidth() / 2.0));
755 int tmp2 = (int) (((sp.coord[1] - centre[1]) * scale) + ((float) getHeight() / 2.0));
757 if ((tmp1 > x1) && (tmp1 < x2) && (tmp2 > y1) && (tmp2 < y2))
761 if (!av.getSelectionGroup().getSequences(null)
762 .contains(sp.sequence))
764 av.getSelectionGroup().addSequence(sp.sequence, true);
771 // fireSequenceSelectionEvent(av.getSelection());
783 * @return DOCUMENT ME!
785 public SequenceI findPoint(int x, int y)
787 int halfwidth = getWidth() / 2;
788 int halfheight = getHeight() / 2;
792 for (int i = 0; i < npoint; i++)
794 SequencePoint sp = (SequencePoint) points.elementAt(i);
795 int px = (int) ((float) (sp.coord[0] - centre[0]) * scale)
797 int py = (int) ((float) (sp.coord[1] - centre[1]) * scale)
800 if ((Math.abs(px - x) < 3) && (Math.abs(py - y) < 3))
808 return ((SequencePoint) points.elementAt(found)).sequence;
816 AlignmentPanel[] getAssociatedPanels()
820 return PaintRefresher.getAssociatedPanels(av.getSequenceSetId());
824 return new AlignmentPanel[]
831 * @return x,y,z positions of point s (index into points) under current
834 public double[] getPointPosition(int s)
836 double pts[] = new double[3];
837 float[] p = ((SequencePoint) points.elementAt(s)).coord;