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.datamodel.VisibleContigsIterator;
29 import jalview.renderer.ScaleRenderer;
30 import jalview.renderer.ScaleRenderer.ScaleMark;
31 import jalview.util.Comparison;
32 import jalview.viewmodel.ViewportListenerI;
33 import jalview.viewmodel.ViewportRanges;
35 import java.awt.AlphaComposite;
36 import java.awt.BasicStroke;
37 import java.awt.BorderLayout;
38 import java.awt.Color;
39 import java.awt.FontMetrics;
40 import java.awt.Graphics;
41 import java.awt.Graphics2D;
42 import java.awt.RenderingHints;
43 import java.awt.Shape;
44 import java.awt.image.BufferedImage;
45 import java.beans.PropertyChangeEvent;
46 import java.util.Iterator;
47 import java.util.List;
49 import javax.swing.JComponent;
52 * The Swing component on which the alignment sequences, and annotations (if
53 * shown), are drawn. This includes scales above, left and right (if shown) in
54 * Wrapped mode, but not the scale above in Unwrapped mode.
57 public class SeqCanvas extends JComponent implements ViewportListenerI
59 private static final String ZEROS = "0000000000";
61 final FeatureRenderer fr;
71 private final SequenceRenderer seqRdr;
73 private boolean fastPaint = false;
75 private boolean fastpainting = false;
77 private AnnotationPanel annotations;
80 * measurements for drawing a wrapped alignment
82 private int labelWidthEast; // label right width in pixels if shown
84 private int labelWidthWest; // label left width in pixels if shown
86 private int wrappedSpaceAboveAlignment; // gap between widths
88 private int wrappedRepeatHeightPx; // height in pixels of wrapped width
90 private int wrappedVisibleWidths; // number of wrapped widths displayed
92 private Graphics2D gg;
95 * Creates a new SeqCanvas object.
99 public SeqCanvas(AlignmentPanel ap)
102 fr = new FeatureRenderer(ap);
103 seqRdr = new SequenceRenderer(av);
104 setLayout(new BorderLayout());
105 PaintRefresher.Register(this, av.getSequenceSetId());
106 setBackground(Color.white);
108 av.getRanges().addPropertyChangeListener(this);
111 public SequenceRenderer getSequenceRenderer()
116 public FeatureRenderer getFeatureRenderer()
122 * Draws the scale above a region of a wrapped alignment, consisting of a
123 * column number every major interval (10 columns).
126 * the graphics context to draw on, positioned at the start (bottom
127 * left) of the line on which to draw any scale marks
129 * start alignment column (0..)
131 * end alignment column (0..)
133 * y offset to draw at
135 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
137 int charHeight = av.getCharHeight();
138 int charWidth = av.getCharWidth();
141 * white fill the scale space (for the fastPaint case)
143 g.setColor(Color.white);
144 g.fillRect(0, ypos - charHeight - charHeight / 2, getWidth(),
145 charHeight * 3 / 2 + 2);
146 g.setColor(Color.black);
148 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
150 for (ScaleMark mark : marks)
152 int mpos = mark.column; // (i - startx - 1)
157 String mstring = mark.text;
163 g.drawString(mstring, mpos * charWidth, ypos - (charHeight / 2));
167 * draw a tick mark below the column number, centred on the column;
168 * height of tick mark is 4 pixels less than half a character
170 int xpos = (mpos * charWidth) + (charWidth / 2);
171 g.drawLine(xpos, (ypos + 2) - (charHeight / 2), xpos, ypos - 2);
177 * Draw the scale to the left or right of a wrapped alignment
180 * graphics context, positioned at the start of the scale to be drawn
182 * first column of wrapped width (0.. excluding any hidden columns)
184 * last column of wrapped width (0.. excluding any hidden columns)
186 * vertical offset at which to begin the scale
188 * if true, scale is left of residues, if false, scale is right
190 void drawVerticalScale(Graphics g, final int startx, final int endx,
191 final int ypos, final boolean left)
193 int charHeight = av.getCharHeight();
194 int charWidth = av.getCharWidth();
196 int yPos = ypos + charHeight;
200 if (av.hasHiddenColumns())
202 HiddenColumns hiddenColumns = av.getAlignment().getHiddenColumns();
203 startX = hiddenColumns.visibleToAbsoluteColumn(startx);
204 endX = hiddenColumns.visibleToAbsoluteColumn(endx);
206 FontMetrics fm = getFontMetrics(av.getFont());
208 for (int i = 0; i < av.getAlignment().getHeight(); i++)
210 SequenceI seq = av.getAlignment().getSequenceAt(i);
213 * find sequence position of first non-gapped position -
214 * to the right if scale left, to the left if scale right
216 int index = left ? startX : endX;
218 while (index >= startX && index <= endX)
220 if (!Comparison.isGap(seq.getCharAt(index)))
222 value = seq.findPosition(index);
236 * white fill the space for the scale
238 g.setColor(Color.white);
239 int y = (yPos + (i * charHeight)) - (charHeight / 5);
240 // fillRect origin is top left of rectangle
241 g.fillRect(0, y - charHeight, left ? labelWidthWest : labelWidthEast,
247 * draw scale value, right justified within its width less half a
248 * character width padding on the right
250 int labelSpace = left ? labelWidthWest : labelWidthEast;
251 labelSpace -= charWidth / 2; // leave space to the right
252 String valueAsString = String.valueOf(value);
253 int labelLength = fm.stringWidth(valueAsString);
254 int xOffset = labelSpace - labelLength;
255 g.setColor(Color.black);
256 g.drawString(valueAsString, xOffset, y);
262 * Does a fast paint of an alignment in response to a scroll. Most of the
263 * visible region is simply copied and shifted, and then any newly visible
264 * columns or rows are drawn. The scroll may be horizontal or vertical, but
265 * not both at once. Scrolling may be the result of
267 * <li>dragging a scroll bar</li>
268 * <li>clicking in the scroll bar</li>
269 * <li>scrolling by trackpad, middle mouse button, or other device</li>
270 * <li>by moving the box in the Overview window</li>
271 * <li>programmatically to make a highlighted position visible</li>
275 * columns to shift right (positive) or left (negative)
277 * rows to shift down (positive) or up (negative)
279 public void fastPaint(int horizontal, int vertical)
281 if (fastpainting || gg == null || img == null)
290 int charHeight = av.getCharHeight();
291 int charWidth = av.getCharWidth();
293 ViewportRanges ranges = av.getRanges();
294 int startRes = ranges.getStartRes();
295 int endRes = ranges.getEndRes();
296 int startSeq = ranges.getStartSeq();
297 int endSeq = ranges.getEndSeq();
301 gg.copyArea(horizontal * charWidth, vertical * charHeight,
302 img.getWidth(), img.getHeight(), -horizontal * charWidth,
303 -vertical * charHeight);
305 if (horizontal > 0) // scrollbar pulled right, image to the left
307 transX = (endRes - startRes - horizontal) * charWidth;
308 startRes = endRes - horizontal;
310 else if (horizontal < 0)
312 endRes = startRes - horizontal;
315 if (vertical > 0) // scroll down
317 startSeq = endSeq - vertical;
319 if (startSeq < ranges.getStartSeq())
320 { // ie scrolling too fast, more than a page at a time
321 startSeq = ranges.getStartSeq();
325 transY = img.getHeight() - ((vertical + 1) * charHeight);
328 else if (vertical < 0)
330 endSeq = startSeq - vertical;
332 if (endSeq > ranges.getEndSeq())
334 endSeq = ranges.getEndSeq();
338 gg.translate(transX, transY);
339 drawPanel(gg, startRes, endRes, startSeq, endSeq, 0);
340 gg.translate(-transX, -transY);
342 // Call repaint on alignment panel so that repaints from other alignment
343 // panel components can be aggregated. Otherwise performance of the
344 // overview window and others may be adversely affected.
345 av.getAlignPanel().repaint();
348 fastpainting = false;
353 public void paintComponent(Graphics g)
355 super.paintComponent(g);
357 int charHeight = av.getCharHeight();
358 int charWidth = av.getCharWidth();
360 ViewportRanges ranges = av.getRanges();
362 int width = getWidth();
363 int height = getHeight();
365 width -= (width % charWidth);
366 height -= (height % charHeight);
368 // selectImage is the selection group outline image
369 BufferedImage selectImage = drawSelectionGroup(
370 ranges.getStartRes(), ranges.getEndRes(),
371 ranges.getStartSeq(), ranges.getEndSeq());
373 if ((img != null) && (fastPaint
374 || (getVisibleRect().width != g.getClipBounds().width)
375 || (getVisibleRect().height != g.getClipBounds().height)))
377 BufferedImage lcimg = buildLocalImage(selectImage);
378 g.drawImage(lcimg, 0, 0, this);
381 else if ((width > 0) && (height > 0))
383 // img is a cached version of the last view we drew, if any
384 // if we have no img or the size has changed, make a new one
385 if (img == null || width != img.getWidth()
386 || height != img.getHeight())
393 gg = (Graphics2D) img.getGraphics();
394 gg.setFont(av.getFont());
399 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
400 RenderingHints.VALUE_ANTIALIAS_ON);
403 gg.setColor(Color.white);
404 gg.fillRect(0, 0, img.getWidth(), img.getHeight());
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 // lcimg is a local *copy* of img which we'll draw selectImage on top of
417 BufferedImage lcimg = buildLocalImage(selectImage);
418 g.drawImage(lcimg, 0, 0, this);
424 drawCursor(g, ranges.getStartRes(), ranges.getEndRes(),
425 ranges.getStartSeq(), ranges.getEndSeq());
430 * Draw an alignment panel for printing
433 * Graphics object to draw with
435 * start residue of print area
437 * end residue of print area
439 * start sequence of print area
441 * end sequence of print area
443 public void drawPanelForPrinting(Graphics g1, int startRes, int endRes,
444 int startSeq, int endSeq)
446 drawPanel(g1, startRes, endRes, startSeq, endSeq, 0);
448 BufferedImage selectImage = drawSelectionGroup(startRes, endRes,
450 if (selectImage != null)
452 ((Graphics2D) g1).setComposite(AlphaComposite
453 .getInstance(AlphaComposite.SRC_OVER));
454 g1.drawImage(selectImage, 0, 0, this);
459 * Draw a wrapped alignment panel for printing
462 * Graphics object to draw with
464 * width of drawing area
465 * @param canvasHeight
466 * height of drawing area
468 * start residue of print area
470 public void drawWrappedPanelForPrinting(Graphics g, int canvasWidth,
471 int canvasHeight, int startRes)
473 SequenceGroup group = av.getSelectionGroup();
475 drawWrappedPanel(g, canvasWidth, canvasHeight, startRes);
479 BufferedImage selectImage = null;
482 selectImage = new BufferedImage(canvasWidth, canvasHeight,
483 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
484 } catch (OutOfMemoryError er)
487 System.err.println("Print image OutOfMemory Error.\n" + er);
488 new OOMWarning("Creating wrapped alignment image for printing", er);
490 if (selectImage != null)
492 Graphics2D g2 = selectImage.createGraphics();
493 setupSelectionGroup(g2, selectImage);
494 drawWrappedSelection(g2, group, canvasWidth, canvasHeight,
498 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
499 g.drawImage(selectImage, 0, 0, this);
506 * Make a local image by combining the cached image img
509 private BufferedImage buildLocalImage(BufferedImage selectImage)
511 // clone the cached image
512 BufferedImage lcimg = new BufferedImage(img.getWidth(), img.getHeight(),
515 // BufferedImage lcimg = new BufferedImage(img.getWidth(), img.getHeight(),
517 Graphics2D g2d = lcimg.createGraphics();
518 g2d.drawImage(img, 0, 0, null);
520 // overlay selection group on lcimg
521 if (selectImage != null)
524 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
525 g2d.drawImage(selectImage, 0, 0, this);
534 * Set up a buffered image of the correct height and size for the sequence canvas
536 private BufferedImage setupImage()
538 BufferedImage lcimg = null;
540 int charWidth = av.getCharWidth();
541 int charHeight = av.getCharHeight();
543 int width = getWidth();
544 int height = getHeight();
546 width -= (width % charWidth);
547 height -= (height % charHeight);
549 if ((width < 1) || (height < 1))
556 lcimg = new BufferedImage(width, height,
557 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
558 } catch (OutOfMemoryError er)
562 "Group image OutOfMemory Redraw Error.\n" + er);
563 new OOMWarning("Creating alignment image for display", er);
572 * Returns the visible width of the canvas in residues, after allowing for
573 * East or West scales (if shown)
576 * the width in pixels (possibly including scales)
580 public int getWrappedCanvasWidth(int canvasWidth)
582 int charWidth = av.getCharWidth();
584 FontMetrics fm = getFontMetrics(av.getFont());
588 if (av.getScaleRightWrapped() || av.getScaleLeftWrapped())
590 labelWidth = getLabelWidth(fm);
593 labelWidthEast = av.getScaleRightWrapped() ? labelWidth : 0;
595 labelWidthWest = av.getScaleLeftWrapped() ? labelWidth : 0;
597 return (canvasWidth - labelWidthEast - labelWidthWest) / charWidth;
601 * Returns a pixel width sufficient to show the largest sequence coordinate
602 * (end position) in the alignment, calculated as the FontMetrics width of
603 * zeroes "0000000" limited to the number of decimal digits to be shown (3 for
604 * 1-10, 4 for 11-99 etc). One character width is added to this, to allow for
605 * half a character width space on either side.
610 protected int getLabelWidth(FontMetrics fm)
613 * find the biggest sequence end position we need to show
614 * (note this is not necessarily the sequence length)
617 AlignmentI alignment = av.getAlignment();
618 for (int i = 0; i < alignment.getHeight(); i++)
620 maxWidth = Math.max(maxWidth, alignment.getSequenceAt(i).getEnd());
624 for (int i = maxWidth; i > 0; i /= 10)
629 return fm.stringWidth(ZEROS.substring(0, length)) + av.getCharWidth();
633 * Draws as many widths of a wrapped alignment as can fit in the visible
638 * available width in pixels
639 * @param canvasHeight
640 * available height in pixels
642 * the first column (0...) of the alignment to draw
644 public void drawWrappedPanel(Graphics g, int canvasWidth,
645 int canvasHeight, final int startColumn)
647 int wrappedWidthInResidues = calculateWrappedGeometry(canvasWidth,
650 av.setWrappedWidth(wrappedWidthInResidues);
652 ViewportRanges ranges = av.getRanges();
653 ranges.setViewportStartAndWidth(startColumn, wrappedWidthInResidues);
655 // we need to call this again to make sure the startColumn +
656 // wrappedWidthInResidues values are used to calculate wrappedVisibleWidths
658 calculateWrappedGeometry(canvasWidth, canvasHeight);
661 * draw one width at a time (including any scales or annotation shown),
662 * until we have run out of either alignment or vertical space available
664 int ypos = wrappedSpaceAboveAlignment;
665 int maxWidth = ranges.getVisibleAlignmentWidth();
667 int start = startColumn;
668 int currentWidth = 0;
669 while ((currentWidth < wrappedVisibleWidths) && (start < maxWidth))
672 .min(maxWidth, start + wrappedWidthInResidues - 1);
673 drawWrappedWidth(g, ypos, start, endColumn, canvasHeight);
674 ypos += wrappedRepeatHeightPx;
675 start += wrappedWidthInResidues;
679 drawWrappedDecorators(g, startColumn);
683 * Calculates and saves values needed when rendering a wrapped alignment.
684 * These depend on many factors, including
686 * <li>canvas width and height</li>
687 * <li>number of visible sequences, and height of annotations if shown</li>
688 * <li>font and character width</li>
689 * <li>whether scales are shown left, right or above the alignment</li>
693 * @param canvasHeight
694 * @return the number of residue columns in each width
696 protected int calculateWrappedGeometry(int canvasWidth, int canvasHeight)
698 int charHeight = av.getCharHeight();
701 * vertical space in pixels between wrapped widths of alignment
702 * - one character height, or two if scale above is drawn
704 wrappedSpaceAboveAlignment = charHeight
705 * (av.getScaleAboveWrapped() ? 2 : 1);
708 * height in pixels of the wrapped widths
710 wrappedRepeatHeightPx = wrappedSpaceAboveAlignment;
712 wrappedRepeatHeightPx += av.getRanges().getViewportHeight()
714 // add annotations panel height if shown
715 wrappedRepeatHeightPx += getAnnotationHeight();
718 * number of visible widths (the last one may be part height),
719 * ensuring a part height includes at least one sequence
721 ViewportRanges ranges = av.getRanges();
722 wrappedVisibleWidths = canvasHeight / wrappedRepeatHeightPx;
723 int remainder = canvasHeight % wrappedRepeatHeightPx;
724 if (remainder >= (wrappedSpaceAboveAlignment + charHeight))
726 wrappedVisibleWidths++;
730 * compute width in residues; this also sets East and West label widths
732 int wrappedWidthInResidues = getWrappedCanvasWidth(canvasWidth);
735 * limit visibleWidths to not exceed width of alignment
737 int xMax = ranges.getVisibleAlignmentWidth();
738 int startToEnd = xMax - ranges.getStartRes();
739 int maxWidths = startToEnd / wrappedWidthInResidues;
740 if (startToEnd % wrappedWidthInResidues > 0)
744 wrappedVisibleWidths = Math.min(wrappedVisibleWidths, maxWidths);
746 return wrappedWidthInResidues;
750 * Draws one width of a wrapped alignment, including sequences and
751 * annnotations, if shown, but not scales or hidden column markers
757 * @param canvasHeight
759 protected void drawWrappedWidth(Graphics g, int ypos, int startColumn,
760 int endColumn, int canvasHeight)
762 ViewportRanges ranges = av.getRanges();
763 int viewportWidth = ranges.getViewportWidth();
765 int endx = Math.min(startColumn + viewportWidth - 1, endColumn);
768 * move right before drawing by the width of the scale left (if any)
769 * plus column offset from left margin (usually zero, but may be non-zero
770 * when fast painting is drawing just a few columns)
772 int charWidth = av.getCharWidth();
773 int xOffset = labelWidthWest
774 + ((startColumn - ranges.getStartRes()) % viewportWidth)
776 g.translate(xOffset, 0);
778 // When printing we have an extra clipped region,
779 // the Printable page which we need to account for here
780 Shape clip = g.getClip();
784 g.setClip(0, 0, viewportWidth * charWidth, canvasHeight);
788 g.setClip(0, (int) clip.getBounds().getY(),
789 viewportWidth * charWidth, (int) clip.getBounds().getHeight());
793 * white fill the region to be drawn (so incremental fast paint doesn't
794 * scribble over an existing image)
796 g.setColor(Color.white);
797 g.fillRect(0, ypos, (endx - startColumn + 1) * charWidth,
798 wrappedRepeatHeightPx);
800 drawPanel(g, startColumn, endx, 0, av.getAlignment().getHeight() - 1,
803 int cHeight = av.getAlignment().getHeight() * av.getCharHeight();
805 if (av.isShowAnnotation())
807 g.translate(0, cHeight + ypos + 3);
808 if (annotations == null)
810 annotations = new AnnotationPanel(av);
813 annotations.renderer.drawComponent(annotations, av, g, -1,
814 startColumn, endx + 1);
815 g.translate(0, -cHeight - ypos - 3);
818 g.translate(-xOffset, 0);
822 * Draws scales left, right and above (if shown), and any hidden column
823 * markers, on all widths of the wrapped alignment
828 protected void drawWrappedDecorators(Graphics g, final int startColumn)
830 int charWidth = av.getCharWidth();
832 g.setFont(av.getFont());
833 g.setColor(Color.black);
835 int ypos = wrappedSpaceAboveAlignment;
836 ViewportRanges ranges = av.getRanges();
837 int viewportWidth = ranges.getViewportWidth();
838 int maxWidth = ranges.getVisibleAlignmentWidth();
840 int startCol = startColumn;
842 while (widthsDrawn < wrappedVisibleWidths)
844 int endColumn = Math.min(maxWidth, startCol + viewportWidth - 1);
846 if (av.getScaleLeftWrapped())
848 drawVerticalScale(g, startCol, endColumn - 1, ypos, true);
851 if (av.getScaleRightWrapped())
853 int x = labelWidthWest + viewportWidth * charWidth;
855 drawVerticalScale(g, startCol, endColumn, ypos, false);
860 * white fill region of scale above and hidden column markers
861 * (to support incremental fast paint of image)
863 g.translate(labelWidthWest, 0);
864 g.setColor(Color.white);
865 g.fillRect(0, ypos - wrappedSpaceAboveAlignment, viewportWidth
866 * charWidth + labelWidthWest, wrappedSpaceAboveAlignment);
867 g.setColor(Color.black);
868 g.translate(-labelWidthWest, 0);
870 g.translate(labelWidthWest, 0);
872 if (av.getScaleAboveWrapped())
874 drawNorthScale(g, startCol, endColumn, ypos);
877 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
879 drawHiddenColumnMarkers(g, ypos, startCol, endColumn);
882 g.translate(-labelWidthWest, 0);
884 ypos += wrappedRepeatHeightPx;
885 startCol += viewportWidth;
891 * Draws markers (triangles) above hidden column positions between startColumn
899 protected void drawHiddenColumnMarkers(Graphics g, int ypos,
900 int startColumn, int endColumn)
902 int charHeight = av.getCharHeight();
903 int charWidth = av.getCharWidth();
905 g.setColor(Color.blue);
907 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
909 Iterator<Integer> it = hidden.getStartRegionIterator(startColumn,
913 res = it.next() - startColumn;
915 if (res < 0 || res > endColumn - startColumn + 1)
921 * draw a downward-pointing triangle at the hidden columns location
922 * (before the following visible column)
924 int xMiddle = res * charWidth;
925 int[] xPoints = new int[] { xMiddle - charHeight / 4,
926 xMiddle + charHeight / 4, xMiddle };
927 int yTop = ypos - (charHeight / 2);
928 int[] yPoints = new int[] { yTop, yTop, yTop + 8 };
929 g.fillPolygon(xPoints, yPoints, 3);
934 * Draw a selection group over a wrapped alignment
936 private void drawWrappedSelection(Graphics2D g, SequenceGroup group,
938 int canvasHeight, int startRes)
940 int charHeight = av.getCharHeight();
941 int charWidth = av.getCharWidth();
943 // height gap above each panel
944 int hgap = charHeight;
945 if (av.getScaleAboveWrapped())
950 int cWidth = (canvasWidth - labelWidthEast - labelWidthWest)
952 int cHeight = av.getAlignment().getHeight() * charHeight;
954 int startx = startRes;
956 int ypos = hgap; // vertical offset
957 int maxwidth = av.getAlignment().getVisibleWidth();
959 // chop the wrapped alignment extent up into panel-sized blocks and treat
960 // each block as if it were a block from an unwrapped alignment
961 while ((ypos <= canvasHeight) && (startx < maxwidth))
963 // set end value to be start + width, or maxwidth, whichever is smaller
964 endx = startx + cWidth - 1;
971 g.translate(labelWidthWest, 0);
973 drawUnwrappedSelection(g, group, startx, endx, 0,
974 av.getAlignment().getHeight() - 1,
977 g.translate(-labelWidthWest, 0);
979 // update vertical offset
980 ypos += cHeight + getAnnotationHeight() + hgap;
982 // update horizontal offset
987 int getAnnotationHeight()
989 if (!av.isShowAnnotation())
994 if (annotations == null)
996 annotations = new AnnotationPanel(av);
999 return annotations.adjustPanelHeight();
1003 * Draws the visible region of the alignment on the graphics context. If there
1004 * are hidden column markers in the visible region, then each sub-region
1005 * between the markers is drawn separately, followed by the hidden column
1009 * the graphics context, positioned at the first residue to be drawn
1011 * offset of the first column to draw (0..)
1013 * offset of the last column to draw (0..)
1015 * offset of the first sequence to draw (0..)
1017 * offset of the last sequence to draw (0..)
1019 * vertical offset at which to draw (for wrapped alignments)
1021 public void drawPanel(Graphics g1, final int startRes, final int endRes,
1022 final int startSeq, final int endSeq, final int yOffset)
1024 int charHeight = av.getCharHeight();
1025 int charWidth = av.getCharWidth();
1027 if (!av.hasHiddenColumns())
1029 draw(g1, startRes, endRes, startSeq, endSeq, yOffset);
1037 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
1038 VisibleContigsIterator regions = hidden
1039 .getVisContigsIterator(startRes, endRes + 1, true);
1041 while (regions.hasNext())
1043 int[] region = regions.next();
1044 blockEnd = region[1];
1045 blockStart = region[0];
1048 * draw up to just before the next hidden region, or the end of
1049 * the visible region, whichever comes first
1051 g1.translate(screenY * charWidth, 0);
1053 draw(g1, blockStart, blockEnd, startSeq, endSeq, yOffset);
1056 * draw the downline of the hidden column marker (ScalePanel draws the
1057 * triangle on top) if we reached it
1059 if (av.getShowHiddenMarkers()
1060 && (regions.hasNext() || regions.endsAtHidden()))
1062 g1.setColor(Color.blue);
1064 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
1065 0 + yOffset, (blockEnd - blockStart + 1) * charWidth - 1,
1066 (endSeq - startSeq + 1) * charHeight + yOffset);
1069 g1.translate(-screenY * charWidth, 0);
1070 screenY += blockEnd - blockStart + 1;
1077 * Draws a region of the visible alignment
1081 * offset of the first column in the visible region (0..)
1083 * offset of the last column in the visible region (0..)
1085 * offset of the first sequence in the visible region (0..)
1087 * offset of the last sequence in the visible region (0..)
1089 * vertical offset at which to draw (for wrapped alignments)
1091 private void draw(Graphics g, int startRes, int endRes, int startSeq,
1092 int endSeq, int offset)
1094 int charHeight = av.getCharHeight();
1095 int charWidth = av.getCharWidth();
1097 g.setFont(av.getFont());
1098 seqRdr.prepare(g, av.isRenderGaps());
1102 // / First draw the sequences
1103 // ///////////////////////////
1104 for (int i = startSeq; i <= endSeq; i++)
1106 nextSeq = av.getAlignment().getSequenceAt(i);
1107 if (nextSeq == null)
1109 // occasionally, a race condition occurs such that the alignment row is
1113 seqRdr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
1114 startRes, endRes, offset + ((i - startSeq) * charHeight));
1116 if (av.isShowSequenceFeatures())
1118 fr.drawSequence(g, nextSeq, startRes, endRes,
1119 offset + ((i - startSeq) * charHeight), false);
1123 * highlight search Results once sequence has been drawn
1125 if (av.hasSearchResults())
1127 SearchResultsI searchResults = av.getSearchResults();
1128 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
1130 if (visibleResults != null)
1132 for (int r = 0; r < visibleResults.length; r += 2)
1134 seqRdr.drawHighlightedText(nextSeq, visibleResults[r],
1135 visibleResults[r + 1],
1136 (visibleResults[r] - startRes) * charWidth,
1137 offset + ((i - startSeq) * charHeight));
1143 if (av.getSelectionGroup() != null
1144 || av.getAlignment().getGroups().size() > 0)
1146 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
1151 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
1152 int startSeq, int endSeq, int offset)
1154 Graphics2D g = (Graphics2D) g1;
1156 // ///////////////////////////////////
1157 // Now outline any areas if necessary
1158 // ///////////////////////////////////
1160 SequenceGroup group = null;
1161 int groupIndex = -1;
1163 if (av.getAlignment().getGroups().size() > 0)
1165 group = av.getAlignment().getGroups().get(0);
1171 g.setStroke(new BasicStroke());
1172 g.setColor(group.getOutlineColour());
1176 drawPartialGroupOutline(g, group, startRes, endRes, startSeq,
1181 g.setStroke(new BasicStroke());
1183 if (groupIndex >= av.getAlignment().getGroups().size())
1188 group = av.getAlignment().getGroups().get(groupIndex);
1190 } while (groupIndex < av.getAlignment().getGroups().size());
1198 * Draw the selection group as a separate image and overlay
1200 private BufferedImage drawSelectionGroup(int startRes, int endRes,
1201 int startSeq, int endSeq)
1203 // get a new image of the correct size
1204 BufferedImage selectionImage = setupImage();
1206 if (selectionImage == null)
1211 SequenceGroup group = av.getSelectionGroup();
1218 // set up drawing colour
1219 Graphics2D g = (Graphics2D) selectionImage.getGraphics();
1221 setupSelectionGroup(g, selectionImage);
1223 if (!av.getWrapAlignment())
1225 drawUnwrappedSelection(g, group, startRes, endRes, startSeq, endSeq,
1230 drawWrappedSelection(g, group, getWidth(), getHeight(),
1231 av.getRanges().getStartRes());
1235 return selectionImage;
1239 * Draw the cursor as a separate image and overlay
1242 * start residue of area to draw cursor in
1244 * end residue of area to draw cursor in
1246 * start sequence of area to draw cursor in
1248 * end sequence of are to draw cursor in
1249 * @return a transparent image of the same size as the sequence canvas, with
1250 * the cursor drawn on it, if any
1252 private void drawCursor(Graphics g, int startRes, int endRes,
1256 // convert the cursorY into a position on the visible alignment
1257 int cursor_ypos = cursorY;
1259 // don't do work unless we have to
1260 if (cursor_ypos >= startSeq && cursor_ypos <= endSeq)
1264 int startx = startRes;
1267 // convert the cursorX into a position on the visible alignment
1268 int cursor_xpos = av.getAlignment().getHiddenColumns()
1269 .absoluteToVisibleColumn(cursorX);
1271 if (av.getAlignment().getHiddenColumns().isVisible(cursorX))
1274 if (av.getWrapAlignment())
1276 // work out the correct offsets for the cursor
1277 int charHeight = av.getCharHeight();
1278 int charWidth = av.getCharWidth();
1279 int canvasWidth = getWidth();
1280 int canvasHeight = getHeight();
1282 // height gap above each panel
1283 int hgap = charHeight;
1284 if (av.getScaleAboveWrapped())
1289 int cWidth = (canvasWidth - labelWidthEast - labelWidthWest)
1291 int cHeight = av.getAlignment().getHeight() * charHeight;
1293 endx = startx + cWidth - 1;
1294 int ypos = hgap; // vertical offset
1296 // iterate down the wrapped panels
1297 while ((ypos <= canvasHeight) && (endx < cursor_xpos))
1299 // update vertical offset
1300 ypos += cHeight + getAnnotationHeight() + hgap;
1302 // update horizontal offset
1304 endx = startx + cWidth - 1;
1307 xoffset = labelWidthWest;
1310 // now check if cursor is within range for x values
1311 if (cursor_xpos >= startx && cursor_xpos <= endx)
1313 // get the character the cursor is drawn at
1314 SequenceI seq = av.getAlignment().getSequenceAt(cursorY);
1315 char s = seq.getCharAt(cursorX);
1317 seqRdr.drawCursor(g, s,
1318 xoffset + (cursor_xpos - startx) * av.getCharWidth(),
1319 yoffset + (cursor_ypos - startSeq) * av.getCharHeight());
1327 * Set up graphics for selection group
1329 private void setupSelectionGroup(Graphics2D g,
1330 BufferedImage selectionImage)
1332 // set background to transparent
1333 g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
1334 g.fillRect(0, 0, selectionImage.getWidth(), selectionImage.getHeight());
1336 // set up foreground to draw red dashed line
1337 g.setComposite(AlphaComposite.Src);
1338 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
1339 BasicStroke.JOIN_ROUND, 3f, new float[]
1341 g.setColor(Color.RED);
1345 * Draw a selection group over an unwrapped alignment
1346 * @param g graphics object to draw with
1347 * @param group selection group
1348 * @param startRes start residue of area to draw
1349 * @param endRes end residue of area to draw
1350 * @param startSeq start sequence of area to draw
1351 * @param endSeq end sequence of area to draw
1352 * @param offset vertical offset (used when called from wrapped alignment code)
1354 private void drawUnwrappedSelection(Graphics2D g, SequenceGroup group,
1355 int startRes, int endRes, int startSeq, int endSeq, int offset)
1357 int charWidth = av.getCharWidth();
1359 if (!av.hasHiddenColumns())
1361 drawPartialGroupOutline(g, group, startRes, endRes, startSeq, endSeq,
1366 // package into blocks of visible columns
1371 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
1372 VisibleContigsIterator regions = hidden
1373 .getVisContigsIterator(startRes, endRes + 1, true);
1374 while (regions.hasNext())
1376 int[] region = regions.next();
1377 blockEnd = region[1];
1378 blockStart = region[0];
1380 g.translate(screenY * charWidth, 0);
1381 drawPartialGroupOutline(g, group,
1382 blockStart, blockEnd, startSeq, endSeq, offset);
1384 g.translate(-screenY * charWidth, 0);
1385 screenY += blockEnd - blockStart + 1;
1391 * Draw the selection group as a separate image and overlay
1393 private void drawPartialGroupOutline(Graphics2D g, SequenceGroup group,
1394 int startRes, int endRes, int startSeq, int endSeq,
1397 int charHeight = av.getCharHeight();
1398 int charWidth = av.getCharWidth();
1399 int visWidth = (endRes - startRes + 1) * charWidth;
1403 boolean inGroup = false;
1408 List<SequenceI> seqs = group.getSequences(null);
1410 // position of start residue of group relative to startRes, in pixels
1411 int sx = (group.getStartRes() - startRes) * charWidth;
1413 // width of group in pixels
1414 int xwidth = (((group.getEndRes() + 1) - group.getStartRes())
1417 if (!(sx + xwidth < 0 || sx > visWidth))
1419 for (i = startSeq; i <= endSeq; i++)
1421 sy = verticalOffset + (i - startSeq) * charHeight;
1423 if ((sx <= (endRes - startRes) * charWidth)
1424 && seqs.contains(av.getAlignment().getSequenceAt(i)))
1427 && !seqs.contains(av.getAlignment().getSequenceAt(i + 1)))
1429 bottom = sy + charHeight;
1434 if (((top == -1) && (i == 0)) || !seqs
1435 .contains(av.getAlignment().getSequenceAt(i - 1)))
1446 drawVerticals(g, sx, xwidth, visWidth, oldY, sy);
1447 drawHorizontals(g, sx, xwidth, visWidth, top, bottom);
1449 // reset top and bottom
1457 sy = verticalOffset + ((i - startSeq) * charHeight);
1458 drawVerticals(g, sx, xwidth, visWidth, oldY, sy);
1459 drawHorizontals(g, sx, xwidth, visWidth, top, bottom);
1465 * Draw horizontal selection group boundaries at top and bottom positions
1468 * graphics object to draw on
1474 * visWidth maximum available width
1476 * position to draw top of group at
1478 * position to draw bottom of group at
1480 private void drawHorizontals(Graphics2D g, int sx, int xwidth,
1481 int visWidth, int top, int bottom)
1491 // don't let width extend beyond current block, or group extent
1493 if (startx + width >= visWidth)
1495 width = visWidth - startx;
1500 g.drawLine(startx, top, startx + width, top);
1505 g.drawLine(startx, bottom - 1, startx + width, bottom - 1);
1510 * Draw vertical lines at sx and sx+xwidth providing they lie within
1514 * graphics object to draw on
1520 * visWidth maximum available width
1526 private void drawVerticals(Graphics2D g, int sx, int xwidth, int visWidth,
1529 // if start position is visible, draw vertical line to left of
1531 if (sx >= 0 && sx < visWidth)
1533 g.drawLine(sx, oldY, sx, sy);
1536 // if end position is visible, draw vertical line to right of
1538 if (sx + xwidth < visWidth)
1540 g.drawLine(sx + xwidth, oldY, sx + xwidth, sy);
1545 * Highlights search results in the visible region by rendering as white text
1546 * on a black background. Any previous highlighting is removed. Answers true
1547 * if any highlight was left on the visible alignment (so status bar should be
1548 * set to match), else false.
1550 * Currently fastPaint is not implemented for wrapped alignments. If a wrapped
1551 * alignment had to be scrolled to show the highlighted region, then it should
1552 * be fully redrawn, otherwise a fast paint can be performed. This argument
1553 * could be removed if fast paint of scrolled wrapped alignment is coded in
1554 * future (JAL-2609).
1557 * @param noFastPaint
1560 public boolean highlightSearchResults(SearchResultsI results,
1561 boolean noFastPaint)
1567 boolean wrapped = av.getWrapAlignment();
1570 fastPaint = !noFastPaint;
1571 fastpainting = fastPaint;
1574 * to avoid redrawing the whole visible region, we instead
1575 * redraw just the minimal regions to remove previous highlights
1578 SearchResultsI previous = av.getSearchResults();
1579 av.setSearchResults(results);
1580 boolean redrawn = false;
1581 boolean drawn = false;
1584 redrawn = drawMappedPositionsWrapped(previous);
1585 drawn = drawMappedPositionsWrapped(results);
1590 redrawn = drawMappedPositions(previous);
1591 drawn = drawMappedPositions(results);
1596 * if highlights were either removed or added, repaint
1604 * return true only if highlights were added
1610 fastpainting = false;
1615 * Redraws the minimal rectangle in the visible region (if any) that includes
1616 * mapped positions of the given search results. Whether or not positions are
1617 * highlighted depends on the SearchResults set on the Viewport. This allows
1618 * this method to be called to either clear or set highlighting. Answers true
1619 * if any positions were drawn (in which case a repaint is still required),
1625 protected boolean drawMappedPositions(SearchResultsI results)
1627 if ((results == null) || (gg == null)) // JAL-2784 check gg is not null
1633 * calculate the minimal rectangle to redraw that
1634 * includes both new and existing search results
1636 int firstSeq = Integer.MAX_VALUE;
1638 int firstCol = Integer.MAX_VALUE;
1640 boolean matchFound = false;
1642 ViewportRanges ranges = av.getRanges();
1643 int firstVisibleColumn = ranges.getStartRes();
1644 int lastVisibleColumn = ranges.getEndRes();
1645 AlignmentI alignment = av.getAlignment();
1646 if (av.hasHiddenColumns())
1648 firstVisibleColumn = alignment.getHiddenColumns()
1649 .visibleToAbsoluteColumn(firstVisibleColumn);
1650 lastVisibleColumn = alignment.getHiddenColumns()
1651 .visibleToAbsoluteColumn(lastVisibleColumn);
1654 for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
1655 .getEndSeq(); seqNo++)
1657 SequenceI seq = alignment.getSequenceAt(seqNo);
1659 int[] visibleResults = results.getResults(seq, firstVisibleColumn,
1661 if (visibleResults != null)
1663 for (int i = 0; i < visibleResults.length - 1; i += 2)
1665 int firstMatchedColumn = visibleResults[i];
1666 int lastMatchedColumn = visibleResults[i + 1];
1667 if (firstMatchedColumn <= lastVisibleColumn
1668 && lastMatchedColumn >= firstVisibleColumn)
1671 * found a search results match in the visible region -
1672 * remember the first and last sequence matched, and the first
1673 * and last visible columns in the matched positions
1676 firstSeq = Math.min(firstSeq, seqNo);
1677 lastSeq = Math.max(lastSeq, seqNo);
1678 firstMatchedColumn = Math.max(firstMatchedColumn,
1679 firstVisibleColumn);
1680 lastMatchedColumn = Math.min(lastMatchedColumn,
1682 firstCol = Math.min(firstCol, firstMatchedColumn);
1683 lastCol = Math.max(lastCol, lastMatchedColumn);
1691 if (av.hasHiddenColumns())
1693 firstCol = alignment.getHiddenColumns()
1694 .absoluteToVisibleColumn(firstCol);
1695 lastCol = alignment.getHiddenColumns().absoluteToVisibleColumn(lastCol);
1697 int transX = (firstCol - ranges.getStartRes()) * av.getCharWidth();
1698 int transY = (firstSeq - ranges.getStartSeq()) * av.getCharHeight();
1699 gg.translate(transX, transY);
1700 drawPanel(gg, firstCol, lastCol, firstSeq, lastSeq, 0);
1701 gg.translate(-transX, -transY);
1708 public void propertyChange(PropertyChangeEvent evt)
1710 String eventName = evt.getPropertyName();
1712 if (eventName.equals(SequenceGroup.SEQ_GROUP_CHANGED))
1718 else if (eventName.equals(ViewportRanges.MOVE_VIEWPORT))
1726 if (eventName.equals(ViewportRanges.STARTRES)
1727 || eventName.equals(ViewportRanges.STARTRESANDSEQ))
1729 // Make sure we're not trying to draw a panel
1730 // larger than the visible window
1731 if (eventName.equals(ViewportRanges.STARTRES))
1733 scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
1737 scrollX = ((int[]) evt.getNewValue())[0]
1738 - ((int[]) evt.getOldValue())[0];
1740 ViewportRanges vpRanges = av.getRanges();
1742 int range = vpRanges.getEndRes() - vpRanges.getStartRes();
1743 if (scrollX > range)
1747 else if (scrollX < -range)
1752 // Both scrolling and resizing change viewport ranges: scrolling changes
1753 // both start and end points, but resize only changes end values.
1754 // Here we only want to fastpaint on a scroll, with resize using a normal
1755 // paint, so scroll events are identified as changes to the horizontal or
1756 // vertical start value.
1757 if (eventName.equals(ViewportRanges.STARTRES))
1759 if (av.getWrapAlignment())
1761 fastPaintWrapped(scrollX);
1765 fastPaint(scrollX, 0);
1768 else if (eventName.equals(ViewportRanges.STARTSEQ))
1771 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());
1773 else if (eventName.equals(ViewportRanges.STARTRESANDSEQ))
1775 if (av.getWrapAlignment())
1777 fastPaintWrapped(scrollX);
1781 fastPaint(scrollX, 0);
1784 else if (eventName.equals(ViewportRanges.STARTSEQ))
1787 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());
1789 else if (eventName.equals(ViewportRanges.STARTRESANDSEQ))
1791 if (av.getWrapAlignment())
1793 fastPaintWrapped(scrollX);
1799 * Does a minimal update of the image for a scroll movement. This method
1800 * handles scroll movements of up to one width of the wrapped alignment (one
1801 * click in the vertical scrollbar). Larger movements (for example after a
1802 * scroll to highlight a mapped position) trigger a full redraw instead.
1805 * number of positions scrolled (right if positive, left if negative)
1807 protected void fastPaintWrapped(int scrollX)
1809 ViewportRanges ranges = av.getRanges();
1811 if (Math.abs(scrollX) > ranges.getViewportWidth())
1814 * shift of more than one view width is
1815 * overcomplicated to handle in this method
1822 if (fastpainting || gg == null)
1828 fastpainting = true;
1832 calculateWrappedGeometry(getWidth(), getHeight());
1835 * relocate the regions of the alignment that are still visible
1837 shiftWrappedAlignment(-scrollX);
1840 * add new columns (sequence, annotation)
1841 * - at top left if scrollX < 0
1842 * - at right of last two widths if scrollX > 0
1846 int startRes = ranges.getStartRes();
1847 drawWrappedWidth(gg, wrappedSpaceAboveAlignment, startRes, startRes
1848 - scrollX - 1, getHeight());
1852 fastPaintWrappedAddRight(scrollX);
1856 * draw all scales (if shown) and hidden column markers
1858 drawWrappedDecorators(gg, ranges.getStartRes());
1863 fastpainting = false;
1868 * Draws the specified number of columns at the 'end' (bottom right) of a
1869 * wrapped alignment view, including sequences and annotations if shown, but
1870 * not scales. Also draws the same number of columns at the right hand end of
1871 * the second last width shown, if the last width is not full height (so
1872 * cannot simply be copied from the graphics image).
1876 protected void fastPaintWrappedAddRight(int columns)
1883 ViewportRanges ranges = av.getRanges();
1884 int viewportWidth = ranges.getViewportWidth();
1885 int charWidth = av.getCharWidth();
1888 * draw full height alignment in the second last row, last columns, if the
1889 * last row was not full height
1891 int visibleWidths = wrappedVisibleWidths;
1892 int canvasHeight = getHeight();
1893 boolean lastWidthPartHeight = (wrappedVisibleWidths * wrappedRepeatHeightPx) > canvasHeight;
1895 if (lastWidthPartHeight)
1897 int widthsAbove = Math.max(0, visibleWidths - 2);
1898 int ypos = wrappedRepeatHeightPx * widthsAbove
1899 + wrappedSpaceAboveAlignment;
1900 int endRes = ranges.getEndRes();
1901 endRes += widthsAbove * viewportWidth;
1902 int startRes = endRes - columns;
1903 int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
1907 * white fill first to erase annotations
1909 gg.translate(xOffset, 0);
1910 gg.setColor(Color.white);
1911 gg.fillRect(labelWidthWest, ypos,
1912 (endRes - startRes + 1) * charWidth, wrappedRepeatHeightPx);
1913 gg.translate(-xOffset, 0);
1915 drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
1919 * draw newly visible columns in last wrapped width (none if we
1920 * have reached the end of the alignment)
1921 * y-offset for drawing last width is height of widths above,
1924 int widthsAbove = visibleWidths - 1;
1925 int ypos = wrappedRepeatHeightPx * widthsAbove
1926 + wrappedSpaceAboveAlignment;
1927 int endRes = ranges.getEndRes();
1928 endRes += widthsAbove * viewportWidth;
1929 int startRes = endRes - columns + 1;
1932 * white fill first to erase annotations
1934 int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
1936 gg.translate(xOffset, 0);
1937 gg.setColor(Color.white);
1938 int width = viewportWidth * charWidth - xOffset;
1939 gg.fillRect(labelWidthWest, ypos, width, wrappedRepeatHeightPx);
1940 gg.translate(-xOffset, 0);
1942 gg.setFont(av.getFont());
1943 gg.setColor(Color.black);
1945 if (startRes < ranges.getVisibleAlignmentWidth())
1947 drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
1951 * and finally, white fill any space below the visible alignment
1953 int heightBelow = canvasHeight - visibleWidths * wrappedRepeatHeightPx;
1954 if (heightBelow > 0)
1956 gg.setColor(Color.white);
1957 gg.fillRect(0, canvasHeight - heightBelow, getWidth(), heightBelow);
1962 * Shifts the visible alignment by the specified number of columns - left if
1963 * negative, right if positive. Copies and moves sequences and annotations (if
1964 * shown). Scales, hidden column markers and any newly visible columns must be
1969 protected void shiftWrappedAlignment(int positions)
1975 int charWidth = av.getCharWidth();
1977 int canvasHeight = getHeight();
1978 ViewportRanges ranges = av.getRanges();
1979 int viewportWidth = ranges.getViewportWidth();
1980 int widthToCopy = (ranges.getViewportWidth() - Math.abs(positions))
1982 int heightToCopy = wrappedRepeatHeightPx - wrappedSpaceAboveAlignment;
1983 int xMax = ranges.getVisibleAlignmentWidth();
1988 * shift right (after scroll left)
1989 * for each wrapped width (starting with the last), copy (width-positions)
1990 * columns from the left margin to the right margin, and copy positions
1991 * columns from the right margin of the row above (if any) to the
1992 * left margin of the current row
1996 * get y-offset of last wrapped width, first row of sequences
1998 int y = canvasHeight / wrappedRepeatHeightPx * wrappedRepeatHeightPx;
1999 y += wrappedSpaceAboveAlignment;
2000 int copyFromLeftStart = labelWidthWest;
2001 int copyFromRightStart = copyFromLeftStart + widthToCopy;
2005 gg.copyArea(copyFromLeftStart, y, widthToCopy, heightToCopy,
2006 positions * charWidth, 0);
2009 gg.copyArea(copyFromRightStart, y - wrappedRepeatHeightPx,
2010 positions * charWidth, heightToCopy, -widthToCopy,
2011 wrappedRepeatHeightPx);
2014 y -= wrappedRepeatHeightPx;
2020 * shift left (after scroll right)
2021 * for each wrapped width (starting with the first), copy (width-positions)
2022 * columns from the right margin to the left margin, and copy positions
2023 * columns from the left margin of the row below (if any) to the
2024 * right margin of the current row
2026 int xpos = av.getRanges().getStartRes();
2027 int y = wrappedSpaceAboveAlignment;
2028 int copyFromRightStart = labelWidthWest - positions * charWidth;
2030 while (y < canvasHeight)
2032 gg.copyArea(copyFromRightStart, y, widthToCopy, heightToCopy,
2033 positions * charWidth, 0);
2034 if (y + wrappedRepeatHeightPx < canvasHeight - wrappedRepeatHeightPx
2035 && (xpos + viewportWidth <= xMax))
2037 gg.copyArea(labelWidthWest, y + wrappedRepeatHeightPx, -positions
2038 * charWidth, heightToCopy, widthToCopy,
2039 -wrappedRepeatHeightPx);
2042 y += wrappedRepeatHeightPx;
2043 xpos += viewportWidth;
2050 * Redraws any positions in the search results in the visible region of a
2051 * wrapped alignment. Any highlights are drawn depending on the search results
2052 * set on the Viewport, not the <code>results</code> argument. This allows
2053 * this method to be called either to clear highlights (passing the previous
2054 * search results), or to draw new highlights.
2059 protected boolean drawMappedPositionsWrapped(SearchResultsI results)
2061 if ((results == null) || (gg == null)) // JAL-2784 check gg is not null
2065 int charHeight = av.getCharHeight();
2067 boolean matchFound = false;
2069 calculateWrappedGeometry(getWidth(), getHeight());
2070 int wrappedWidth = av.getWrappedWidth();
2071 int wrappedHeight = wrappedRepeatHeightPx;
2073 ViewportRanges ranges = av.getRanges();
2074 int canvasHeight = getHeight();
2075 int repeats = canvasHeight / wrappedHeight;
2076 if (canvasHeight / wrappedHeight > 0)
2081 int firstVisibleColumn = ranges.getStartRes();
2082 int lastVisibleColumn = ranges.getStartRes() + repeats
2083 * ranges.getViewportWidth() - 1;
2085 AlignmentI alignment = av.getAlignment();
2086 if (av.hasHiddenColumns())
2088 firstVisibleColumn = alignment.getHiddenColumns()
2089 .visibleToAbsoluteColumn(firstVisibleColumn);
2090 lastVisibleColumn = alignment.getHiddenColumns()
2091 .visibleToAbsoluteColumn(lastVisibleColumn);
2094 int gapHeight = charHeight * (av.getScaleAboveWrapped() ? 2 : 1);
2096 for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
2097 .getEndSeq(); seqNo++)
2099 SequenceI seq = alignment.getSequenceAt(seqNo);
2101 int[] visibleResults = results.getResults(seq, firstVisibleColumn,
2103 if (visibleResults != null)
2105 for (int i = 0; i < visibleResults.length - 1; i += 2)
2107 int firstMatchedColumn = visibleResults[i];
2108 int lastMatchedColumn = visibleResults[i + 1];
2109 if (firstMatchedColumn <= lastVisibleColumn
2110 && lastMatchedColumn >= firstVisibleColumn)
2113 * found a search results match in the visible region
2115 firstMatchedColumn = Math.max(firstMatchedColumn,
2116 firstVisibleColumn);
2117 lastMatchedColumn = Math.min(lastMatchedColumn,
2121 * draw each mapped position separately (as contiguous positions may
2122 * wrap across lines)
2124 for (int mappedPos = firstMatchedColumn; mappedPos <= lastMatchedColumn; mappedPos++)
2126 int displayColumn = mappedPos;
2127 if (av.hasHiddenColumns())
2129 displayColumn = alignment.getHiddenColumns()
2130 .absoluteToVisibleColumn(displayColumn);
2134 * transX: offset from left edge of canvas to residue position
2136 int transX = labelWidthWest
2137 + ((displayColumn - ranges.getStartRes()) % wrappedWidth)
2138 * av.getCharWidth();
2141 * transY: offset from top edge of canvas to residue position
2143 int transY = gapHeight;
2144 transY += (displayColumn - ranges.getStartRes())
2145 / wrappedWidth * wrappedHeight;
2146 transY += (seqNo - ranges.getStartSeq()) * av.getCharHeight();
2149 * yOffset is from graphics origin to start of visible region
2151 int yOffset = 0;// (displayColumn / wrappedWidth) * wrappedHeight;
2152 if (transY < getHeight())
2155 gg.translate(transX, transY);
2156 drawPanel(gg, displayColumn, displayColumn, seqNo, seqNo,
2158 gg.translate(-transX, -transY);
2170 * Answers the width in pixels of the left scale labels (0 if not shown)
2174 int getLabelWidthWest()
2176 return labelWidthWest;