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.SearchResultsI;
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 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)
94 g.setColor(Color.black);
95 for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
98 int mpos = mark.column; // (i - startx - 1)
103 String mstring = mark.text;
109 g.drawString(mstring, mpos * avcharWidth, ypos
110 - (avcharHeight / 2));
112 g.drawLine((mpos * avcharWidth) + (avcharWidth / 2), (ypos + 2)
113 - (avcharHeight / 2), (mpos * avcharWidth)
114 + (avcharWidth / 2), ypos - 2);
119 private void drawWestScale(Graphics g, int startx, int endx, int ypos)
121 FontMetrics fm = getFontMetrics(av.getFont());
122 ypos += avcharHeight;
123 if (av.hasHiddenColumns())
125 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
126 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
129 int maxwidth = av.getAlignment().getWidth();
130 if (av.hasHiddenColumns())
132 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
136 for (int i = 0; i < av.getAlignment().getHeight(); i++)
138 SequenceI seq = av.getAlignment().getSequenceAt(i);
144 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
151 value = av.getAlignment().getSequenceAt(i).findPosition(index);
158 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
160 g.drawString(value + "", x, (ypos + (i * avcharHeight))
161 - (avcharHeight / 5));
166 private void drawEastScale(Graphics g, int startx, int endx, int ypos)
168 ypos += avcharHeight;
170 if (av.hasHiddenColumns())
172 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
177 for (int i = 0; i < av.getAlignment().getHeight(); i++)
179 seq = av.getAlignment().getSequenceAt(i);
183 while (index > startx)
185 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
192 value = seq.findPosition(index);
199 g.drawString(String.valueOf(value), 0, (ypos + (i * avcharHeight))
200 - (avcharHeight / 5));
207 void fastPaint(int horizontal, int vertical)
209 if (fastPaint || gg == null)
216 // Its possible on certain browsers that the call to fastpaint
217 // is faster than it can paint, so this check here catches
219 if (lastsr + horizontal != av.getStartRes())
221 horizontal = av.getStartRes() - lastsr;
224 lastsr = av.getStartRes();
227 gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
228 - horizontal * avcharWidth,
229 imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
230 -vertical * avcharHeight);
232 int sr = av.getStartRes(), er = av.getEndRes(), ss = av.getStartSeq(), es = av
233 .getEndSeq(), transX = 0, transY = 0;
235 if (horizontal > 0) // scrollbar pulled right, image to the left
237 transX = (er - sr - horizontal) * avcharWidth;
238 sr = er - horizontal;
240 else if (horizontal < 0)
242 er = sr - horizontal;
245 else if (vertical > 0) // scroll down
248 if (ss < av.getStartSeq()) // ie scrolling too fast, more than a page at a
251 ss = av.getStartSeq();
255 transY = imgHeight - vertical * avcharHeight;
258 else if (vertical < 0)
261 if (es > av.getEndSeq())
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.getStartRes());
340 drawPanel(gg, av.getStartRes(), av.getEndRes(), av.getStartSeq(),
344 g.drawImage(img, 0, 0, this);
348 int LABEL_WEST, LABEL_EAST;
350 public int getWrappedCanvasWidth(int cwidth)
352 cwidth -= cwidth % av.getCharWidth();
354 FontMetrics fm = getFontMetrics(av.getFont());
359 if (av.getScaleRightWrapped())
361 LABEL_EAST = fm.stringWidth(getMask());
364 if (av.getScaleLeftWrapped())
366 LABEL_WEST = fm.stringWidth(getMask());
369 return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
373 * Generates a string of zeroes.
382 AlignmentI alignment = av.getAlignment();
383 for (int i = 0; i < alignment.getHeight(); i++)
385 tmp = alignment.getSequenceAt(i).getEnd();
392 for (int i = maxWidth; i > 0; i /= 10)
399 private void drawWrappedPanel(Graphics g, int canvasWidth,
400 int canvasHeight, int startRes)
402 AlignmentI al = av.getAlignment();
404 FontMetrics fm = getFontMetrics(av.getFont());
406 if (av.getScaleRightWrapped())
408 LABEL_EAST = fm.stringWidth(getMask());
411 if (av.getScaleLeftWrapped())
413 LABEL_WEST = fm.stringWidth(getMask());
416 int hgap = avcharHeight;
417 if (av.getScaleAboveWrapped())
419 hgap += avcharHeight;
422 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
423 int cHeight = av.getAlignment().getHeight() * avcharHeight;
425 av.setWrappedWidth(cWidth);
427 av.setEndRes(av.getStartRes() + cWidth);
432 int maxwidth = av.getAlignment().getWidth() - 1;
434 if (av.hasHiddenColumns())
436 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
439 while ((ypos <= canvasHeight) && (startRes < maxwidth))
441 endx = startRes + cWidth - 1;
448 g.setColor(Color.black);
450 if (av.getScaleLeftWrapped())
452 drawWestScale(g, startRes, endx, ypos);
455 if (av.getScaleRightWrapped())
457 g.translate(canvasWidth - LABEL_EAST, 0);
458 drawEastScale(g, startRes, endx, ypos);
459 g.translate(-(canvasWidth - LABEL_EAST), 0);
462 g.translate(LABEL_WEST, 0);
464 if (av.getScaleAboveWrapped())
466 drawNorthScale(g, startRes, endx, ypos);
468 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
470 g.setColor(Color.blue);
472 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
475 res = av.getColumnSelection().findHiddenRegionPosition(i)
478 if (res < 0 || res > endx - startRes)
483 gg.fillPolygon(new int[] { res * avcharWidth - avcharHeight / 4,
484 res * avcharWidth + avcharHeight / 4, res * avcharWidth },
485 new int[] { ypos - (avcharHeight / 2),
486 ypos - (avcharHeight / 2),
487 ypos - (avcharHeight / 2) + 8 }, 3);
492 if (g.getClip() == null)
494 g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
497 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
500 if (av.isShowAnnotation())
502 g.translate(0, cHeight + ypos + 4);
503 if (annotations == null)
505 annotations = new AnnotationPanel(av);
508 annotations.drawComponent(g, startRes, endx + 1);
509 g.translate(0, -cHeight - ypos - 4);
511 g.translate(-LABEL_WEST, 0);
513 ypos += cHeight + getAnnotationHeight() + hgap;
520 AnnotationPanel annotations;
522 int getAnnotationHeight()
524 if (!av.isShowAnnotation())
529 if (annotations == null)
531 annotations = new AnnotationPanel(av);
534 return annotations.adjustPanelHeight();
537 private void drawPanel(Graphics g1, int startRes, int endRes,
538 int startSeq, int endSeq, int offset)
541 if (!av.hasHiddenColumns())
543 draw(g1, startRes, endRes, startSeq, endSeq, offset);
549 int blockStart = startRes;
550 int blockEnd = endRes;
552 if (av.hasHiddenColumns())
554 for (int[] region : av.getColumnSelection().getHiddenColumns())
556 int hideStart = region[0];
557 int hideEnd = region[1];
559 if (hideStart <= blockStart)
561 blockStart += (hideEnd - hideStart) + 1;
565 blockEnd = hideStart - 1;
567 g1.translate(screenY * avcharWidth, 0);
569 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
571 if (av.getShowHiddenMarkers())
573 g1.setColor(Color.blue);
574 g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
575 0 + offset, (blockEnd - blockStart + 1) * avcharWidth
576 - 1, (endSeq - startSeq) * avcharHeight
580 g1.translate(-screenY * avcharWidth, 0);
581 screenY += blockEnd - blockStart + 1;
582 blockStart = hideEnd + 1;
584 if (screenY > (endRes - startRes))
586 // already rendered last block
591 if (screenY <= (endRes - startRes))
593 // remaining visible region to render
594 blockEnd = blockStart + (endRes - startRes) - screenY;
595 g1.translate(screenY * avcharWidth, 0);
596 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
598 g1.translate(-screenY * avcharWidth, 0);
604 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
605 // int x1, int x2, int y1, int y2, int startx, int starty,
606 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
609 g.setFont(av.getFont());
610 sr.prepare(g, av.isRenderGaps());
614 // / First draw the sequences
615 // ///////////////////////////
616 for (int i = startSeq; i < endSeq; i++)
618 nextSeq = av.getAlignment().getSequenceAt(i);
625 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
626 startRes, endRes, offset + ((i - startSeq) * avcharHeight));
628 if (av.isShowSequenceFeatures())
630 fr.drawSequence(g, nextSeq, startRes, endRes, offset
631 + ((i - startSeq) * avcharHeight));
634 // / Highlight search Results once all sequences have been drawn
635 // ////////////////////////////////////////////////////////
636 if (av.hasSearchResults())
638 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
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(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(SearchResultsI results)
850 av.setSearchResults(results);