2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.appletgui;
24 import java.awt.event.*;
26 import jalview.api.RotatableCanvasI;
27 import jalview.datamodel.*;
28 import jalview.math.*;
29 import jalview.util.*;
31 public class RotatableCanvas extends Panel implements MouseListener,
32 MouseMotionListener, KeyListener, RotatableCanvasI
34 RotatableMatrix idmat = new RotatableMatrix(3, 3);
36 RotatableMatrix objmat = new RotatableMatrix(3, 3);
38 RotatableMatrix rotmat = new RotatableMatrix(3, 3);
44 // RubberbandRectangle rubberband;
46 boolean drawAxes = true;
62 float centre[] = new float[3];
64 float width[] = new float[3];
66 float max[] = new float[3];
68 float min[] = new float[3];
98 float scalefactor = 1;
102 boolean showLabels = false;
104 public RotatableCanvas(AlignViewport av)
109 public void showLabels(boolean b)
115 public void setPoints(Vector points, int npoint)
117 this.points = points;
118 this.npoint = npoint;
119 PaintRefresher.Register(this, av.getSequenceSetId());
121 prefsize = getPreferredSize();
122 orig = new float[npoint][3];
124 for (int i = 0; i < npoint; i++)
126 SequencePoint sp = (SequencePoint) points.elementAt(i);
127 for (int j = 0; j < 3; j++)
129 orig[i][j] = sp.coord[j];
132 // Initialize the matrices to identity
134 for (int i = 0; i < 3; i++)
136 for (int j = 0; j < 3; j++)
140 idmat.addElement(i, j, 0);
141 objmat.addElement(i, j, 0);
142 rotmat.addElement(i, j, 0);
146 idmat.addElement(i, j, 0);
147 objmat.addElement(i, j, 0);
148 rotmat.addElement(i, j, 0);
153 axes = new float[3][3];
161 // System.out.println("Scale factor = " + scale);
163 addMouseListener(this);
164 addKeyListener(this);
165 // if (getParent() != null) {
166 // getParent().addKeyListener(this);
168 addMouseMotionListener(this);
171 // rubberband = new RubberbandRectangle(this);
172 // rubberband.setActive(true);
173 // rubberband.addListener(this);
177 * public boolean handleSequenceSelectionEvent(SequenceSelectionEvent evt) {
178 * redrawneeded = true; repaint(); return true; }
180 * public void removeNotify() { controller.removeListener(this);
181 * super.removeNotify(); }
184 public void initAxes()
186 for (int i = 0; i < 3; i++)
188 for (int j = 0; j < 3; j++)
202 public void findWidth()
207 max[0] = (float) -1e30;
208 max[1] = (float) -1e30;
209 max[2] = (float) -1e30;
211 min[0] = (float) 1e30;
212 min[1] = (float) 1e30;
213 min[2] = (float) 1e30;
215 for (int i = 0; i < 3; i++)
217 for (int j = 0; j < npoint; j++)
219 SequencePoint sp = (SequencePoint) points.elementAt(j);
220 if (sp.coord[i] >= max[i])
222 max[i] = sp.coord[i];
224 if (sp.coord[i] <= min[i])
226 min[i] = sp.coord[i];
231 // System.out.println("xmax " + max[0] + " min " + min[0]);
232 // System.out.println("ymax " + max[1] + " min " + min[1]);
233 // System.out.println("zmax " + max[2] + " min " + min[2]);
235 width[0] = Math.abs(max[0] - min[0]);
236 width[1] = Math.abs(max[1] - min[1]);
237 width[2] = Math.abs(max[2] - min[2]);
241 if (width[1] > width[0])
245 if (width[2] > width[1])
250 // System.out.println("Maxwidth = " + maxwidth);
253 public float findScale()
255 int dim, width, height;
256 if (getSize().width != 0)
258 width = getSize().width;
259 height = getSize().height;
263 width = prefsize.width;
264 height = prefsize.height;
276 return (float) (dim * scalefactor / (2 * maxwidth));
279 public void findCentre()
281 // Find centre coordinate
284 centre[0] = (max[0] + min[0]) / 2;
285 centre[1] = (max[1] + min[1]) / 2;
286 centre[2] = (max[2] + min[2]) / 2;
288 // System.out.println("Centre x " + centre[0]);
289 // System.out.println("Centre y " + centre[1]);
290 // System.out.println("Centre z " + centre[2]);
293 public Dimension getPreferredSize()
295 if (prefsize != null)
301 return new Dimension(400, 400);
305 public Dimension getMinimumSize()
307 return getPreferredSize();
310 public void update(Graphics g)
315 public void paint(Graphics g)
319 g.setFont(new Font("Verdana", Font.PLAIN, 18));
320 g.drawString(MessageManager.getString("label.calculating_pca") + "....", 20, getSize().height / 2);
325 // Only create the image at the beginning -
326 if ((img == null) || (prefsize.width != getSize().width)
327 || (prefsize.height != getSize().height))
329 prefsize.width = getSize().width;
330 prefsize.height = getSize().height;
334 // System.out.println("New scale = " + scale);
335 img = createImage(getSize().width, getSize().height);
336 ig = img.getGraphics();
340 drawBackground(ig, Color.black);
342 if (drawAxes == true)
349 ig.setColor(Color.red);
350 ig.drawString(tooltip, toolx, tooly);
353 g.drawImage(img, 0, 0, this);
357 public void drawAxes(Graphics g)
360 g.setColor(Color.yellow);
361 for (int i = 0; i < 3; i++)
363 g.drawLine(getSize().width / 2, getSize().height / 2,
364 (int) (axes[i][0] * scale * max[0] + getSize().width / 2),
365 (int) (axes[i][1] * scale * max[1] + getSize().height / 2));
369 public void drawBackground(Graphics g, Color col)
372 g.fillRect(0, 0, prefsize.width, prefsize.height);
375 public void drawScene(Graphics g)
377 // boolean darker = false;
379 int halfwidth = getSize().width / 2;
380 int halfheight = getSize().height / 2;
382 for (int i = 0; i < npoint; i++)
384 SequencePoint sp = (SequencePoint) points.elementAt(i);
385 int x = (int) ((float) (sp.coord[0] - centre[0]) * scale) + halfwidth;
386 int y = (int) ((float) (sp.coord[1] - centre[1]) * scale)
388 float z = sp.coord[1] - centre[2];
390 if (av.getSequenceColour(sp.sequence) == Color.black)
392 g.setColor(Color.white);
396 g.setColor(av.getSequenceColour(sp.sequence));
399 if (av.getSelectionGroup() != null)
401 if (av.getSelectionGroup().getSequences(null)
402 .contains(((SequencePoint) points.elementAt(i)).sequence))
404 g.setColor(Color.gray);
409 g.setColor(g.getColor().darker());
412 g.fillRect(x - 3, y - 3, 6, 6);
415 g.setColor(Color.red);
417 ((SequencePoint) points.elementAt(i)).sequence.getName(),
423 public Dimension minimumsize()
428 public Dimension preferredsize()
433 public void keyTyped(KeyEvent evt)
437 public void keyReleased(KeyEvent evt)
441 public void keyPressed(KeyEvent evt)
443 if (evt.getKeyCode() == KeyEvent.VK_UP)
445 scalefactor = (float) (scalefactor * 1.1);
448 else if (evt.getKeyCode() == KeyEvent.VK_DOWN)
450 scalefactor = (float) (scalefactor * 0.9);
453 else if (evt.getKeyChar() == 's')
455 System.err.println("DEBUG: Rectangle selection"); // log.debug
456 if (rectx2 != -1 && recty2 != -1)
458 rectSelect(rectx1, recty1, rectx2, recty2);
465 public void printPoints()
467 for (int i = 0; i < npoint; i++)
469 SequencePoint sp = (SequencePoint) points.elementAt(i);
470 Format.print(System.out, "%5d ", i);
471 for (int j = 0; j < 3; j++)
473 Format.print(System.out, "%13.3f ", sp.coord[j]);
475 System.out.println();
479 public void mouseClicked(MouseEvent evt)
483 public void mouseEntered(MouseEvent evt)
487 public void mouseExited(MouseEvent evt)
491 public void mouseReleased(MouseEvent evt)
495 public void mousePressed(MouseEvent evt)
515 SequenceI found = findPoint(x, y);
519 // TODO: applet PCA is not associatable with multi-panels - only parent
521 if (av.getSelectionGroup() != null)
523 av.getSelectionGroup().addOrRemove(found, true);
524 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
528 av.setSelectionGroup(new SequenceGroup());
529 av.getSelectionGroup().addOrRemove(found, true);
530 av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
533 PaintRefresher.Refresh(this, av.getSequenceSetId());
539 public void mouseMoved(MouseEvent evt)
541 SequenceI found = findPoint(evt.getX(), evt.getY());
548 tooltip = found.getName();
555 public void mouseDragged(MouseEvent evt)
560 rotmat.setIdentity();
562 rotmat.rotate((float) (my - omy), 'x');
563 rotmat.rotate((float) (mx - omx), 'y');
565 for (int i = 0; i < npoint; i++)
567 SequencePoint sp = (SequencePoint) points.elementAt(i);
568 sp.coord[0] -= centre[0];
569 sp.coord[1] -= centre[1];
570 sp.coord[2] -= centre[2];
572 // Now apply the rotation matrix
573 sp.coord = rotmat.vectorMultiply(sp.coord);
575 // Now translate back again
576 sp.coord[0] += centre[0];
577 sp.coord[1] += centre[1];
578 sp.coord[2] += centre[2];
581 for (int i = 0; i < 3; i++)
583 axes[i] = rotmat.vectorMultiply(axes[i]);
588 paint(this.getGraphics());
591 public void rectSelect(int x1, int y1, int x2, int y2)
593 // boolean changedSel = false;
594 for (int i = 0; i < npoint; i++)
596 SequencePoint sp = (SequencePoint) points.elementAt(i);
597 int tmp1 = (int) ((sp.coord[0] - centre[0]) * scale + (float) getSize().width / 2.0);
598 int tmp2 = (int) ((sp.coord[1] - centre[1]) * scale + (float) getSize().height / 2.0);
600 if (tmp1 > x1 && tmp1 < x2 && tmp2 > y1 && tmp2 < y2)
604 if (!av.getSelectionGroup().getSequences(null)
605 .contains(sp.sequence))
607 av.getSelectionGroup().addSequence(sp.sequence, true);
614 public SequenceI findPoint(int x, int y)
617 int halfwidth = getSize().width / 2;
618 int halfheight = getSize().height / 2;
622 for (int i = 0; i < npoint; i++)
625 SequencePoint sp = (SequencePoint) points.elementAt(i);
626 int px = (int) ((float) (sp.coord[0] - centre[0]) * scale)
628 int py = (int) ((float) (sp.coord[1] - centre[1]) * scale)
631 if (Math.abs(px - x) < 3 && Math.abs(py - y) < 3)
638 return ((SequencePoint) points.elementAt(found)).sequence;