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.SearchResults;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.renderer.ScaleRenderer;
28 import jalview.renderer.ScaleRenderer.ScaleMark;
29 import jalview.util.ColorUtils;
30 import jalview.viewmodel.AlignmentViewport;
32 import java.awt.Color;
33 import java.awt.FontMetrics;
34 import java.awt.Graphics;
35 import java.awt.Image;
36 import java.awt.Panel;
38 public class SeqCanvas extends Panel
54 SearchResults searchResults = null;
56 boolean fastPaint = false;
62 public SeqCanvas(AlignViewport av)
65 fr = new FeatureRenderer(av);
66 sr = new SequenceRenderer(av);
67 PaintRefresher.Register(this, av.getSequenceSetId());
71 int avcharHeight = 0, avcharWidth = 0;
73 private void updateViewport()
75 avcharHeight = av.getCharHeight();
76 avcharWidth = av.getCharWidth();
79 public AlignmentViewport getViewport()
84 public FeatureRenderer getFeatureRenderer()
89 public SequenceRenderer getSequenceRenderer()
94 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
97 g.setColor(Color.black);
98 for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
101 int mpos = mark.column; // (i - startx - 1)
106 String mstring = mark.text;
112 g.drawString(mstring, mpos * avcharWidth, ypos
113 - (avcharHeight / 2));
115 g.drawLine((mpos * avcharWidth) + (avcharWidth / 2), (ypos + 2)
116 - (avcharHeight / 2), (mpos * avcharWidth)
123 private void drawWestScale(Graphics g, int startx, int endx, int ypos)
125 FontMetrics fm = getFontMetrics(av.getFont());
126 ypos += avcharHeight;
127 if (av.hasHiddenColumns())
129 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
130 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
133 int maxwidth = av.getAlignment().getWidth();
134 if (av.hasHiddenColumns())
136 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
140 for (int i = 0; i < av.getAlignment().getHeight(); i++)
142 SequenceI seq = av.getAlignment().getSequenceAt(i);
148 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
155 value = av.getAlignment().getSequenceAt(i).findPosition(index);
162 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
164 g.drawString(value + "", x, (ypos + (i * avcharHeight))
165 - (avcharHeight / 5));
170 private void drawEastScale(Graphics g, int startx, int endx, int ypos)
172 ypos += avcharHeight;
174 if (av.hasHiddenColumns())
176 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
181 for (int i = 0; i < av.getAlignment().getHeight(); i++)
183 seq = av.getAlignment().getSequenceAt(i);
187 while (index > startx)
189 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
196 value = seq.findPosition(index);
203 g.drawString(String.valueOf(value), 0, (ypos + (i * avcharHeight))
204 - (avcharHeight / 5));
211 void fastPaint(int horizontal, int vertical)
213 if (fastPaint || gg == null)
220 // Its possible on certain browsers that the call to fastpaint
221 // is faster than it can paint, so this check here catches
223 if (lastsr + horizontal != av.startRes)
225 horizontal = av.startRes - lastsr;
228 lastsr = av.startRes;
231 gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
232 - horizontal * avcharWidth,
233 imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
234 -vertical * avcharHeight);
236 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
238 if (horizontal > 0) // scrollbar pulled right, image to the left
240 transX = (er - sr - horizontal) * avcharWidth;
241 sr = er - horizontal;
243 else if (horizontal < 0)
245 er = sr - horizontal;
248 else if (vertical > 0) // scroll down
251 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
257 transY = imgHeight - vertical * avcharHeight;
260 else if (vertical < 0)
269 gg.translate(transX, transY);
271 drawPanel(gg, sr, er, ss, es, 0);
272 gg.translate(-transX, -transY);
279 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
280 * towards! startx is the first residue (starting at 0) to display. endx is
281 * the last residue to display (starting at 0). starty is the first sequence
282 * to display (starting at 0). endy is the last sequence to display (starting
283 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
284 * adjustment listener in SeqPanel when the scrollbars move.
287 public void update(Graphics g)
293 public void paint(Graphics g)
297 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
298 .getClipBounds().height)))
300 g.drawImage(img, 0, 0, this);
307 g.drawImage(img, 0, 0, this);
313 // this draws the whole of the alignment
314 imgWidth = this.getSize().width;
315 imgHeight = this.getSize().height;
317 imgWidth -= imgWidth % avcharWidth;
318 imgHeight -= imgHeight % avcharHeight;
320 if (imgWidth < 1 || imgHeight < 1)
325 if (img == null || imgWidth != img.getWidth(this)
326 || imgHeight != img.getHeight(this))
328 img = createImage(imgWidth, imgHeight);
329 gg = img.getGraphics();
330 gg.setFont(av.getFont());
333 gg.setColor(Color.white);
334 gg.fillRect(0, 0, imgWidth, imgHeight);
336 if (av.getWrapAlignment())
338 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
342 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
345 g.drawImage(img, 0, 0, this);
349 int LABEL_WEST, LABEL_EAST;
351 public int getWrappedCanvasWidth(int cwidth)
353 cwidth -= cwidth % av.getCharWidth();
355 FontMetrics fm = getFontMetrics(av.getFont());
360 if (av.getScaleRightWrapped())
362 LABEL_EAST = fm.stringWidth(getMask());
365 if (av.getScaleLeftWrapped())
367 LABEL_WEST = fm.stringWidth(getMask());
370 return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
374 * Generates a string of zeroes.
383 AlignmentI alignment = av.getAlignment();
384 for (int i = 0; i < alignment.getHeight(); i++)
386 tmp = alignment.getSequenceAt(i).getEnd();
393 for (int i = maxWidth; i > 0; i /= 10)
400 private void drawWrappedPanel(Graphics g, int canvasWidth,
401 int canvasHeight, int startRes)
403 AlignmentI al = av.getAlignment();
405 FontMetrics fm = getFontMetrics(av.getFont());
407 if (av.getScaleRightWrapped())
409 LABEL_EAST = fm.stringWidth(getMask());
412 if (av.getScaleLeftWrapped())
414 LABEL_WEST = fm.stringWidth(getMask());
417 int hgap = avcharHeight;
418 if (av.getScaleAboveWrapped())
420 hgap += avcharHeight;
423 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
424 int cHeight = av.getAlignment().getHeight() * avcharHeight;
426 av.setWrappedWidth(cWidth);
428 av.endRes = av.startRes + cWidth;
433 int maxwidth = av.getAlignment().getWidth() - 1;
435 if (av.hasHiddenColumns())
437 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
440 while ((ypos <= canvasHeight) && (startRes < maxwidth))
442 endx = startRes + cWidth - 1;
449 g.setColor(Color.black);
451 if (av.getScaleLeftWrapped())
453 drawWestScale(g, startRes, endx, ypos);
456 if (av.getScaleRightWrapped())
458 g.translate(canvasWidth - LABEL_EAST, 0);
459 drawEastScale(g, startRes, endx, ypos);
460 g.translate(-(canvasWidth - LABEL_EAST), 0);
463 g.translate(LABEL_WEST, 0);
465 if (av.getScaleAboveWrapped())
467 drawNorthScale(g, startRes, endx, ypos);
469 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
471 g.setColor(Color.blue);
473 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
476 res = av.getColumnSelection().findHiddenRegionPosition(i)
479 if (res < 0 || res > endx - startRes)
484 gg.fillPolygon(new int[] { res * avcharWidth - avcharHeight / 4,
485 res * avcharWidth + avcharHeight / 4, res * avcharWidth },
486 new int[] { ypos - (avcharHeight / 2),
487 ypos - (avcharHeight / 2),
488 ypos - (avcharHeight / 2) + 8 }, 3);
493 if (g.getClip() == null)
495 g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
498 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
501 if (av.isShowAnnotation())
503 g.translate(0, cHeight + ypos + 4);
504 if (annotations == null)
506 annotations = new AnnotationPanel(av);
509 annotations.drawComponent(g, startRes, endx + 1);
510 g.translate(0, -cHeight - ypos - 4);
512 g.translate(-LABEL_WEST, 0);
514 ypos += cHeight + getAnnotationHeight() + hgap;
521 AnnotationPanel annotations;
523 int getAnnotationHeight()
525 if (!av.isShowAnnotation())
530 if (annotations == null)
532 annotations = new AnnotationPanel(av);
535 return annotations.adjustPanelHeight();
538 private void drawPanel(Graphics g1, int startRes, int endRes,
539 int startSeq, int endSeq, int offset)
542 if (!av.hasHiddenColumns())
544 draw(g1, startRes, endRes, startSeq, endSeq, offset);
550 int blockStart = startRes;
551 int blockEnd = endRes;
553 if (av.hasHiddenColumns())
555 for (int[] region : av.getColumnSelection().getHiddenColumns())
557 int hideStart = region[0];
558 int hideEnd = region[1];
560 if (hideStart <= blockStart)
562 blockStart += (hideEnd - hideStart) + 1;
566 blockEnd = hideStart - 1;
568 g1.translate(screenY * avcharWidth, 0);
570 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
572 if (av.getShowHiddenMarkers())
574 g1.setColor(Color.blue);
575 g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
576 0 + offset, (blockEnd - blockStart + 1) * avcharWidth
577 - 1, (endSeq - startSeq) * avcharHeight
581 g1.translate(-screenY * avcharWidth, 0);
582 screenY += blockEnd - blockStart + 1;
583 blockStart = hideEnd + 1;
585 if (screenY > (endRes - startRes))
587 // already rendered last block
592 if (screenY <= (endRes - startRes))
594 // remaining visible region to render
595 blockEnd = blockStart + (endRes - startRes) - screenY;
596 g1.translate(screenY * avcharWidth, 0);
597 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
599 g1.translate(-screenY * avcharWidth, 0);
605 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
606 // int x1, int x2, int y1, int y2, int startx, int starty,
607 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
610 g.setFont(av.getFont());
611 sr.prepare(g, av.isRenderGaps());
615 // / First draw the sequences
616 // ///////////////////////////
617 for (int i = startSeq; i < endSeq; i++)
619 nextSeq = av.getAlignment().getSequenceAt(i);
626 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
627 startRes, endRes, offset + ((i - startSeq) * avcharHeight));
629 if (av.isShowSequenceFeatures())
631 fr.drawSequence(g, nextSeq, startRes, endRes, offset
632 + ((i - startSeq) * avcharHeight));
635 // / Highlight search Results once all sequences have been drawn
636 // ////////////////////////////////////////////////////////
637 if (searchResults != null)
639 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
641 if (visibleResults != null)
643 for (int r = 0; r < visibleResults.length; r += 2)
645 sr.drawHighlightedText(nextSeq, visibleResults[r],
646 visibleResults[r + 1], (visibleResults[r] - startRes)
647 * avcharWidth, offset
648 + ((i - startSeq) * avcharHeight));
653 if (av.cursorMode && cursorY == i && cursorX >= startRes
654 && cursorX <= endRes)
656 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
657 offset + ((i - startSeq) * avcharHeight));
661 if (av.getSelectionGroup() != null
662 || av.getAlignment().getGroups().size() > 0)
664 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
669 private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
670 int startSeq, int endSeq, int offset)
673 // ///////////////////////////////////
674 // Now outline any areas if necessary
675 // ///////////////////////////////////
676 SequenceGroup group = av.getSelectionGroup();
683 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
685 group = av.getAlignment().getGroups().get(0);
695 boolean inGroup = false;
698 int alHeight = av.getAlignment().getHeight() - 1;
700 for (i = startSeq; i < endSeq; i++)
702 sx = (group.getStartRes() - startRes) * avcharWidth;
703 sy = offset + ((i - startSeq) * avcharHeight);
704 ex = (((group.getEndRes() + 1) - group.getStartRes()) * avcharWidth) - 1;
706 if (sx + ex < 0 || sx > imgWidth)
711 if ((sx <= (endRes - startRes) * avcharWidth)
712 && group.getSequences(null).contains(
713 av.getAlignment().getSequenceAt(i)))
716 && (i >= alHeight || !group.getSequences(null)
718 av.getAlignment().getSequenceAt(i + 1))))
720 bottom = sy + avcharHeight;
725 if (((top == -1) && (i == 0))
726 || !group.getSequences(null).contains(
727 av.getAlignment().getSequenceAt(i - 1)))
735 if (group == av.getSelectionGroup())
737 g.setColor(Color.red);
741 g.setColor(ColorUtils.getColor(group.getOutlineColour()));
749 if (sx >= 0 && sx < imgWidth)
751 g.drawLine(sx, oldY, sx, sy);
754 if (sx + ex < imgWidth)
756 g.drawLine(sx + ex, oldY, sx + ex, sy);
765 if (sx + ex > imgWidth)
770 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
772 ex = (endRes - startRes + 1) * avcharWidth;
777 g.drawLine(sx, top, sx + ex, top);
783 g.drawLine(sx, bottom, sx + ex, bottom);
794 sy = offset + ((i - startSeq) * avcharHeight);
795 if (sx >= 0 && sx < imgWidth)
797 g.drawLine(sx, oldY, sx, sy);
800 if (sx + ex < imgWidth)
802 g.drawLine(sx + ex, oldY, sx + ex, sy);
811 if (sx + ex > imgWidth)
815 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
817 ex = (endRes - startRes + 1) * avcharWidth;
822 g.drawLine(sx, top, sx + ex, top);
828 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
837 if (groupIndex >= av.getAlignment().getGroups().size())
842 group = av.getAlignment().getGroups().get(groupIndex);
843 } while (groupIndex < av.getAlignment().getGroups().size());
848 public void highlightSearchResults(SearchResults results)
850 searchResults = results;