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)
522 if (!av.hasHiddenColumns())
524 draw(g1, startRes, endRes, startSeq, endSeq, offset);
530 int blockStart = startRes;
531 int blockEnd = endRes;
533 if (av.hasHiddenColumns())
535 for (int[] region : av.getColumnSelection().getHiddenColumns())
537 int hideStart = region[0];
538 int hideEnd = region[1];
540 if (hideStart <= blockStart)
542 blockStart += (hideEnd - hideStart) + 1;
546 blockEnd = hideStart - 1;
548 g1.translate(screenY * av.charWidth, 0);
550 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
552 if (av.getShowHiddenMarkers())
554 g1.setColor(Color.blue);
555 g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
556 0 + offset, (blockEnd - blockStart + 1) * av.charWidth
557 - 1, (endSeq - startSeq) * av.charHeight
561 g1.translate(-screenY * av.charWidth, 0);
562 screenY += blockEnd - blockStart + 1;
563 blockStart = hideEnd + 1;
566 if (screenY <= (endRes - startRes))
568 blockEnd = blockStart + (endRes - startRes) - screenY;
569 g1.translate(screenY * av.charWidth, 0);
570 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
572 g1.translate(-screenY * av.charWidth, 0);
578 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
579 // int x1, int x2, int y1, int y2, int startx, int starty,
580 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
583 g.setFont(av.getFont());
584 sr.prepare(g, av.renderGaps);
588 // / First draw the sequences
589 // ///////////////////////////
590 for (int i = startSeq; i < endSeq; i++)
592 nextSeq = av.getAlignment().getSequenceAt(i);
599 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
600 startRes, endRes, offset + ((i - startSeq) * av.charHeight));
602 if (av.isShowSequenceFeatures())
604 fr.drawSequence(g, nextSeq, startRes, endRes, offset
605 + ((i - startSeq) * av.charHeight));
608 // / Highlight search Results once all sequences have been drawn
609 // ////////////////////////////////////////////////////////
610 if (searchResults != null)
612 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
614 if (visibleResults != null)
616 for (int r = 0; r < visibleResults.length; r += 2)
618 sr.drawHighlightedText(nextSeq, visibleResults[r],
619 visibleResults[r + 1], (visibleResults[r] - startRes)
620 * av.charWidth, offset
621 + ((i - startSeq) * av.charHeight));
626 if (av.cursorMode && cursorY == i && cursorX >= startRes
627 && cursorX <= endRes)
629 sr.drawCursor(nextSeq, cursorX,
630 (cursorX - startRes) * av.charWidth, offset
631 + ((i - startSeq) * av.charHeight));
635 if (av.getSelectionGroup() != null
636 || av.getAlignment().getGroups().size() > 0)
638 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
643 void drawGroupsBoundaries(Graphics g, int startRes, int endRes,
644 int startSeq, int endSeq, int offset)
647 // ///////////////////////////////////
648 // Now outline any areas if necessary
649 // ///////////////////////////////////
650 SequenceGroup group = av.getSelectionGroup();
657 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
659 group = av.getAlignment().getGroups().get(0);
669 boolean inGroup = false;
672 int alHeight = av.getAlignment().getHeight() - 1;
674 for (i = startSeq; i < endSeq; i++)
676 sx = (group.getStartRes() - startRes) * av.charWidth;
677 sy = offset + ((i - startSeq) * av.charHeight);
678 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
680 if (sx + ex < 0 || sx > imgWidth)
685 if ((sx <= (endRes - startRes) * av.charWidth)
686 && group.getSequences(null).contains(
687 av.getAlignment().getSequenceAt(i)))
690 && (i >= alHeight || !group.getSequences(null)
692 av.getAlignment().getSequenceAt(i + 1))))
694 bottom = sy + av.charHeight;
699 if (((top == -1) && (i == 0))
700 || !group.getSequences(null).contains(
701 av.getAlignment().getSequenceAt(i - 1)))
709 if (group == av.getSelectionGroup())
711 g.setColor(Color.red);
715 g.setColor(group.getOutlineColour());
723 if (sx >= 0 && sx < imgWidth)
725 g.drawLine(sx, oldY, sx, sy);
728 if (sx + ex < imgWidth)
730 g.drawLine(sx + ex, oldY, sx + ex, sy);
739 if (sx + ex > imgWidth)
744 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
746 ex = (endRes - startRes + 1) * av.charWidth;
751 g.drawLine(sx, top, sx + ex, top);
757 g.drawLine(sx, bottom, sx + ex, bottom);
768 sy = offset + ((i - startSeq) * av.charHeight);
769 if (sx >= 0 && sx < imgWidth)
771 g.drawLine(sx, oldY, sx, sy);
774 if (sx + ex < imgWidth)
776 g.drawLine(sx + ex, oldY, sx + ex, sy);
785 if (sx + ex > imgWidth)
789 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
791 ex = (endRes - startRes + 1) * av.charWidth;
796 g.drawLine(sx, top, sx + ex, top);
802 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
811 if (groupIndex >= av.getAlignment().getGroups().size())
816 group = av.getAlignment().getGroups()
818 } while (groupIndex < av.getAlignment().getGroups().size());
823 public void highlightSearchResults(SearchResults results)
825 searchResults = results;