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;
25 import jalview.datamodel.*;
27 public class SeqCanvas extends Panel
43 SearchResults searchResults = null;
45 boolean fastPaint = false;
51 public SeqCanvas(AlignViewport av)
54 fr = new FeatureRenderer(av);
55 sr = new SequenceRenderer(av);
56 PaintRefresher.Register(this, av.getSequenceSetId());
59 public AlignViewport getViewport()
64 public FeatureRenderer getFeatureRenderer()
69 public SequenceRenderer getSequenceRenderer()
74 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
76 int scalestartx = startx - startx % 10 + 10;
78 g.setColor(Color.black);
81 for (int i = scalestartx; i < endx; i += 10)
84 if (av.hasHiddenColumns())
86 value = av.getColumnSelection().adjustForHiddenColumns(value);
89 g.drawString(String.valueOf(value), (i - startx - 1) * av.charWidth,
90 ypos - (av.charHeight / 2));
92 g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
93 (ypos + 2) - (av.charHeight / 2),
94 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
99 void drawWestScale(Graphics g, int startx, int endx, int ypos)
101 FontMetrics fm = getFontMetrics(av.getFont());
102 ypos += av.charHeight;
103 if (av.hasHiddenColumns())
105 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
106 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
109 int maxwidth = av.getAlignment().getWidth();
110 if (av.hasHiddenColumns())
112 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
116 for (int i = 0; i < av.getAlignment().getHeight(); i++)
118 SequenceI seq = av.getAlignment().getSequenceAt(i);
124 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
131 value = av.getAlignment().getSequenceAt(i).findPosition(index);
138 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
140 g.drawString(value + "", x, (ypos + (i * av.charHeight))
141 - (av.charHeight / 5));
146 void drawEastScale(Graphics g, int startx, int endx, int ypos)
148 ypos += av.charHeight;
150 if (av.hasHiddenColumns())
152 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
157 for (int i = 0; i < av.getAlignment().getHeight(); i++)
159 seq = av.getAlignment().getSequenceAt(i);
163 while (index > startx)
165 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
172 value = seq.findPosition(index);
179 g.drawString(String.valueOf(value), 0, (ypos + (i * av.charHeight))
180 - (av.charHeight / 5));
187 void fastPaint(int horizontal, int vertical)
189 if (fastPaint || gg == null)
194 // Its possible on certain browsers that the call to fastpaint
195 // is faster than it can paint, so this check here catches
197 if (lastsr + horizontal != av.startRes)
199 horizontal = av.startRes - lastsr;
202 lastsr = av.startRes;
205 gg.copyArea(horizontal * av.charWidth, vertical * av.charHeight,
206 imgWidth - horizontal * av.charWidth, imgHeight - vertical
207 * av.charHeight, -horizontal * av.charWidth, -vertical
210 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq, transX = 0, transY = 0;
212 if (horizontal > 0) // scrollbar pulled right, image to the left
214 transX = (er - sr - horizontal) * av.charWidth;
215 sr = er - horizontal;
217 else if (horizontal < 0)
219 er = sr - horizontal;
222 else if (vertical > 0) // scroll down
225 if (ss < av.startSeq) // ie scrolling too fast, more than a page at a time
231 transY = imgHeight - vertical * av.charHeight;
234 else if (vertical < 0)
243 gg.translate(transX, transY);
245 drawPanel(gg, sr, er, ss, es, 0);
246 gg.translate(-transX, -transY);
253 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
254 * towards! startx is the first residue (starting at 0) to display. endx is
255 * the last residue to display (starting at 0). starty is the first sequence
256 * to display (starting at 0). endy is the last sequence to display (starting
257 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
258 * adjustment listener in SeqPanel when the scrollbars move.
260 public void update(Graphics g)
265 public void paint(Graphics g)
269 && (fastPaint || (getSize().width != g.getClipBounds().width) || (getSize().height != g
270 .getClipBounds().height)))
272 g.drawImage(img, 0, 0, this);
279 g.drawImage(img, 0, 0, this);
284 // this draws the whole of the alignment
285 imgWidth = this.getSize().width;
286 imgHeight = this.getSize().height;
288 imgWidth -= imgWidth % av.charWidth;
289 imgHeight -= imgHeight % av.charHeight;
291 if (imgWidth < 1 || imgHeight < 1)
296 if (img == null || imgWidth != img.getWidth(this)
297 || imgHeight != img.getHeight(this))
299 img = createImage(imgWidth, imgHeight);
300 gg = img.getGraphics();
301 gg.setFont(av.getFont());
304 gg.setColor(Color.white);
305 gg.fillRect(0, 0, imgWidth, imgHeight);
307 if (av.getWrapAlignment())
309 drawWrappedPanel(gg, imgWidth, imgHeight, av.startRes);
313 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
316 g.drawImage(img, 0, 0, this);
320 int LABEL_WEST, LABEL_EAST;
322 public int getWrappedCanvasWidth(int cwidth)
324 cwidth -= cwidth % av.charWidth;
326 FontMetrics fm = getFontMetrics(av.getFont());
331 if (av.scaleRightWrapped)
333 LABEL_EAST = fm.stringWidth(getMask());
336 if (av.scaleLeftWrapped)
338 LABEL_WEST = fm.stringWidth(getMask());
341 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
345 * Generates a string of zeroes.
354 AlignmentI alignment = av.getAlignment();
355 for (int i = 0; i < alignment.getHeight(); i++)
357 tmp = alignment.getSequenceAt(i).getEnd();
364 for (int i = maxWidth; i > 0; i /= 10)
371 public void drawWrappedPanel(Graphics g, int canvasWidth,
372 int canvasHeight, int startRes)
374 AlignmentI al = av.getAlignment();
376 FontMetrics fm = getFontMetrics(av.getFont());
378 if (av.scaleRightWrapped)
380 LABEL_EAST = fm.stringWidth(getMask());
383 if (av.scaleLeftWrapped)
385 LABEL_WEST = fm.stringWidth(getMask());
388 int hgap = av.charHeight;
389 if (av.scaleAboveWrapped)
391 hgap += av.charHeight;
394 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
395 int cHeight = av.getAlignment().getHeight() * av.charHeight;
397 av.setWrappedWidth(cWidth);
399 av.endRes = av.startRes + cWidth;
404 int maxwidth = av.getAlignment().getWidth() - 1;
406 if (av.hasHiddenColumns())
408 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
411 while ((ypos <= canvasHeight) && (startRes < maxwidth))
413 endx = startRes + cWidth - 1;
420 g.setColor(Color.black);
422 if (av.scaleLeftWrapped)
424 drawWestScale(g, startRes, endx, ypos);
427 if (av.scaleRightWrapped)
429 g.translate(canvasWidth - LABEL_EAST, 0);
430 drawEastScale(g, startRes, endx, ypos);
431 g.translate(-(canvasWidth - LABEL_EAST), 0);
434 g.translate(LABEL_WEST, 0);
436 if (av.scaleAboveWrapped)
438 drawNorthScale(g, startRes, endx, ypos);
440 if (av.hasHiddenColumns() && av.showHiddenMarkers)
442 g.setColor(Color.blue);
444 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
447 res = av.getColumnSelection().findHiddenRegionPosition(i)
450 if (res < 0 || res > endx - startRes)
455 gg.fillPolygon(new int[]
456 { res * av.charWidth - av.charHeight / 4,
457 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
459 { ypos - (av.charHeight / 2), ypos - (av.charHeight / 2),
460 ypos - (av.charHeight / 2) + 8 }, 3);
465 if (g.getClip() == null)
467 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
470 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
473 if (av.showAnnotation)
475 g.translate(0, cHeight + ypos + 4);
476 if (annotations == null)
478 annotations = new AnnotationPanel(av);
481 annotations.drawComponent(g, startRes, endx + 1);
482 g.translate(0, -cHeight - ypos - 4);
484 g.translate(-LABEL_WEST, 0);
486 ypos += cHeight + getAnnotationHeight() + hgap;
493 AnnotationPanel annotations;
495 int getAnnotationHeight()
497 if (!av.showAnnotation)
502 if (annotations == null)
504 annotations = new AnnotationPanel(av);
507 return annotations.adjustPanelHeight();
510 void drawPanel(Graphics g1, int startRes, int endRes, int startSeq,
511 int endSeq, int offset)
513 if (!av.hasHiddenColumns())
515 draw(g1, startRes, endRes, startSeq, endSeq, offset);
519 java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
522 int blockStart = startRes;
523 int blockEnd = endRes;
525 for (int i = 0; i < regions.size(); i++)
527 int[] region = (int[]) regions.elementAt(i);
528 int hideStart = region[0];
529 int hideEnd = region[1];
531 if (hideStart <= blockStart)
533 blockStart += (hideEnd - hideStart) + 1;
537 blockEnd = hideStart - 1;
539 g1.translate(screenY * av.charWidth, 0);
541 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
543 if (av.getShowHiddenMarkers())
545 g1.setColor(Color.blue);
546 g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
547 0 + offset, (blockEnd - blockStart + 1) * av.charWidth
548 - 1, (endSeq - startSeq) * av.charHeight + offset);
551 g1.translate(-screenY * av.charWidth, 0);
552 screenY += blockEnd - blockStart + 1;
553 blockStart = hideEnd + 1;
556 if (screenY <= (endRes - startRes))
558 blockEnd = blockStart + (endRes - startRes) - screenY;
559 g1.translate(screenY * av.charWidth, 0);
560 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
562 g1.translate(-screenY * av.charWidth, 0);
568 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
569 // int x1, int x2, int y1, int y2, int startx, int starty,
570 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
573 g.setFont(av.getFont());
574 sr.prepare(g, av.renderGaps);
578 // / First draw the sequences
579 // ///////////////////////////
580 for (int i = startSeq; i < endSeq; i++)
582 nextSeq = av.getAlignment().getSequenceAt(i);
589 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
590 startRes, endRes, offset + ((i - startSeq) * av.charHeight));
592 if (av.showSequenceFeatures)
594 fr.drawSequence(g, nextSeq, startRes, endRes, offset
595 + ((i - startSeq) * av.charHeight));
598 // / Highlight search Results once all sequences have been drawn
599 // ////////////////////////////////////////////////////////
600 if (searchResults != null)
602 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
604 if (visibleResults != null)
606 for (int r = 0; r < visibleResults.length; r += 2)
608 sr.drawHighlightedText(nextSeq, visibleResults[r],
609 visibleResults[r + 1], (visibleResults[r] - startRes)
610 * av.charWidth, offset
611 + ((i - startSeq) * av.charHeight));
616 if (av.cursorMode && cursorY == i && cursorX >= startRes
617 && cursorX <= endRes)
619 sr.drawCursor(nextSeq, cursorX,
620 (cursorX - startRes) * av.charWidth, offset
621 + ((i - startSeq) * av.charHeight));
625 if (av.getSelectionGroup() != null
626 || av.getAlignment().getGroups().size() > 0)
628 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
633 void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
634 int startSeq, int endSeq, int offset)
637 // ///////////////////////////////////
638 // Now outline any areas if necessary
639 // ///////////////////////////////////
640 SequenceGroup group = av.getSelectionGroup();
647 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
649 group = (SequenceGroup) av.getAlignment().getGroups().get(0);
659 boolean inGroup = false;
662 int alHeight = av.getAlignment().getHeight() - 1;
664 for (i = startSeq; i < endSeq; i++)
666 sx = (group.getStartRes() - startRes) * av.charWidth;
667 sy = offset + ((i - startSeq) * av.charHeight);
668 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
670 if (sx + ex < 0 || sx > imgWidth)
675 if ((sx <= (endRes - startRes) * av.charWidth)
676 && group.getSequences(null).contains(
677 av.getAlignment().getSequenceAt(i)))
680 && (i >= alHeight || !group.getSequences(null)
682 av.getAlignment().getSequenceAt(i + 1))))
684 bottom = sy + av.charHeight;
689 if (((top == -1) && (i == 0))
690 || !group.getSequences(null).contains(
691 av.getAlignment().getSequenceAt(i - 1)))
699 if (group == av.getSelectionGroup())
701 g.setColor(Color.red);
705 g.setColor(group.getOutlineColour());
713 if (sx >= 0 && sx < imgWidth)
715 g.drawLine(sx, oldY, sx, sy);
718 if (sx + ex < imgWidth)
720 g.drawLine(sx + ex, oldY, sx + ex, sy);
729 if (sx + ex > imgWidth)
734 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
736 ex = (endRes - startRes + 1) * av.charWidth;
741 g.drawLine(sx, top, sx + ex, top);
747 g.drawLine(sx, bottom, sx + ex, bottom);
758 sy = offset + ((i - startSeq) * av.charHeight);
759 if (sx >= 0 && sx < imgWidth)
761 g.drawLine(sx, oldY, sx, sy);
764 if (sx + ex < imgWidth)
766 g.drawLine(sx + ex, oldY, sx + ex, sy);
775 if (sx + ex > imgWidth)
779 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
781 ex = (endRes - startRes + 1) * av.charWidth;
786 g.drawLine(sx, top, sx + ex, top);
792 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
801 if (groupIndex >= av.getAlignment().getGroups().size())
806 group = (SequenceGroup) av.getAlignment().getGroups()
808 } while (groupIndex < av.getAlignment().getGroups().size());
813 public void highlightSearchResults(SearchResults results)
815 searchResults = results;