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.BasicStroke;
36 import java.awt.BorderLayout;
37 import java.awt.Color;
38 import java.awt.FontMetrics;
39 import java.awt.Graphics;
40 import java.awt.Graphics2D;
41 import java.awt.Rectangle;
42 import java.awt.RenderingHints;
43 import java.awt.image.BufferedImage;
44 import java.beans.PropertyChangeEvent;
45 import java.util.Iterator;
46 import java.util.List;
48 import javax.swing.JPanel;
51 * The Swing component on which the alignment sequences, and annotations (if
52 * shown), are drawn. This includes scales above, left and right (if shown) in
53 * Wrapped mode, but not the scale above in Unwrapped mode.
56 @SuppressWarnings("serial")
57 public class SeqCanvas extends JPanel implements ViewportListenerI
60 * vertical gap in pixels between sequences and annotations when in wrapped mode
62 static final int SEQS_ANNOTATION_GAP = 3;
64 private static final String ZEROS = "0000000000";
66 final FeatureRenderer fr;
76 private final SequenceRenderer seqRdr;
78 boolean fastPaint = false;
80 private boolean fastpainting = false;
82 private AnnotationPanel annotations;
85 * measurements for drawing a wrapped alignment
87 private int labelWidthEast; // label right width in pixels if shown
89 private int labelWidthWest; // label left width in pixels if shown
91 int wrappedSpaceAboveAlignment; // gap between widths
93 int wrappedRepeatHeightPx; // height in pixels of wrapped width
95 private int wrappedVisibleWidths; // number of wrapped widths displayed
97 // Don't do this! Graphics handles are supposed to be transient
98 //private Graphics2D gg;
101 * Creates a new SeqCanvas object.
105 public SeqCanvas(AlignmentPanel ap)
108 fr = new FeatureRenderer(ap);
109 seqRdr = new SequenceRenderer(av);
110 setLayout(new BorderLayout());
111 PaintRefresher.Register(this, av.getSequenceSetId());
112 setBackground(Color.white);
114 av.getRanges().addPropertyChangeListener(this);
117 public SequenceRenderer getSequenceRenderer()
122 public FeatureRenderer getFeatureRenderer()
128 * Draws the scale above a region of a wrapped alignment, consisting of a
129 * column number every major interval (10 columns).
132 * the graphics context to draw on, positioned at the start (bottom
133 * left) of the line on which to draw any scale marks
135 * start alignment column (0..)
137 * end alignment column (0..)
139 * y offset to draw at
141 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
143 int charHeight = av.getCharHeight();
144 int charWidth = av.getCharWidth();
147 * white fill the scale space (for the fastPaint case)
149 g.setColor(Color.white);
150 g.fillRect(0, ypos - charHeight - charHeight / 2, getWidth(),
151 charHeight * 3 / 2 + 2);
152 g.setColor(Color.black);
154 List<ScaleMark> marks = new ScaleRenderer().calculateMarks(av, startx,
156 for (ScaleMark mark : marks)
158 int mpos = mark.column; // (i - startx - 1)
163 String mstring = mark.text;
169 g.drawString(mstring, mpos * charWidth, ypos - (charHeight / 2));
173 * draw a tick mark below the column number, centred on the column;
174 * height of tick mark is 4 pixels less than half a character
176 int xpos = (mpos * charWidth) + (charWidth / 2);
177 g.drawLine(xpos, (ypos + 2) - (charHeight / 2), xpos, ypos - 2);
183 * Draw the scale to the left or right of a wrapped alignment
186 * graphics context, positioned at the start of the scale to be drawn
188 * first column of wrapped width (0.. excluding any hidden columns)
190 * last column of wrapped width (0.. excluding any hidden columns)
192 * vertical offset at which to begin the scale
194 * if true, scale is left of residues, if false, scale is right
196 void drawVerticalScale(Graphics g, final int startx, final int endx,
197 final int ypos, final boolean left)
199 int charHeight = av.getCharHeight();
200 int charWidth = av.getCharWidth();
202 int yPos = ypos + charHeight;
206 if (av.hasHiddenColumns())
208 HiddenColumns hiddenColumns = av.getAlignment().getHiddenColumns();
209 startX = hiddenColumns.visibleToAbsoluteColumn(startx);
210 endX = hiddenColumns.visibleToAbsoluteColumn(endx);
212 FontMetrics fm = getFontMetrics(av.getFont());
214 for (int i = 0; i < av.getAlignment().getHeight(); i++)
216 SequenceI seq = av.getAlignment().getSequenceAt(i);
219 * find sequence position of first non-gapped position -
220 * to the right if scale left, to the left if scale right
222 int index = left ? startX : endX;
224 while (index >= startX && index <= endX)
226 if (!Comparison.isGap(seq.getCharAt(index)))
228 value = seq.findPosition(index);
243 * white fill the space for the scale
245 g.setColor(Color.white);
246 int y = (yPos + (i * charHeight)) - (charHeight / 5);
247 // fillRect origin is top left of rectangle
248 g.fillRect(0, y - charHeight, left ? labelWidthWest : labelWidthEast,
254 * draw scale value, right justified within its width less half a
255 * character width padding on the right
257 int labelSpace = left ? labelWidthWest : labelWidthEast;
258 labelSpace -= charWidth / 2; // leave space to the right
259 String valueAsString = String.valueOf(value);
260 int labelLength = fm.stringWidth(valueAsString);
261 int xOffset = labelSpace - labelLength;
262 g.setColor(Color.black);
263 g.drawString(valueAsString, xOffset, y);
270 * Does a fast paint of an alignment in response to a scroll. Most of the
271 * visible region is simply copied and shifted, and then any newly visible
272 * columns or rows are drawn. The scroll may be horizontal or vertical, but
273 * not both at once. Scrolling may be the result of
275 * <li>dragging a scroll bar</li>
276 * <li>clicking in the scroll bar</li>
277 * <li>scrolling by trackpad, middle mouse button, or other device</li>
278 * <li>by moving the box in the Overview window</li>
279 * <li>programmatically to make a highlighted position visible</li>
280 * <li>pasting a block of sequences</li>
284 * columns to shift right (positive) or left (negative)
286 * rows to shift down (positive) or up (negative)
288 public void fastPaint(int horizontal, int vertical)
292 // if (horizontal != 0 && vertical != 0)
293 // throw new InvalidArgumentException();
294 if (fastpainting || img == null)
302 int charHeight = av.getCharHeight();
303 int charWidth = av.getCharWidth();
305 ViewportRanges ranges = av.getRanges();
306 int startRes = ranges.getStartRes();
307 int endRes = ranges.getEndRes();
308 int startSeq = ranges.getStartSeq();
309 int endSeq = ranges.getEndSeq();
313 if (horizontal > 0) // scrollbar pulled right, image to the left
315 transX = (endRes - startRes - horizontal) * charWidth;
316 startRes = endRes - horizontal;
318 else if (horizontal < 0)
320 endRes = startRes - horizontal;
323 if (vertical > 0) // scroll down
325 startSeq = endSeq - vertical;
327 if (startSeq < ranges.getStartSeq())
328 { // ie scrolling too fast, more than a page at a time
329 startSeq = ranges.getStartSeq();
333 transY = img.getHeight() - ((vertical + 1) * charHeight);
336 else if (vertical < 0)
338 endSeq = startSeq - vertical;
340 if (endSeq > ranges.getEndSeq())
342 endSeq = ranges.getEndSeq();
347 // System.err.println(">>> FastPaint to " + transX + " " + transY + " "
348 // + horizontal + " " + vertical + " " + startRes + " " + endRes
349 // + " " + startSeq + " " + endSeq);
351 Graphics gg = img.getGraphics();
352 gg.copyArea(horizontal * charWidth, vertical * charHeight,
353 img.getWidth(), img.getHeight(), -horizontal * charWidth,
354 -vertical * charHeight);
356 /** @j2sNative xxi = this.img */
358 gg.translate(transX, transY);
359 drawPanel(gg, startRes, endRes, startSeq, endSeq, 0);
360 gg.translate(-transX, -transY);
363 // Call repaint on alignment panel so that repaints from other alignment
364 // panel components can be aggregated. Otherwise performance of the
365 // overview window and others may be adversely affected.
366 // System.out.println("SeqCanvas fastPaint() repaint() request...");
367 av.getAlignPanel().repaint();
370 fastpainting = false;
375 public void paintComponent(Graphics g)
378 int charHeight = av.getCharHeight();
379 int charWidth = av.getCharWidth();
381 int width = getWidth();
382 int height = getHeight();
384 width -= (width % charWidth);
385 height -= (height % charHeight);
387 // BH 2019 can't possibly fastPaint if either width or height is 0
389 if (width == 0 || height == 0)
394 ViewportRanges ranges = av.getRanges();
395 int startRes = ranges.getStartRes();
396 int startSeq = ranges.getStartSeq();
397 int endRes = ranges.getEndRes();
398 int endSeq = ranges.getEndSeq();
400 // [JAL-3226] problem that JavaScript (or Java) may consolidate multiple
401 // repaint() requests in unpredictable ways. In this case, the issue was
402 // that in response to a CTRL-C/CTRL-V paste request, in Java a fast
403 // repaint request preceded two full requests, thus resulting
404 // in a full request for paint. In constrast, in JavaScript, the three
405 // requests were bundled together into one, so the fastPaint flag was
406 // still present for the second and third request.
408 // This resulted in incomplete painting.
410 // The solution was to set seqCanvas.fastPaint and idCanvas.fastPaint false
411 // in PaintRefresher when the target to be painted is one of those two
416 // An initial idea; can be removed once we determine this issue is closed:
417 // if (av.isFastPaintDisabled())
419 // fastPaint = false;
425 || (vis = getVisibleRect()).width != (clip = g
426 .getClipBounds()).width
427 || vis.height != clip.height))
429 g.drawImage(img, 0, 0, this);
430 drawSelectionGroup((Graphics2D) g, startRes, endRes, startSeq,
436 // img is a cached version of the last view we drew.
437 // If we have no img or the size has changed, make a new one.
439 if (img == null || width != img.getWidth()
440 || height != img.getHeight())
442 img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
445 Graphics2D gg = (Graphics2D) img.getGraphics();
446 gg.setFont(av.getFont());
450 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
451 RenderingHints.VALUE_ANTIALIAS_ON);
454 gg.setColor(Color.white);
455 gg.fillRect(0, 0, img.getWidth(), img.getHeight());
457 if (av.getWrapAlignment())
459 drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
463 drawPanel(gg, startRes, endRes, startSeq, endSeq, 0);
466 drawSelectionGroup(gg, startRes, endRes, startSeq, endSeq);
468 g.drawImage(img, 0, 0, this);
474 drawCursor(g, startRes, endRes, startSeq, endSeq);
479 * Draw an alignment panel for printing
482 * Graphics object to draw with
484 * start residue of print area
486 * end residue of print area
488 * start sequence of print area
490 * end sequence of print area
492 public void drawPanelForPrinting(Graphics g1, int startRes, int endRes,
493 int startSeq, int endSeq)
495 drawPanel(g1, startRes, endRes, startSeq, endSeq, 0);
497 drawSelectionGroup((Graphics2D) g1, startRes, endRes,
502 * Draw a wrapped alignment panel for printing
505 * Graphics object to draw with
507 * width of drawing area
508 * @param canvasHeight
509 * height of drawing area
511 * start residue of print area
513 public void drawWrappedPanelForPrinting(Graphics g, int canvasWidth,
514 int canvasHeight, int startRes)
516 drawWrappedPanel(g, canvasWidth, canvasHeight, startRes);
518 SequenceGroup group = av.getSelectionGroup();
521 drawWrappedSelection((Graphics2D) g, group, canvasWidth, canvasHeight,
527 * Returns the visible width of the canvas in residues, after allowing for
528 * East or West scales (if shown)
531 * the width in pixels (possibly including scales)
535 public int getWrappedCanvasWidth(int canvasWidth)
537 int charWidth = av.getCharWidth();
539 FontMetrics fm = getFontMetrics(av.getFont());
543 if (av.getScaleRightWrapped() || av.getScaleLeftWrapped())
545 labelWidth = getLabelWidth(fm);
548 labelWidthEast = av.getScaleRightWrapped() ? labelWidth : 0;
550 labelWidthWest = av.getScaleLeftWrapped() ? labelWidth : 0;
552 return (canvasWidth - labelWidthEast - labelWidthWest) / charWidth;
556 * Returns a pixel width sufficient to show the largest sequence coordinate
557 * (end position) in the alignment, calculated as the FontMetrics width of
558 * zeroes "0000000" limited to the number of decimal digits to be shown (3 for
559 * 1-10, 4 for 11-99 etc). One character width is added to this, to allow for
560 * half a character width space on either side.
565 protected int getLabelWidth(FontMetrics fm)
568 * find the biggest sequence end position we need to show
569 * (note this is not necessarily the sequence length)
572 AlignmentI alignment = av.getAlignment();
573 for (int i = 0; i < alignment.getHeight(); i++)
575 maxWidth = Math.max(maxWidth, alignment.getSequenceAt(i).getEnd());
579 for (int i = maxWidth; i > 0; i /= 10)
584 return fm.stringWidth(ZEROS.substring(0, length)) + av.getCharWidth();
588 * Draws as many widths of a wrapped alignment as can fit in the visible
593 * available width in pixels
594 * @param canvasHeight
595 * available height in pixels
597 * the first column (0...) of the alignment to draw
599 public void drawWrappedPanel(Graphics g, int canvasWidth,
600 int canvasHeight, final int startColumn)
602 int wrappedWidthInResidues = calculateWrappedGeometry(canvasWidth,
605 av.setWrappedWidth(wrappedWidthInResidues);
607 ViewportRanges ranges = av.getRanges();
608 ranges.setViewportStartAndWidth(startColumn, wrappedWidthInResidues);
610 // we need to call this again to make sure the startColumn +
611 // wrappedWidthInResidues values are used to calculate wrappedVisibleWidths
613 calculateWrappedGeometry(canvasWidth, canvasHeight);
616 * draw one width at a time (excluding any scales shown),
617 * until we have run out of either alignment or vertical space available
619 int ypos = wrappedSpaceAboveAlignment;
620 int maxWidth = ranges.getVisibleAlignmentWidth();
622 int start = startColumn;
623 int currentWidth = 0;
624 while ((currentWidth < wrappedVisibleWidths) && (start < maxWidth))
627 .min(maxWidth, start + wrappedWidthInResidues - 1);
628 drawWrappedWidth(g, ypos, start, endColumn, canvasHeight);
629 ypos += wrappedRepeatHeightPx;
630 start += wrappedWidthInResidues;
634 drawWrappedDecorators(g, startColumn);
638 * Calculates and saves values needed when rendering a wrapped alignment.
639 * These depend on many factors, including
641 * <li>canvas width and height</li>
642 * <li>number of visible sequences, and height of annotations if shown</li>
643 * <li>font and character width</li>
644 * <li>whether scales are shown left, right or above the alignment</li>
648 * @param canvasHeight
649 * @return the number of residue columns in each width
651 protected int calculateWrappedGeometry(int canvasWidth, int canvasHeight)
653 int charHeight = av.getCharHeight();
656 * vertical space in pixels between wrapped widths of alignment
657 * - one character height, or two if scale above is drawn
659 wrappedSpaceAboveAlignment = charHeight
660 * (av.getScaleAboveWrapped() ? 2 : 1);
663 * compute height in pixels of the wrapped widths
664 * - start with space above plus sequences
666 wrappedRepeatHeightPx = wrappedSpaceAboveAlignment;
667 wrappedRepeatHeightPx += av.getAlignment().getHeight()
671 * add annotations panel height if shown
672 * also gap between sequences and annotations
674 if (av.isShowAnnotation())
676 wrappedRepeatHeightPx += getAnnotationHeight();
677 wrappedRepeatHeightPx += SEQS_ANNOTATION_GAP; // 3px
681 * number of visible widths (the last one may be part height),
682 * ensuring a part height includes at least one sequence
684 ViewportRanges ranges = av.getRanges();
685 wrappedVisibleWidths = canvasHeight / wrappedRepeatHeightPx;
686 int remainder = canvasHeight % wrappedRepeatHeightPx;
687 if (remainder >= (wrappedSpaceAboveAlignment + charHeight))
689 wrappedVisibleWidths++;
693 * compute width in residues; this also sets East and West label widths
695 int wrappedWidthInResidues = getWrappedCanvasWidth(canvasWidth);
698 * limit visibleWidths to not exceed width of alignment
700 int xMax = ranges.getVisibleAlignmentWidth();
701 int startToEnd = xMax - ranges.getStartRes();
702 int maxWidths = startToEnd / wrappedWidthInResidues;
703 if (startToEnd % wrappedWidthInResidues > 0)
707 wrappedVisibleWidths = Math.min(wrappedVisibleWidths, maxWidths);
709 return wrappedWidthInResidues;
713 * Draws one width of a wrapped alignment, including sequences and
714 * annnotations, if shown, but not scales or hidden column markers
720 * @param canvasHeight
722 protected void drawWrappedWidth(Graphics g, final int ypos,
723 final int startColumn, final int endColumn,
724 final int canvasHeight)
726 ViewportRanges ranges = av.getRanges();
727 int viewportWidth = ranges.getViewportWidth();
729 int endx = Math.min(startColumn + viewportWidth - 1, endColumn);
732 * move right before drawing by the width of the scale left (if any)
733 * plus column offset from left margin (usually zero, but may be non-zero
734 * when fast painting is drawing just a few columns)
736 int charWidth = av.getCharWidth();
737 int xOffset = labelWidthWest
738 + ((startColumn - ranges.getStartRes()) % viewportWidth)
741 g.translate(xOffset, 0);
744 * white fill the region to be drawn (so incremental fast paint doesn't
745 * scribble over an existing image)
747 g.setColor(Color.white);
748 g.fillRect(0, ypos, (endx - startColumn + 1) * charWidth,
749 wrappedRepeatHeightPx);
751 drawPanel(g, startColumn, endx, 0, av.getAlignment().getHeight() - 1,
754 int cHeight = av.getAlignment().getHeight() * av.getCharHeight();
756 if (av.isShowAnnotation())
758 final int yShift = cHeight + ypos + SEQS_ANNOTATION_GAP;
759 g.translate(0, yShift);
760 if (annotations == null)
762 annotations = new AnnotationPanel(av);
765 annotations.renderer.drawComponent(annotations, av, g, -1,
766 startColumn, endx + 1);
767 g.translate(0, -yShift);
769 g.translate(-xOffset, 0);
773 * Draws scales left, right and above (if shown), and any hidden column
774 * markers, on all widths of the wrapped alignment
779 protected void drawWrappedDecorators(Graphics g, final int startColumn)
781 int charWidth = av.getCharWidth();
783 g.setFont(av.getFont());
785 g.setColor(Color.black);
787 int ypos = wrappedSpaceAboveAlignment;
788 ViewportRanges ranges = av.getRanges();
789 int viewportWidth = ranges.getViewportWidth();
790 int maxWidth = ranges.getVisibleAlignmentWidth();
792 int startCol = startColumn;
794 while (widthsDrawn < wrappedVisibleWidths)
796 int endColumn = Math.min(maxWidth, startCol + viewportWidth - 1);
798 if (av.getScaleLeftWrapped())
800 drawVerticalScale(g, startCol, endColumn - 1, ypos, true);
803 if (av.getScaleRightWrapped())
805 int x = labelWidthWest + viewportWidth * charWidth;
808 drawVerticalScale(g, startCol, endColumn, ypos, false);
813 * white fill region of scale above and hidden column markers
814 * (to support incremental fast paint of image)
816 g.translate(labelWidthWest, 0);
817 g.setColor(Color.white);
818 g.fillRect(0, ypos - wrappedSpaceAboveAlignment, viewportWidth
819 * charWidth + labelWidthWest, wrappedSpaceAboveAlignment);
820 g.setColor(Color.black);
821 g.translate(-labelWidthWest, 0);
823 g.translate(labelWidthWest, 0);
825 if (av.getScaleAboveWrapped())
827 drawNorthScale(g, startCol, endColumn, ypos);
830 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
832 drawHiddenColumnMarkers(g, ypos, startCol, endColumn);
835 g.translate(-labelWidthWest, 0);
837 ypos += wrappedRepeatHeightPx;
838 startCol += viewportWidth;
844 * Draws markers (triangles) above hidden column positions between startColumn
852 protected void drawHiddenColumnMarkers(Graphics g, int ypos,
853 int startColumn, int endColumn)
855 int charHeight = av.getCharHeight();
856 int charWidth = av.getCharWidth();
858 g.setColor(Color.blue);
860 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
862 Iterator<Integer> it = hidden.getStartRegionIterator(startColumn,
866 res = it.next() - startColumn;
868 if (res < 0 || res > endColumn - startColumn + 1)
874 * draw a downward-pointing triangle at the hidden columns location
875 * (before the following visible column)
877 int xMiddle = res * charWidth;
878 int[] xPoints = new int[] { xMiddle - charHeight / 4,
879 xMiddle + charHeight / 4, xMiddle };
880 int yTop = ypos - (charHeight / 2);
881 int[] yPoints = new int[] { yTop, yTop, yTop + 8 };
882 g.fillPolygon(xPoints, yPoints, 3);
887 * Draw a selection group over a wrapped alignment
889 private void drawWrappedSelection(Graphics2D g, SequenceGroup group,
891 int canvasHeight, int startRes)
893 // chop the wrapped alignment extent up into panel-sized blocks and treat
894 // each block as if it were a block from an unwrapped alignment
895 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
896 BasicStroke.JOIN_ROUND, 3f, new float[]
898 g.setColor(Color.RED);
900 int charWidth = av.getCharWidth();
901 int cWidth = (canvasWidth - labelWidthEast - labelWidthWest)
903 int startx = startRes;
904 int maxwidth = av.getAlignment().getVisibleWidth();
905 int ypos = wrappedSpaceAboveAlignment;
907 while ((ypos <= canvasHeight) && (startx < maxwidth))
909 // set end value to be start + width, or maxwidth, whichever is smaller
910 int endx = startx + cWidth - 1;
917 g.translate(labelWidthWest, 0);
918 drawUnwrappedSelection(g, group, startx, endx, 0,
919 av.getAlignment().getHeight() - 1,
921 g.translate(-labelWidthWest, 0);
923 ypos += wrappedRepeatHeightPx;
927 g.setStroke(new BasicStroke());
931 * Answers zero if annotations are not shown, otherwise recalculates and answers
932 * the total height of all annotation rows in pixels
936 int getAnnotationHeight()
938 if (!av.isShowAnnotation())
943 if (annotations == null)
945 annotations = new AnnotationPanel(av);
948 return annotations.adjustPanelHeight();
952 * Draws the visible region of the alignment on the graphics context. If there
953 * are hidden column markers in the visible region, then each sub-region
954 * between the markers is drawn separately, followed by the hidden column
958 * the graphics context, positioned at the first residue to be drawn
960 * offset of the first column to draw (0..)
962 * offset of the last column to draw (0..)
964 * offset of the first sequence to draw (0..)
966 * offset of the last sequence to draw (0..)
968 * vertical offset at which to draw (for wrapped alignments)
970 public void drawPanel(Graphics g1, final int startRes, final int endRes,
971 final int startSeq, final int endSeq, final int yOffset)
973 int charHeight = av.getCharHeight();
974 int charWidth = av.getCharWidth();
976 if (!av.hasHiddenColumns())
978 draw(g1, startRes, endRes, startSeq, endSeq, yOffset);
986 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
987 VisibleContigsIterator regions = hidden
988 .getVisContigsIterator(startRes, endRes + 1, true);
990 while (regions.hasNext())
992 int[] region = regions.next();
993 blockEnd = region[1];
994 blockStart = region[0];
997 * draw up to just before the next hidden region, or the end of
998 * the visible region, whichever comes first
1000 g1.translate(screenY * charWidth, 0);
1002 draw(g1, blockStart, blockEnd, startSeq, endSeq, yOffset);
1005 * draw the downline of the hidden column marker (ScalePanel draws the
1006 * triangle on top) if we reached it
1008 if (av.getShowHiddenMarkers()
1009 && (regions.hasNext() || regions.endsAtHidden()))
1011 g1.setColor(Color.blue);
1013 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
1014 0 + yOffset, (blockEnd - blockStart + 1) * charWidth - 1,
1015 (endSeq - startSeq + 1) * charHeight + yOffset);
1018 g1.translate(-screenY * charWidth, 0);
1019 screenY += blockEnd - blockStart + 1;
1026 * Draws a region of the visible alignment
1030 * offset of the first column in the visible region (0..)
1032 * offset of the last column in the visible region (0..)
1034 * offset of the first sequence in the visible region (0..)
1036 * offset of the last sequence in the visible region (0..)
1038 * vertical offset at which to draw (for wrapped alignments)
1040 private void draw(Graphics g, int startRes, int endRes, int startSeq,
1041 int endSeq, int offset)
1043 int charHeight = av.getCharHeight();
1044 int charWidth = av.getCharWidth();
1046 g.setFont(av.getFont());
1047 seqRdr.prepare(g, av.isRenderGaps());
1051 // / First draw the sequences
1052 // ///////////////////////////
1053 for (int i = startSeq; i <= endSeq; i++)
1055 nextSeq = av.getAlignment().getSequenceAt(i);
1056 if (nextSeq == null)
1058 // occasionally, a race condition occurs such that the alignment row is
1062 seqRdr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
1063 startRes, endRes, offset + ((i - startSeq) * charHeight));
1065 if (av.isShowSequenceFeatures())
1067 fr.drawSequence(g, nextSeq, startRes, endRes,
1068 offset + ((i - startSeq) * charHeight), false);
1072 * highlight search Results once sequence has been drawn
1074 if (av.hasSearchResults())
1076 SearchResultsI searchResults = av.getSearchResults();
1077 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
1079 if (visibleResults != null)
1081 for (int r = 0; r < visibleResults.length; r += 2)
1083 seqRdr.drawHighlightedText(nextSeq, visibleResults[r],
1084 visibleResults[r + 1],
1085 (visibleResults[r] - startRes) * charWidth,
1086 offset + ((i - startSeq) * charHeight));
1092 if (av.getSelectionGroup() != null
1093 || av.getAlignment().getGroups().size() > 0)
1095 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
1101 * Draws the outlines of any groups defined on the alignment (excluding the
1102 * current selection group, if any)
1111 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
1112 int startSeq, int endSeq, int offset)
1114 Graphics2D g = (Graphics2D) g1;
1116 SequenceGroup group = null;
1117 int groupIndex = -1;
1119 if (av.getAlignment().getGroups().size() > 0)
1121 group = av.getAlignment().getGroups().get(0);
1129 g.setColor(group.getOutlineColour());
1130 drawPartialGroupOutline(g, group, startRes, endRes, startSeq,
1134 if (groupIndex >= av.getAlignment().getGroups().size())
1138 group = av.getAlignment().getGroups().get(groupIndex);
1139 } while (groupIndex < av.getAlignment().getGroups().size());
1144 * Draws the outline of the current selection group (if any)
1152 private void drawSelectionGroup(Graphics2D g, int startRes, int endRes,
1153 int startSeq, int endSeq)
1155 SequenceGroup group = av.getSelectionGroup();
1161 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
1162 BasicStroke.JOIN_ROUND, 3f, new float[]
1164 g.setColor(Color.RED);
1165 if (!av.getWrapAlignment())
1167 drawUnwrappedSelection(g, group, startRes, endRes, startSeq, endSeq,
1172 drawWrappedSelection(g, group, getWidth(), getHeight(),
1173 av.getRanges().getStartRes());
1175 g.setStroke(new BasicStroke());
1179 * Draw the cursor as a separate image and overlay
1182 * start residue of area to draw cursor in
1184 * end residue of area to draw cursor in
1186 * start sequence of area to draw cursor in
1188 * end sequence of are to draw cursor in
1189 * @return a transparent image of the same size as the sequence canvas, with
1190 * the cursor drawn on it, if any
1192 private void drawCursor(Graphics g, int startRes, int endRes,
1196 // convert the cursorY into a position on the visible alignment
1197 int cursor_ypos = cursorY;
1199 // don't do work unless we have to
1200 if (cursor_ypos >= startSeq && cursor_ypos <= endSeq)
1204 int startx = startRes;
1207 // convert the cursorX into a position on the visible alignment
1208 int cursor_xpos = av.getAlignment().getHiddenColumns()
1209 .absoluteToVisibleColumn(cursorX);
1211 if (av.getAlignment().getHiddenColumns().isVisible(cursorX))
1214 if (av.getWrapAlignment())
1216 // work out the correct offsets for the cursor
1217 int charHeight = av.getCharHeight();
1218 int charWidth = av.getCharWidth();
1219 int canvasWidth = getWidth();
1220 int canvasHeight = getHeight();
1222 // height gap above each panel
1223 int hgap = charHeight;
1224 if (av.getScaleAboveWrapped())
1229 int cWidth = (canvasWidth - labelWidthEast - labelWidthWest)
1231 int cHeight = av.getAlignment().getHeight() * charHeight;
1233 endx = startx + cWidth - 1;
1234 int ypos = hgap; // vertical offset
1236 // iterate down the wrapped panels
1237 while ((ypos <= canvasHeight) && (endx < cursor_xpos))
1239 // update vertical offset
1240 ypos += cHeight + getAnnotationHeight() + hgap;
1242 // update horizontal offset
1244 endx = startx + cWidth - 1;
1247 xoffset = labelWidthWest;
1250 // now check if cursor is within range for x values
1251 if (cursor_xpos >= startx && cursor_xpos <= endx)
1253 // get the character the cursor is drawn at
1254 SequenceI seq = av.getAlignment().getSequenceAt(cursorY);
1255 char s = seq.getCharAt(cursorX);
1257 seqRdr.drawCursor(g, s,
1258 xoffset + (cursor_xpos - startx) * av.getCharWidth(),
1259 yoffset + (cursor_ypos - startSeq) * av.getCharHeight());
1267 * Draw a selection group over an unwrapped alignment
1270 * graphics object to draw with
1274 * start residue of area to draw
1276 * end residue of area to draw
1278 * start sequence of area to draw
1280 * end sequence of area to draw
1282 * vertical offset (used when called from wrapped alignment code)
1284 private void drawUnwrappedSelection(Graphics2D g, SequenceGroup group,
1285 int startRes, int endRes, int startSeq, int endSeq, int offset)
1287 int charWidth = av.getCharWidth();
1289 if (!av.hasHiddenColumns())
1291 drawPartialGroupOutline(g, group, startRes, endRes, startSeq, endSeq,
1296 // package into blocks of visible columns
1301 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
1302 VisibleContigsIterator regions = hidden
1303 .getVisContigsIterator(startRes, endRes + 1, true);
1304 while (regions.hasNext())
1306 int[] region = regions.next();
1307 blockEnd = region[1];
1308 blockStart = region[0];
1310 g.translate(screenY * charWidth, 0);
1311 drawPartialGroupOutline(g, group,
1312 blockStart, blockEnd, startSeq, endSeq, offset);
1314 g.translate(-screenY * charWidth, 0);
1315 screenY += blockEnd - blockStart + 1;
1321 * Draws part of a selection group outline
1329 * @param verticalOffset
1331 private void drawPartialGroupOutline(Graphics2D g, SequenceGroup group,
1332 int startRes, int endRes, int startSeq, int endSeq,
1335 int charHeight = av.getCharHeight();
1336 int charWidth = av.getCharWidth();
1337 int visWidth = (endRes - startRes + 1) * charWidth;
1341 boolean inGroup = false;
1346 List<SequenceI> seqs = group.getSequences(null);
1348 // position of start residue of group relative to startRes, in pixels
1349 int sx = (group.getStartRes() - startRes) * charWidth;
1351 // width of group in pixels
1352 int xwidth = (((group.getEndRes() + 1) - group.getStartRes())
1355 if (!(sx + xwidth < 0 || sx > visWidth))
1357 for (i = startSeq; i <= endSeq; i++)
1359 sy = verticalOffset + (i - startSeq) * charHeight;
1361 if ((sx <= (endRes - startRes) * charWidth)
1362 && seqs.contains(av.getAlignment().getSequenceAt(i)))
1365 && !seqs.contains(av.getAlignment().getSequenceAt(i + 1)))
1367 bottom = sy + charHeight;
1372 if (((top == -1) && (i == 0)) || !seqs
1373 .contains(av.getAlignment().getSequenceAt(i - 1)))
1384 drawVerticals(g, sx, xwidth, visWidth, oldY, sy);
1385 drawHorizontals(g, sx, xwidth, visWidth, top, bottom);
1387 // reset top and bottom
1395 sy = verticalOffset + ((i - startSeq) * charHeight);
1396 drawVerticals(g, sx, xwidth, visWidth, oldY, sy);
1397 drawHorizontals(g, sx, xwidth, visWidth, top, bottom);
1403 * Draw horizontal selection group boundaries at top and bottom positions
1406 * graphics object to draw on
1412 * visWidth maximum available width
1414 * position to draw top of group at
1416 * position to draw bottom of group at
1418 private void drawHorizontals(Graphics2D g, int sx, int xwidth,
1419 int visWidth, int top, int bottom)
1429 // don't let width extend beyond current block, or group extent
1431 if (startx + width >= visWidth)
1433 width = visWidth - startx;
1438 g.drawLine(startx, top, startx + width, top);
1443 g.drawLine(startx, bottom - 1, startx + width, bottom - 1);
1448 * Draw vertical lines at sx and sx+xwidth providing they lie within
1452 * graphics object to draw on
1458 * visWidth maximum available width
1464 private void drawVerticals(Graphics2D g, int sx, int xwidth, int visWidth,
1467 // if start position is visible, draw vertical line to left of
1469 if (sx >= 0 && sx < visWidth)
1471 g.drawLine(sx, oldY, sx, sy);
1474 // if end position is visible, draw vertical line to right of
1476 if (sx + xwidth < visWidth)
1478 g.drawLine(sx + xwidth, oldY, sx + xwidth, sy);
1483 * Highlights search results in the visible region by rendering as white text
1484 * on a black background. Any previous highlighting is removed. Answers true
1485 * if any highlight was left on the visible alignment (so status bar should be
1486 * set to match), else false. This method does _not_ set the 'fastPaint' flag,
1487 * so allows the next repaint to update the whole display.
1492 public boolean highlightSearchResults(SearchResultsI results)
1494 return highlightSearchResults(results, false);
1499 * Highlights search results in the visible region by rendering as white text
1500 * on a black background. Any previous highlighting is removed. Answers true
1501 * if any highlight was left on the visible alignment (so status bar should be
1502 * set to match), else false.
1504 * Optionally, set the 'fastPaint' flag for a faster redraw if only the
1505 * highlighted regions are modified. This speeds up highlighting across linked
1508 * Currently fastPaint is not implemented for scrolled wrapped alignments. If
1509 * a wrapped alignment had to be scrolled to show the highlighted region, then
1510 * it should be fully redrawn, otherwise a fast paint can be performed. This
1511 * argument could be removed if fast paint of scrolled wrapped alignment is
1512 * coded in future (JAL-2609).
1515 * @param doFastPaint
1516 * if true, sets a flag so the next repaint only redraws the modified
1520 public boolean highlightSearchResults(SearchResultsI results,
1521 boolean doFastPaint)
1527 boolean wrapped = av.getWrapAlignment();
1530 fastPaint = doFastPaint;
1531 fastpainting = fastPaint;
1534 * to avoid redrawing the whole visible region, we instead
1535 * redraw just the minimal regions to remove previous highlights
1538 SearchResultsI previous = av.getSearchResults();
1539 av.setSearchResults(results);
1540 boolean redrawn = false;
1541 boolean drawn = false;
1544 redrawn = drawMappedPositionsWrapped(previous);
1545 drawn = drawMappedPositionsWrapped(results);
1550 redrawn = drawMappedPositions(previous);
1551 drawn = drawMappedPositions(results);
1556 * if highlights were either removed or added, repaint
1564 * return true only if highlights were added
1570 fastpainting = false;
1575 * Redraws the minimal rectangle in the visible region (if any) that includes
1576 * mapped positions of the given search results. Whether or not positions are
1577 * highlighted depends on the SearchResults set on the Viewport. This allows
1578 * this method to be called to either clear or set highlighting. Answers true
1579 * if any positions were drawn (in which case a repaint is still required),
1585 protected boolean drawMappedPositions(SearchResultsI results)
1587 if ((results == null) || (img == null)) // JAL-2784 check gg is not null
1593 * calculate the minimal rectangle to redraw that
1594 * includes both new and existing search results
1596 int firstSeq = Integer.MAX_VALUE;
1598 int firstCol = Integer.MAX_VALUE;
1600 boolean matchFound = false;
1602 ViewportRanges ranges = av.getRanges();
1603 int firstVisibleColumn = ranges.getStartRes();
1604 int lastVisibleColumn = ranges.getEndRes();
1605 AlignmentI alignment = av.getAlignment();
1606 if (av.hasHiddenColumns())
1608 firstVisibleColumn = alignment.getHiddenColumns()
1609 .visibleToAbsoluteColumn(firstVisibleColumn);
1610 lastVisibleColumn = alignment.getHiddenColumns()
1611 .visibleToAbsoluteColumn(lastVisibleColumn);
1614 for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
1615 .getEndSeq(); seqNo++)
1617 SequenceI seq = alignment.getSequenceAt(seqNo);
1619 int[] visibleResults = results.getResults(seq, firstVisibleColumn,
1621 if (visibleResults != null)
1623 for (int i = 0; i < visibleResults.length - 1; i += 2)
1625 int firstMatchedColumn = visibleResults[i];
1626 int lastMatchedColumn = visibleResults[i + 1];
1627 if (firstMatchedColumn <= lastVisibleColumn
1628 && lastMatchedColumn >= firstVisibleColumn)
1631 * found a search results match in the visible region -
1632 * remember the first and last sequence matched, and the first
1633 * and last visible columns in the matched positions
1636 firstSeq = Math.min(firstSeq, seqNo);
1637 lastSeq = Math.max(lastSeq, seqNo);
1638 firstMatchedColumn = Math.max(firstMatchedColumn,
1639 firstVisibleColumn);
1640 lastMatchedColumn = Math.min(lastMatchedColumn,
1642 firstCol = Math.min(firstCol, firstMatchedColumn);
1643 lastCol = Math.max(lastCol, lastMatchedColumn);
1651 if (av.hasHiddenColumns())
1653 firstCol = alignment.getHiddenColumns()
1654 .absoluteToVisibleColumn(firstCol);
1655 lastCol = alignment.getHiddenColumns().absoluteToVisibleColumn(lastCol);
1657 int transX = (firstCol - ranges.getStartRes()) * av.getCharWidth();
1658 int transY = (firstSeq - ranges.getStartSeq()) * av.getCharHeight();
1659 Graphics gg = img.getGraphics();
1660 gg.translate(transX, transY);
1661 drawPanel(gg, firstCol, lastCol, firstSeq, lastSeq, 0);
1662 gg.translate(-transX, -transY);
1670 public void propertyChange(PropertyChangeEvent evt)
1672 String eventName = evt.getPropertyName();
1673 // System.err.println(">>SeqCanvas propertyChange " + eventName);
1674 if (eventName.equals(SequenceGroup.SEQ_GROUP_CHANGED))
1680 else if (eventName.equals(ViewportRanges.MOVE_VIEWPORT))
1683 // System.err.println("!!!! fastPaint false from MOVE_VIEWPORT");
1689 if (eventName.equals(ViewportRanges.STARTRES)
1690 || eventName.equals(ViewportRanges.STARTRESANDSEQ))
1692 // Make sure we're not trying to draw a panel
1693 // larger than the visible window
1694 if (eventName.equals(ViewportRanges.STARTRES))
1696 scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
1700 scrollX = ((int[]) evt.getNewValue())[0]
1701 - ((int[]) evt.getOldValue())[0];
1703 ViewportRanges vpRanges = av.getRanges();
1705 int range = vpRanges.getEndRes() - vpRanges.getStartRes() + 1;
1706 if (scrollX > range)
1710 else if (scrollX < -range)
1715 // Both scrolling and resizing change viewport ranges: scrolling changes
1716 // both start and end points, but resize only changes end values.
1717 // Here we only want to fastpaint on a scroll, with resize using a normal
1718 // paint, so scroll events are identified as changes to the horizontal or
1719 // vertical start value.
1720 if (eventName.equals(ViewportRanges.STARTRES))
1722 if (av.getWrapAlignment())
1724 fastPaintWrapped(scrollX);
1728 fastPaint(scrollX, 0);
1731 else if (eventName.equals(ViewportRanges.STARTSEQ))
1734 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());
1736 else if (eventName.equals(ViewportRanges.STARTRESANDSEQ))
1738 if (av.getWrapAlignment())
1740 fastPaintWrapped(scrollX);
1744 fastPaint(scrollX, 0);
1747 else if (eventName.equals(ViewportRanges.STARTSEQ))
1750 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());
1752 else if (eventName.equals(ViewportRanges.STARTRESANDSEQ))
1754 if (av.getWrapAlignment())
1756 fastPaintWrapped(scrollX);
1762 * Does a minimal update of the image for a scroll movement. This method
1763 * handles scroll movements of up to one width of the wrapped alignment (one
1764 * click in the vertical scrollbar). Larger movements (for example after a
1765 * scroll to highlight a mapped position) trigger a full redraw instead.
1768 * number of positions scrolled (right if positive, left if negative)
1770 protected void fastPaintWrapped(int scrollX)
1772 ViewportRanges ranges = av.getRanges();
1774 if (Math.abs(scrollX) >= ranges.getViewportWidth())
1777 * shift of one view width or more is
1778 * overcomplicated to handle in this method
1785 if (fastpainting || img == null)
1791 fastpainting = true;
1796 Graphics gg = img.getGraphics();
1798 calculateWrappedGeometry(getWidth(), getHeight());
1801 * relocate the regions of the alignment that are still visible
1803 shiftWrappedAlignment(-scrollX);
1806 * add new columns (sequence, annotation)
1807 * - at top left if scrollX < 0
1808 * - at right of last two widths if scrollX > 0
1812 int startRes = ranges.getStartRes();
1813 drawWrappedWidth(gg, wrappedSpaceAboveAlignment, startRes, startRes
1814 - scrollX - 1, getHeight());
1818 fastPaintWrappedAddRight(scrollX);
1822 * draw all scales (if shown) and hidden column markers
1824 drawWrappedDecorators(gg, ranges.getStartRes());
1831 fastpainting = false;
1836 * Draws the specified number of columns at the 'end' (bottom right) of a
1837 * wrapped alignment view, including sequences and annotations if shown, but
1838 * not scales. Also draws the same number of columns at the right hand end of
1839 * the second last width shown, if the last width is not full height (so
1840 * cannot simply be copied from the graphics image).
1844 protected void fastPaintWrappedAddRight(int columns)
1851 Graphics gg = img.getGraphics();
1853 ViewportRanges ranges = av.getRanges();
1854 int viewportWidth = ranges.getViewportWidth();
1855 int charWidth = av.getCharWidth();
1858 * draw full height alignment in the second last row, last columns, if the
1859 * last row was not full height
1861 int visibleWidths = wrappedVisibleWidths;
1862 int canvasHeight = getHeight();
1863 boolean lastWidthPartHeight = (wrappedVisibleWidths * wrappedRepeatHeightPx) > canvasHeight;
1865 if (lastWidthPartHeight)
1867 int widthsAbove = Math.max(0, visibleWidths - 2);
1868 int ypos = wrappedRepeatHeightPx * widthsAbove
1869 + wrappedSpaceAboveAlignment;
1870 int endRes = ranges.getEndRes();
1871 endRes += widthsAbove * viewportWidth;
1872 int startRes = endRes - columns;
1873 int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
1877 * white fill first to erase annotations
1881 gg.translate(xOffset, 0);
1882 gg.setColor(Color.white);
1883 gg.fillRect(labelWidthWest, ypos,
1884 (endRes - startRes + 1) * charWidth, wrappedRepeatHeightPx);
1885 gg.translate(-xOffset, 0);
1887 drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
1892 * draw newly visible columns in last wrapped width (none if we
1893 * have reached the end of the alignment)
1894 * y-offset for drawing last width is height of widths above,
1897 int widthsAbove = visibleWidths - 1;
1898 int ypos = wrappedRepeatHeightPx * widthsAbove
1899 + wrappedSpaceAboveAlignment;
1900 int endRes = ranges.getEndRes();
1901 endRes += widthsAbove * viewportWidth;
1902 int startRes = endRes - columns + 1;
1905 * white fill first to erase annotations
1907 int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
1909 gg.translate(xOffset, 0);
1910 gg.setColor(Color.white);
1911 int width = viewportWidth * charWidth - xOffset;
1912 gg.fillRect(labelWidthWest, ypos, width, wrappedRepeatHeightPx);
1913 gg.translate(-xOffset, 0);
1915 gg.setFont(av.getFont());
1916 gg.setColor(Color.black);
1918 if (startRes < ranges.getVisibleAlignmentWidth())
1920 drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
1924 * and finally, white fill any space below the visible alignment
1926 int heightBelow = canvasHeight - visibleWidths * wrappedRepeatHeightPx;
1927 if (heightBelow > 0)
1929 gg.setColor(Color.white);
1930 gg.fillRect(0, canvasHeight - heightBelow, getWidth(), heightBelow);
1936 * Shifts the visible alignment by the specified number of columns - left if
1937 * negative, right if positive. Copies and moves sequences and annotations (if
1938 * shown). Scales, hidden column markers and any newly visible columns must be
1943 protected void shiftWrappedAlignment(int positions)
1950 Graphics gg = img.getGraphics();
1952 int charWidth = av.getCharWidth();
1954 int canvasHeight = getHeight();
1955 ViewportRanges ranges = av.getRanges();
1956 int viewportWidth = ranges.getViewportWidth();
1957 int widthToCopy = (ranges.getViewportWidth() - Math.abs(positions))
1959 int heightToCopy = wrappedRepeatHeightPx - wrappedSpaceAboveAlignment;
1960 int xMax = ranges.getVisibleAlignmentWidth();
1965 * shift right (after scroll left)
1966 * for each wrapped width (starting with the last), copy (width-positions)
1967 * columns from the left margin to the right margin, and copy positions
1968 * columns from the right margin of the row above (if any) to the
1969 * left margin of the current row
1973 * get y-offset of last wrapped width, first row of sequences
1975 int y = canvasHeight / wrappedRepeatHeightPx * wrappedRepeatHeightPx;
1976 y += wrappedSpaceAboveAlignment;
1977 int copyFromLeftStart = labelWidthWest;
1978 int copyFromRightStart = copyFromLeftStart + widthToCopy;
1983 * shift 'widthToCopy' residues by 'positions' places to the right
1985 gg.copyArea(copyFromLeftStart, y, widthToCopy, heightToCopy,
1986 positions * charWidth, 0);
1990 * copy 'positions' residue from the row above (right hand end)
1991 * to this row's left hand end
1993 gg.copyArea(copyFromRightStart, y - wrappedRepeatHeightPx,
1994 positions * charWidth, heightToCopy, -widthToCopy,
1995 wrappedRepeatHeightPx);
1998 y -= wrappedRepeatHeightPx;
2004 * shift left (after scroll right)
2005 * for each wrapped width (starting with the first), copy (width-positions)
2006 * columns from the right margin to the left margin, and copy positions
2007 * columns from the left margin of the row below (if any) to the
2008 * right margin of the current row
2010 int xpos = av.getRanges().getStartRes();
2011 int y = wrappedSpaceAboveAlignment;
2012 int copyFromRightStart = labelWidthWest - positions * charWidth;
2014 while (y < canvasHeight)
2016 gg.copyArea(copyFromRightStart, y, widthToCopy, heightToCopy,
2017 positions * charWidth, 0);
2018 if (y + wrappedRepeatHeightPx < canvasHeight - wrappedRepeatHeightPx
2019 && (xpos + viewportWidth <= xMax))
2021 gg.copyArea(labelWidthWest, y + wrappedRepeatHeightPx, -positions
2022 * charWidth, heightToCopy, widthToCopy,
2023 -wrappedRepeatHeightPx);
2025 y += wrappedRepeatHeightPx;
2026 xpos += viewportWidth;
2034 * Redraws any positions in the search results in the visible region of a
2035 * wrapped alignment. Any highlights are drawn depending on the search results
2036 * set on the Viewport, not the <code>results</code> argument. This allows
2037 * this method to be called either to clear highlights (passing the previous
2038 * search results), or to draw new highlights.
2043 protected boolean drawMappedPositionsWrapped(SearchResultsI results)
2045 if ((results == null) || (img == null)) // JAL-2784 check gg is not null
2049 int charHeight = av.getCharHeight();
2051 boolean matchFound = false;
2053 calculateWrappedGeometry(getWidth(), getHeight());
2054 int wrappedWidth = av.getWrappedWidth();
2055 int wrappedHeight = wrappedRepeatHeightPx;
2057 ViewportRanges ranges = av.getRanges();
2058 int canvasHeight = getHeight();
2059 int repeats = canvasHeight / wrappedHeight;
2060 if (canvasHeight / wrappedHeight > 0)
2065 int firstVisibleColumn = ranges.getStartRes();
2066 int lastVisibleColumn = ranges.getStartRes() + repeats
2067 * ranges.getViewportWidth() - 1;
2069 AlignmentI alignment = av.getAlignment();
2070 if (av.hasHiddenColumns())
2072 firstVisibleColumn = alignment.getHiddenColumns()
2073 .visibleToAbsoluteColumn(firstVisibleColumn);
2074 lastVisibleColumn = alignment.getHiddenColumns()
2075 .visibleToAbsoluteColumn(lastVisibleColumn);
2078 int gapHeight = charHeight * (av.getScaleAboveWrapped() ? 2 : 1);
2081 Graphics gg = img.getGraphics();
2083 for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
2084 .getEndSeq(); seqNo++)
2086 SequenceI seq = alignment.getSequenceAt(seqNo);
2088 int[] visibleResults = results.getResults(seq, firstVisibleColumn,
2090 if (visibleResults != null)
2092 for (int i = 0; i < visibleResults.length - 1; i += 2)
2094 int firstMatchedColumn = visibleResults[i];
2095 int lastMatchedColumn = visibleResults[i + 1];
2096 if (firstMatchedColumn <= lastVisibleColumn
2097 && lastMatchedColumn >= firstVisibleColumn)
2100 * found a search results match in the visible region
2102 firstMatchedColumn = Math.max(firstMatchedColumn,
2103 firstVisibleColumn);
2104 lastMatchedColumn = Math.min(lastMatchedColumn,
2108 * draw each mapped position separately (as contiguous positions may
2109 * wrap across lines)
2111 for (int mappedPos = firstMatchedColumn; mappedPos <= lastMatchedColumn; mappedPos++)
2113 int displayColumn = mappedPos;
2114 if (av.hasHiddenColumns())
2116 displayColumn = alignment.getHiddenColumns()
2117 .absoluteToVisibleColumn(displayColumn);
2121 * transX: offset from left edge of canvas to residue position
2123 int transX = labelWidthWest
2124 + ((displayColumn - ranges.getStartRes()) % wrappedWidth)
2125 * av.getCharWidth();
2128 * transY: offset from top edge of canvas to residue position
2130 int transY = gapHeight;
2131 transY += (displayColumn - ranges.getStartRes())
2132 / wrappedWidth * wrappedHeight;
2133 transY += (seqNo - ranges.getStartSeq()) * av.getCharHeight();
2136 * yOffset is from graphics origin to start of visible region
2138 int yOffset = 0;// (displayColumn / wrappedWidth) * wrappedHeight;
2139 if (transY < getHeight())
2142 gg.translate(transX, transY);
2143 drawPanel(gg, displayColumn, displayColumn, seqNo, seqNo,
2145 gg.translate(-transX, -transY);
2159 * Answers the width in pixels of the left scale labels (0 if not shown)
2163 int getLabelWidthWest()
2165 return labelWidthWest;