2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9.0b2)
3 * Copyright (C) 2015 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.viewmodel.AlignmentViewport;
29 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
32 import java.awt.Image;
33 import java.awt.Panel;
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());
68 int avcharHeight = 0, avcharWidth = 0;
70 private void updateViewport()
72 avcharHeight = av.getCharHeight();
73 avcharWidth = av.getCharWidth();
76 public AlignmentViewport getViewport()
81 public FeatureRenderer getFeatureRenderer()
86 public SequenceRenderer getSequenceRenderer()
91 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
93 int scalestartx = startx - startx % 10 + 10;
95 g.setColor(Color.black);
98 for (int i = scalestartx; i < endx; i += 10)
101 if (av.hasHiddenColumns())
103 value = av.getColumnSelection().adjustForHiddenColumns(value);
106 g.drawString(String.valueOf(value), (i - startx - 1) * avcharWidth,
107 ypos - (avcharHeight / 2));
109 g.drawLine(((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
110 (ypos + 2) - (avcharHeight / 2),
111 ((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
116 private void drawWestScale(Graphics g, int startx, int endx, int ypos)
118 FontMetrics fm = getFontMetrics(av.getFont());
119 ypos += avcharHeight;
120 if (av.hasHiddenColumns())
122 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
123 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
126 int maxwidth = av.getAlignment().getWidth();
127 if (av.hasHiddenColumns())
129 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
133 for (int i = 0; i < av.getAlignment().getHeight(); i++)
135 SequenceI seq = av.getAlignment().getSequenceAt(i);
141 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
148 value = av.getAlignment().getSequenceAt(i).findPosition(index);
155 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
157 g.drawString(value + "", x, (ypos + (i * avcharHeight))
158 - (avcharHeight / 5));
163 private void drawEastScale(Graphics g, int startx, int endx, int ypos)
165 ypos += avcharHeight;
167 if (av.hasHiddenColumns())
169 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
174 for (int i = 0; i < av.getAlignment().getHeight(); i++)
176 seq = av.getAlignment().getSequenceAt(i);
180 while (index > startx)
182 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
189 value = seq.findPosition(index);
196 g.drawString(String.valueOf(value), 0, (ypos + (i * avcharHeight))
197 - (avcharHeight / 5));
204 void fastPaint(int horizontal, int vertical)
206 if (fastPaint || gg == null)
213 // Its possible on certain browsers that the call to fastpaint
214 // is faster than it can paint, so this check here catches
216 if (lastsr + horizontal != av.startRes)
218 horizontal = av.startRes - lastsr;
221 lastsr = av.startRes;
224 gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
225 - horizontal * avcharWidth,
226 imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
227 -vertical * avcharHeight);
229 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
231 if (horizontal > 0) // scrollbar pulled right, image to the left
233 transX = (er - sr - horizontal) * avcharWidth;
234 sr = er - horizontal;
236 else if (horizontal < 0)
238 er = sr - horizontal;
241 else if (vertical > 0) // scroll down
244 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
250 transY = imgHeight - vertical * avcharHeight;
253 else if (vertical < 0)
262 gg.translate(transX, transY);
264 drawPanel(gg, sr, er, ss, es, 0);
265 gg.translate(-transX, -transY);
272 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
273 * towards! startx is the first residue (starting at 0) to display. endx is
274 * the last residue to display (starting at 0). starty is the first sequence
275 * to display (starting at 0). endy is the last sequence to display (starting
276 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
277 * adjustment listener in SeqPanel when the scrollbars move.
279 public void update(Graphics g)
285 public void paint(Graphics g)
289 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
290 .getClipBounds().height)))
292 g.drawImage(img, 0, 0, this);
299 g.drawImage(img, 0, 0, this);
305 // this draws the whole of the alignment
306 imgWidth = this.getSize().width;
307 imgHeight = this.getSize().height;
309 imgWidth -= imgWidth % avcharWidth;
310 imgHeight -= imgHeight % avcharHeight;
312 if (imgWidth < 1 || imgHeight < 1)
317 if (img == null || imgWidth != img.getWidth(this)
318 || imgHeight != img.getHeight(this))
320 img = createImage(imgWidth, imgHeight);
321 gg = img.getGraphics();
322 gg.setFont(av.getFont());
325 gg.setColor(Color.white);
326 gg.fillRect(0, 0, imgWidth, imgHeight);
328 if (av.getWrapAlignment())
330 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
334 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
337 g.drawImage(img, 0, 0, this);
341 int LABEL_WEST, LABEL_EAST;
343 public int getWrappedCanvasWidth(int cwidth)
345 cwidth -= cwidth % av.getCharWidth();
347 FontMetrics fm = getFontMetrics(av.getFont());
352 if (av.getScaleRightWrapped())
354 LABEL_EAST = fm.stringWidth(getMask());
357 if (av.getScaleLeftWrapped())
359 LABEL_WEST = fm.stringWidth(getMask());
362 return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
366 * Generates a string of zeroes.
375 AlignmentI alignment = av.getAlignment();
376 for (int i = 0; i < alignment.getHeight(); i++)
378 tmp = alignment.getSequenceAt(i).getEnd();
385 for (int i = maxWidth; i > 0; i /= 10)
392 private void drawWrappedPanel(Graphics g, int canvasWidth,
393 int canvasHeight, int startRes)
395 AlignmentI al = av.getAlignment();
397 FontMetrics fm = getFontMetrics(av.getFont());
399 if (av.getScaleRightWrapped())
401 LABEL_EAST = fm.stringWidth(getMask());
404 if (av.getScaleLeftWrapped())
406 LABEL_WEST = fm.stringWidth(getMask());
409 int hgap = avcharHeight;
410 if (av.getScaleAboveWrapped())
412 hgap += avcharHeight;
415 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
416 int cHeight = av.getAlignment().getHeight() * avcharHeight;
418 av.setWrappedWidth(cWidth);
420 av.endRes = av.startRes + cWidth;
425 int maxwidth = av.getAlignment().getWidth() - 1;
427 if (av.hasHiddenColumns())
429 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
432 while ((ypos <= canvasHeight) && (startRes < maxwidth))
434 endx = startRes + cWidth - 1;
441 g.setColor(Color.black);
443 if (av.getScaleLeftWrapped())
445 drawWestScale(g, startRes, endx, ypos);
448 if (av.getScaleRightWrapped())
450 g.translate(canvasWidth - LABEL_EAST, 0);
451 drawEastScale(g, startRes, endx, ypos);
452 g.translate(-(canvasWidth - LABEL_EAST), 0);
455 g.translate(LABEL_WEST, 0);
457 if (av.getScaleAboveWrapped())
459 drawNorthScale(g, startRes, endx, ypos);
461 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
463 g.setColor(Color.blue);
465 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
468 res = av.getColumnSelection().findHiddenRegionPosition(i)
471 if (res < 0 || res > endx - startRes)
476 gg.fillPolygon(new int[] { res * avcharWidth - avcharHeight / 4,
477 res * avcharWidth + avcharHeight / 4, res * avcharWidth },
478 new int[] { ypos - (avcharHeight / 2),
479 ypos - (avcharHeight / 2),
480 ypos - (avcharHeight / 2) + 8 }, 3);
485 if (g.getClip() == null)
487 g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
490 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
493 if (av.isShowAnnotation())
495 g.translate(0, cHeight + ypos + 4);
496 if (annotations == null)
498 annotations = new AnnotationPanel(av);
501 annotations.drawComponent(g, startRes, endx + 1);
502 g.translate(0, -cHeight - ypos - 4);
504 g.translate(-LABEL_WEST, 0);
506 ypos += cHeight + getAnnotationHeight() + hgap;
513 AnnotationPanel annotations;
515 int getAnnotationHeight()
517 if (!av.isShowAnnotation())
522 if (annotations == null)
524 annotations = new AnnotationPanel(av);
527 return annotations.adjustPanelHeight();
530 private void drawPanel(Graphics g1, int startRes, int endRes,
531 int startSeq, int endSeq, int offset)
534 if (!av.hasHiddenColumns())
536 draw(g1, startRes, endRes, startSeq, endSeq, offset);
542 int blockStart = startRes;
543 int blockEnd = endRes;
545 if (av.hasHiddenColumns())
547 for (int[] region : av.getColumnSelection().getHiddenColumns())
549 int hideStart = region[0];
550 int hideEnd = region[1];
552 if (hideStart <= blockStart)
554 blockStart += (hideEnd - hideStart) + 1;
558 blockEnd = hideStart - 1;
560 g1.translate(screenY * avcharWidth, 0);
562 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
564 if (av.getShowHiddenMarkers())
566 g1.setColor(Color.blue);
567 g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
568 0 + offset, (blockEnd - blockStart + 1) * avcharWidth
569 - 1, (endSeq - startSeq) * avcharHeight
573 g1.translate(-screenY * avcharWidth, 0);
574 screenY += blockEnd - blockStart + 1;
575 blockStart = hideEnd + 1;
578 if (screenY <= (endRes - startRes))
580 blockEnd = blockStart + (endRes - startRes) - screenY;
581 g1.translate(screenY * avcharWidth, 0);
582 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
584 g1.translate(-screenY * avcharWidth, 0);
590 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
591 // int x1, int x2, int y1, int y2, int startx, int starty,
592 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
595 g.setFont(av.getFont());
596 sr.prepare(g, av.isRenderGaps());
600 // / First draw the sequences
601 // ///////////////////////////
602 for (int i = startSeq; i < endSeq; i++)
604 nextSeq = av.getAlignment().getSequenceAt(i);
611 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
612 startRes, endRes, offset + ((i - startSeq) * avcharHeight));
614 if (av.isShowSequenceFeatures())
616 fr.drawSequence(g, nextSeq, startRes, endRes, offset
617 + ((i - startSeq) * avcharHeight));
620 // / Highlight search Results once all sequences have been drawn
621 // ////////////////////////////////////////////////////////
622 if (searchResults != null)
624 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
626 if (visibleResults != null)
628 for (int r = 0; r < visibleResults.length; r += 2)
630 sr.drawHighlightedText(nextSeq, visibleResults[r],
631 visibleResults[r + 1], (visibleResults[r] - startRes)
632 * avcharWidth, offset
633 + ((i - startSeq) * avcharHeight));
638 if (av.cursorMode && cursorY == i && cursorX >= startRes
639 && cursorX <= endRes)
641 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
642 offset + ((i - startSeq) * avcharHeight));
646 if (av.getSelectionGroup() != null
647 || av.getAlignment().getGroups().size() > 0)
649 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
654 private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
655 int startSeq, int endSeq, int offset)
658 // ///////////////////////////////////
659 // Now outline any areas if necessary
660 // ///////////////////////////////////
661 SequenceGroup group = av.getSelectionGroup();
668 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
670 group = av.getAlignment().getGroups().get(0);
680 boolean inGroup = false;
683 int alHeight = av.getAlignment().getHeight() - 1;
685 for (i = startSeq; i < endSeq; i++)
687 sx = (group.getStartRes() - startRes) * avcharWidth;
688 sy = offset + ((i - startSeq) * avcharHeight);
689 ex = (((group.getEndRes() + 1) - group.getStartRes()) * avcharWidth) - 1;
691 if (sx + ex < 0 || sx > imgWidth)
696 if ((sx <= (endRes - startRes) * avcharWidth)
697 && group.getSequences(null).contains(
698 av.getAlignment().getSequenceAt(i)))
701 && (i >= alHeight || !group.getSequences(null)
703 av.getAlignment().getSequenceAt(i + 1))))
705 bottom = sy + avcharHeight;
710 if (((top == -1) && (i == 0))
711 || !group.getSequences(null).contains(
712 av.getAlignment().getSequenceAt(i - 1)))
720 if (group == av.getSelectionGroup())
722 g.setColor(Color.red);
726 g.setColor(group.getOutlineColour());
734 if (sx >= 0 && sx < imgWidth)
736 g.drawLine(sx, oldY, sx, sy);
739 if (sx + ex < imgWidth)
741 g.drawLine(sx + ex, oldY, sx + ex, sy);
750 if (sx + ex > imgWidth)
755 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
757 ex = (endRes - startRes + 1) * avcharWidth;
762 g.drawLine(sx, top, sx + ex, top);
768 g.drawLine(sx, bottom, sx + ex, bottom);
779 sy = offset + ((i - startSeq) * avcharHeight);
780 if (sx >= 0 && sx < imgWidth)
782 g.drawLine(sx, oldY, sx, sy);
785 if (sx + ex < imgWidth)
787 g.drawLine(sx + ex, oldY, sx + ex, sy);
796 if (sx + ex > imgWidth)
800 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
802 ex = (endRes - startRes + 1) * avcharWidth;
807 g.drawLine(sx, top, sx + ex, top);
813 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
822 if (groupIndex >= av.getAlignment().getGroups().size())
827 group = av.getAlignment().getGroups().get(groupIndex);
828 } while (groupIndex < av.getAlignment().getGroups().size());
833 public void highlightSearchResults(SearchResults results)
835 searchResults = results;