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)
122 private void drawWestScale(Graphics g, int startx, int endx, int ypos)
124 FontMetrics fm = getFontMetrics(av.getFont());
125 ypos += avcharHeight;
126 if (av.hasHiddenColumns())
128 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
129 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
132 int maxwidth = av.getAlignment().getWidth();
133 if (av.hasHiddenColumns())
135 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
139 for (int i = 0; i < av.getAlignment().getHeight(); i++)
141 SequenceI seq = av.getAlignment().getSequenceAt(i);
147 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
154 value = av.getAlignment().getSequenceAt(i).findPosition(index);
161 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
163 g.drawString(value + "", x, (ypos + (i * avcharHeight))
164 - (avcharHeight / 5));
169 private void drawEastScale(Graphics g, int startx, int endx, int ypos)
171 ypos += avcharHeight;
173 if (av.hasHiddenColumns())
175 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
180 for (int i = 0; i < av.getAlignment().getHeight(); i++)
182 seq = av.getAlignment().getSequenceAt(i);
186 while (index > startx)
188 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
195 value = seq.findPosition(index);
202 g.drawString(String.valueOf(value), 0, (ypos + (i * avcharHeight))
203 - (avcharHeight / 5));
210 void fastPaint(int horizontal, int vertical)
212 if (fastPaint || gg == null)
219 // Its possible on certain browsers that the call to fastpaint
220 // is faster than it can paint, so this check here catches
222 if (lastsr + horizontal != av.startRes)
224 horizontal = av.startRes - lastsr;
227 lastsr = av.startRes;
230 gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
231 - horizontal * avcharWidth,
232 imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
233 -vertical * avcharHeight);
235 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
237 if (horizontal > 0) // scrollbar pulled right, image to the left
239 transX = (er - sr - horizontal) * avcharWidth;
240 sr = er - horizontal;
242 else if (horizontal < 0)
244 er = sr - horizontal;
247 else if (vertical > 0) // scroll down
250 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
256 transY = imgHeight - vertical * avcharHeight;
259 else if (vertical < 0)
268 gg.translate(transX, transY);
270 drawPanel(gg, sr, er, ss, es, 0);
271 gg.translate(-transX, -transY);
278 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
279 * towards! startx is the first residue (starting at 0) to display. endx is
280 * the last residue to display (starting at 0). starty is the first sequence
281 * to display (starting at 0). endy is the last sequence to display (starting
282 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
283 * adjustment listener in SeqPanel when the scrollbars move.
286 public void update(Graphics g)
292 public void paint(Graphics g)
296 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
297 .getClipBounds().height)))
299 g.drawImage(img, 0, 0, this);
306 g.drawImage(img, 0, 0, this);
312 // this draws the whole of the alignment
313 imgWidth = this.getSize().width;
314 imgHeight = this.getSize().height;
316 imgWidth -= imgWidth % avcharWidth;
317 imgHeight -= imgHeight % avcharHeight;
319 if (imgWidth < 1 || imgHeight < 1)
324 if (img == null || imgWidth != img.getWidth(this)
325 || imgHeight != img.getHeight(this))
327 img = createImage(imgWidth, imgHeight);
328 gg = img.getGraphics();
329 gg.setFont(av.getFont());
332 gg.setColor(Color.white);
333 gg.fillRect(0, 0, imgWidth, imgHeight);
335 if (av.getWrapAlignment())
337 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
341 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
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.endRes = av.startRes + 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 (searchResults != null)
638 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
640 if (visibleResults != null)
642 for (int r = 0; r < visibleResults.length; r += 2)
644 sr.drawHighlightedText(nextSeq, visibleResults[r],
645 visibleResults[r + 1], (visibleResults[r] - startRes)
646 * avcharWidth, offset
647 + ((i - startSeq) * avcharHeight));
652 if (av.cursorMode && cursorY == i && cursorX >= startRes
653 && cursorX <= endRes)
655 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
656 offset + ((i - startSeq) * avcharHeight));
660 if (av.getSelectionGroup() != null
661 || av.getAlignment().getGroups().size() > 0)
663 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
668 private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
669 int startSeq, int endSeq, int offset)
672 // ///////////////////////////////////
673 // Now outline any areas if necessary
674 // ///////////////////////////////////
675 SequenceGroup group = av.getSelectionGroup();
682 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
684 group = av.getAlignment().getGroups().get(0);
694 boolean inGroup = false;
697 int alHeight = av.getAlignment().getHeight() - 1;
699 for (i = startSeq; i < endSeq; i++)
701 sx = (group.getStartRes() - startRes) * avcharWidth;
702 sy = offset + ((i - startSeq) * avcharHeight);
703 ex = (((group.getEndRes() + 1) - group.getStartRes()) * avcharWidth) - 1;
705 if (sx + ex < 0 || sx > imgWidth)
710 if ((sx <= (endRes - startRes) * avcharWidth)
711 && group.getSequences(null).contains(
712 av.getAlignment().getSequenceAt(i)))
715 && (i >= alHeight || !group.getSequences(null)
717 av.getAlignment().getSequenceAt(i + 1))))
719 bottom = sy + avcharHeight;
724 if (((top == -1) && (i == 0))
725 || !group.getSequences(null).contains(
726 av.getAlignment().getSequenceAt(i - 1)))
734 if (group == av.getSelectionGroup())
736 g.setColor(Color.red);
740 g.setColor(group.getOutlineColour());
748 if (sx >= 0 && sx < imgWidth)
750 g.drawLine(sx, oldY, sx, sy);
753 if (sx + ex < imgWidth)
755 g.drawLine(sx + ex, oldY, sx + ex, sy);
764 if (sx + ex > imgWidth)
769 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
771 ex = (endRes - startRes + 1) * avcharWidth;
776 g.drawLine(sx, top, sx + ex, top);
782 g.drawLine(sx, bottom, sx + ex, bottom);
793 sy = offset + ((i - startSeq) * avcharHeight);
794 if (sx >= 0 && sx < imgWidth)
796 g.drawLine(sx, oldY, sx, sy);
799 if (sx + ex < imgWidth)
801 g.drawLine(sx + ex, oldY, sx + ex, sy);
810 if (sx + ex > imgWidth)
814 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
816 ex = (endRes - startRes + 1) * avcharWidth;
821 g.drawLine(sx, top, sx + ex, top);
827 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
836 if (groupIndex >= av.getAlignment().getGroups().size())
841 group = av.getAlignment().getGroups().get(groupIndex);
842 } while (groupIndex < av.getAlignment().getGroups().size());
847 public void highlightSearchResults(SearchResults results)
849 searchResults = results;