2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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/>.
18 package jalview.appletgui;
23 import java.awt.event.*;
25 import jalview.datamodel.*;
26 import jalview.math.*;
27 import jalview.util.*;
29 public class RotatableCanvas extends Panel implements MouseListener,
30 MouseMotionListener, KeyListener
32 RotatableMatrix idmat = new RotatableMatrix(3, 3);
34 RotatableMatrix objmat = new RotatableMatrix(3, 3);
36 RotatableMatrix rotmat = new RotatableMatrix(3, 3);
42 // RubberbandRectangle rubberband;
44 boolean drawAxes = true;
60 float centre[] = new float[3];
62 float width[] = new float[3];
64 float max[] = new float[3];
66 float min[] = new float[3];
96 float scalefactor = 1;
100 boolean showLabels = false;
102 public RotatableCanvas(AlignViewport av)
107 public void showLabels(boolean b)
113 public void setPoints(Vector points, int npoint)
115 this.points = points;
116 this.npoint = npoint;
117 PaintRefresher.Register(this, av.getSequenceSetId());
119 prefsize = getPreferredSize();
120 orig = new float[npoint][3];
122 for (int i = 0; i < npoint; i++)
124 SequencePoint sp = (SequencePoint) points.elementAt(i);
125 for (int j = 0; j < 3; j++)
127 orig[i][j] = sp.coord[j];
130 // Initialize the matrices to identity
132 for (int i = 0; i < 3; i++)
134 for (int j = 0; j < 3; j++)
138 idmat.addElement(i, j, 0);
139 objmat.addElement(i, j, 0);
140 rotmat.addElement(i, j, 0);
144 idmat.addElement(i, j, 0);
145 objmat.addElement(i, j, 0);
146 rotmat.addElement(i, j, 0);
151 axes = new float[3][3];
159 // System.out.println("Scale factor = " + scale);
161 addMouseListener(this);
162 addKeyListener(this);
163 // if (getParent() != null) {
164 // getParent().addKeyListener(this);
166 addMouseMotionListener(this);
169 // rubberband = new RubberbandRectangle(this);
170 // rubberband.setActive(true);
171 // rubberband.addListener(this);
175 * public boolean handleSequenceSelectionEvent(SequenceSelectionEvent evt) {
176 * redrawneeded = true; repaint(); return true; }
178 * public void removeNotify() { controller.removeListener(this);
179 * super.removeNotify(); }
182 public void initAxes()
184 for (int i = 0; i < 3; i++)
186 for (int j = 0; j < 3; j++)
200 public void findWidth()
205 max[0] = (float) -1e30;
206 max[1] = (float) -1e30;
207 max[2] = (float) -1e30;
209 min[0] = (float) 1e30;
210 min[1] = (float) 1e30;
211 min[2] = (float) 1e30;
213 for (int i = 0; i < 3; i++)
215 for (int j = 0; j < npoint; j++)
217 SequencePoint sp = (SequencePoint) points.elementAt(j);
218 if (sp.coord[i] >= max[i])
220 max[i] = sp.coord[i];
222 if (sp.coord[i] <= min[i])
224 min[i] = sp.coord[i];
229 // System.out.println("xmax " + max[0] + " min " + min[0]);
230 // System.out.println("ymax " + max[1] + " min " + min[1]);
231 // System.out.println("zmax " + max[2] + " min " + min[2]);
233 width[0] = Math.abs(max[0] - min[0]);
234 width[1] = Math.abs(max[1] - min[1]);
235 width[2] = Math.abs(max[2] - min[2]);
239 if (width[1] > width[0])
243 if (width[2] > width[1])
248 // System.out.println("Maxwidth = " + maxwidth);
251 public float findScale()
253 int dim, width, height;
254 if (getSize().width != 0)
256 width = getSize().width;
257 height = getSize().height;
261 width = prefsize.width;
262 height = prefsize.height;
274 return (float) (dim * scalefactor / (2 * maxwidth));
277 public void findCentre()
279 // Find centre coordinate
282 centre[0] = (max[0] + min[0]) / 2;
283 centre[1] = (max[1] + min[1]) / 2;
284 centre[2] = (max[2] + min[2]) / 2;
286 // System.out.println("Centre x " + centre[0]);
287 // System.out.println("Centre y " + centre[1]);
288 // System.out.println("Centre z " + centre[2]);
291 public Dimension getPreferredSize()
293 if (prefsize != null)
299 return new Dimension(400, 400);
303 public Dimension getMinimumSize()
305 return getPreferredSize();
308 public void update(Graphics g)
313 public void paint(Graphics g)
317 g.setFont(new Font("Verdana", Font.PLAIN, 18));
318 g.drawString("Calculating PCA....", 20, getSize().height / 2);
323 // Only create the image at the beginning -
324 if ((img == null) || (prefsize.width != getSize().width)
325 || (prefsize.height != getSize().height))
327 prefsize.width = getSize().width;
328 prefsize.height = getSize().height;
332 // System.out.println("New scale = " + scale);
333 img = createImage(getSize().width, getSize().height);
334 ig = img.getGraphics();
338 drawBackground(ig, Color.black);
340 if (drawAxes == true)
347 ig.setColor(Color.red);
348 ig.drawString(tooltip, toolx, tooly);
351 g.drawImage(img, 0, 0, this);
355 public void drawAxes(Graphics g)
358 g.setColor(Color.yellow);
359 for (int i = 0; i < 3; i++)
361 g.drawLine(getSize().width / 2, getSize().height / 2,
362 (int) (axes[i][0] * scale * max[0] + getSize().width / 2),
363 (int) (axes[i][1] * scale * max[1] + getSize().height / 2));
367 public void drawBackground(Graphics g, Color col)
370 g.fillRect(0, 0, prefsize.width, prefsize.height);
373 public void drawScene(Graphics g)
375 // boolean darker = false;
377 int halfwidth = getSize().width / 2;
378 int halfheight = getSize().height / 2;
380 for (int i = 0; i < npoint; i++)
382 SequencePoint sp = (SequencePoint) points.elementAt(i);
383 int x = (int) ((float) (sp.coord[0] - centre[0]) * scale) + halfwidth;
384 int y = (int) ((float) (sp.coord[1] - centre[1]) * scale)
386 float z = sp.coord[1] - centre[2];
388 if (av.getSequenceColour(sp.sequence) == Color.black)
390 g.setColor(Color.white);
394 g.setColor(av.getSequenceColour(sp.sequence));
397 if (av.getSelectionGroup() != null)
399 if (av.getSelectionGroup().getSequences(null)
400 .contains(((SequencePoint) points.elementAt(i)).sequence))
402 g.setColor(Color.gray);
407 g.setColor(g.getColor().darker());
410 g.fillRect(x - 3, y - 3, 6, 6);
413 g.setColor(Color.red);
415 ((SequencePoint) points.elementAt(i)).sequence.getName(),
421 public Dimension minimumsize()
426 public Dimension preferredsize()
431 public void keyTyped(KeyEvent evt)
435 public void keyReleased(KeyEvent evt)
439 public void keyPressed(KeyEvent evt)
441 if (evt.getKeyCode() == KeyEvent.VK_UP)
443 scalefactor = (float) (scalefactor * 1.1);
446 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
448 scalefactor = (float) (scalefactor * 0.9);
451 else if (evt.getKeyChar() == 's')
453 System.err.println("DEBUG: Rectangle selection"); // log.debug
454 if (rectx2 != -1 && recty2 != -1)
456 rectSelect(rectx1, recty1, rectx2, recty2);
463 public void printPoints()
465 for (int i = 0; i < npoint; i++)
467 SequencePoint sp = (SequencePoint) points.elementAt(i);
468 Format.print(System.out, "%5d ", i);
469 for (int j = 0; j < 3; j++)
471 Format.print(System.out, "%13.3f ", sp.coord[j]);
473 System.out.println();
477 public void mouseClicked(MouseEvent evt)
481 public void mouseEntered(MouseEvent evt)
485 public void mouseExited(MouseEvent evt)
489 public void mouseReleased(MouseEvent evt)
493 public void mousePressed(MouseEvent evt)
513 SequenceI found = findPoint(x, y);
517 // TODO: applet PCA is not associatable with multi-panels - only parent view
518 if (av.getSelectionGroup() != null)
520 av.getSelectionGroup().addOrRemove(found, true);
521 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
525 av.setSelectionGroup(new SequenceGroup());
526 av.getSelectionGroup().addOrRemove(found, true);
527 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
530 PaintRefresher.Refresh(this, av.getSequenceSetId());
536 public void mouseMoved(MouseEvent evt)
538 SequenceI found = findPoint(evt.getX(), evt.getY());
545 tooltip = found.getName();
552 public void mouseDragged(MouseEvent evt)
557 rotmat.setIdentity();
559 rotmat.rotate((float) (my - omy), 'x');
560 rotmat.rotate((float) (mx - omx), 'y');
562 for (int i = 0; i < npoint; i++)
564 SequencePoint sp = (SequencePoint) points.elementAt(i);
565 sp.coord[0] -= centre[0];
566 sp.coord[1] -= centre[1];
567 sp.coord[2] -= centre[2];
569 // Now apply the rotation matrix
570 sp.coord = rotmat.vectorMultiply(sp.coord);
572 // Now translate back again
573 sp.coord[0] += centre[0];
574 sp.coord[1] += centre[1];
575 sp.coord[2] += centre[2];
578 for (int i = 0; i < 3; i++)
580 axes[i] = rotmat.vectorMultiply(axes[i]);
585 paint(this.getGraphics());
588 public void rectSelect(int x1, int y1, int x2, int y2)
590 // boolean changedSel = false;
591 for (int i = 0; i < npoint; i++)
593 SequencePoint sp = (SequencePoint) points.elementAt(i);
594 int tmp1 = (int) ((sp.coord[0] - centre[0]) * scale + (float) getSize().width / 2.0);
595 int tmp2 = (int) ((sp.coord[1] - centre[1]) * scale + (float) getSize().height / 2.0);
597 if (tmp1 > x1 && tmp1 < x2 && tmp2 > y1 && tmp2 < y2)
601 if (!av.getSelectionGroup().getSequences(null)
602 .contains(sp.sequence))
604 av.getSelectionGroup().addSequence(sp.sequence, true);
611 public SequenceI findPoint(int x, int y)
614 int halfwidth = getSize().width / 2;
615 int halfheight = getSize().height / 2;
619 for (int i = 0; i < npoint; i++)
622 SequencePoint sp = (SequencePoint) points.elementAt(i);
623 int px = (int) ((float) (sp.coord[0] - centre[0]) * scale)
625 int py = (int) ((float) (sp.coord[1] - centre[1]) * scale)
628 if (Math.abs(px - x) < 3 && Math.abs(py - y) < 3)
635 return ((SequencePoint) points.elementAt(found)).sequence;