2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.gui;
\r
21 import jalview.datamodel.*;
\r
23 import jalview.math.*;
\r
26 import java.awt.event.*;
\r
30 import javax.swing.*;
\r
37 * @version $Revision$
\r
39 public class RotatableCanvas extends JPanel implements MouseListener,
\r
40 MouseMotionListener, KeyListener
\r
42 RotatableMatrix idmat = new RotatableMatrix(3, 3);
\r
43 RotatableMatrix objmat = new RotatableMatrix(3, 3);
\r
44 RotatableMatrix rotmat = new RotatableMatrix(3, 3);
\r
46 //RubberbandRectangle rubberband;
\r
47 boolean drawAxes = true;
\r
55 float[] centre = new float[3];
\r
56 float[] width = new float[3];
\r
57 float[] max = new float[3];
\r
58 float[] min = new float[3];
\r
73 float scalefactor = 1;
\r
75 boolean showLabels = false;
\r
76 Color bgColour = Color.black;
\r
78 // Controller controller;
\r
79 public RotatableCanvas(AlignViewport av)
\r
84 public void showLabels(boolean b)
\r
90 public void setPoints(Vector points, int npoint)
\r
92 this.points = points;
\r
93 this.npoint = npoint;
\r
94 ToolTipManager.sharedInstance().registerComponent(this);
\r
95 ToolTipManager.sharedInstance().setInitialDelay(0);
\r
96 ToolTipManager.sharedInstance().setDismissDelay(10000);
\r
97 PaintRefresher.Register(this, av.getSequenceSetId());
\r
99 prefsize = getPreferredSize();
\r
100 orig = new float[npoint][3];
\r
102 for (int i = 0; i < npoint; i++)
\r
104 SequencePoint sp = (SequencePoint) points.elementAt(i);
\r
106 for (int j = 0; j < 3; j++)
\r
108 orig[i][j] = sp.coord[j];
\r
112 //Initialize the matrices to identity
\r
113 for (int i = 0; i < 3; i++)
\r
115 for (int j = 0; j < 3; j++)
\r
119 idmat.addElement(i, j, 0);
\r
120 objmat.addElement(i, j, 0);
\r
121 rotmat.addElement(i, j, 0);
\r
125 idmat.addElement(i, j, 0);
\r
126 objmat.addElement(i, j, 0);
\r
127 rotmat.addElement(i, j, 0);
\r
132 axes = new float[3][3];
\r
138 scale = findScale();
\r
140 addMouseListener(this);
\r
141 addKeyListener(this);
\r
143 addMouseMotionListener(this);
\r
147 public void initAxes()
\r
149 for (int i = 0; i < 3; i++)
\r
151 for (int j = 0; j < 3; j++)
\r
168 public void findWidth()
\r
170 max = new float[3];
\r
171 min = new float[3];
\r
173 max[0] = (float) -1e30;
\r
174 max[1] = (float) -1e30;
\r
175 max[2] = (float) -1e30;
\r
177 min[0] = (float) 1e30;
\r
178 min[1] = (float) 1e30;
\r
179 min[2] = (float) 1e30;
\r
181 for (int i = 0; i < 3; i++)
\r
183 for (int j = 0; j < npoint; j++)
\r
185 SequencePoint sp = (SequencePoint) points.elementAt(j);
\r
187 if (sp.coord[i] >= max[i])
\r
189 max[i] = sp.coord[i];
\r
192 if (sp.coord[i] <= min[i])
\r
194 min[i] = sp.coord[i];
\r
199 // System.out.println("xmax " + max[0] + " min " + min[0]);
\r
200 //System.out.println("ymax " + max[1] + " min " + min[1]);
\r
201 //System.out.println("zmax " + max[2] + " min " + min[2]);
\r
202 width[0] = Math.abs(max[0] - min[0]);
\r
203 width[1] = Math.abs(max[1] - min[1]);
\r
204 width[2] = Math.abs(max[2] - min[2]);
\r
206 maxwidth = width[0];
\r
208 if (width[1] > width[0])
\r
210 maxwidth = width[1];
\r
213 if (width[2] > width[1])
\r
215 maxwidth = width[2];
\r
218 //System.out.println("Maxwidth = " + maxwidth);
\r
224 * @return DOCUMENT ME!
\r
226 public float findScale()
\r
232 if (getWidth() != 0)
\r
234 width = getWidth();
\r
235 height = getHeight();
\r
239 width = prefsize.width;
\r
240 height = prefsize.height;
\r
243 if (width < height)
\r
252 return (float) ((dim * scalefactor) / (2 * maxwidth));
\r
258 public void findCentre()
\r
260 //Find centre coordinate
\r
263 centre[0] = (max[0] + min[0]) / 2;
\r
264 centre[1] = (max[1] + min[1]) / 2;
\r
265 centre[2] = (max[2] + min[2]) / 2;
\r
267 // System.out.println("Centre x " + centre[0]);
\r
268 //System.out.println("Centre y " + centre[1]);
\r
269 //System.out.println("Centre z " + centre[2]);
\r
275 * @return DOCUMENT ME!
\r
277 public Dimension getPreferredSize()
\r
279 if (prefsize != null)
\r
285 return new Dimension(400, 400);
\r
292 * @return DOCUMENT ME!
\r
294 public Dimension getMinimumSize()
\r
296 return getPreferredSize();
\r
302 * @param g DOCUMENT ME!
\r
304 public void paintComponent(Graphics g1)
\r
307 Graphics2D g = (Graphics2D) g1;
\r
309 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
310 RenderingHints.VALUE_ANTIALIAS_ON);
\r
313 g.setFont(new Font("Verdana", Font.PLAIN, 18));
\r
314 g.drawString("Calculating PCA....", 20, getHeight()/2);
\r
318 //Only create the image at the beginning -
\r
319 if ( (img == null) || (prefsize.width != getWidth()) ||
\r
320 (prefsize.height != getHeight()))
\r
322 prefsize.width = getWidth();
\r
323 prefsize.height = getHeight();
\r
325 scale = findScale();
\r
327 // System.out.println("New scale = " + scale);
\r
328 img = createImage(getWidth(), getHeight());
\r
329 ig = img.getGraphics();
\r
333 drawBackground(ig, bgColour);
\r
336 if (drawAxes == true)
\r
341 g.drawImage(img, 0, 0, this);
\r
348 * @param g DOCUMENT ME!
\r
350 public void drawAxes(Graphics g)
\r
353 g.setColor(Color.yellow);
\r
355 for (int i = 0; i < 3; i++)
\r
357 g.drawLine(getWidth() / 2, getHeight() / 2,
\r
358 (int) ((axes[i][0] * scale * max[0]) + (getWidth() / 2)),
\r
359 (int) ((axes[i][1] * scale * max[1]) + (getHeight() / 2)));
\r
366 * @param g DOCUMENT ME!
\r
367 * @param col DOCUMENT ME!
\r
369 public void drawBackground(Graphics g, Color col)
\r
372 g.fillRect(0, 0, prefsize.width, prefsize.height);
\r
378 * @param g DOCUMENT ME!
\r
380 public void drawScene(Graphics g1)
\r
383 Graphics2D g = (Graphics2D) g1;
\r
385 g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
386 RenderingHints.VALUE_ANTIALIAS_ON);
\r
389 int halfwidth = getWidth() / 2;
\r
390 int halfheight = getHeight() / 2;
\r
392 for (int i = 0; i < npoint; i++)
\r
394 SequencePoint sp = (SequencePoint) points.elementAt(i);
\r
395 int x = (int) ((float) (sp.coord[0] - centre[0]) * scale) +
\r
397 int y = (int) ((float) (sp.coord[1] - centre[1]) * scale) +
\r
399 float z = sp.coord[1] - centre[2];
\r
401 if (sp.sequence.getColor() == Color.black)
\r
403 g.setColor(Color.white);
\r
407 g.setColor(sp.sequence.getColor());
\r
410 if (av.getSelectionGroup() != null)
\r
412 if (av.getSelectionGroup().getSequences(false).contains(
\r
413 ((SequencePoint) points.elementAt(i)).sequence))
\r
415 g.setColor(Color.gray);
\r
421 g.setColor(g.getColor().darker());
\r
424 g.fillRect(x - 3, y - 3, 6, 6);
\r
427 g.setColor(Color.red);
\r
428 g.drawString( ( (SequencePoint) points.elementAt(i)).sequence.
\r
434 // //Now the rectangle
\r
435 // if (rectx2 != -1 && recty2 != -1) {
\r
436 // g.setColor(Color.white);
\r
438 // g.drawRect(rectx1,recty1,rectx2-rectx1,recty2-recty1);
\r
445 * @return DOCUMENT ME!
\r
447 public Dimension minimumsize()
\r
455 * @return DOCUMENT ME!
\r
457 public Dimension preferredsize()
\r
465 * @param evt DOCUMENT ME!
\r
467 public void keyTyped(KeyEvent evt)
\r
474 * @param evt DOCUMENT ME!
\r
476 public void keyReleased(KeyEvent evt)
\r
483 * @param evt DOCUMENT ME!
\r
485 public void keyPressed(KeyEvent evt)
\r
487 if (evt.getKeyCode() == KeyEvent.VK_UP)
\r
489 scalefactor = (float) (scalefactor * 1.1);
\r
490 scale = findScale();
\r
492 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
\r
494 scalefactor = (float) (scalefactor * 0.9);
\r
495 scale = findScale();
\r
497 else if (evt.getKeyChar() == 's')
\r
499 System.err.println("DEBUG: Rectangle selection"); // log.debug
\r
501 if ((rectx2 != -1) && (recty2 != -1))
\r
503 rectSelect(rectx1, recty1, rectx2, recty2);
\r
514 * @param evt DOCUMENT ME!
\r
516 public void mouseClicked(MouseEvent evt)
\r
523 * @param evt DOCUMENT ME!
\r
525 public void mouseEntered(MouseEvent evt)
\r
532 * @param evt DOCUMENT ME!
\r
534 public void mouseExited(MouseEvent evt)
\r
541 * @param evt DOCUMENT ME!
\r
543 public void mouseReleased(MouseEvent evt)
\r
550 * @param evt DOCUMENT ME!
\r
552 public void mousePressed(MouseEvent evt)
\r
554 int x = evt.getX();
\r
555 int y = evt.getY();
\r
572 SequenceI found = findPoint(x, y);
\r
576 if (av.getSelectionGroup() != null)
\r
578 av.getSelectionGroup().addOrRemove(found, true);
\r
579 PaintRefresher.Refresh(this, av.getSequenceSetId());
\r
583 av.setSelectionGroup(new SequenceGroup());
\r
584 av.getSelectionGroup().addOrRemove(found, true);
\r
585 av.getSelectionGroup().setEndRes(av.alignment.getWidth()-1);
\r
592 // private void fireSequenceSelectionEvent(Selection sel) {
\r
593 // controller.handleSequenceSelectionEvent(new SequenceSelectionEvent(this,sel));
\r
595 public void mouseMoved(MouseEvent evt)
\r
597 SequenceI found = findPoint(evt.getX(), evt.getY());
\r
601 this.setToolTipText(found.getName());
\r
605 this.setToolTipText(null);
\r
612 * @param evt DOCUMENT ME!
\r
614 public void mouseDragged(MouseEvent evt)
\r
619 //Check if this is a rectangle drawing drag
\r
620 if ((evt.getModifiers() & InputEvent.BUTTON2_MASK) != 0)
\r
622 // rectx2 = evt.getX();
\r
623 // recty2 = evt.getY();
\r
627 rotmat.setIdentity();
\r
629 rotmat.rotate((float) (my - omy), 'x');
\r
630 rotmat.rotate((float) (mx - omx), 'y');
\r
632 for (int i = 0; i < npoint; i++)
\r
634 SequencePoint sp = (SequencePoint) points.elementAt(i);
\r
635 sp.coord[0] -= centre[0];
\r
636 sp.coord[1] -= centre[1];
\r
637 sp.coord[2] -= centre[2];
\r
639 //Now apply the rotation matrix
\r
640 sp.coord = rotmat.vectorMultiply(sp.coord);
\r
642 //Now translate back again
\r
643 sp.coord[0] += centre[0];
\r
644 sp.coord[1] += centre[1];
\r
645 sp.coord[2] += centre[2];
\r
648 for (int i = 0; i < 3; i++)
\r
650 axes[i] = rotmat.vectorMultiply(axes[i]);
\r
656 paint(this.getGraphics());
\r
663 * @param x1 DOCUMENT ME!
\r
664 * @param y1 DOCUMENT ME!
\r
665 * @param x2 DOCUMENT ME!
\r
666 * @param y2 DOCUMENT ME!
\r
668 public void rectSelect(int x1, int y1, int x2, int y2)
\r
670 for (int i = 0; i < npoint; i++)
\r
672 SequencePoint sp = (SequencePoint) points.elementAt(i);
\r
673 int tmp1 = (int) (((sp.coord[0] - centre[0]) * scale) +
\r
674 ((float) getWidth() / 2.0));
\r
675 int tmp2 = (int) (((sp.coord[1] - centre[1]) * scale) +
\r
676 ((float) getHeight() / 2.0));
\r
678 if ((tmp1 > x1) && (tmp1 < x2) && (tmp2 > y1) && (tmp2 < y2))
\r
682 if (!av.getSelectionGroup().getSequences(false).contains(sp.sequence))
\r
684 av.getSelectionGroup().addSequence(sp.sequence, true);
\r
690 // if (changedSel) {
\r
691 // fireSequenceSelectionEvent(av.getSelection());
\r
698 * @param x DOCUMENT ME!
\r
699 * @param y DOCUMENT ME!
\r
701 * @return DOCUMENT ME!
\r
703 public SequenceI findPoint(int x, int y)
\r
705 int halfwidth = getWidth() / 2;
\r
706 int halfheight = getHeight() / 2;
\r
710 for (int i = 0; i < npoint; i++)
\r
712 SequencePoint sp = (SequencePoint) points.elementAt(i);
\r
713 int px = (int) ((float) (sp.coord[0] - centre[0]) * scale) +
\r
715 int py = (int) ((float) (sp.coord[1] - centre[1]) * scale) +
\r
718 if ((Math.abs(px - x) < 3) && (Math.abs(py - y) < 3))
\r
726 return ((SequencePoint) points.elementAt(found)).sequence;
\r
734 /* public boolean handleRubberbandEvent(RubberbandEvent evt) {
\r
735 System.out.println("Rubberband handler called in RotatableCanvas with " +
\r
738 Rubberband rb = (Rubberband)evt.getSource();
\r
740 // Clear the current selection (instance variable)
\r
741 //if ((rb.getModifiers() & Event.SHIFT_MASK) == 0) {
\r
742 // clearSelection();
\r
745 if (rb.getComponent() == this) {
\r
746 Rectangle bounds = evt.getBounds();
\r
747 rectSelect(bounds.x,bounds.y,bounds.x+bounds.width,bounds.y+bounds.height);
\r
750 redrawneeded = true;
\r
751 paint(this.getGraphics());
\r