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;
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.SearchResults;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
28 import java.awt.Color;
29 import java.awt.FontMetrics;
30 import java.awt.Graphics;
31 import java.awt.Image;
32 import java.awt.Panel;
33 import java.util.List;
35 public class SeqCanvas extends Panel
51 SearchResults searchResults = null;
53 boolean fastPaint = false;
59 public SeqCanvas(AlignViewport av)
62 fr = new FeatureRenderer(av);
63 sr = new SequenceRenderer(av);
64 PaintRefresher.Register(this, av.getSequenceSetId());
67 public AlignViewport getViewport()
72 public FeatureRenderer getFeatureRenderer()
77 public SequenceRenderer getSequenceRenderer()
82 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
84 int scalestartx = startx - startx % 10 + 10;
86 g.setColor(Color.black);
89 for (int i = scalestartx; i < endx; i += 10)
92 if (av.hasHiddenColumns())
94 value = av.getColumnSelection().adjustForHiddenColumns(value);
97 g.drawString(String.valueOf(value), (i - startx - 1) * av.charWidth,
98 ypos - (av.charHeight / 2));
100 g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
101 (ypos + 2) - (av.charHeight / 2),
102 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
107 void drawWestScale(Graphics g, int startx, int endx, int ypos)
109 FontMetrics fm = getFontMetrics(av.getFont());
110 ypos += av.charHeight;
111 if (av.hasHiddenColumns())
113 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
114 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
117 int maxwidth = av.getAlignment().getWidth();
118 if (av.hasHiddenColumns())
120 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
124 for (int i = 0; i < av.getAlignment().getHeight(); i++)
126 SequenceI seq = av.getAlignment().getSequenceAt(i);
132 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
139 value = av.getAlignment().getSequenceAt(i).findPosition(index);
146 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
148 g.drawString(value + "", x, (ypos + (i * av.charHeight))
149 - (av.charHeight / 5));
154 void drawEastScale(Graphics g, int startx, int endx, int ypos)
156 ypos += av.charHeight;
158 if (av.hasHiddenColumns())
160 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
165 for (int i = 0; i < av.getAlignment().getHeight(); i++)
167 seq = av.getAlignment().getSequenceAt(i);
171 while (index > startx)
173 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
180 value = seq.findPosition(index);
187 g.drawString(String.valueOf(value), 0, (ypos + (i * av.charHeight))
188 - (av.charHeight / 5));
195 void fastPaint(int horizontal, int vertical)
197 if (fastPaint || gg == null)
202 // Its possible on certain browsers that the call to fastpaint
203 // is faster than it can paint, so this check here catches
205 if (lastsr + horizontal != av.startRes)
207 horizontal = av.startRes - lastsr;
210 lastsr = av.startRes;
213 gg.copyArea(horizontal * av.charWidth, vertical * av.charHeight,
214 imgWidth - horizontal * av.charWidth, imgHeight - vertical
215 * av.charHeight, -horizontal * av.charWidth, -vertical
218 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
220 if (horizontal > 0) // scrollbar pulled right, image to the left
222 transX = (er - sr - horizontal) * av.charWidth;
223 sr = er - horizontal;
225 else if (horizontal < 0)
227 er = sr - horizontal;
230 else if (vertical > 0) // scroll down
233 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
239 transY = imgHeight - vertical * av.charHeight;
242 else if (vertical < 0)
251 gg.translate(transX, transY);
253 drawPanel(gg, sr, er, ss, es, 0);
254 gg.translate(-transX, -transY);
261 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
262 * towards! startx is the first residue (starting at 0) to display. endx is
263 * the last residue to display (starting at 0). starty is the first sequence
264 * to display (starting at 0). endy is the last sequence to display (starting
265 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
266 * adjustment listener in SeqPanel when the scrollbars move.
268 public void update(Graphics g)
273 public void paint(Graphics g)
277 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
278 .getClipBounds().height)))
280 g.drawImage(img, 0, 0, this);
287 g.drawImage(img, 0, 0, this);
292 // this draws the whole of the alignment
293 imgWidth = this.getSize().width;
294 imgHeight = this.getSize().height;
296 imgWidth -= imgWidth % av.charWidth;
297 imgHeight -= imgHeight % av.charHeight;
299 if (imgWidth < 1 || imgHeight < 1)
304 if (img == null || imgWidth != img.getWidth(this)
305 || imgHeight != img.getHeight(this))
307 img = createImage(imgWidth, imgHeight);
308 gg = img.getGraphics();
309 gg.setFont(av.getFont());
312 gg.setColor(Color.white);
313 gg.fillRect(0, 0, imgWidth, imgHeight);
315 if (av.getWrapAlignment())
317 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
321 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
324 g.drawImage(img, 0, 0, this);
328 int LABEL_WEST, LABEL_EAST;
330 public int getWrappedCanvasWidth(int cwidth)
332 cwidth -= cwidth % av.charWidth;
334 FontMetrics fm = getFontMetrics(av.getFont());
339 if (av.scaleRightWrapped)
341 LABEL_EAST = fm.stringWidth(getMask());
344 if (av.scaleLeftWrapped)
346 LABEL_WEST = fm.stringWidth(getMask());
349 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
353 * Generates a string of zeroes.
362 AlignmentI alignment = av.getAlignment();
363 for (int i = 0; i < alignment.getHeight(); i++)
365 tmp = alignment.getSequenceAt(i).getEnd();
372 for (int i = maxWidth; i > 0; i /= 10)
379 public void drawWrappedPanel(Graphics g, int canvasWidth,
380 int canvasHeight, int startRes)
382 AlignmentI al = av.getAlignment();
384 FontMetrics fm = getFontMetrics(av.getFont());
386 if (av.scaleRightWrapped)
388 LABEL_EAST = fm.stringWidth(getMask());
391 if (av.scaleLeftWrapped)
393 LABEL_WEST = fm.stringWidth(getMask());
396 int hgap = av.charHeight;
397 if (av.scaleAboveWrapped)
399 hgap += av.charHeight;
402 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
403 int cHeight = av.getAlignment().getHeight() * av.charHeight;
405 av.setWrappedWidth(cWidth);
407 av.endRes = av.startRes + cWidth;
412 int maxwidth = av.getAlignment().getWidth() - 1;
414 if (av.hasHiddenColumns())
416 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
419 while ((ypos <= canvasHeight) && (startRes < maxwidth))
421 endx = startRes + cWidth - 1;
428 g.setColor(Color.black);
430 if (av.scaleLeftWrapped)
432 drawWestScale(g, startRes, endx, ypos);
435 if (av.scaleRightWrapped)
437 g.translate(canvasWidth - LABEL_EAST, 0);
438 drawEastScale(g, startRes, endx, ypos);
439 g.translate(-(canvasWidth - LABEL_EAST), 0);
442 g.translate(LABEL_WEST, 0);
444 if (av.scaleAboveWrapped)
446 drawNorthScale(g, startRes, endx, ypos);
448 if (av.hasHiddenColumns() && av.showHiddenMarkers)
450 g.setColor(Color.blue);
452 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
455 res = av.getColumnSelection().findHiddenRegionPosition(i)
458 if (res < 0 || res > endx - startRes)
463 gg.fillPolygon(new int[]
464 { res * av.charWidth - av.charHeight / 4,
465 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
467 { ypos - (av.charHeight / 2), ypos - (av.charHeight / 2),
468 ypos - (av.charHeight / 2) + 8 }, 3);
473 if (g.getClip() == null)
475 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
478 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
481 if (av.showAnnotation)
483 g.translate(0, cHeight + ypos + 4);
484 if (annotations == null)
486 annotations = new AnnotationPanel(av);
489 annotations.drawComponent(g, startRes, endx + 1);
490 g.translate(0, -cHeight - ypos - 4);
492 g.translate(-LABEL_WEST, 0);
494 ypos += cHeight + getAnnotationHeight() + hgap;
501 AnnotationPanel annotations;
503 int getAnnotationHeight()
505 if (!av.showAnnotation)
510 if (annotations == null)
512 annotations = new AnnotationPanel(av);
515 return annotations.adjustPanelHeight();
518 void drawPanel(Graphics g1, int startRes, int endRes, int startSeq,
519 int endSeq, int offset)
521 if (!av.hasHiddenColumns())
523 draw(g1, startRes, endRes, startSeq, endSeq, offset);
527 List<int[]> regions = av.getColumnSelection().getHiddenColumns();
530 int blockStart = startRes;
531 int blockEnd = endRes;
533 for (int[] region : regions)
535 int hideStart = region[0];
536 int hideEnd = region[1];
538 if (hideStart <= blockStart)
540 blockStart += (hideEnd - hideStart) + 1;
544 blockEnd = hideStart - 1;
546 g1.translate(screenY * av.charWidth, 0);
548 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
550 if (av.getShowHiddenMarkers())
552 g1.setColor(Color.blue);
553 g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
554 0 + offset, (blockEnd - blockStart + 1) * av.charWidth
555 - 1, (endSeq - startSeq) * av.charHeight + offset);
558 g1.translate(-screenY * av.charWidth, 0);
559 screenY += blockEnd - blockStart + 1;
560 blockStart = hideEnd + 1;
563 if (screenY <= (endRes - startRes))
565 blockEnd = blockStart + (endRes - startRes) - screenY;
566 g1.translate(screenY * av.charWidth, 0);
567 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
569 g1.translate(-screenY * av.charWidth, 0);
575 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
576 // int x1, int x2, int y1, int y2, int startx, int starty,
577 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
580 g.setFont(av.getFont());
581 sr.prepare(g, av.renderGaps);
585 // / First draw the sequences
586 // ///////////////////////////
587 for (int i = startSeq; i < endSeq; i++)
589 nextSeq = av.getAlignment().getSequenceAt(i);
596 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
597 startRes, endRes, offset + ((i - startSeq) * av.charHeight));
599 if (av.showSequenceFeatures)
601 fr.drawSequence(g, nextSeq, startRes, endRes, offset
602 + ((i - startSeq) * av.charHeight));
605 // / Highlight search Results once all sequences have been drawn
606 // ////////////////////////////////////////////////////////
607 if (searchResults != null)
609 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
611 if (visibleResults != null)
613 for (int r = 0; r < visibleResults.length; r += 2)
615 sr.drawHighlightedText(nextSeq, visibleResults[r],
616 visibleResults[r + 1], (visibleResults[r] - startRes)
617 * av.charWidth, offset
618 + ((i - startSeq) * av.charHeight));
623 if (av.cursorMode && cursorY == i && cursorX >= startRes
624 && cursorX <= endRes)
626 sr.drawCursor(nextSeq, cursorX,
627 (cursorX - startRes) * av.charWidth, offset
628 + ((i - startSeq) * av.charHeight));
632 if (av.getSelectionGroup() != null
633 || av.getAlignment().getGroups().size() > 0)
635 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
640 void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
641 int startSeq, int endSeq, int offset)
644 // ///////////////////////////////////
645 // Now outline any areas if necessary
646 // ///////////////////////////////////
647 SequenceGroup group = av.getSelectionGroup();
654 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
656 group = av.getAlignment().getGroups().get(0);
666 boolean inGroup = false;
669 int alHeight = av.getAlignment().getHeight() - 1;
671 for (i = startSeq; i < endSeq; i++)
673 sx = (group.getStartRes() - startRes) * av.charWidth;
674 sy = offset + ((i - startSeq) * av.charHeight);
675 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
677 if (sx + ex < 0 || sx > imgWidth)
682 if ((sx <= (endRes - startRes) * av.charWidth)
683 && group.getSequences(null).contains(
684 av.getAlignment().getSequenceAt(i)))
687 && (i >= alHeight || !group.getSequences(null)
689 av.getAlignment().getSequenceAt(i + 1))))
691 bottom = sy + av.charHeight;
696 if (((top == -1) && (i == 0))
697 || !group.getSequences(null).contains(
698 av.getAlignment().getSequenceAt(i - 1)))
706 if (group == av.getSelectionGroup())
708 g.setColor(Color.red);
712 g.setColor(group.getOutlineColour());
720 if (sx >= 0 && sx < imgWidth)
722 g.drawLine(sx, oldY, sx, sy);
725 if (sx + ex < imgWidth)
727 g.drawLine(sx + ex, oldY, sx + ex, sy);
736 if (sx + ex > imgWidth)
741 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
743 ex = (endRes - startRes + 1) * av.charWidth;
748 g.drawLine(sx, top, sx + ex, top);
754 g.drawLine(sx, bottom, sx + ex, bottom);
765 sy = offset + ((i - startSeq) * av.charHeight);
766 if (sx >= 0 && sx < imgWidth)
768 g.drawLine(sx, oldY, sx, sy);
771 if (sx + ex < imgWidth)
773 g.drawLine(sx + ex, oldY, sx + ex, sy);
782 if (sx + ex > imgWidth)
786 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
788 ex = (endRes - startRes + 1) * av.charWidth;
793 g.drawLine(sx, top, sx + ex, top);
799 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
808 if (groupIndex >= av.getAlignment().getGroups().size())
813 group = av.getAlignment().getGroups()
815 } while (groupIndex < av.getAlignment().getGroups().size());
820 public void highlightSearchResults(SearchResults results)
822 searchResults = results;