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.startRes)
221 horizontal = av.startRes - lastsr;
224 lastsr = av.startRes;
227 gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
228 - horizontal * avcharWidth,
229 imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
230 -vertical * avcharHeight);
232 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
234 if (horizontal > 0) // scrollbar pulled right, image to the left
236 transX = (er - sr - horizontal) * avcharWidth;
237 sr = er - horizontal;
239 else if (horizontal < 0)
241 er = sr - horizontal;
244 else if (vertical > 0) // scroll down
247 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
253 transY = imgHeight - vertical * avcharHeight;
256 else if (vertical < 0)
265 gg.translate(transX, transY);
267 drawPanel(gg, sr, er, ss, es, 0);
268 gg.translate(-transX, -transY);
275 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
276 * towards! startx is the first residue (starting at 0) to display. endx is
277 * the last residue to display (starting at 0). starty is the first sequence
278 * to display (starting at 0). endy is the last sequence to display (starting
279 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
280 * adjustment listener in SeqPanel when the scrollbars move.
283 public void update(Graphics g)
289 public void paint(Graphics g)
293 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
294 .getClipBounds().height)))
296 g.drawImage(img, 0, 0, this);
303 g.drawImage(img, 0, 0, this);
309 // this draws the whole of the alignment
310 imgWidth = this.getSize().width;
311 imgHeight = this.getSize().height;
313 imgWidth -= imgWidth % avcharWidth;
314 imgHeight -= imgHeight % avcharHeight;
316 if (imgWidth < 1 || imgHeight < 1)
321 if (img == null || imgWidth != img.getWidth(this)
322 || imgHeight != img.getHeight(this))
324 img = createImage(imgWidth, imgHeight);
325 gg = img.getGraphics();
326 gg.setFont(av.getFont());
329 gg.setColor(Color.white);
330 gg.fillRect(0, 0, imgWidth, imgHeight);
332 if (av.getWrapAlignment())
334 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
338 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
341 g.drawImage(img, 0, 0, this);
345 int LABEL_WEST, LABEL_EAST;
347 public int getWrappedCanvasWidth(int cwidth)
349 cwidth -= cwidth % av.getCharWidth();
351 FontMetrics fm = getFontMetrics(av.getFont());
356 if (av.getScaleRightWrapped())
358 LABEL_EAST = fm.stringWidth(getMask());
361 if (av.getScaleLeftWrapped())
363 LABEL_WEST = fm.stringWidth(getMask());
366 return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
370 * Generates a string of zeroes.
379 AlignmentI alignment = av.getAlignment();
380 for (int i = 0; i < alignment.getHeight(); i++)
382 tmp = alignment.getSequenceAt(i).getEnd();
389 for (int i = maxWidth; i > 0; i /= 10)
396 private void drawWrappedPanel(Graphics g, int canvasWidth,
397 int canvasHeight, int startRes)
399 AlignmentI al = av.getAlignment();
401 FontMetrics fm = getFontMetrics(av.getFont());
403 if (av.getScaleRightWrapped())
405 LABEL_EAST = fm.stringWidth(getMask());
408 if (av.getScaleLeftWrapped())
410 LABEL_WEST = fm.stringWidth(getMask());
413 int hgap = avcharHeight;
414 if (av.getScaleAboveWrapped())
416 hgap += avcharHeight;
419 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
420 int cHeight = av.getAlignment().getHeight() * avcharHeight;
422 av.setWrappedWidth(cWidth);
424 av.endRes = av.startRes + cWidth;
429 int maxwidth = av.getAlignment().getWidth() - 1;
431 if (av.hasHiddenColumns())
433 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
436 while ((ypos <= canvasHeight) && (startRes < maxwidth))
438 endx = startRes + cWidth - 1;
445 g.setColor(Color.black);
447 if (av.getScaleLeftWrapped())
449 drawWestScale(g, startRes, endx, ypos);
452 if (av.getScaleRightWrapped())
454 g.translate(canvasWidth - LABEL_EAST, 0);
455 drawEastScale(g, startRes, endx, ypos);
456 g.translate(-(canvasWidth - LABEL_EAST), 0);
459 g.translate(LABEL_WEST, 0);
461 if (av.getScaleAboveWrapped())
463 drawNorthScale(g, startRes, endx, ypos);
465 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
467 g.setColor(Color.blue);
469 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
472 res = av.getColumnSelection().findHiddenRegionPosition(i)
475 if (res < 0 || res > endx - startRes)
480 gg.fillPolygon(new int[] { res * avcharWidth - avcharHeight / 4,
481 res * avcharWidth + avcharHeight / 4, res * avcharWidth },
482 new int[] { ypos - (avcharHeight / 2),
483 ypos - (avcharHeight / 2),
484 ypos - (avcharHeight / 2) + 8 }, 3);
489 if (g.getClip() == null)
491 g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
494 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
497 if (av.isShowAnnotation())
499 g.translate(0, cHeight + ypos + 4);
500 if (annotations == null)
502 annotations = new AnnotationPanel(av);
505 annotations.drawComponent(g, startRes, endx + 1);
506 g.translate(0, -cHeight - ypos - 4);
508 g.translate(-LABEL_WEST, 0);
510 ypos += cHeight + getAnnotationHeight() + hgap;
517 AnnotationPanel annotations;
519 int getAnnotationHeight()
521 if (!av.isShowAnnotation())
526 if (annotations == null)
528 annotations = new AnnotationPanel(av);
531 return annotations.adjustPanelHeight();
534 private void drawPanel(Graphics g1, int startRes, int endRes,
535 int startSeq, int endSeq, int offset)
538 if (!av.hasHiddenColumns())
540 draw(g1, startRes, endRes, startSeq, endSeq, offset);
546 int blockStart = startRes;
547 int blockEnd = endRes;
549 if (av.hasHiddenColumns())
551 for (int[] region : av.getColumnSelection().getHiddenColumns())
553 int hideStart = region[0];
554 int hideEnd = region[1];
556 if (hideStart <= blockStart)
558 blockStart += (hideEnd - hideStart) + 1;
562 blockEnd = hideStart - 1;
564 g1.translate(screenY * avcharWidth, 0);
566 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
568 if (av.getShowHiddenMarkers())
570 g1.setColor(Color.blue);
571 g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
572 0 + offset, (blockEnd - blockStart + 1) * avcharWidth
573 - 1, (endSeq - startSeq) * avcharHeight
577 g1.translate(-screenY * avcharWidth, 0);
578 screenY += blockEnd - blockStart + 1;
579 blockStart = hideEnd + 1;
581 if (screenY > (endRes - startRes))
583 // already rendered last block
588 if (screenY <= (endRes - startRes))
590 // remaining visible region to render
591 blockEnd = blockStart + (endRes - startRes) - screenY;
592 g1.translate(screenY * avcharWidth, 0);
593 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
595 g1.translate(-screenY * avcharWidth, 0);
601 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
602 // int x1, int x2, int y1, int y2, int startx, int starty,
603 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
606 g.setFont(av.getFont());
607 sr.prepare(g, av.isRenderGaps());
611 // / First draw the sequences
612 // ///////////////////////////
613 for (int i = startSeq; i < endSeq; i++)
615 nextSeq = av.getAlignment().getSequenceAt(i);
622 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
623 startRes, endRes, offset + ((i - startSeq) * avcharHeight));
625 if (av.isShowSequenceFeatures())
627 fr.drawSequence(g, nextSeq, startRes, endRes, offset
628 + ((i - startSeq) * avcharHeight));
631 // / Highlight search Results once all sequences have been drawn
632 // ////////////////////////////////////////////////////////
633 if (av.hasSearchResults())
635 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
638 if (visibleResults != null)
640 for (int r = 0; r < visibleResults.length; r += 2)
642 sr.drawHighlightedText(nextSeq, visibleResults[r],
643 visibleResults[r + 1], (visibleResults[r] - startRes)
644 * avcharWidth, offset
645 + ((i - startSeq) * avcharHeight));
650 if (av.cursorMode && cursorY == i && cursorX >= startRes
651 && cursorX <= endRes)
653 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
654 offset + ((i - startSeq) * avcharHeight));
658 if (av.getSelectionGroup() != null
659 || av.getAlignment().getGroups().size() > 0)
661 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
666 private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
667 int startSeq, int endSeq, int offset)
670 // ///////////////////////////////////
671 // Now outline any areas if necessary
672 // ///////////////////////////////////
673 SequenceGroup group = av.getSelectionGroup();
680 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
682 group = av.getAlignment().getGroups().get(0);
692 boolean inGroup = false;
695 int alHeight = av.getAlignment().getHeight() - 1;
697 for (i = startSeq; i < endSeq; i++)
699 sx = (group.getStartRes() - startRes) * avcharWidth;
700 sy = offset + ((i - startSeq) * avcharHeight);
701 ex = (((group.getEndRes() + 1) - group.getStartRes()) * avcharWidth) - 1;
703 if (sx + ex < 0 || sx > imgWidth)
708 if ((sx <= (endRes - startRes) * avcharWidth)
709 && group.getSequences(null).contains(
710 av.getAlignment().getSequenceAt(i)))
713 && (i >= alHeight || !group.getSequences(null)
715 av.getAlignment().getSequenceAt(i + 1))))
717 bottom = sy + avcharHeight;
722 if (((top == -1) && (i == 0))
723 || !group.getSequences(null).contains(
724 av.getAlignment().getSequenceAt(i - 1)))
732 if (group == av.getSelectionGroup())
734 g.setColor(Color.red);
738 g.setColor(group.getOutlineColour());
746 if (sx >= 0 && sx < imgWidth)
748 g.drawLine(sx, oldY, sx, sy);
751 if (sx + ex < imgWidth)
753 g.drawLine(sx + ex, oldY, sx + ex, sy);
762 if (sx + ex > imgWidth)
767 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
769 ex = (endRes - startRes + 1) * avcharWidth;
774 g.drawLine(sx, top, sx + ex, top);
780 g.drawLine(sx, bottom, sx + ex, bottom);
791 sy = offset + ((i - startSeq) * avcharHeight);
792 if (sx >= 0 && sx < imgWidth)
794 g.drawLine(sx, oldY, sx, sy);
797 if (sx + ex < imgWidth)
799 g.drawLine(sx + ex, oldY, sx + ex, sy);
808 if (sx + ex > imgWidth)
812 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
814 ex = (endRes - startRes + 1) * avcharWidth;
819 g.drawLine(sx, top, sx + ex, top);
825 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
834 if (groupIndex >= av.getAlignment().getGroups().size())
839 group = av.getAlignment().getGroups().get(groupIndex);
840 } while (groupIndex < av.getAlignment().getGroups().size());
845 public void highlightSearchResults(SearchResultsI results)
847 av.setSearchResults(results);