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;
28 import java.awt.Color;
29 import java.awt.FontMetrics;
30 import java.awt.Graphics;
31 import java.awt.Image;
32 import java.awt.Panel;
34 public class SeqCanvas extends Panel
50 SearchResults searchResults = null;
52 boolean fastPaint = false;
58 public SeqCanvas(AlignViewport av)
61 fr = new FeatureRenderer(av);
62 sr = new SequenceRenderer(av);
63 PaintRefresher.Register(this, av.getSequenceSetId());
67 int avcharHeight = 0, avcharWidth = 0;
69 private void updateViewport()
71 avcharHeight = av.getCharHeight();
72 avcharWidth = av.getCharWidth();
75 public AlignViewport getViewport()
80 public FeatureRenderer getFeatureRenderer()
85 public SequenceRenderer getSequenceRenderer()
90 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
92 int scalestartx = startx - startx % 10 + 10;
94 g.setColor(Color.black);
97 for (int i = scalestartx; i < endx; i += 10)
100 if (av.hasHiddenColumns())
102 value = av.getColumnSelection().adjustForHiddenColumns(value);
105 g.drawString(String.valueOf(value), (i - startx - 1) * avcharWidth,
106 ypos - (avcharHeight / 2));
108 g.drawLine(((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
109 (ypos + 2) - (avcharHeight / 2),
110 ((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
115 private void drawWestScale(Graphics g, int startx, int endx, int ypos)
117 FontMetrics fm = getFontMetrics(av.getFont());
118 ypos += avcharHeight;
119 if (av.hasHiddenColumns())
121 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
122 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
125 int maxwidth = av.getAlignment().getWidth();
126 if (av.hasHiddenColumns())
128 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
132 for (int i = 0; i < av.getAlignment().getHeight(); i++)
134 SequenceI seq = av.getAlignment().getSequenceAt(i);
140 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
147 value = av.getAlignment().getSequenceAt(i).findPosition(index);
154 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
156 g.drawString(value + "", x, (ypos + (i * avcharHeight))
157 - (avcharHeight / 5));
162 private void drawEastScale(Graphics g, int startx, int endx, int ypos)
164 ypos += avcharHeight;
166 if (av.hasHiddenColumns())
168 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
173 for (int i = 0; i < av.getAlignment().getHeight(); i++)
175 seq = av.getAlignment().getSequenceAt(i);
179 while (index > startx)
181 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
188 value = seq.findPosition(index);
195 g.drawString(String.valueOf(value), 0, (ypos + (i * avcharHeight))
196 - (avcharHeight / 5));
203 void fastPaint(int horizontal, int vertical)
205 if (fastPaint || gg == null)
212 // Its possible on certain browsers that the call to fastpaint
213 // is faster than it can paint, so this check here catches
215 if (lastsr + horizontal != av.startRes)
217 horizontal = av.startRes - lastsr;
220 lastsr = av.startRes;
223 gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
224 - horizontal * avcharWidth,
225 imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
226 -vertical * avcharHeight);
228 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
230 if (horizontal > 0) // scrollbar pulled right, image to the left
232 transX = (er - sr - horizontal) * avcharWidth;
233 sr = er - horizontal;
235 else if (horizontal < 0)
237 er = sr - horizontal;
240 else if (vertical > 0) // scroll down
243 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
249 transY = imgHeight - vertical * avcharHeight;
252 else if (vertical < 0)
261 gg.translate(transX, transY);
263 drawPanel(gg, sr, er, ss, es, 0);
264 gg.translate(-transX, -transY);
271 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
272 * towards! startx is the first residue (starting at 0) to display. endx is
273 * the last residue to display (starting at 0). starty is the first sequence
274 * to display (starting at 0). endy is the last sequence to display (starting
275 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
276 * adjustment listener in SeqPanel when the scrollbars move.
278 public void update(Graphics g)
284 public void paint(Graphics g)
288 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
289 .getClipBounds().height)))
291 g.drawImage(img, 0, 0, this);
298 g.drawImage(img, 0, 0, this);
304 // this draws the whole of the alignment
305 imgWidth = this.getSize().width;
306 imgHeight = this.getSize().height;
308 imgWidth -= imgWidth % avcharWidth;
309 imgHeight -= imgHeight % avcharHeight;
311 if (imgWidth < 1 || imgHeight < 1)
316 if (img == null || imgWidth != img.getWidth(this)
317 || imgHeight != img.getHeight(this))
319 img = createImage(imgWidth, imgHeight);
320 gg = img.getGraphics();
321 gg.setFont(av.getFont());
324 gg.setColor(Color.white);
325 gg.fillRect(0, 0, imgWidth, imgHeight);
327 if (av.getWrapAlignment())
329 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
333 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
336 g.drawImage(img, 0, 0, this);
340 int LABEL_WEST, LABEL_EAST;
342 public int getWrappedCanvasWidth(int cwidth)
344 cwidth -= cwidth % av.getCharWidth();
346 FontMetrics fm = getFontMetrics(av.getFont());
351 if (av.getScaleRightWrapped())
353 LABEL_EAST = fm.stringWidth(getMask());
356 if (av.getScaleLeftWrapped())
358 LABEL_WEST = fm.stringWidth(getMask());
361 return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
365 * Generates a string of zeroes.
374 AlignmentI alignment = av.getAlignment();
375 for (int i = 0; i < alignment.getHeight(); i++)
377 tmp = alignment.getSequenceAt(i).getEnd();
384 for (int i = maxWidth; i > 0; i /= 10)
391 private void drawWrappedPanel(Graphics g, int canvasWidth,
392 int canvasHeight, int startRes)
394 AlignmentI al = av.getAlignment();
396 FontMetrics fm = getFontMetrics(av.getFont());
398 if (av.getScaleRightWrapped())
400 LABEL_EAST = fm.stringWidth(getMask());
403 if (av.getScaleLeftWrapped())
405 LABEL_WEST = fm.stringWidth(getMask());
408 int hgap = avcharHeight;
409 if (av.getScaleAboveWrapped())
411 hgap += avcharHeight;
414 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
415 int cHeight = av.getAlignment().getHeight() * avcharHeight;
417 av.setWrappedWidth(cWidth);
419 av.endRes = av.startRes + cWidth;
424 int maxwidth = av.getAlignment().getWidth() - 1;
426 if (av.hasHiddenColumns())
428 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
431 while ((ypos <= canvasHeight) && (startRes < maxwidth))
433 endx = startRes + cWidth - 1;
440 g.setColor(Color.black);
442 if (av.getScaleLeftWrapped())
444 drawWestScale(g, startRes, endx, ypos);
447 if (av.getScaleRightWrapped())
449 g.translate(canvasWidth - LABEL_EAST, 0);
450 drawEastScale(g, startRes, endx, ypos);
451 g.translate(-(canvasWidth - LABEL_EAST), 0);
454 g.translate(LABEL_WEST, 0);
456 if (av.getScaleAboveWrapped())
458 drawNorthScale(g, startRes, endx, ypos);
460 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
462 g.setColor(Color.blue);
464 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
467 res = av.getColumnSelection().findHiddenRegionPosition(i)
470 if (res < 0 || res > endx - startRes)
475 gg.fillPolygon(new int[]
476 { res * avcharWidth - avcharHeight / 4,
477 res * avcharWidth + avcharHeight / 4, res * avcharWidth },
479 { ypos - (avcharHeight / 2), 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,
532 int endSeq, int offset)
536 if (!av.hasHiddenColumns())
538 draw(g1, startRes, endRes, startSeq, endSeq, offset);
544 int blockStart = startRes;
545 int blockEnd = endRes;
547 if (av.hasHiddenColumns())
549 for (int[] region : av.getColumnSelection().getHiddenColumns())
551 int hideStart = region[0];
552 int hideEnd = region[1];
554 if (hideStart <= blockStart)
556 blockStart += (hideEnd - hideStart) + 1;
560 blockEnd = hideStart - 1;
562 g1.translate(screenY * avcharWidth, 0);
564 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
566 if (av.getShowHiddenMarkers())
568 g1.setColor(Color.blue);
569 g1.drawLine((blockEnd - blockStart + 1) * avcharWidth - 1,
570 0 + offset, (blockEnd - blockStart + 1) * avcharWidth
571 - 1, (endSeq - startSeq) * avcharHeight
575 g1.translate(-screenY * avcharWidth, 0);
576 screenY += blockEnd - blockStart + 1;
577 blockStart = hideEnd + 1;
580 if (screenY <= (endRes - startRes))
582 blockEnd = blockStart + (endRes - startRes) - screenY;
583 g1.translate(screenY * avcharWidth, 0);
584 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
586 g1.translate(-screenY * avcharWidth, 0);
592 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
593 // int x1, int x2, int y1, int y2, int startx, int starty,
594 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
597 g.setFont(av.getFont());
598 sr.prepare(g, av.isRenderGaps());
602 // / First draw the sequences
603 // ///////////////////////////
604 for (int i = startSeq; i < endSeq; i++)
606 nextSeq = av.getAlignment().getSequenceAt(i);
613 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
614 startRes, endRes, offset + ((i - startSeq) * avcharHeight));
616 if (av.isShowSequenceFeatures())
618 fr.drawSequence(g, nextSeq, startRes, endRes, offset
619 + ((i - startSeq) * avcharHeight));
622 // / Highlight search Results once all sequences have been drawn
623 // ////////////////////////////////////////////////////////
624 if (searchResults != null)
626 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
628 if (visibleResults != null)
630 for (int r = 0; r < visibleResults.length; r += 2)
632 sr.drawHighlightedText(nextSeq, visibleResults[r],
633 visibleResults[r + 1], (visibleResults[r] - startRes)
634 * avcharWidth, offset
635 + ((i - startSeq) * avcharHeight));
640 if (av.cursorMode && cursorY == i && cursorX >= startRes
641 && cursorX <= endRes)
643 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
644 offset + ((i - startSeq) * avcharHeight));
648 if (av.getSelectionGroup() != null
649 || av.getAlignment().getGroups().size() > 0)
651 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
656 private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
657 int startSeq, int endSeq, int offset)
660 // ///////////////////////////////////
661 // Now outline any areas if necessary
662 // ///////////////////////////////////
663 SequenceGroup group = av.getSelectionGroup();
670 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
672 group = av.getAlignment().getGroups().get(0);
682 boolean inGroup = false;
685 int alHeight = av.getAlignment().getHeight() - 1;
687 for (i = startSeq; i < endSeq; i++)
689 sx = (group.getStartRes() - startRes) * avcharWidth;
690 sy = offset + ((i - startSeq) * avcharHeight);
691 ex = (((group.getEndRes() + 1) - group.getStartRes()) * avcharWidth) - 1;
693 if (sx + ex < 0 || sx > imgWidth)
698 if ((sx <= (endRes - startRes) * avcharWidth)
699 && group.getSequences(null).contains(
700 av.getAlignment().getSequenceAt(i)))
703 && (i >= alHeight || !group.getSequences(null)
705 av.getAlignment().getSequenceAt(i + 1))))
707 bottom = sy + avcharHeight;
712 if (((top == -1) && (i == 0))
713 || !group.getSequences(null).contains(
714 av.getAlignment().getSequenceAt(i - 1)))
722 if (group == av.getSelectionGroup())
724 g.setColor(Color.red);
728 g.setColor(group.getOutlineColour());
736 if (sx >= 0 && sx < imgWidth)
738 g.drawLine(sx, oldY, sx, sy);
741 if (sx + ex < imgWidth)
743 g.drawLine(sx + ex, oldY, sx + ex, sy);
752 if (sx + ex > imgWidth)
757 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
759 ex = (endRes - startRes + 1) * avcharWidth;
764 g.drawLine(sx, top, sx + ex, top);
770 g.drawLine(sx, bottom, sx + ex, bottom);
781 sy = offset + ((i - startSeq) * avcharHeight);
782 if (sx >= 0 && sx < imgWidth)
784 g.drawLine(sx, oldY, sx, sy);
787 if (sx + ex < imgWidth)
789 g.drawLine(sx + ex, oldY, sx + ex, sy);
798 if (sx + ex > imgWidth)
802 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
804 ex = (endRes - startRes + 1) * avcharWidth;
809 g.drawLine(sx, top, sx + ex, top);
815 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
824 if (groupIndex >= av.getAlignment().getGroups().size())
829 group = av.getAlignment().getGroups()
831 } while (groupIndex < av.getAlignment().getGroups().size());
836 public void highlightSearchResults(SearchResults results)
838 searchResults = results;