2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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());
66 public AlignViewport getViewport()
71 public FeatureRenderer getFeatureRenderer()
76 public SequenceRenderer getSequenceRenderer()
81 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
83 int scalestartx = startx - startx % 10 + 10;
85 g.setColor(Color.black);
88 for (int i = scalestartx; i < endx; i += 10)
91 if (av.hasHiddenColumns())
93 value = av.getColumnSelection().adjustForHiddenColumns(value);
96 g.drawString(String.valueOf(value), (i - startx - 1) * av.charWidth,
97 ypos - (av.charHeight / 2));
99 g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
100 (ypos + 2) - (av.charHeight / 2),
101 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
106 void drawWestScale(Graphics g, int startx, int endx, int ypos)
108 FontMetrics fm = getFontMetrics(av.getFont());
109 ypos += av.charHeight;
110 if (av.hasHiddenColumns())
112 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
113 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
116 int maxwidth = av.getAlignment().getWidth();
117 if (av.hasHiddenColumns())
119 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
123 for (int i = 0; i < av.getAlignment().getHeight(); i++)
125 SequenceI seq = av.getAlignment().getSequenceAt(i);
131 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
138 value = av.getAlignment().getSequenceAt(i).findPosition(index);
145 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
147 g.drawString(value + "", x, (ypos + (i * av.charHeight))
148 - (av.charHeight / 5));
153 void drawEastScale(Graphics g, int startx, int endx, int ypos)
155 ypos += av.charHeight;
157 if (av.hasHiddenColumns())
159 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
164 for (int i = 0; i < av.getAlignment().getHeight(); i++)
166 seq = av.getAlignment().getSequenceAt(i);
170 while (index > startx)
172 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
179 value = seq.findPosition(index);
186 g.drawString(String.valueOf(value), 0, (ypos + (i * av.charHeight))
187 - (av.charHeight / 5));
194 void fastPaint(int horizontal, int vertical)
196 if (fastPaint || gg == null)
201 // Its possible on certain browsers that the call to fastpaint
202 // is faster than it can paint, so this check here catches
204 if (lastsr + horizontal != av.startRes)
206 horizontal = av.startRes - lastsr;
209 lastsr = av.startRes;
212 gg.copyArea(horizontal * av.charWidth, vertical * av.charHeight,
213 imgWidth - horizontal * av.charWidth, imgHeight - vertical
214 * av.charHeight, -horizontal * av.charWidth, -vertical
217 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
219 if (horizontal > 0) // scrollbar pulled right, image to the left
221 transX = (er - sr - horizontal) * av.charWidth;
222 sr = er - horizontal;
224 else if (horizontal < 0)
226 er = sr - horizontal;
229 else if (vertical > 0) // scroll down
232 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
238 transY = imgHeight - vertical * av.charHeight;
241 else if (vertical < 0)
250 gg.translate(transX, transY);
252 drawPanel(gg, sr, er, ss, es, 0);
253 gg.translate(-transX, -transY);
260 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
261 * towards! startx is the first residue (starting at 0) to display. endx is
262 * the last residue to display (starting at 0). starty is the first sequence
263 * to display (starting at 0). endy is the last sequence to display (starting
264 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
265 * adjustment listener in SeqPanel when the scrollbars move.
267 public void update(Graphics g)
272 public void paint(Graphics g)
276 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
277 .getClipBounds().height)))
279 g.drawImage(img, 0, 0, this);
286 g.drawImage(img, 0, 0, this);
291 // this draws the whole of the alignment
292 imgWidth = this.getSize().width;
293 imgHeight = this.getSize().height;
295 imgWidth -= imgWidth % av.charWidth;
296 imgHeight -= imgHeight % av.charHeight;
298 if (imgWidth < 1 || imgHeight < 1)
303 if (img == null || imgWidth != img.getWidth(this)
304 || imgHeight != img.getHeight(this))
306 img = createImage(imgWidth, imgHeight);
307 gg = img.getGraphics();
308 gg.setFont(av.getFont());
311 gg.setColor(Color.white);
312 gg.fillRect(0, 0, imgWidth, imgHeight);
314 if (av.getWrapAlignment())
316 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
320 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
323 g.drawImage(img, 0, 0, this);
327 int LABEL_WEST, LABEL_EAST;
329 public int getWrappedCanvasWidth(int cwidth)
331 cwidth -= cwidth % av.charWidth;
333 FontMetrics fm = getFontMetrics(av.getFont());
338 if (av.scaleRightWrapped)
340 LABEL_EAST = fm.stringWidth(getMask());
343 if (av.scaleLeftWrapped)
345 LABEL_WEST = fm.stringWidth(getMask());
348 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
352 * Generates a string of zeroes.
361 AlignmentI alignment = av.getAlignment();
362 for (int i = 0; i < alignment.getHeight(); i++)
364 tmp = alignment.getSequenceAt(i).getEnd();
371 for (int i = maxWidth; i > 0; i /= 10)
378 public void drawWrappedPanel(Graphics g, int canvasWidth,
379 int canvasHeight, int startRes)
381 AlignmentI al = av.getAlignment();
383 FontMetrics fm = getFontMetrics(av.getFont());
385 if (av.scaleRightWrapped)
387 LABEL_EAST = fm.stringWidth(getMask());
390 if (av.scaleLeftWrapped)
392 LABEL_WEST = fm.stringWidth(getMask());
395 int hgap = av.charHeight;
396 if (av.scaleAboveWrapped)
398 hgap += av.charHeight;
401 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
402 int cHeight = av.getAlignment().getHeight() * av.charHeight;
404 av.setWrappedWidth(cWidth);
406 av.endRes = av.startRes + cWidth;
411 int maxwidth = av.getAlignment().getWidth() - 1;
413 if (av.hasHiddenColumns())
415 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
418 while ((ypos <= canvasHeight) && (startRes < maxwidth))
420 endx = startRes + cWidth - 1;
427 g.setColor(Color.black);
429 if (av.scaleLeftWrapped)
431 drawWestScale(g, startRes, endx, ypos);
434 if (av.scaleRightWrapped)
436 g.translate(canvasWidth - LABEL_EAST, 0);
437 drawEastScale(g, startRes, endx, ypos);
438 g.translate(-(canvasWidth - LABEL_EAST), 0);
441 g.translate(LABEL_WEST, 0);
443 if (av.scaleAboveWrapped)
445 drawNorthScale(g, startRes, endx, ypos);
447 if (av.hasHiddenColumns() && av.showHiddenMarkers)
449 g.setColor(Color.blue);
451 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
454 res = av.getColumnSelection().findHiddenRegionPosition(i)
457 if (res < 0 || res > endx - startRes)
462 gg.fillPolygon(new int[]
463 { res * av.charWidth - av.charHeight / 4,
464 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
466 { ypos - (av.charHeight / 2), ypos - (av.charHeight / 2),
467 ypos - (av.charHeight / 2) + 8 }, 3);
472 if (g.getClip() == null)
474 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
477 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
480 if (av.showAnnotation)
482 g.translate(0, cHeight + ypos + 4);
483 if (annotations == null)
485 annotations = new AnnotationPanel(av);
488 annotations.drawComponent(g, startRes, endx + 1);
489 g.translate(0, -cHeight - ypos - 4);
491 g.translate(-LABEL_WEST, 0);
493 ypos += cHeight + getAnnotationHeight() + hgap;
500 AnnotationPanel annotations;
502 int getAnnotationHeight()
504 if (!av.showAnnotation)
509 if (annotations == null)
511 annotations = new AnnotationPanel(av);
514 return annotations.adjustPanelHeight();
517 void drawPanel(Graphics g1, int startRes, int endRes, int startSeq,
518 int endSeq, int offset)
520 if (!av.hasHiddenColumns())
522 draw(g1, startRes, endRes, startSeq, endSeq, offset);
526 java.util.List<int[]> regions = av.getColumnSelection()
530 int blockStart = startRes;
531 int blockEnd = endRes;
533 for (int i = 0; i < regions.size(); i++)
535 int[] region = regions.get(i);
536 int hideStart = region[0];
537 int hideEnd = region[1];
539 if (hideStart <= blockStart)
541 blockStart += (hideEnd - hideStart) + 1;
545 blockEnd = hideStart - 1;
547 g1.translate(screenY * av.charWidth, 0);
549 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
551 if (av.getShowHiddenMarkers())
553 g1.setColor(Color.blue);
554 g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
555 0 + offset, (blockEnd - blockStart + 1) * av.charWidth
556 - 1, (endSeq - startSeq) * av.charHeight + offset);
559 g1.translate(-screenY * av.charWidth, 0);
560 screenY += blockEnd - blockStart + 1;
561 blockStart = hideEnd + 1;
564 if (screenY <= (endRes - startRes))
566 blockEnd = blockStart + (endRes - startRes) - screenY;
567 g1.translate(screenY * av.charWidth, 0);
568 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
570 g1.translate(-screenY * av.charWidth, 0);
576 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
577 // int x1, int x2, int y1, int y2, int startx, int starty,
578 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
581 g.setFont(av.getFont());
582 sr.prepare(g, av.renderGaps);
586 // / First draw the sequences
587 // ///////////////////////////
588 for (int i = startSeq; i < endSeq; i++)
590 nextSeq = av.getAlignment().getSequenceAt(i);
597 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
598 startRes, endRes, offset + ((i - startSeq) * av.charHeight));
600 if (av.isShowSequenceFeatures())
602 fr.drawSequence(g, nextSeq, startRes, endRes, offset
603 + ((i - startSeq) * av.charHeight));
606 // / Highlight search Results once all sequences have been drawn
607 // ////////////////////////////////////////////////////////
608 if (searchResults != null)
610 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
612 if (visibleResults != null)
614 for (int r = 0; r < visibleResults.length; r += 2)
616 sr.drawHighlightedText(nextSeq, visibleResults[r],
617 visibleResults[r + 1], (visibleResults[r] - startRes)
618 * av.charWidth, offset
619 + ((i - startSeq) * av.charHeight));
624 if (av.cursorMode && cursorY == i && cursorX >= startRes
625 && cursorX <= endRes)
627 sr.drawCursor(nextSeq, cursorX,
628 (cursorX - startRes) * av.charWidth, offset
629 + ((i - startSeq) * av.charHeight));
633 if (av.getSelectionGroup() != null
634 || av.getAlignment().getGroups().size() > 0)
636 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
641 void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
642 int startSeq, int endSeq, int offset)
645 // ///////////////////////////////////
646 // Now outline any areas if necessary
647 // ///////////////////////////////////
648 SequenceGroup group = av.getSelectionGroup();
655 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
657 group = av.getAlignment().getGroups().get(0);
667 boolean inGroup = false;
670 int alHeight = av.getAlignment().getHeight() - 1;
672 for (i = startSeq; i < endSeq; i++)
674 sx = (group.getStartRes() - startRes) * av.charWidth;
675 sy = offset + ((i - startSeq) * av.charHeight);
676 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
678 if (sx + ex < 0 || sx > imgWidth)
683 if ((sx <= (endRes - startRes) * av.charWidth)
684 && group.getSequences(null).contains(
685 av.getAlignment().getSequenceAt(i)))
688 && (i >= alHeight || !group.getSequences(null)
690 av.getAlignment().getSequenceAt(i + 1))))
692 bottom = sy + av.charHeight;
697 if (((top == -1) && (i == 0))
698 || !group.getSequences(null).contains(
699 av.getAlignment().getSequenceAt(i - 1)))
707 if (group == av.getSelectionGroup())
709 g.setColor(Color.red);
713 g.setColor(group.getOutlineColour());
721 if (sx >= 0 && sx < imgWidth)
723 g.drawLine(sx, oldY, sx, sy);
726 if (sx + ex < imgWidth)
728 g.drawLine(sx + ex, oldY, sx + ex, sy);
737 if (sx + ex > imgWidth)
742 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
744 ex = (endRes - startRes + 1) * av.charWidth;
749 g.drawLine(sx, top, sx + ex, top);
755 g.drawLine(sx, bottom, sx + ex, bottom);
766 sy = offset + ((i - startSeq) * av.charHeight);
767 if (sx >= 0 && sx < imgWidth)
769 g.drawLine(sx, oldY, sx, sy);
772 if (sx + ex < imgWidth)
774 g.drawLine(sx + ex, oldY, sx + ex, sy);
783 if (sx + ex > imgWidth)
787 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
789 ex = (endRes - startRes + 1) * av.charWidth;
794 g.drawLine(sx, top, sx + ex, top);
800 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
809 if (groupIndex >= av.getAlignment().getGroups().size())
814 group = av.getAlignment().getGroups()
816 } while (groupIndex < av.getAlignment().getGroups().size());
821 public void highlightSearchResults(SearchResults results)
823 searchResults = results;