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.util.ColorUtils;
28 import jalview.viewmodel.AlignmentViewport;
30 import java.awt.Color;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
33 import java.awt.Image;
34 import java.awt.Panel;
36 public class SeqCanvas extends Panel
52 SearchResults searchResults = null;
54 boolean fastPaint = false;
60 public SeqCanvas(AlignViewport av)
63 fr = new FeatureRenderer(av);
64 sr = new SequenceRenderer(av);
65 PaintRefresher.Register(this, av.getSequenceSetId());
69 int avcharHeight = 0, avcharWidth = 0;
71 private void updateViewport()
73 avcharHeight = av.getCharHeight();
74 avcharWidth = av.getCharWidth();
77 public AlignmentViewport getViewport()
82 public FeatureRenderer getFeatureRenderer()
87 public SequenceRenderer getSequenceRenderer()
92 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
94 int scalestartx = startx - startx % 10 + 10;
96 g.setColor(Color.black);
99 for (int i = scalestartx; i < endx; i += 10)
102 if (av.hasHiddenColumns())
104 value = av.getColumnSelection().adjustForHiddenColumns(value);
107 g.drawString(String.valueOf(value), (i - startx - 1) * avcharWidth,
108 ypos - (avcharHeight / 2));
110 g.drawLine(((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
111 (ypos + 2) - (avcharHeight / 2),
112 ((i - startx - 1) * avcharWidth) + (avcharWidth / 2),
117 private void drawWestScale(Graphics g, int startx, int endx, int ypos)
119 FontMetrics fm = getFontMetrics(av.getFont());
120 ypos += avcharHeight;
121 if (av.hasHiddenColumns())
123 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
124 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
127 int maxwidth = av.getAlignment().getWidth();
128 if (av.hasHiddenColumns())
130 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
134 for (int i = 0; i < av.getAlignment().getHeight(); i++)
136 SequenceI seq = av.getAlignment().getSequenceAt(i);
142 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
149 value = av.getAlignment().getSequenceAt(i).findPosition(index);
156 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
158 g.drawString(value + "", x, (ypos + (i * avcharHeight))
159 - (avcharHeight / 5));
164 private void drawEastScale(Graphics g, int startx, int endx, int ypos)
166 ypos += avcharHeight;
168 if (av.hasHiddenColumns())
170 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
175 for (int i = 0; i < av.getAlignment().getHeight(); i++)
177 seq = av.getAlignment().getSequenceAt(i);
181 while (index > startx)
183 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
190 value = seq.findPosition(index);
197 g.drawString(String.valueOf(value), 0, (ypos + (i * avcharHeight))
198 - (avcharHeight / 5));
205 void fastPaint(int horizontal, int vertical)
207 if (fastPaint || gg == null)
214 // Its possible on certain browsers that the call to fastpaint
215 // is faster than it can paint, so this check here catches
217 if (lastsr + horizontal != av.startRes)
219 horizontal = av.startRes - lastsr;
222 lastsr = av.startRes;
225 gg.copyArea(horizontal * avcharWidth, vertical * avcharHeight, imgWidth
226 - horizontal * avcharWidth,
227 imgHeight - vertical * avcharHeight, -horizontal * avcharWidth,
228 -vertical * avcharHeight);
230 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
232 if (horizontal > 0) // scrollbar pulled right, image to the left
234 transX = (er - sr - horizontal) * avcharWidth;
235 sr = er - horizontal;
237 else if (horizontal < 0)
239 er = sr - horizontal;
242 else if (vertical > 0) // scroll down
245 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
251 transY = imgHeight - vertical * avcharHeight;
254 else if (vertical < 0)
263 gg.translate(transX, transY);
265 drawPanel(gg, sr, er, ss, es, 0);
266 gg.translate(-transX, -transY);
273 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
274 * towards! startx is the first residue (starting at 0) to display. endx is
275 * the last residue to display (starting at 0). starty is the first sequence
276 * to display (starting at 0). endy is the last sequence to display (starting
277 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
278 * adjustment listener in SeqPanel when the scrollbars move.
281 public void update(Graphics g)
287 public void paint(Graphics g)
291 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
292 .getClipBounds().height)))
294 g.drawImage(img, 0, 0, this);
301 g.drawImage(img, 0, 0, this);
307 // this draws the whole of the alignment
308 imgWidth = this.getSize().width;
309 imgHeight = this.getSize().height;
311 imgWidth -= imgWidth % avcharWidth;
312 imgHeight -= imgHeight % avcharHeight;
314 if (imgWidth < 1 || imgHeight < 1)
319 if (img == null || imgWidth != img.getWidth(this)
320 || imgHeight != img.getHeight(this))
322 img = createImage(imgWidth, imgHeight);
323 gg = img.getGraphics();
324 gg.setFont(av.getFont());
327 gg.setColor(Color.white);
328 gg.fillRect(0, 0, imgWidth, imgHeight);
330 if (av.getWrapAlignment())
332 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
336 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
339 g.drawImage(img, 0, 0, this);
343 int LABEL_WEST, LABEL_EAST;
345 public int getWrappedCanvasWidth(int cwidth)
347 cwidth -= cwidth % av.getCharWidth();
349 FontMetrics fm = getFontMetrics(av.getFont());
354 if (av.getScaleRightWrapped())
356 LABEL_EAST = fm.stringWidth(getMask());
359 if (av.getScaleLeftWrapped())
361 LABEL_WEST = fm.stringWidth(getMask());
364 return (cwidth - LABEL_EAST - LABEL_WEST) / av.getCharWidth();
368 * Generates a string of zeroes.
377 AlignmentI alignment = av.getAlignment();
378 for (int i = 0; i < alignment.getHeight(); i++)
380 tmp = alignment.getSequenceAt(i).getEnd();
387 for (int i = maxWidth; i > 0; i /= 10)
394 private void drawWrappedPanel(Graphics g, int canvasWidth,
395 int canvasHeight, int startRes)
397 AlignmentI al = av.getAlignment();
399 FontMetrics fm = getFontMetrics(av.getFont());
401 if (av.getScaleRightWrapped())
403 LABEL_EAST = fm.stringWidth(getMask());
406 if (av.getScaleLeftWrapped())
408 LABEL_WEST = fm.stringWidth(getMask());
411 int hgap = avcharHeight;
412 if (av.getScaleAboveWrapped())
414 hgap += avcharHeight;
417 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / avcharWidth;
418 int cHeight = av.getAlignment().getHeight() * avcharHeight;
420 av.setWrappedWidth(cWidth);
422 av.endRes = av.startRes + cWidth;
427 int maxwidth = av.getAlignment().getWidth() - 1;
429 if (av.hasHiddenColumns())
431 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
434 while ((ypos <= canvasHeight) && (startRes < maxwidth))
436 endx = startRes + cWidth - 1;
443 g.setColor(Color.black);
445 if (av.getScaleLeftWrapped())
447 drawWestScale(g, startRes, endx, ypos);
450 if (av.getScaleRightWrapped())
452 g.translate(canvasWidth - LABEL_EAST, 0);
453 drawEastScale(g, startRes, endx, ypos);
454 g.translate(-(canvasWidth - LABEL_EAST), 0);
457 g.translate(LABEL_WEST, 0);
459 if (av.getScaleAboveWrapped())
461 drawNorthScale(g, startRes, endx, ypos);
463 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
465 g.setColor(Color.blue);
467 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
470 res = av.getColumnSelection().findHiddenRegionPosition(i)
473 if (res < 0 || res > endx - startRes)
478 gg.fillPolygon(new int[] { res * avcharWidth - avcharHeight / 4,
479 res * avcharWidth + avcharHeight / 4, res * avcharWidth },
480 new int[] { ypos - (avcharHeight / 2),
481 ypos - (avcharHeight / 2),
482 ypos - (avcharHeight / 2) + 8 }, 3);
487 if (g.getClip() == null)
489 g.setClip(0, 0, cWidth * avcharWidth, canvasHeight);
492 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
495 if (av.isShowAnnotation())
497 g.translate(0, cHeight + ypos + 4);
498 if (annotations == null)
500 annotations = new AnnotationPanel(av);
503 annotations.drawComponent(g, startRes, endx + 1);
504 g.translate(0, -cHeight - ypos - 4);
506 g.translate(-LABEL_WEST, 0);
508 ypos += cHeight + getAnnotationHeight() + hgap;
515 AnnotationPanel annotations;
517 int getAnnotationHeight()
519 if (!av.isShowAnnotation())
524 if (annotations == null)
526 annotations = new AnnotationPanel(av);
529 return annotations.adjustPanelHeight();
532 private void drawPanel(Graphics g1, int startRes, int endRes,
533 int startSeq, 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;
579 if (screenY > (endRes - startRes))
581 // already rendered last block
586 if (screenY <= (endRes - startRes))
588 // remaining visible region to render
589 blockEnd = blockStart + (endRes - startRes) - screenY;
590 g1.translate(screenY * avcharWidth, 0);
591 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
593 g1.translate(-screenY * avcharWidth, 0);
599 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
600 // int x1, int x2, int y1, int y2, int startx, int starty,
601 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
604 g.setFont(av.getFont());
605 sr.prepare(g, av.isRenderGaps());
609 // / First draw the sequences
610 // ///////////////////////////
611 for (int i = startSeq; i < endSeq; i++)
613 nextSeq = av.getAlignment().getSequenceAt(i);
620 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
621 startRes, endRes, offset + ((i - startSeq) * avcharHeight));
623 if (av.isShowSequenceFeatures())
625 fr.drawSequence(g, nextSeq, startRes, endRes, offset
626 + ((i - startSeq) * avcharHeight));
629 // / Highlight search Results once all sequences have been drawn
630 // ////////////////////////////////////////////////////////
631 if (searchResults != null)
633 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
635 if (visibleResults != null)
637 for (int r = 0; r < visibleResults.length; r += 2)
639 sr.drawHighlightedText(nextSeq, visibleResults[r],
640 visibleResults[r + 1], (visibleResults[r] - startRes)
641 * avcharWidth, offset
642 + ((i - startSeq) * avcharHeight));
647 if (av.cursorMode && cursorY == i && cursorX >= startRes
648 && cursorX <= endRes)
650 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * avcharWidth,
651 offset + ((i - startSeq) * avcharHeight));
655 if (av.getSelectionGroup() != null
656 || av.getAlignment().getGroups().size() > 0)
658 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
663 private void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
664 int startSeq, int endSeq, int offset)
667 // ///////////////////////////////////
668 // Now outline any areas if necessary
669 // ///////////////////////////////////
670 SequenceGroup group = av.getSelectionGroup();
677 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
679 group = av.getAlignment().getGroups().get(0);
689 boolean inGroup = false;
692 int alHeight = av.getAlignment().getHeight() - 1;
694 for (i = startSeq; i < endSeq; i++)
696 sx = (group.getStartRes() - startRes) * avcharWidth;
697 sy = offset + ((i - startSeq) * avcharHeight);
698 ex = (((group.getEndRes() + 1) - group.getStartRes()) * avcharWidth) - 1;
700 if (sx + ex < 0 || sx > imgWidth)
705 if ((sx <= (endRes - startRes) * avcharWidth)
706 && group.getSequences(null).contains(
707 av.getAlignment().getSequenceAt(i)))
710 && (i >= alHeight || !group.getSequences(null)
712 av.getAlignment().getSequenceAt(i + 1))))
714 bottom = sy + avcharHeight;
719 if (((top == -1) && (i == 0))
720 || !group.getSequences(null).contains(
721 av.getAlignment().getSequenceAt(i - 1)))
729 if (group == av.getSelectionGroup())
731 g.setColor(Color.red);
735 g.setColor(ColorUtils.getColor(group.getOutlineColour()));
743 if (sx >= 0 && sx < imgWidth)
745 g.drawLine(sx, oldY, sx, sy);
748 if (sx + ex < imgWidth)
750 g.drawLine(sx + ex, oldY, sx + ex, sy);
759 if (sx + ex > imgWidth)
764 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
766 ex = (endRes - startRes + 1) * avcharWidth;
771 g.drawLine(sx, top, sx + ex, top);
777 g.drawLine(sx, bottom, sx + ex, bottom);
788 sy = offset + ((i - startSeq) * avcharHeight);
789 if (sx >= 0 && sx < imgWidth)
791 g.drawLine(sx, oldY, sx, sy);
794 if (sx + ex < imgWidth)
796 g.drawLine(sx + ex, oldY, sx + ex, sy);
805 if (sx + ex > imgWidth)
809 else if (sx + ex >= (endRes - startRes + 1) * avcharWidth)
811 ex = (endRes - startRes + 1) * avcharWidth;
816 g.drawLine(sx, top, sx + ex, top);
822 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
831 if (groupIndex >= av.getAlignment().getGroups().size())
836 group = av.getAlignment().getGroups().get(groupIndex);
837 } while (groupIndex < av.getAlignment().getGroups().size());
842 public void highlightSearchResults(SearchResults results)
844 searchResults = results;