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.
24 import java.awt.image.*;
27 import jalview.datamodel.*;
35 public class SeqCanvas extends JComponent
37 final FeatureRenderer fr;
39 final SequenceRenderer sr;
51 SearchResults searchResults = null;
53 boolean fastPaint = false;
64 * Creates a new SeqCanvas object.
69 public SeqCanvas(AlignmentPanel ap)
72 fr = new FeatureRenderer(ap);
73 sr = new SequenceRenderer(av);
74 setLayout(new BorderLayout());
75 PaintRefresher.Register(this, av.getSequenceSetId());
76 setBackground(Color.white);
79 public SequenceRenderer getSequenceRenderer()
84 public FeatureRenderer getFeatureRenderer()
101 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
103 int scalestartx = startx - (startx % 10) + 10;
105 g.setColor(Color.black);
108 for (int i = scalestartx; i < endx; i += 10)
111 if (av.hasHiddenColumns())
113 value = av.getColumnSelection().adjustForHiddenColumns(value);
116 g.drawString(String.valueOf(value), (i - startx - 1) * av.charWidth,
117 ypos - (av.charHeight / 2));
119 g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
120 (ypos + 2) - (av.charHeight / 2),
121 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
138 void drawWestScale(Graphics g, int startx, int endx, int ypos)
140 FontMetrics fm = getFontMetrics(av.getFont());
141 ypos += av.charHeight;
143 if (av.hasHiddenColumns())
145 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
146 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
149 int maxwidth = av.getAlignment().getWidth();
150 if (av.hasHiddenColumns())
152 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
156 for (int i = 0; i < av.getAlignment().getHeight(); i++)
158 SequenceI seq = av.getAlignment().getSequenceAt(i);
164 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
171 value = av.getAlignment().getSequenceAt(i).findPosition(index);
178 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
180 g.drawString(value + "", x, (ypos + (i * av.charHeight))
181 - (av.charHeight / 5));
198 void drawEastScale(Graphics g, int startx, int endx, int ypos)
200 ypos += av.charHeight;
202 if (av.hasHiddenColumns())
204 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
209 for (int i = 0; i < av.getAlignment().getHeight(); i++)
211 seq = av.getAlignment().getSequenceAt(i);
215 while (index > startx)
217 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
224 value = seq.findPosition(index);
231 g.drawString(String.valueOf(value), 0, (ypos + (i * av.charHeight))
232 - (av.charHeight / 5));
237 boolean fastpainting = false;
240 * need to make this thread safe move alignment rendering in response to
246 * shift up or down in repaint
248 public void fastPaint(int horizontal, int vertical)
262 gg.copyArea(horizontal * av.charWidth, vertical * av.charHeight,
263 imgWidth, imgHeight, -horizontal * av.charWidth, -vertical
266 int sr = av.startRes;
268 int ss = av.startSeq;
273 if (horizontal > 0) // scrollbar pulled right, image to the left
276 transX = (er - sr - horizontal) * av.charWidth;
277 sr = er - horizontal;
279 else if (horizontal < 0)
281 er = sr - horizontal - 1;
283 else if (vertical > 0) // scroll down
287 if (ss < av.startSeq)
288 { // ie scrolling too fast, more than a page at a time
293 transY = imgHeight - (vertical * av.charHeight);
296 else if (vertical < 0)
306 gg.translate(transX, transY);
307 drawPanel(gg, sr, er, ss, es, 0);
308 gg.translate(-transX, -transY);
311 fastpainting = false;
315 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
316 * towards! startx is the first residue (starting at 0) to display. endx is
317 * the last residue to display (starting at 0). starty is the first sequence
318 * to display (starting at 0). endy is the last sequence to display (starting
319 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
320 * adjustment listener in SeqPanel when the scrollbars move.
323 // Set this to false to force a full panel paint
324 public void paintComponent(Graphics g)
326 BufferedImage lcimg = img; // take reference since other threads may null
327 // img and call later.
328 super.paintComponent(g);
332 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
333 .getClipBounds().height)))
335 g.drawImage(lcimg, 0, 0, this);
340 // this draws the whole of the alignment
341 imgWidth = getWidth();
342 imgHeight = getHeight();
344 imgWidth -= (imgWidth % av.charWidth);
345 imgHeight -= (imgHeight % av.charHeight);
347 if ((imgWidth < 1) || (imgHeight < 1))
352 if (lcimg == null || imgWidth != lcimg.getWidth()
353 || imgHeight != lcimg.getHeight())
357 lcimg = img = new BufferedImage(imgWidth, imgHeight,
358 BufferedImage.TYPE_INT_RGB);
359 gg = (Graphics2D) img.getGraphics();
360 gg.setFont(av.getFont());
361 } catch (OutOfMemoryError er)
364 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
365 new OOMWarning("Creating alignment image for display", er);
373 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
374 RenderingHints.VALUE_ANTIALIAS_ON);
377 gg.setColor(Color.white);
378 gg.fillRect(0, 0, imgWidth, imgHeight);
380 if (av.getWrapAlignment())
382 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
386 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
389 g.drawImage(lcimg, 0, 0, this);
399 * @return DOCUMENT ME!
401 public int getWrappedCanvasWidth(int cwidth)
403 FontMetrics fm = getFontMetrics(av.getFont());
408 if (av.scaleRightWrapped)
410 LABEL_EAST = fm.stringWidth(getMask());
413 if (av.scaleLeftWrapped)
415 LABEL_WEST = fm.stringWidth(getMask());
418 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
422 * Generates a string of zeroes.
431 for (int i = 0; i < av.getAlignment().getHeight(); i++)
433 tmp = av.getAlignment().getSequenceAt(i).getEnd();
440 for (int i = maxWidth; i > 0; i /= 10)
454 * @param canvasHeight
459 public void drawWrappedPanel(Graphics g, int canvasWidth,
460 int canvasHeight, int startRes)
462 AlignmentI al = av.getAlignment();
464 FontMetrics fm = getFontMetrics(av.getFont());
466 if (av.scaleRightWrapped)
468 LABEL_EAST = fm.stringWidth(getMask());
471 if (av.scaleLeftWrapped)
473 LABEL_WEST = fm.stringWidth(getMask());
476 int hgap = av.charHeight;
477 if (av.scaleAboveWrapped)
479 hgap += av.charHeight;
482 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
483 int cHeight = av.getAlignment().getHeight() * av.charHeight;
485 av.setWrappedWidth(cWidth);
487 av.endRes = av.startRes + cWidth;
491 int maxwidth = av.getAlignment().getWidth() - 1;
493 if (av.hasHiddenColumns())
495 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
498 while ((ypos <= canvasHeight) && (startRes < maxwidth))
500 endx = startRes + cWidth - 1;
507 g.setFont(av.getFont());
508 g.setColor(Color.black);
510 if (av.scaleLeftWrapped)
512 drawWestScale(g, startRes, endx, ypos);
515 if (av.scaleRightWrapped)
517 g.translate(canvasWidth - LABEL_EAST, 0);
518 drawEastScale(g, startRes, endx, ypos);
519 g.translate(-(canvasWidth - LABEL_EAST), 0);
522 g.translate(LABEL_WEST, 0);
524 if (av.scaleAboveWrapped)
526 drawNorthScale(g, startRes, endx, ypos);
529 if (av.hasHiddenColumns() && av.showHiddenMarkers)
531 g.setColor(Color.blue);
533 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
536 res = av.getColumnSelection().findHiddenRegionPosition(i)
539 if (res < 0 || res > endx - startRes)
544 gg.fillPolygon(new int[]
545 { res * av.charWidth - av.charHeight / 4,
546 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
548 { ypos - (av.charHeight / 2), ypos - (av.charHeight / 2),
549 ypos - (av.charHeight / 2) + 8 }, 3);
554 // When printing we have an extra clipped region,
555 // the Printable page which we need to account for here
556 Shape clip = g.getClip();
560 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
564 g.setClip(0, (int) clip.getBounds().getY(), cWidth * av.charWidth,
565 (int) clip.getBounds().getHeight());
568 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
570 if (av.showAnnotation)
572 g.translate(0, cHeight + ypos + 3);
573 if (annotations == null)
575 annotations = new AnnotationPanel(av);
578 annotations.renderer.drawComponent(annotations, av, (Graphics2D) g,
579 -1, startRes, endx + 1);
580 g.translate(0, -cHeight - ypos - 3);
583 g.translate(-LABEL_WEST, 0);
585 ypos += cHeight + getAnnotationHeight() + hgap;
591 AnnotationPanel annotations;
593 int getAnnotationHeight()
595 if (!av.showAnnotation)
600 if (annotations == null)
602 annotations = new AnnotationPanel(av);
605 return annotations.adjustPanelHeight();
624 void drawPanel(Graphics g1, int startRes, int endRes, int startSeq,
625 int endSeq, int offset)
627 if (!av.hasHiddenColumns())
629 draw(g1, startRes, endRes, startSeq, endSeq, offset);
633 java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
636 int blockStart = startRes;
637 int blockEnd = endRes;
639 for (int i = 0; regions != null && i < regions.size(); i++)
641 int[] region = (int[]) regions.elementAt(i);
642 int hideStart = region[0];
643 int hideEnd = region[1];
645 if (hideStart <= blockStart)
647 blockStart += (hideEnd - hideStart) + 1;
651 blockEnd = hideStart - 1;
653 g1.translate(screenY * av.charWidth, 0);
655 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
657 if (av.getShowHiddenMarkers())
659 g1.setColor(Color.blue);
661 g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
662 0 + offset, (blockEnd - blockStart + 1) * av.charWidth
663 - 1, (endSeq - startSeq) * av.charHeight + offset);
666 g1.translate(-screenY * av.charWidth, 0);
667 screenY += blockEnd - blockStart + 1;
668 blockStart = hideEnd + 1;
671 if (screenY <= (endRes - startRes))
673 blockEnd = blockStart + (endRes - startRes) - screenY;
674 g1.translate(screenY * av.charWidth, 0);
675 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
677 g1.translate(-screenY * av.charWidth, 0);
683 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
684 // int x1, int x2, int y1, int y2, int startx, int starty,
685 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
688 g.setFont(av.getFont());
689 sr.prepare(g, av.renderGaps);
693 // / First draw the sequences
694 // ///////////////////////////
695 for (int i = startSeq; i < endSeq; i++)
697 nextSeq = av.getAlignment().getSequenceAt(i);
700 // occasionally, a race condition occurs such that the alignment row is
704 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
705 startRes, endRes, offset + ((i - startSeq) * av.charHeight));
707 if (av.showSequenceFeatures)
709 fr.drawSequence(g, nextSeq, startRes, endRes, offset
710 + ((i - startSeq) * av.charHeight));
713 // / Highlight search Results once all sequences have been drawn
714 // ////////////////////////////////////////////////////////
715 if (searchResults != null)
717 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
719 if (visibleResults != null)
721 for (int r = 0; r < visibleResults.length; r += 2)
723 sr.drawHighlightedText(nextSeq, visibleResults[r],
724 visibleResults[r + 1], (visibleResults[r] - startRes)
725 * av.charWidth, offset
726 + ((i - startSeq) * av.charHeight));
731 if (av.cursorMode && cursorY == i && cursorX >= startRes
732 && cursorX <= endRes)
734 sr.drawCursor(nextSeq, cursorX,
735 (cursorX - startRes) * av.charWidth, offset
736 + ((i - startSeq) * av.charHeight));
740 if (av.getSelectionGroup() != null
741 || av.getAlignment().getGroups().size() > 0)
743 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
748 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
749 int startSeq, int endSeq, int offset)
751 Graphics2D g = (Graphics2D) g1;
753 // ///////////////////////////////////
754 // Now outline any areas if necessary
755 // ///////////////////////////////////
756 SequenceGroup group = av.getSelectionGroup();
762 int visWidth = (endRes - startRes + 1) * av.charWidth;
764 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
766 group = (SequenceGroup) av.getAlignment().getGroups().get(0);
776 boolean inGroup = false;
780 for (i = startSeq; i < endSeq; i++)
782 sx = (group.getStartRes() - startRes) * av.charWidth;
783 sy = offset + ((i - startSeq) * av.charHeight);
784 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
786 if (sx + ex < 0 || sx > visWidth)
791 if ((sx <= (endRes - startRes) * av.charWidth)
792 && group.getSequences(null).contains(
793 av.getAlignment().getSequenceAt(i)))
796 && !group.getSequences(null).contains(
797 av.getAlignment().getSequenceAt(i + 1)))
799 bottom = sy + av.charHeight;
804 if (((top == -1) && (i == 0))
805 || !group.getSequences(null).contains(
806 av.getAlignment().getSequenceAt(i - 1)))
814 if (group == av.getSelectionGroup())
816 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
817 BasicStroke.JOIN_ROUND, 3f, new float[]
819 g.setColor(Color.RED);
823 g.setStroke(new BasicStroke());
824 g.setColor(group.getOutlineColour());
832 if (sx >= 0 && sx < visWidth)
834 g.drawLine(sx, oldY, sx, sy);
837 if (sx + ex < visWidth)
839 g.drawLine(sx + ex, oldY, sx + ex, sy);
848 if (sx + ex > visWidth)
853 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
855 ex = (endRes - startRes + 1) * av.charWidth;
860 g.drawLine(sx, top, sx + ex, top);
866 g.drawLine(sx, bottom, sx + ex, bottom);
877 sy = offset + ((i - startSeq) * av.charHeight);
878 if (sx >= 0 && sx < visWidth)
880 g.drawLine(sx, oldY, sx, sy);
883 if (sx + ex < visWidth)
885 g.drawLine(sx + ex, oldY, sx + ex, sy);
894 if (sx + ex > visWidth)
898 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
900 ex = (endRes - startRes + 1) * av.charWidth;
905 g.drawLine(sx, top, sx + ex, top);
911 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
920 g.setStroke(new BasicStroke());
922 if (groupIndex >= av.getAlignment().getGroups().size())
927 group = (SequenceGroup) av.getAlignment().getGroups()
930 } while (groupIndex < av.getAlignment().getGroups().size());
942 public void highlightSearchResults(SearchResults results)
946 searchResults = results;