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;
34 public class SeqCanvas extends Panel
50 SearchResults searchResults = null;
52 boolean fastPaint = false;
58 public SeqCanvas(AlignViewport av)
61 fr = new FeatureRenderer(av);
62 sr = new SequenceRenderer(av);
63 PaintRefresher.Register(this, av.getSequenceSetId());
67 int avcharHeight = 0, avcharWidth = 0;
69 private void updateViewport()
71 avcharHeight = av.getCharHeight();
72 avcharWidth = av.getCharWidth();
75 public AlignViewport getViewport()
80 public FeatureRenderer getFeatureRenderer()
85 public SequenceRenderer getSequenceRenderer()
90 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
92 int scalestartx = startx - startx % 10 + 10;
94 g.setColor(Color.black);
97 for (int i = scalestartx; i < endx; i += 10)
100 if (av.hasHiddenColumns())
102 value = av.getColumnSelection().adjustForHiddenColumns(value);
105 g.drawString(String.valueOf(value), (i - startx - 1) * avcharWidth,
106 ypos - (avcharHeight / 2));
108 g.drawLine(((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
109 (ypos + 2) - (avcharHeight / 2),
110 ((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
115 private void drawWestScale(Graphics g, int startx, int endx, int ypos)
117 FontMetrics fm = getFontMetrics(av.getFont());
118 ypos += avcharHeight;
119 if (av.hasHiddenColumns())
121 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
122 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
125 int maxwidth = av.getAlignment().getWidth();
126 if (av.hasHiddenColumns())
128 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
132 for (int i = 0; i < av.getAlignment().getHeight(); i++)
134 SequenceI seq = av.getAlignment().getSequenceAt(i);
140 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
147 value = av.getAlignment().getSequenceAt(i).findPosition(index);
154 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
156 g.drawString(value + "", x, (ypos + (i * avcharHeight))
157 - (avcharHeight / 5));
162 private void drawEastScale(Graphics g, int startx, int endx, int ypos)
164 ypos += avcharHeight;
166 if (av.hasHiddenColumns())
168 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
173 for (int i = 0; i < av.getAlignment().getHeight(); i++)
175 seq = av.getAlignment().getSequenceAt(i);
179 while (index > startx)
181 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
188 value = seq.findPosition(index);
195 g.drawString(String.valueOf(value), 0, (ypos + (i * avcharHeight))
196 - (avcharHeight / 5));
203 void fastPaint(int horizontal, int vertical)
205 if (fastPaint || gg == null)
212 // Its possible on certain browsers that the call to fastpaint
213 // is faster than it can paint, so this check here catches
215 if (lastsr + horizontal != av.startRes)
217 horizontal = av.startRes - lastsr;
220 lastsr = av.startRes;
223 gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
224 - horizontal * avcharWidth,
225 imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
226 -vertical * avcharHeight);
228 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
230 if (horizontal > 0) // scrollbar pulled right, image to the left
232 transX = (er - sr - horizontal) * avcharWidth;
233 sr = er - horizontal;
235 else if (horizontal < 0)
237 er = sr - horizontal;
240 else if (vertical > 0) // scroll down
243 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
249 transY = imgHeight - vertical * avcharHeight;
252 else if (vertical < 0)
261 gg.translate(transX, transY);
263 drawPanel(gg, sr, er, ss, es, 0);
264 gg.translate(-transX, -transY);
271 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
272 * towards! startx is the first residue (starting at 0) to display. endx is
273 * the last residue to display (starting at 0). starty is the first sequence
274 * to display (starting at 0). endy is the last sequence to display (starting
275 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
276 * adjustment listener in SeqPanel when the scrollbars move.
278 public void update(Graphics g)
283 public void paint(Graphics g)
287 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
288 .getClipBounds().height)))
290 g.drawImage(img, 0, 0, this);
297 g.drawImage(img, 0, 0, this);
303 // this draws the whole of the alignment
304 imgWidth = this.getSize().width;
305 imgHeight = this.getSize().height;
307 imgWidth -= imgWidth % avcharWidth;
308 imgHeight -= imgHeight % avcharHeight;
310 if (imgWidth < 1 || imgHeight < 1)
315 if (img == null || imgWidth != img.getWidth(this)
316 || imgHeight != img.getHeight(this))
318 img = createImage(imgWidth, imgHeight);
319 gg = img.getGraphics();
320 gg.setFont(av.getFont());
323 gg.setColor(Color.white);
324 gg.fillRect(0, 0, imgWidth, imgHeight);
326 if (av.getWrapAlignment())
328 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
332 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
335 g.drawImage(img, 0, 0, this);
339 int LABEL_WEST, LABEL_EAST;
341 public int getWrappedCanvasWidth(int cwidth)
343 cwidth -= cwidth % av.getCharWidth();
345 FontMetrics fm = getFontMetrics(av.getFont());
350 if (av.getScaleRightWrapped())
352 LABEL_EAST = fm.stringWidth(getMask());
355 if (av.getScaleLeftWrapped())
357 LABEL_WEST = fm.stringWidth(getMask());
360 return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
364 * Generates a string of zeroes.
373 AlignmentI alignment = av.getAlignment();
374 for (int i = 0; i < alignment.getHeight(); i++)
376 tmp = alignment.getSequenceAt(i).getEnd();
383 for (int i = maxWidth; i > 0; i /= 10)
390 private void drawWrappedPanel(Graphics g, int canvasWidth,
391 int canvasHeight, int startRes)
393 AlignmentI al = av.getAlignment();
395 FontMetrics fm = getFontMetrics(av.getFont());
397 if (av.getScaleRightWrapped())
399 LABEL_EAST = fm.stringWidth(getMask());
402 if (av.getScaleLeftWrapped())
404 LABEL_WEST = fm.stringWidth(getMask());
407 int hgap = avcharHeight;
408 if (av.getScaleAboveWrapped())
410 hgap += avcharHeight;
413 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
414 int cHeight = av.getAlignment().getHeight() * avcharHeight;
416 av.setWrappedWidth(cWidth);
418 av.endRes = av.startRes + cWidth;
423 int maxwidth = av.getAlignment().getWidth() - 1;
425 if (av.hasHiddenColumns())
427 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
430 while ((ypos <= canvasHeight) && (startRes < maxwidth))
432 endx = startRes + cWidth - 1;
439 g.setColor(Color.black);
441 if (av.getScaleLeftWrapped())
443 drawWestScale(g, startRes, endx, ypos);
446 if (av.getScaleRightWrapped())
448 g.translate(canvasWidth - LABEL_EAST, 0);
449 drawEastScale(g, startRes, endx, ypos);
450 g.translate(-(canvasWidth - LABEL_EAST), 0);
453 g.translate(LABEL_WEST, 0);
455 if (av.getScaleAboveWrapped())
457 drawNorthScale(g, startRes, endx, ypos);
459 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
461 g.setColor(Color.blue);
463 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
466 res = av.getColumnSelection().findHiddenRegionPosition(i)
469 if (res < 0 || res > endx - startRes)
474 gg.fillPolygon(new int[]
475 { res * avcharWidth - avcharHeight / 4,
476 res * avcharWidth + avcharHeight / 4, res * avcharWidth },
478 { ypos - (avcharHeight / 2), ypos - (avcharHeight / 2),
479 ypos - (avcharHeight / 2) + 8 }, 3);
484 if (g.getClip() == null)
486 g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
489 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
492 if (av.isShowAnnotation())
494 g.translate(0, cHeight + ypos + 4);
495 if (annotations == null)
497 annotations = new AnnotationPanel(av);
500 annotations.drawComponent(g, startRes, endx + 1);
501 g.translate(0, -cHeight - ypos - 4);
503 g.translate(-LABEL_WEST, 0);
505 ypos += cHeight + getAnnotationHeight() + hgap;
512 AnnotationPanel annotations;
514 int getAnnotationHeight()
516 if (!av.isShowAnnotation())
521 if (annotations == null)
523 annotations = new AnnotationPanel(av);
526 return annotations.adjustPanelHeight();
529 private void drawPanel(Graphics g1, int startRes, int endRes,
531 int endSeq, int offset)
535 if (!av.hasHiddenColumns())
537 draw(g1, startRes, endRes, startSeq, endSeq, offset);
543 int blockStart = startRes;
544 int blockEnd = endRes;
546 if (av.hasHiddenColumns())
548 for (int[] region : av.getColumnSelection().getHiddenColumns())
550 int hideStart = region[0];
551 int hideEnd = region[1];
553 if (hideStart <= blockStart)
555 blockStart += (hideEnd - hideStart) + 1;
559 blockEnd = hideStart - 1;
561 g1.translate(screenY * avcharWidth, 0);
563 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
565 if (av.getShowHiddenMarkers())
567 g1.setColor(Color.blue);
568 g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
569 0 + offset, (blockEnd - blockStart + 1) * avcharWidth
570 - 1, (endSeq - startSeq) * avcharHeight
574 g1.translate(-screenY * avcharWidth, 0);
575 screenY += blockEnd - blockStart + 1;
576 blockStart = hideEnd + 1;
579 if (screenY <= (endRes - startRes))
581 blockEnd = blockStart + (endRes - startRes) - screenY;
582 g1.translate(screenY * avcharWidth, 0);
583 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
585 g1.translate(-screenY * avcharWidth, 0);
591 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
592 // int x1, int x2, int y1, int y2, int startx, int starty,
593 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
596 g.setFont(av.getFont());
597 sr.prepare(g, av.isRenderGaps());
601 // / First draw the sequences
602 // ///////////////////////////
603 for (int i = startSeq; i < endSeq; i++)
605 nextSeq = av.getAlignment().getSequenceAt(i);
612 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
613 startRes, endRes, offset + ((i - startSeq) * avcharHeight));
615 if (av.isShowSequenceFeatures())
617 fr.drawSequence(g, nextSeq, startRes, endRes, offset
618 + ((i - startSeq) * avcharHeight));
621 // / Highlight search Results once all sequences have been drawn
622 // ////////////////////////////////////////////////////////
623 if (searchResults != null)
625 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
627 if (visibleResults != null)
629 for (int r = 0; r < visibleResults.length; r += 2)
631 sr.drawHighlightedText(nextSeq, visibleResults[r],
632 visibleResults[r + 1], (visibleResults[r] - startRes)
633 * avcharWidth, offset
634 + ((i - startSeq) * avcharHeight));
639 if (av.cursorMode && cursorY == i && cursorX >= startRes
640 && cursorX <= endRes)
642 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
643 offset + ((i - startSeq) * avcharHeight));
647 if (av.getSelectionGroup() != null
648 || av.getAlignment().getGroups().size() > 0)
650 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
655 private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
656 int startSeq, int endSeq, int offset)
659 // ///////////////////////////////////
660 // Now outline any areas if necessary
661 // ///////////////////////////////////
662 SequenceGroup group = av.getSelectionGroup();
669 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
671 group = av.getAlignment().getGroups().get(0);
681 boolean inGroup = false;
684 int alHeight = av.getAlignment().getHeight() - 1;
686 for (i = startSeq; i < endSeq; i++)
688 sx = (group.getStartRes() - startRes) * avcharWidth;
689 sy = offset + ((i - startSeq) * avcharHeight);
690 ex = (((group.getEndRes() + 1) - group.getStartRes()) * avcharWidth) - 1;
692 if (sx + ex < 0 || sx > imgWidth)
697 if ((sx <= (endRes - startRes) * avcharWidth)
698 && group.getSequences(null).contains(
699 av.getAlignment().getSequenceAt(i)))
702 && (i >= alHeight || !group.getSequences(null)
704 av.getAlignment().getSequenceAt(i + 1))))
706 bottom = sy + avcharHeight;
711 if (((top == -1) && (i == 0))
712 || !group.getSequences(null).contains(
713 av.getAlignment().getSequenceAt(i - 1)))
721 if (group == av.getSelectionGroup())
723 g.setColor(Color.red);
727 g.setColor(group.getOutlineColour());
735 if (sx >= 0 && sx < imgWidth)
737 g.drawLine(sx, oldY, sx, sy);
740 if (sx + ex < imgWidth)
742 g.drawLine(sx + ex, oldY, sx + ex, sy);
751 if (sx + ex > imgWidth)
756 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
758 ex = (endRes - startRes + 1) * avcharWidth;
763 g.drawLine(sx, top, sx + ex, top);
769 g.drawLine(sx, bottom, sx + ex, bottom);
780 sy = offset + ((i - startSeq) * avcharHeight);
781 if (sx >= 0 && sx < imgWidth)
783 g.drawLine(sx, oldY, sx, sy);
786 if (sx + ex < imgWidth)
788 g.drawLine(sx + ex, oldY, sx + ex, sy);
797 if (sx + ex > imgWidth)
801 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
803 ex = (endRes - startRes + 1) * avcharWidth;
808 g.drawLine(sx, top, sx + ex, top);
814 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
823 if (groupIndex >= av.getAlignment().getGroups().size())
828 group = av.getAlignment().getGroups()
830 } while (groupIndex < av.getAlignment().getGroups().size());
835 public void highlightSearchResults(SearchResults results)
837 searchResults = results;