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.viewmodel.AlignmentViewport;
31 import java.awt.Color;
32 import java.awt.FontMetrics;
33 import java.awt.Graphics;
34 import java.awt.Image;
35 import java.awt.Panel;
37 public class SeqCanvas extends Panel
53 SearchResults searchResults = null;
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.getColumnSelection().adjustForHiddenColumns(startx);
128 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
131 int maxwidth = av.getAlignment().getWidth();
132 if (av.hasHiddenColumns())
134 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
138 for (int i = 0; i < av.getAlignment().getHeight(); i++)
140 SequenceI seq = av.getAlignment().getSequenceAt(i);
146 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
153 value = av.getAlignment().getSequenceAt(i).findPosition(index);
160 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
162 g.drawString(value + "", x, (ypos + (i * avcharHeight))
163 - (avcharHeight / 5));
168 private void drawEastScale(Graphics g, int startx, int endx, int ypos)
170 ypos += avcharHeight;
172 if (av.hasHiddenColumns())
174 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
179 for (int i = 0; i < av.getAlignment().getHeight(); i++)
181 seq = av.getAlignment().getSequenceAt(i);
185 while (index > startx)
187 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
194 value = seq.findPosition(index);
201 g.drawString(String.valueOf(value), 0, (ypos + (i * avcharHeight))
202 - (avcharHeight / 5));
209 void fastPaint(int horizontal, int vertical)
211 if (fastPaint || gg == null)
218 // Its possible on certain browsers that the call to fastpaint
219 // is faster than it can paint, so this check here catches
221 if (lastsr + horizontal != av.startRes)
223 horizontal = av.startRes - lastsr;
226 lastsr = av.startRes;
229 gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
230 - horizontal * avcharWidth,
231 imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
232 -vertical * avcharHeight);
234 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
236 if (horizontal > 0) // scrollbar pulled right, image to the left
238 transX = (er - sr - horizontal) * avcharWidth;
239 sr = er - horizontal;
241 else if (horizontal < 0)
243 er = sr - horizontal;
246 else if (vertical > 0) // scroll down
249 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
255 transY = imgHeight - vertical * avcharHeight;
258 else if (vertical < 0)
267 gg.translate(transX, transY);
269 drawPanel(gg, sr, er, ss, es, 0);
270 gg.translate(-transX, -transY);
277 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
278 * towards! startx is the first residue (starting at 0) to display. endx is
279 * the last residue to display (starting at 0). starty is the first sequence
280 * to display (starting at 0). endy is the last sequence to display (starting
281 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
282 * adjustment listener in SeqPanel when the scrollbars move.
285 public void update(Graphics g)
291 public void paint(Graphics g)
295 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
296 .getClipBounds().height)))
298 g.drawImage(img, 0, 0, this);
305 g.drawImage(img, 0, 0, this);
311 // this draws the whole of the alignment
312 imgWidth = this.getSize().width;
313 imgHeight = this.getSize().height;
315 imgWidth -= imgWidth % avcharWidth;
316 imgHeight -= imgHeight % avcharHeight;
318 if (imgWidth < 1 || imgHeight < 1)
323 if (img == null || imgWidth != img.getWidth(this)
324 || imgHeight != img.getHeight(this))
326 img = createImage(imgWidth, imgHeight);
327 gg = img.getGraphics();
328 gg.setFont(av.getFont());
331 gg.setColor(Color.white);
332 gg.fillRect(0, 0, imgWidth, imgHeight);
334 if (av.getWrapAlignment())
336 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
340 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
343 g.drawImage(img, 0, 0, this);
347 int LABEL_WEST, LABEL_EAST;
349 public int getWrappedCanvasWidth(int cwidth)
351 cwidth -= cwidth % av.getCharWidth();
353 FontMetrics fm = getFontMetrics(av.getFont());
358 if (av.getScaleRightWrapped())
360 LABEL_EAST = fm.stringWidth(getMask());
363 if (av.getScaleLeftWrapped())
365 LABEL_WEST = fm.stringWidth(getMask());
368 return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
372 * Generates a string of zeroes.
381 AlignmentI alignment = av.getAlignment();
382 for (int i = 0; i < alignment.getHeight(); i++)
384 tmp = alignment.getSequenceAt(i).getEnd();
391 for (int i = maxWidth; i > 0; i /= 10)
398 private void drawWrappedPanel(Graphics g, int canvasWidth,
399 int canvasHeight, int startRes)
401 AlignmentI al = av.getAlignment();
403 FontMetrics fm = getFontMetrics(av.getFont());
405 if (av.getScaleRightWrapped())
407 LABEL_EAST = fm.stringWidth(getMask());
410 if (av.getScaleLeftWrapped())
412 LABEL_WEST = fm.stringWidth(getMask());
415 int hgap = avcharHeight;
416 if (av.getScaleAboveWrapped())
418 hgap += avcharHeight;
421 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
422 int cHeight = av.getAlignment().getHeight() * avcharHeight;
424 av.setWrappedWidth(cWidth);
426 av.endRes = av.startRes + cWidth;
431 int maxwidth = av.getAlignment().getWidth() - 1;
433 if (av.hasHiddenColumns())
435 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
438 while ((ypos <= canvasHeight) && (startRes < maxwidth))
440 endx = startRes + cWidth - 1;
447 g.setColor(Color.black);
449 if (av.getScaleLeftWrapped())
451 drawWestScale(g, startRes, endx, ypos);
454 if (av.getScaleRightWrapped())
456 g.translate(canvasWidth - LABEL_EAST, 0);
457 drawEastScale(g, startRes, endx, ypos);
458 g.translate(-(canvasWidth - LABEL_EAST), 0);
461 g.translate(LABEL_WEST, 0);
463 if (av.getScaleAboveWrapped())
465 drawNorthScale(g, startRes, endx, ypos);
467 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
469 g.setColor(Color.blue);
471 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
474 res = av.getColumnSelection().findHiddenRegionPosition(i)
477 if (res < 0 || res > endx - startRes)
482 gg.fillPolygon(new int[] { res * avcharWidth - avcharHeight / 4,
483 res * avcharWidth + avcharHeight / 4, res * avcharWidth },
484 new int[] { ypos - (avcharHeight / 2),
485 ypos - (avcharHeight / 2),
486 ypos - (avcharHeight / 2) + 8 }, 3);
491 if (g.getClip() == null)
493 g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
496 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
499 if (av.isShowAnnotation())
501 g.translate(0, cHeight + ypos + 4);
502 if (annotations == null)
504 annotations = new AnnotationPanel(av);
507 annotations.drawComponent(g, startRes, endx + 1);
508 g.translate(0, -cHeight - ypos - 4);
510 g.translate(-LABEL_WEST, 0);
512 ypos += cHeight + getAnnotationHeight() + hgap;
519 AnnotationPanel annotations;
521 int getAnnotationHeight()
523 if (!av.isShowAnnotation())
528 if (annotations == null)
530 annotations = new AnnotationPanel(av);
533 return annotations.adjustPanelHeight();
536 private void drawPanel(Graphics g1, int startRes, int endRes,
537 int startSeq, int endSeq, int offset)
540 if (!av.hasHiddenColumns())
542 draw(g1, startRes, endRes, startSeq, endSeq, offset);
548 int blockStart = startRes;
549 int blockEnd = endRes;
551 if (av.hasHiddenColumns())
553 for (int[] region : av.getColumnSelection().getHiddenColumns())
555 int hideStart = region[0];
556 int hideEnd = region[1];
558 if (hideStart <= blockStart)
560 blockStart += (hideEnd - hideStart) + 1;
564 blockEnd = hideStart - 1;
566 g1.translate(screenY * avcharWidth, 0);
568 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
570 if (av.getShowHiddenMarkers())
572 g1.setColor(Color.blue);
573 g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
574 0 + offset, (blockEnd - blockStart + 1) * avcharWidth
575 - 1, (endSeq - startSeq) * avcharHeight
579 g1.translate(-screenY * avcharWidth, 0);
580 screenY += blockEnd - blockStart + 1;
581 blockStart = hideEnd + 1;
583 if (screenY > (endRes - startRes))
585 // already rendered last block
590 if (screenY <= (endRes - startRes))
592 // remaining visible region to render
593 blockEnd = blockStart + (endRes - startRes) - screenY;
594 g1.translate(screenY * avcharWidth, 0);
595 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
597 g1.translate(-screenY * avcharWidth, 0);
603 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
604 // int x1, int x2, int y1, int y2, int startx, int starty,
605 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
608 g.setFont(av.getFont());
609 sr.prepare(g, av.isRenderGaps());
613 // / First draw the sequences
614 // ///////////////////////////
615 for (int i = startSeq; i < endSeq; i++)
617 nextSeq = av.getAlignment().getSequenceAt(i);
624 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
625 startRes, endRes, offset + ((i - startSeq) * avcharHeight));
627 if (av.isShowSequenceFeatures())
629 fr.drawSequence(g, nextSeq, startRes, endRes, offset
630 + ((i - startSeq) * avcharHeight));
633 // / Highlight search Results once all sequences have been drawn
634 // ////////////////////////////////////////////////////////
635 if (searchResults != null)
637 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
639 if (visibleResults != null)
641 for (int r = 0; r < visibleResults.length; r += 2)
643 sr.drawHighlightedText(nextSeq, visibleResults[r],
644 visibleResults[r + 1], (visibleResults[r] - startRes)
645 * avcharWidth, offset
646 + ((i - startSeq) * avcharHeight));
651 if (av.cursorMode && cursorY == i && cursorX >= startRes
652 && cursorX <= endRes)
654 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
655 offset + ((i - startSeq) * avcharHeight));
659 if (av.getSelectionGroup() != null
660 || av.getAlignment().getGroups().size() > 0)
662 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
667 private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
668 int startSeq, int endSeq, int offset)
671 // ///////////////////////////////////
672 // Now outline any areas if necessary
673 // ///////////////////////////////////
674 SequenceGroup group = av.getSelectionGroup();
681 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
683 group = av.getAlignment().getGroups().get(0);
693 boolean inGroup = false;
696 int alHeight = av.getAlignment().getHeight() - 1;
698 for (i = startSeq; i < endSeq; i++)
700 sx = (group.getStartRes() - startRes) * avcharWidth;
701 sy = offset + ((i - startSeq) * avcharHeight);
702 ex = (((group.getEndRes() + 1) - group.getStartRes()) * avcharWidth) - 1;
704 if (sx + ex < 0 || sx > imgWidth)
709 if ((sx <= (endRes - startRes) * avcharWidth)
710 && group.getSequences(null).contains(
711 av.getAlignment().getSequenceAt(i)))
714 && (i >= alHeight || !group.getSequences(null)
716 av.getAlignment().getSequenceAt(i + 1))))
718 bottom = sy + avcharHeight;
723 if (((top == -1) && (i == 0))
724 || !group.getSequences(null).contains(
725 av.getAlignment().getSequenceAt(i - 1)))
733 if (group == av.getSelectionGroup())
735 g.setColor(Color.red);
739 g.setColor(group.getOutlineColour());
747 if (sx >= 0 && sx < imgWidth)
749 g.drawLine(sx, oldY, sx, sy);
752 if (sx + ex < imgWidth)
754 g.drawLine(sx + ex, oldY, sx + ex, sy);
763 if (sx + ex > imgWidth)
768 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
770 ex = (endRes - startRes + 1) * avcharWidth;
775 g.drawLine(sx, top, sx + ex, top);
781 g.drawLine(sx, bottom, sx + ex, bottom);
792 sy = offset + ((i - startSeq) * avcharHeight);
793 if (sx >= 0 && sx < imgWidth)
795 g.drawLine(sx, oldY, sx, sy);
798 if (sx + ex < imgWidth)
800 g.drawLine(sx + ex, oldY, sx + ex, sy);
809 if (sx + ex > imgWidth)
813 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
815 ex = (endRes - startRes + 1) * avcharWidth;
820 g.drawLine(sx, top, sx + ex, top);
826 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
835 if (groupIndex >= av.getAlignment().getGroups().size())
840 group = av.getAlignment().getGroups().get(groupIndex);
841 } while (groupIndex < av.getAlignment().getGroups().size());
846 public void highlightSearchResults(SearchResults results)
848 searchResults = results;