2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.
21 package jalview.appletgui;
26 import java.awt.event.*;
28 import jalview.api.RotatableCanvasI;
29 import jalview.datamodel.*;
30 import jalview.math.*;
31 import jalview.util.*;
33 public class RotatableCanvas extends Panel implements MouseListener,
34 MouseMotionListener, KeyListener, RotatableCanvasI
36 RotatableMatrix idmat = new RotatableMatrix(3, 3);
38 RotatableMatrix objmat = new RotatableMatrix(3, 3);
40 RotatableMatrix rotmat = new RotatableMatrix(3, 3);
46 // RubberbandRectangle rubberband;
48 boolean drawAxes = true;
64 float centre[] = new float[3];
66 float width[] = new float[3];
68 float max[] = new float[3];
70 float min[] = new float[3];
100 float scalefactor = 1;
104 boolean showLabels = false;
106 public RotatableCanvas(AlignViewport av)
111 public void showLabels(boolean b)
117 public void setPoints(Vector points, int npoint)
119 this.points = points;
120 this.npoint = npoint;
121 PaintRefresher.Register(this, av.getSequenceSetId());
123 prefsize = getPreferredSize();
124 orig = new float[npoint][3];
126 for (int i = 0; i < npoint; i++)
128 SequencePoint sp = (SequencePoint) points.elementAt(i);
129 for (int j = 0; j < 3; j++)
131 orig[i][j] = sp.coord[j];
134 // Initialize the matrices to identity
136 for (int i = 0; i < 3; i++)
138 for (int j = 0; j < 3; j++)
142 idmat.addElement(i, j, 0);
143 objmat.addElement(i, j, 0);
144 rotmat.addElement(i, j, 0);
148 idmat.addElement(i, j, 0);
149 objmat.addElement(i, j, 0);
150 rotmat.addElement(i, j, 0);
155 axes = new float[3][3];
163 // System.out.println("Scale factor = " + scale);
165 addMouseListener(this);
166 addKeyListener(this);
167 // if (getParent() != null) {
168 // getParent().addKeyListener(this);
170 addMouseMotionListener(this);
173 // rubberband = new RubberbandRectangle(this);
174 // rubberband.setActive(true);
175 // rubberband.addListener(this);
179 * public boolean handleSequenceSelectionEvent(SequenceSelectionEvent evt) {
180 * redrawneeded = true; repaint(); return true; }
182 * public void removeNotify() { controller.removeListener(this);
183 * super.removeNotify(); }
186 public void initAxes()
188 for (int i = 0; i < 3; i++)
190 for (int j = 0; j < 3; j++)
204 public void findWidth()
209 max[0] = (float) -1e30;
210 max[1] = (float) -1e30;
211 max[2] = (float) -1e30;
213 min[0] = (float) 1e30;
214 min[1] = (float) 1e30;
215 min[2] = (float) 1e30;
217 for (int i = 0; i < 3; i++)
219 for (int j = 0; j < npoint; j++)
221 SequencePoint sp = (SequencePoint) points.elementAt(j);
222 if (sp.coord[i] >= max[i])
224 max[i] = sp.coord[i];
226 if (sp.coord[i] <= min[i])
228 min[i] = sp.coord[i];
233 // System.out.println("xmax " + max[0] + " min " + min[0]);
234 // System.out.println("ymax " + max[1] + " min " + min[1]);
235 // System.out.println("zmax " + max[2] + " min " + min[2]);
237 width[0] = Math.abs(max[0] - min[0]);
238 width[1] = Math.abs(max[1] - min[1]);
239 width[2] = Math.abs(max[2] - min[2]);
243 if (width[1] > width[0])
247 if (width[2] > width[1])
252 // System.out.println("Maxwidth = " + maxwidth);
255 public float findScale()
257 int dim, width, height;
258 if (getSize().width != 0)
260 width = getSize().width;
261 height = getSize().height;
265 width = prefsize.width;
266 height = prefsize.height;
278 return (float) (dim * scalefactor / (2 * maxwidth));
281 public void findCentre()
283 // Find centre coordinate
286 centre[0] = (max[0] + min[0]) / 2;
287 centre[1] = (max[1] + min[1]) / 2;
288 centre[2] = (max[2] + min[2]) / 2;
290 // System.out.println("Centre x " + centre[0]);
291 // System.out.println("Centre y " + centre[1]);
292 // System.out.println("Centre z " + centre[2]);
295 public Dimension getPreferredSize()
297 if (prefsize != null)
303 return new Dimension(400, 400);
307 public Dimension getMinimumSize()
309 return getPreferredSize();
312 public void update(Graphics g)
317 public void paint(Graphics g)
321 g.setFont(new Font("Verdana", Font.PLAIN, 18));
322 g.drawString(MessageManager.getString("label.calculating_pca") + "....", 20, getSize().height / 2);
327 // Only create the image at the beginning -
328 if ((img == null) || (prefsize.width != getSize().width)
329 || (prefsize.height != getSize().height))
331 prefsize.width = getSize().width;
332 prefsize.height = getSize().height;
336 // System.out.println("New scale = " + scale);
337 img = createImage(getSize().width, getSize().height);
338 ig = img.getGraphics();
342 drawBackground(ig, Color.black);
344 if (drawAxes == true)
351 ig.setColor(Color.red);
352 ig.drawString(tooltip, toolx, tooly);
355 g.drawImage(img, 0, 0, this);
359 public void drawAxes(Graphics g)
362 g.setColor(Color.yellow);
363 for (int i = 0; i < 3; i++)
365 g.drawLine(getSize().width / 2, getSize().height / 2,
366 (int) (axes[i][0] * scale * max[0] + getSize().width / 2),
367 (int) (axes[i][1] * scale * max[1] + getSize().height / 2));
371 public void drawBackground(Graphics g, Color col)
374 g.fillRect(0, 0, prefsize.width, prefsize.height);
377 public void drawScene(Graphics g)
379 // boolean darker = false;
381 int halfwidth = getSize().width / 2;
382 int halfheight = getSize().height / 2;
384 for (int i = 0; i < npoint; i++)
386 SequencePoint sp = (SequencePoint) points.elementAt(i);
387 int x = (int) ((float) (sp.coord[0] - centre[0]) * scale) + halfwidth;
388 int y = (int) ((float) (sp.coord[1] - centre[1]) * scale)
390 float z = sp.coord[1] - centre[2];
392 if (av.getSequenceColour(sp.sequence) == Color.black)
394 g.setColor(Color.white);
398 g.setColor(av.getSequenceColour(sp.sequence));
401 if (av.getSelectionGroup() != null)
403 if (av.getSelectionGroup().getSequences(null)
404 .contains(((SequencePoint) points.elementAt(i)).sequence))
406 g.setColor(Color.gray);
411 g.setColor(g.getColor().darker());
414 g.fillRect(x - 3, y - 3, 6, 6);
417 g.setColor(Color.red);
419 ((SequencePoint) points.elementAt(i)).sequence.getName(),
425 public Dimension minimumsize()
430 public Dimension preferredsize()
435 public void keyTyped(KeyEvent evt)
439 public void keyReleased(KeyEvent evt)
443 public void keyPressed(KeyEvent evt)
445 if (evt.getKeyCode() == KeyEvent.VK_UP)
447 scalefactor = (float) (scalefactor * 1.1);
450 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
452 scalefactor = (float) (scalefactor * 0.9);
455 else if (evt.getKeyChar() == 's')
457 System.err.println("DEBUG: Rectangle selection"); // log.debug
458 if (rectx2 != -1 && recty2 != -1)
460 rectSelect(rectx1, recty1, rectx2, recty2);
467 public void printPoints()
469 for (int i = 0; i < npoint; i++)
471 SequencePoint sp = (SequencePoint) points.elementAt(i);
472 Format.print(System.out, "%5d ", i);
473 for (int j = 0; j < 3; j++)
475 Format.print(System.out, "%13.3f ", sp.coord[j]);
477 System.out.println();
481 public void mouseClicked(MouseEvent evt)
485 public void mouseEntered(MouseEvent evt)
489 public void mouseExited(MouseEvent evt)
493 public void mouseReleased(MouseEvent evt)
497 public void mousePressed(MouseEvent evt)
517 SequenceI found = findPoint(x, y);
521 // TODO: applet PCA is not associatable with multi-panels - only parent
523 if (av.getSelectionGroup() != null)
525 av.getSelectionGroup().addOrRemove(found, true);
526 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
530 av.setSelectionGroup(new SequenceGroup());
531 av.getSelectionGroup().addOrRemove(found, true);
532 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
535 PaintRefresher.Refresh(this, av.getSequenceSetId());
541 public void mouseMoved(MouseEvent evt)
543 SequenceI found = findPoint(evt.getX(), evt.getY());
550 tooltip = found.getName();
557 public void mouseDragged(MouseEvent evt)
562 rotmat.setIdentity();
564 rotmat.rotate((float) (my - omy), 'x');
565 rotmat.rotate((float) (mx - omx), 'y');
567 for (int i = 0; i < npoint; i++)
569 SequencePoint sp = (SequencePoint) points.elementAt(i);
570 sp.coord[0] -= centre[0];
571 sp.coord[1] -= centre[1];
572 sp.coord[2] -= centre[2];
574 // Now apply the rotation matrix
575 sp.coord = rotmat.vectorMultiply(sp.coord);
577 // Now translate back again
578 sp.coord[0] += centre[0];
579 sp.coord[1] += centre[1];
580 sp.coord[2] += centre[2];
583 for (int i = 0; i < 3; i++)
585 axes[i] = rotmat.vectorMultiply(axes[i]);
590 paint(this.getGraphics());
593 public void rectSelect(int x1, int y1, int x2, int y2)
595 // boolean changedSel = false;
596 for (int i = 0; i < npoint; i++)
598 SequencePoint sp = (SequencePoint) points.elementAt(i);
599 int tmp1 = (int) ((sp.coord[0] - centre[0]) * scale + (float) getSize().width / 2.0);
600 int tmp2 = (int) ((sp.coord[1] - centre[1]) * scale + (float) getSize().height / 2.0);
602 if (tmp1 > x1 && tmp1 < x2 && tmp2 > y1 && tmp2 < y2)
606 if (!av.getSelectionGroup().getSequences(null)
607 .contains(sp.sequence))
609 av.getSelectionGroup().addSequence(sp.sequence, true);
616 public SequenceI findPoint(int x, int y)
619 int halfwidth = getSize().width / 2;
620 int halfheight = getSize().height / 2;
624 for (int i = 0; i < npoint; i++)
627 SequencePoint sp = (SequencePoint) points.elementAt(i);
628 int px = (int) ((float) (sp.coord[0] - centre[0]) * scale)
630 int py = (int) ((float) (sp.coord[1] - centre[1]) * scale)
633 if (Math.abs(px - x) < 3 && Math.abs(py - y) < 3)
640 return ((SequencePoint) points.elementAt(found)).sequence;