2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.HiddenColumns;
25 import jalview.datamodel.SearchResultsI;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.datamodel.SequenceI;
28 import jalview.renderer.ScaleRenderer;
29 import jalview.renderer.ScaleRenderer.ScaleMark;
30 import jalview.viewmodel.AlignmentViewport;
31 import jalview.viewmodel.ViewportRanges;
33 import java.awt.Color;
34 import java.awt.FontMetrics;
35 import java.awt.Graphics;
36 import java.awt.Image;
37 import java.awt.Panel;
39 public class SeqCanvas extends Panel
55 boolean fastPaint = false;
61 public SeqCanvas(AlignViewport av)
64 fr = new FeatureRenderer(av);
65 sr = new SequenceRenderer(av);
66 PaintRefresher.Register(this, av.getSequenceSetId());
70 int avcharHeight = 0, avcharWidth = 0;
72 private void updateViewport()
74 avcharHeight = av.getCharHeight();
75 avcharWidth = av.getCharWidth();
78 public AlignmentViewport getViewport()
83 public FeatureRenderer getFeatureRenderer()
88 public SequenceRenderer getSequenceRenderer()
93 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
96 g.setColor(Color.black);
97 for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
100 int mpos = mark.column; // (i - startx - 1)
105 String mstring = mark.text;
111 g.drawString(mstring, mpos * avcharWidth, ypos
112 - (avcharHeight / 2));
114 g.drawLine((mpos * avcharWidth) + (avcharWidth / 2), (ypos + 2)
115 - (avcharHeight / 2), (mpos * avcharWidth)
116 + (avcharWidth / 2), ypos - 2);
121 private void drawWestScale(Graphics g, int startx, int endx, int ypos)
123 FontMetrics fm = getFontMetrics(av.getFont());
124 ypos += avcharHeight;
125 if (av.hasHiddenColumns())
127 startx = av.getAlignment().getHiddenColumns()
128 .adjustForHiddenColumns(startx);
129 endx = av.getAlignment().getHiddenColumns()
130 .adjustForHiddenColumns(endx);
133 int maxwidth = av.getAlignment().getWidth();
134 if (av.hasHiddenColumns())
136 maxwidth = av.getAlignment().getHiddenColumns()
137 .findColumnPosition(maxwidth) - 1;
141 for (int i = 0; i < av.getAlignment().getHeight(); i++)
143 SequenceI seq = av.getAlignment().getSequenceAt(i);
149 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
156 value = av.getAlignment().getSequenceAt(i).findPosition(index);
163 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
165 g.drawString(value + "", x, (ypos + (i * avcharHeight))
166 - (avcharHeight / 5));
171 private void drawEastScale(Graphics g, int startx, int endx, int ypos)
173 ypos += avcharHeight;
175 if (av.hasHiddenColumns())
177 endx = av.getAlignment().getHiddenColumns()
178 .adjustForHiddenColumns(endx);
183 for (int i = 0; i < av.getAlignment().getHeight(); i++)
185 seq = av.getAlignment().getSequenceAt(i);
189 while (index > startx)
191 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
198 value = seq.findPosition(index);
205 g.drawString(String.valueOf(value), 0, (ypos + (i * avcharHeight))
206 - (avcharHeight / 5));
213 void fastPaint(int horizontal, int vertical)
215 if (fastPaint || gg == null)
220 ViewportRanges ranges = av.getRanges();
224 // Its possible on certain browsers that the call to fastpaint
225 // is faster than it can paint, so this check here catches
227 if (lastsr + horizontal != ranges.getStartRes())
229 horizontal = ranges.getStartRes() - lastsr;
232 lastsr = ranges.getStartRes();
235 gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
236 - horizontal * avcharWidth,
237 imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
238 -vertical * avcharHeight);
240 int sr = ranges.getStartRes(), er = ranges.getEndRes(), ss = ranges
241 .getStartSeq(), es = ranges
242 .getEndSeq(), transX = 0, transY = 0;
244 if (horizontal > 0) // scrollbar pulled right, image to the left
246 transX = (er - sr - horizontal) * avcharWidth;
247 sr = er - horizontal;
249 else if (horizontal < 0)
251 er = sr - horizontal;
254 else if (vertical > 0) // scroll down
257 if (ss < ranges.getStartSeq()) // ie scrolling too fast, more than a page
261 ss = ranges.getStartSeq();
265 transY = imgHeight - ((vertical + 1) * avcharHeight);
268 else if (vertical < 0)
271 if (es > ranges.getEndSeq())
273 es = ranges.getEndSeq();
277 gg.translate(transX, transY);
279 drawPanel(gg, sr, er, ss, es, 0);
280 gg.translate(-transX, -transY);
287 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
288 * towards! startx is the first residue (starting at 0) to display. endx is
289 * the last residue to display (starting at 0). starty is the first sequence
290 * to display (starting at 0). endy is the last sequence to display (starting
291 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
292 * adjustment listener in SeqPanel when the scrollbars move.
295 public void update(Graphics g)
301 public void paint(Graphics g)
305 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
306 .getClipBounds().height)))
308 g.drawImage(img, 0, 0, this);
315 g.drawImage(img, 0, 0, this);
321 // this draws the whole of the alignment
322 imgWidth = this.getSize().width;
323 imgHeight = this.getSize().height;
325 imgWidth -= imgWidth % avcharWidth;
326 imgHeight -= imgHeight % avcharHeight;
328 if (imgWidth < 1 || imgHeight < 1)
333 if (img == null || imgWidth != img.getWidth(this)
334 || imgHeight != img.getHeight(this))
336 img = createImage(imgWidth, imgHeight);
337 gg = img.getGraphics();
338 gg.setFont(av.getFont());
341 gg.setColor(Color.white);
342 gg.fillRect(0, 0, imgWidth, imgHeight);
344 ViewportRanges ranges = av.getRanges();
346 if (av.getWrapAlignment())
348 drawWrappedPanel(gg, imgWidth, imgHeight, ranges.getStartRes());
352 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
353 ranges.getStartSeq(), ranges.getEndSeq(), 0);
356 g.drawImage(img, 0, 0, this);
360 int LABEL_WEST, LABEL_EAST;
362 public int getWrappedCanvasWidth(int cwidth)
364 cwidth -= cwidth % av.getCharWidth();
366 FontMetrics fm = getFontMetrics(av.getFont());
371 if (av.getScaleRightWrapped())
373 LABEL_EAST = fm.stringWidth(getMask());
376 if (av.getScaleLeftWrapped())
378 LABEL_WEST = fm.stringWidth(getMask());
381 return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
385 * Generates a string of zeroes.
394 AlignmentI alignment = av.getAlignment();
395 for (int i = 0; i < alignment.getHeight(); i++)
397 tmp = alignment.getSequenceAt(i).getEnd();
404 for (int i = maxWidth; i > 0; i /= 10)
411 private void drawWrappedPanel(Graphics g, int canvasWidth,
412 int canvasHeight, int startRes)
414 AlignmentI al = av.getAlignment();
416 FontMetrics fm = getFontMetrics(av.getFont());
418 if (av.getScaleRightWrapped())
420 LABEL_EAST = fm.stringWidth(getMask());
423 if (av.getScaleLeftWrapped())
425 LABEL_WEST = fm.stringWidth(getMask());
428 int hgap = avcharHeight;
429 if (av.getScaleAboveWrapped())
431 hgap += avcharHeight;
434 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
435 int cHeight = av.getAlignment().getHeight() * avcharHeight;
437 av.setWrappedWidth(cWidth);
439 av.getRanges().setEndRes(av.getRanges().getStartRes() + cWidth);
444 int maxwidth = av.getAlignment().getWidth() - 1;
446 if (av.hasHiddenColumns())
448 maxwidth = av.getAlignment().getHiddenColumns()
449 .findColumnPosition(maxwidth) - 1;
452 while ((ypos <= canvasHeight) && (startRes < maxwidth))
454 endx = startRes + cWidth - 1;
461 g.setColor(Color.black);
463 if (av.getScaleLeftWrapped())
465 drawWestScale(g, startRes, endx, ypos);
468 if (av.getScaleRightWrapped())
470 g.translate(canvasWidth - LABEL_EAST, 0);
471 drawEastScale(g, startRes, endx, ypos);
472 g.translate(-(canvasWidth - LABEL_EAST), 0);
475 g.translate(LABEL_WEST, 0);
477 if (av.getScaleAboveWrapped())
479 drawNorthScale(g, startRes, endx, ypos);
481 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
483 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
484 g.setColor(Color.blue);
486 for (int i = 0; i < hidden.getListOfCols()
489 res = hidden.findHiddenRegionPosition(i)
492 if (res < 0 || res > endx - startRes)
497 gg.fillPolygon(new int[] { res * avcharWidth - avcharHeight / 4,
498 res * avcharWidth + avcharHeight / 4, res * avcharWidth },
499 new int[] { ypos - (avcharHeight / 2),
500 ypos - (avcharHeight / 2),
501 ypos - (avcharHeight / 2) + 8 }, 3);
506 if (g.getClip() == null)
508 g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
511 drawPanel(g, startRes, endx, 0, al.getHeight() - 1, ypos);
514 if (av.isShowAnnotation())
516 g.translate(0, cHeight + ypos + 4);
517 if (annotations == null)
519 annotations = new AnnotationPanel(av);
522 annotations.drawComponent(g, startRes, endx + 1);
523 g.translate(0, -cHeight - ypos - 4);
525 g.translate(-LABEL_WEST, 0);
527 ypos += cHeight + getAnnotationHeight() + hgap;
534 AnnotationPanel annotations;
536 int getAnnotationHeight()
538 if (!av.isShowAnnotation())
543 if (annotations == null)
545 annotations = new AnnotationPanel(av);
548 return annotations.adjustPanelHeight();
551 private void drawPanel(Graphics g1, int startRes, int endRes,
552 int startSeq, int endSeq, int offset)
555 if (!av.hasHiddenColumns())
557 draw(g1, startRes, endRes, startSeq, endSeq, offset);
563 int blockStart = startRes;
564 int blockEnd = endRes;
566 if (av.hasHiddenColumns())
568 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
569 for (int[] region : hidden.getListOfCols())
571 int hideStart = region[0];
572 int hideEnd = region[1];
574 if (hideStart <= blockStart)
576 blockStart += (hideEnd - hideStart) + 1;
580 blockEnd = hideStart - 1;
582 g1.translate(screenY * avcharWidth, 0);
584 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
586 if (av.getShowHiddenMarkers())
588 g1.setColor(Color.blue);
589 g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
590 0 + offset, (blockEnd - blockStart + 1) * avcharWidth
591 - 1, (endSeq - startSeq + 1) * avcharHeight
595 g1.translate(-screenY * avcharWidth, 0);
596 screenY += blockEnd - blockStart + 1;
597 blockStart = hideEnd + 1;
599 if (screenY > (endRes - startRes))
601 // already rendered last block
606 if (screenY <= (endRes - startRes))
608 // remaining visible region to render
609 blockEnd = blockStart + (endRes - startRes) - screenY;
610 g1.translate(screenY * avcharWidth, 0);
611 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
613 g1.translate(-screenY * avcharWidth, 0);
619 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
620 // int x1, int x2, int y1, int y2, int startx, int starty,
621 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
624 g.setFont(av.getFont());
625 sr.prepare(g, av.isRenderGaps());
629 // / First draw the sequences
630 // ///////////////////////////
631 for (int i = startSeq; i <= endSeq; i++)
633 nextSeq = av.getAlignment().getSequenceAt(i);
640 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
641 startRes, endRes, offset + ((i - startSeq) * avcharHeight));
643 if (av.isShowSequenceFeatures())
645 fr.drawSequence(g, nextSeq, startRes, endRes, offset
646 + ((i - startSeq) * avcharHeight), false);
649 // / Highlight search Results once all sequences have been drawn
650 // ////////////////////////////////////////////////////////
651 if (av.hasSearchResults())
653 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
656 if (visibleResults != null)
658 for (int r = 0; r < visibleResults.length; r += 2)
660 sr.drawHighlightedText(nextSeq, visibleResults[r],
661 visibleResults[r + 1], (visibleResults[r] - startRes)
662 * avcharWidth, offset
663 + ((i - startSeq) * avcharHeight));
668 if (av.cursorMode && cursorY == i && cursorX >= startRes
669 && cursorX <= endRes)
671 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
672 offset + ((i - startSeq) * avcharHeight));
676 if (av.getSelectionGroup() != null
677 || av.getAlignment().getGroups().size() > 0)
679 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
684 private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
685 int startSeq, int endSeq, int offset)
688 // ///////////////////////////////////
689 // Now outline any areas if necessary
690 // ///////////////////////////////////
691 SequenceGroup group = av.getSelectionGroup();
698 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
700 group = av.getAlignment().getGroups().get(0);
710 boolean inGroup = false;
713 int alHeight = av.getAlignment().getHeight() - 1;
715 for (i = startSeq; i <= endSeq; i++)
717 sx = (group.getStartRes() - startRes) * avcharWidth;
718 sy = offset + ((i - startSeq) * avcharHeight);
719 ex = (((group.getEndRes() + 1) - group.getStartRes()) * avcharWidth) - 1;
721 if (sx + ex < 0 || sx > imgWidth)
726 if ((sx <= (endRes - startRes) * avcharWidth)
727 && group.getSequences(null).contains(
728 av.getAlignment().getSequenceAt(i)))
731 && (i >= alHeight || !group.getSequences(null)
733 av.getAlignment().getSequenceAt(i + 1))))
735 bottom = sy + avcharHeight;
740 if (((top == -1) && (i == 0))
741 || !group.getSequences(null).contains(
742 av.getAlignment().getSequenceAt(i - 1)))
750 if (group == av.getSelectionGroup())
752 g.setColor(Color.red);
756 g.setColor(group.getOutlineColour());
764 if (sx >= 0 && sx < imgWidth)
766 g.drawLine(sx, oldY, sx, sy);
769 if (sx + ex < imgWidth)
771 g.drawLine(sx + ex, oldY, sx + ex, sy);
780 if (sx + ex > imgWidth)
785 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
787 ex = (endRes - startRes + 1) * avcharWidth;
792 g.drawLine(sx, top, sx + ex, top);
798 g.drawLine(sx, bottom, sx + ex, bottom);
809 sy = offset + ((i - startSeq) * avcharHeight);
810 if (sx >= 0 && sx < imgWidth)
812 g.drawLine(sx, oldY, sx, sy);
815 if (sx + ex < imgWidth)
817 g.drawLine(sx + ex, oldY, sx + ex, sy);
826 if (sx + ex > imgWidth)
830 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
832 ex = (endRes - startRes + 1) * avcharWidth;
837 g.drawLine(sx, top, sx + ex, top);
843 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
852 if (groupIndex >= av.getAlignment().getGroups().size())
857 group = av.getAlignment().getGroups().get(groupIndex);
858 } while (groupIndex < av.getAlignment().getGroups().size());
863 public void highlightSearchResults(SearchResultsI results)
865 av.setSearchResults(results);