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.
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.HiddenColumns;
25 import jalview.datamodel.SearchResultsI;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.datamodel.SequenceI;
28 import jalview.renderer.ScaleRenderer;
29 import jalview.renderer.ScaleRenderer.ScaleMark;
30 import jalview.viewmodel.ViewportRanges;
32 import java.awt.BasicStroke;
33 import java.awt.BorderLayout;
34 import java.awt.Color;
35 import java.awt.FontMetrics;
36 import java.awt.Graphics;
37 import java.awt.Graphics2D;
38 import java.awt.RenderingHints;
39 import java.awt.Shape;
40 import java.awt.image.BufferedImage;
41 import java.util.List;
43 import javax.swing.JComponent;
51 public class SeqCanvas extends JComponent
53 final FeatureRenderer fr;
55 final SequenceRenderer sr;
67 boolean fastPaint = false;
78 * Creates a new SeqCanvas object.
83 public SeqCanvas(AlignmentPanel ap)
87 fr = new FeatureRenderer(ap);
88 sr = new SequenceRenderer(av);
89 setLayout(new BorderLayout());
90 PaintRefresher.Register(this, av.getSequenceSetId());
91 setBackground(Color.white);
94 public SequenceRenderer getSequenceRenderer()
99 public FeatureRenderer getFeatureRenderer()
104 int charHeight = 0, charWidth = 0;
106 private void updateViewport()
108 charHeight = av.getCharHeight();
109 charWidth = av.getCharWidth();
124 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
127 for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
130 int mpos = mark.column; // (i - startx - 1)
135 String mstring = mark.text;
141 g.drawString(mstring, mpos * charWidth, ypos - (charHeight / 2));
143 g.drawLine((mpos * charWidth) + (charWidth / 2), (ypos + 2)
144 - (charHeight / 2), (mpos * charWidth) + (charWidth / 2),
162 void drawWestScale(Graphics g, int startx, int endx, int ypos)
164 FontMetrics fm = getFontMetrics(av.getFont());
167 if (av.hasHiddenColumns())
169 startx = av.getAlignment().getHiddenColumns()
170 .adjustForHiddenColumns(startx);
171 endx = av.getAlignment().getHiddenColumns()
172 .adjustForHiddenColumns(endx);
175 int maxwidth = av.getAlignment().getWidth();
176 if (av.hasHiddenColumns())
178 maxwidth = av.getAlignment().getHiddenColumns()
179 .findColumnPosition(maxwidth) - 1;
183 for (int i = 0; i < av.getAlignment().getHeight(); i++)
185 SequenceI seq = av.getAlignment().getSequenceAt(i);
191 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
198 value = av.getAlignment().getSequenceAt(i).findPosition(index);
205 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
207 g.drawString(value + "", x, (ypos + (i * charHeight))
225 void drawEastScale(Graphics g, int startx, int endx, int ypos)
229 if (av.hasHiddenColumns())
231 endx = av.getAlignment().getHiddenColumns()
232 .adjustForHiddenColumns(endx);
237 for (int i = 0; i < av.getAlignment().getHeight(); i++)
239 seq = av.getAlignment().getSequenceAt(i);
243 while (index > startx)
245 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
252 value = seq.findPosition(index);
259 g.drawString(String.valueOf(value), 0, (ypos + (i * charHeight))
265 boolean fastpainting = false;
268 * need to make this thread safe move alignment rendering in response to
274 * shift up or down in repaint
276 public void fastPaint(int horizontal, int vertical)
278 if (fastpainting || gg == null)
285 gg.copyArea(horizontal * charWidth, vertical * charHeight, imgWidth,
286 imgHeight, -horizontal * charWidth, -vertical * charHeight);
288 ViewportRanges ranges = av.getRanges();
289 int sr = ranges.getStartRes();
290 int er = ranges.getEndRes();
291 int ss = ranges.getStartSeq();
292 int es = ranges.getEndSeq();
296 if (horizontal > 0) // scrollbar pulled right, image to the left
299 transX = (er - sr - horizontal) * charWidth;
300 sr = er - horizontal;
302 else if (horizontal < 0)
304 er = sr - horizontal - 1;
306 else if (vertical > 0) // scroll down
310 if (ss < ranges.getStartSeq())
311 { // ie scrolling too fast, more than a page at a time
312 ss = ranges.getStartSeq();
316 transY = imgHeight - ((vertical + 1) * charHeight);
319 else if (vertical < 0)
323 if (es > ranges.getEndSeq())
325 es = ranges.getEndSeq();
329 gg.translate(transX, transY);
330 drawPanel(gg, sr, er, ss, es, 0);
331 gg.translate(-transX, -transY);
334 fastpainting = false;
338 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
339 * towards! startx is the first residue (starting at 0) to display. endx is
340 * the last residue to display (starting at 0). starty is the first sequence
341 * to display (starting at 0). endy is the last sequence to display (starting
342 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
343 * adjustment listener in SeqPanel when the scrollbars move.
346 // Set this to false to force a full panel paint
348 public void paintComponent(Graphics g)
351 BufferedImage lcimg = img; // take reference since other threads may null
352 // img and call later.
353 super.paintComponent(g);
357 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
358 .getClipBounds().height)))
360 g.drawImage(lcimg, 0, 0, this);
365 // this draws the whole of the alignment
366 imgWidth = getWidth();
367 imgHeight = getHeight();
369 imgWidth -= (imgWidth % charWidth);
370 imgHeight -= (imgHeight % charHeight);
372 if ((imgWidth < 1) || (imgHeight < 1))
377 if (lcimg == null || imgWidth != lcimg.getWidth()
378 || imgHeight != lcimg.getHeight())
382 lcimg = img = new BufferedImage(imgWidth, imgHeight,
383 BufferedImage.TYPE_INT_RGB);
384 gg = (Graphics2D) img.getGraphics();
385 gg.setFont(av.getFont());
386 } catch (OutOfMemoryError er)
389 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
390 new OOMWarning("Creating alignment image for display", er);
398 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
399 RenderingHints.VALUE_ANTIALIAS_ON);
402 gg.setColor(Color.white);
403 gg.fillRect(0, 0, imgWidth, imgHeight);
405 ViewportRanges ranges = av.getRanges();
406 if (av.getWrapAlignment())
408 drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
412 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
413 ranges.getStartSeq(), ranges.getEndSeq(), 0);
416 g.drawImage(lcimg, 0, 0, this);
426 * @return DOCUMENT ME!
428 public int getWrappedCanvasWidth(int cwidth)
430 FontMetrics fm = getFontMetrics(av.getFont());
435 if (av.getScaleRightWrapped())
437 LABEL_EAST = fm.stringWidth(getMask());
440 if (av.getScaleLeftWrapped())
442 LABEL_WEST = fm.stringWidth(getMask());
445 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
449 * Generates a string of zeroes.
458 for (int i = 0; i < av.getAlignment().getHeight(); i++)
460 tmp = av.getAlignment().getSequenceAt(i).getEnd();
467 for (int i = maxWidth; i > 0; i /= 10)
481 * @param canvasHeight
486 public void drawWrappedPanel(Graphics g, int canvasWidth,
487 int canvasHeight, int startRes)
490 AlignmentI al = av.getAlignment();
492 FontMetrics fm = getFontMetrics(av.getFont());
494 if (av.getScaleRightWrapped())
496 LABEL_EAST = fm.stringWidth(getMask());
499 if (av.getScaleLeftWrapped())
501 LABEL_WEST = fm.stringWidth(getMask());
504 int hgap = charHeight;
505 if (av.getScaleAboveWrapped())
510 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
511 int cHeight = av.getAlignment().getHeight() * charHeight;
513 av.setWrappedWidth(cWidth);
515 av.getRanges().setEndRes(av.getRanges().getStartRes() + cWidth);
519 int maxwidth = av.getAlignment().getWidth() - 1;
521 if (av.hasHiddenColumns())
523 maxwidth = av.getAlignment().getHiddenColumns()
524 .findColumnPosition(maxwidth) - 1;
527 while ((ypos <= canvasHeight) && (startRes < maxwidth))
529 endx = startRes + cWidth - 1;
536 g.setFont(av.getFont());
537 g.setColor(Color.black);
539 if (av.getScaleLeftWrapped())
541 drawWestScale(g, startRes, endx, ypos);
544 if (av.getScaleRightWrapped())
546 g.translate(canvasWidth - LABEL_EAST, 0);
547 drawEastScale(g, startRes, endx, ypos);
548 g.translate(-(canvasWidth - LABEL_EAST), 0);
551 g.translate(LABEL_WEST, 0);
553 if (av.getScaleAboveWrapped())
555 drawNorthScale(g, startRes, endx, ypos);
558 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
560 g.setColor(Color.blue);
562 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
563 for (int i = 0; i < hidden.getHiddenRegions().size(); i++)
565 res = hidden.findHiddenRegionPosition(i) - startRes;
567 if (res < 0 || res > endx - startRes)
573 new int[] { res * charWidth - charHeight / 4,
574 res * charWidth + charHeight / 4, res * charWidth },
575 new int[] { ypos - (charHeight / 2),
576 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
582 // When printing we have an extra clipped region,
583 // the Printable page which we need to account for here
584 Shape clip = g.getClip();
588 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
592 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
593 (int) clip.getBounds().getHeight());
596 drawPanel(g, startRes, endx, 0, al.getHeight() - 1, ypos);
598 if (av.isShowAnnotation())
600 g.translate(0, cHeight + ypos + 3);
601 if (annotations == null)
603 annotations = new AnnotationPanel(av);
606 annotations.renderer.drawComponent(annotations, av, g, -1,
608 g.translate(0, -cHeight - ypos - 3);
611 g.translate(-LABEL_WEST, 0);
613 ypos += cHeight + getAnnotationHeight() + hgap;
619 AnnotationPanel annotations;
621 int getAnnotationHeight()
623 if (!av.isShowAnnotation())
628 if (annotations == null)
630 annotations = new AnnotationPanel(av);
633 return annotations.adjustPanelHeight();
652 public void drawPanel(Graphics g1, int startRes, int endRes,
653 int startSeq, int endSeq, int offset)
656 if (!av.hasHiddenColumns())
658 draw(g1, startRes, endRes, startSeq, endSeq, offset);
662 List<int[]> regions = av.getAlignment().getHiddenColumns()
666 int blockStart = startRes;
667 int blockEnd = endRes;
669 for (int[] region : regions)
671 int hideStart = region[0];
672 int hideEnd = region[1];
674 if (hideStart <= blockStart)
676 blockStart += (hideEnd - hideStart) + 1;
680 blockEnd = hideStart - 1;
682 g1.translate(screenY * charWidth, 0);
684 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
686 if (av.getShowHiddenMarkers())
688 g1.setColor(Color.blue);
690 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
691 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
692 (endSeq - startSeq + 1) * charHeight + offset);
695 g1.translate(-screenY * charWidth, 0);
696 screenY += blockEnd - blockStart + 1;
697 blockStart = hideEnd + 1;
699 if (screenY > (endRes - startRes))
701 // already rendered last block
706 if (screenY <= (endRes - startRes))
708 // remaining visible region to render
709 blockEnd = blockStart + (endRes - startRes) - screenY;
710 g1.translate(screenY * charWidth, 0);
711 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
713 g1.translate(-screenY * charWidth, 0);
719 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
720 // int x1, int x2, int y1, int y2, int startx, int starty,
721 private void draw(Graphics g, int startRes, int endRes, int startSeq,
722 int endSeq, int offset)
724 g.setFont(av.getFont());
725 sr.prepare(g, av.isRenderGaps());
729 // / First draw the sequences
730 // ///////////////////////////
731 for (int i = startSeq; i <= endSeq; i++)
733 nextSeq = av.getAlignment().getSequenceAt(i);
736 // occasionally, a race condition occurs such that the alignment row is
740 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
741 startRes, endRes, offset + ((i - startSeq) * charHeight));
743 if (av.isShowSequenceFeatures())
745 fr.drawSequence(g, nextSeq, startRes, endRes, offset
746 + ((i - startSeq) * charHeight), false);
749 // / Highlight search Results once all sequences have been drawn
750 // ////////////////////////////////////////////////////////
751 if (av.hasSearchResults())
753 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
755 if (visibleResults != null)
757 for (int r = 0; r < visibleResults.length; r += 2)
759 sr.drawHighlightedText(nextSeq, visibleResults[r],
760 visibleResults[r + 1], (visibleResults[r] - startRes)
762 + ((i - startSeq) * charHeight));
767 if (av.cursorMode && cursorY == i && cursorX >= startRes
768 && cursorX <= endRes)
770 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
771 offset + ((i - startSeq) * charHeight));
775 if (av.getSelectionGroup() != null
776 || av.getAlignment().getGroups().size() > 0)
778 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
783 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
784 int startSeq, int endSeq, int offset)
786 Graphics2D g = (Graphics2D) g1;
788 // ///////////////////////////////////
789 // Now outline any areas if necessary
790 // ///////////////////////////////////
791 SequenceGroup group = av.getSelectionGroup();
797 int visWidth = (endRes - startRes + 1) * charWidth;
799 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
801 group = av.getAlignment().getGroups().get(0);
811 boolean inGroup = false;
815 for (i = startSeq; i <= endSeq; i++)
817 sx = (group.getStartRes() - startRes) * charWidth;
818 sy = offset + ((i - startSeq) * charHeight);
819 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
821 if (sx + ex < 0 || sx > visWidth)
826 if ((sx <= (endRes - startRes) * charWidth)
827 && group.getSequences(null).contains(
828 av.getAlignment().getSequenceAt(i)))
831 && !group.getSequences(null).contains(
832 av.getAlignment().getSequenceAt(i + 1)))
834 bottom = sy + charHeight;
839 if (((top == -1) && (i == 0))
840 || !group.getSequences(null).contains(
841 av.getAlignment().getSequenceAt(i - 1)))
849 if (group == av.getSelectionGroup())
851 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
852 BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f },
854 g.setColor(Color.RED);
858 g.setStroke(new BasicStroke());
859 g.setColor(group.getOutlineColour());
867 if (sx >= 0 && sx < visWidth)
869 g.drawLine(sx, oldY, sx, sy);
872 if (sx + ex < visWidth)
874 g.drawLine(sx + ex, oldY, sx + ex, sy);
883 if (sx + ex > visWidth)
888 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
890 ex = (endRes - startRes + 1) * charWidth;
895 g.drawLine(sx, top, sx + ex, top);
901 g.drawLine(sx, bottom, sx + ex, bottom);
912 sy = offset + ((i - startSeq) * charHeight);
913 if (sx >= 0 && sx < visWidth)
915 g.drawLine(sx, oldY, sx, sy);
918 if (sx + ex < visWidth)
920 g.drawLine(sx + ex, oldY, sx + ex, sy);
929 if (sx + ex > visWidth)
933 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
935 ex = (endRes - startRes + 1) * charWidth;
940 g.drawLine(sx, top, sx + ex, top);
946 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
955 g.setStroke(new BasicStroke());
957 if (groupIndex >= av.getAlignment().getGroups().size())
962 group = av.getAlignment().getGroups().get(groupIndex);
964 } while (groupIndex < av.getAlignment().getGroups().size());
976 public void highlightSearchResults(SearchResultsI results)
980 av.setSearchResults(results);