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.RenderingHints;
42 import java.awt.Shape;
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 public class SeqCanvas extends JPanel implements ViewportListenerI
58 private static final String ZEROS = "0000000000";
60 final FeatureRenderer fr;
70 private final SequenceRenderer seqRdr;
72 private boolean fastPaint = false;
74 private boolean fastpainting = false;
76 private AnnotationPanel annotations;
79 * measurements for drawing a wrapped alignment
81 private int labelWidthEast; // label right width in pixels if shown
83 private int labelWidthWest; // label left width in pixels if shown
85 private int wrappedSpaceAboveAlignment; // gap between widths
87 private int wrappedRepeatHeightPx; // height in pixels of wrapped width
89 private int wrappedVisibleWidths; // number of wrapped widths displayed
91 // Don't do this! Graphics handles are supposed to be transient
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);
237 * white fill the space for the scale
239 g.setColor(Color.white);
240 int y = (yPos + (i * charHeight)) - (charHeight / 5);
241 // fillRect origin is top left of rectangle
242 g.fillRect(0, y - charHeight, left ? labelWidthWest : labelWidthEast,
248 * draw scale value, right justified within its width less half a
249 * character width padding on the right
251 int labelSpace = left ? labelWidthWest : labelWidthEast;
252 labelSpace -= charWidth / 2; // leave space to the right
253 String valueAsString = String.valueOf(value);
254 int labelLength = fm.stringWidth(valueAsString);
255 int xOffset = labelSpace - labelLength;
256 g.setColor(Color.black);
257 g.drawString(valueAsString, xOffset, y);
264 * Does a fast paint of an alignment in response to a scroll. Most of the
265 * visible region is simply copied and shifted, and then any newly visible
266 * columns or rows are drawn. The scroll may be horizontal or vertical, but
267 * not both at once. Scrolling may be the result of
269 * <li>dragging a scroll bar</li>
270 * <li>clicking in the scroll bar</li>
271 * <li>scrolling by trackpad, middle mouse button, or other device</li>
272 * <li>by moving the box in the Overview window</li>
273 * <li>programmatically to make a highlighted position visible</li>
277 * columns to shift right (positive) or left (negative)
279 * rows to shift down (positive) or up (negative)
281 public void fastPaint(int horizontal, int vertical)
283 if (fastpainting || img == null)
292 int charHeight = av.getCharHeight();
293 int charWidth = av.getCharWidth();
295 ViewportRanges ranges = av.getRanges();
296 int startRes = ranges.getStartRes();
297 int endRes = ranges.getEndRes();
298 int startSeq = ranges.getStartSeq();
299 int endSeq = ranges.getEndSeq();
303 Graphics gg = img.getGraphics();
304 gg.copyArea(horizontal * charWidth, vertical * charHeight,
305 img.getWidth(), img.getHeight(), -horizontal * charWidth,
306 -vertical * charHeight);
308 if (horizontal > 0) // scrollbar pulled right, image to the left
310 transX = (endRes - startRes - horizontal) * charWidth;
311 startRes = endRes - horizontal;
313 else if (horizontal < 0)
315 endRes = startRes - horizontal;
318 if (vertical > 0) // scroll down
320 startSeq = endSeq - vertical;
322 if (startSeq < ranges.getStartSeq())
323 { // ie scrolling too fast, more than a page at a time
324 startSeq = ranges.getStartSeq();
328 transY = img.getHeight() - ((vertical + 1) * charHeight);
331 else if (vertical < 0)
333 endSeq = startSeq - vertical;
335 if (endSeq > ranges.getEndSeq())
337 endSeq = ranges.getEndSeq();
341 gg.translate(transX, transY);
342 drawPanel(gg, startRes, endRes, startSeq, endSeq, 0);
343 gg.translate(-transX, -transY);
346 // Call repaint on alignment panel so that repaints from other alignment
347 // panel components can be aggregated. Otherwise performance of the
348 // overview window and others may be adversely affected.
349 av.getAlignPanel().repaint();
352 fastpainting = false;
357 public void paintComponent(Graphics g)
359 super.paintComponent(g);
361 int charHeight = av.getCharHeight();
362 int charWidth = av.getCharWidth();
364 ViewportRanges ranges = av.getRanges();
366 int width = getWidth();
367 int height = getHeight();
369 width -= (width % charWidth);
370 height -= (height % charHeight);
372 if ((img != null) && (fastPaint
373 || (getVisibleRect().width != g.getClipBounds().width)
374 || (getVisibleRect().height != g.getClipBounds().height)))
376 g.drawImage(img, 0, 0, this);
378 drawSelectionGroup((Graphics2D) g, ranges.getStartRes(),
379 ranges.getEndRes(), ranges.getStartSeq(), ranges.getEndSeq());
383 else if (width > 0 && height > 0)
386 * img is a cached version of the last view we drew, if any
387 * if we have no img or the size has changed, make a new one
389 if (img == null || width != img.getWidth()
390 || height != img.getHeight())
392 img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
395 Graphics2D gg = (Graphics2D) img.getGraphics();
396 gg.setFont(av.getFont());
400 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
401 RenderingHints.VALUE_ANTIALIAS_ON);
404 gg.setColor(Color.white);
405 gg.fillRect(0, 0, img.getWidth(), img.getHeight());
407 if (av.getWrapAlignment())
409 drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
413 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
414 ranges.getStartSeq(), ranges.getEndSeq(), 0);
417 drawSelectionGroup(gg, ranges.getStartRes(),
418 ranges.getEndRes(), ranges.getStartSeq(), ranges.getEndSeq());
420 g.drawImage(img, 0, 0, this);
425 drawCursor(g, ranges.getStartRes(), ranges.getEndRes(),
426 ranges.getStartSeq(), ranges.getEndSeq());
431 * Draw an alignment panel for printing
434 * Graphics object to draw with
436 * start residue of print area
438 * end residue of print area
440 * start sequence of print area
442 * end sequence of print area
444 public void drawPanelForPrinting(Graphics g1, int startRes, int endRes,
445 int startSeq, int endSeq)
447 drawPanel(g1, startRes, endRes, startSeq, endSeq, 0);
449 drawSelectionGroup((Graphics2D) g1, startRes, endRes,
454 * Draw a wrapped alignment panel for printing
457 * Graphics object to draw with
459 * width of drawing area
460 * @param canvasHeight
461 * height of drawing area
463 * start residue of print area
465 public void drawWrappedPanelForPrinting(Graphics g, int canvasWidth,
466 int canvasHeight, int startRes)
468 drawWrappedPanel(g, canvasWidth, canvasHeight, startRes);
470 SequenceGroup group = av.getSelectionGroup();
473 drawWrappedSelection((Graphics2D) g, group, canvasWidth, canvasHeight,
479 * Returns the visible width of the canvas in residues, after allowing for
480 * East or West scales (if shown)
483 * the width in pixels (possibly including scales)
487 public int getWrappedCanvasWidth(int canvasWidth)
489 int charWidth = av.getCharWidth();
491 FontMetrics fm = getFontMetrics(av.getFont());
495 if (av.getScaleRightWrapped() || av.getScaleLeftWrapped())
497 labelWidth = getLabelWidth(fm);
500 labelWidthEast = av.getScaleRightWrapped() ? labelWidth : 0;
502 labelWidthWest = av.getScaleLeftWrapped() ? labelWidth : 0;
504 return (canvasWidth - labelWidthEast - labelWidthWest) / charWidth;
508 * Returns a pixel width sufficient to show the largest sequence coordinate
509 * (end position) in the alignment, calculated as the FontMetrics width of
510 * zeroes "0000000" limited to the number of decimal digits to be shown (3 for
511 * 1-10, 4 for 11-99 etc). One character width is added to this, to allow for
512 * half a character width space on either side.
517 protected int getLabelWidth(FontMetrics fm)
520 * find the biggest sequence end position we need to show
521 * (note this is not necessarily the sequence length)
524 AlignmentI alignment = av.getAlignment();
525 for (int i = 0; i < alignment.getHeight(); i++)
527 maxWidth = Math.max(maxWidth, alignment.getSequenceAt(i).getEnd());
531 for (int i = maxWidth; i > 0; i /= 10)
536 return fm.stringWidth(ZEROS.substring(0, length)) + av.getCharWidth();
540 * Draws as many widths of a wrapped alignment as can fit in the visible
545 * available width in pixels
546 * @param canvasHeight
547 * available height in pixels
549 * the first column (0...) of the alignment to draw
551 public void drawWrappedPanel(Graphics g, int canvasWidth,
552 int canvasHeight, final int startColumn)
554 int wrappedWidthInResidues = calculateWrappedGeometry(canvasWidth,
557 av.setWrappedWidth(wrappedWidthInResidues);
559 ViewportRanges ranges = av.getRanges();
560 ranges.setViewportStartAndWidth(startColumn, wrappedWidthInResidues);
562 // we need to call this again to make sure the startColumn +
563 // wrappedWidthInResidues values are used to calculate wrappedVisibleWidths
565 calculateWrappedGeometry(canvasWidth, canvasHeight);
568 * draw one width at a time (excluding any scales or annotation shown),
569 * until we have run out of either alignment or vertical space available
571 int ypos = wrappedSpaceAboveAlignment;
572 int maxWidth = ranges.getVisibleAlignmentWidth();
574 int start = startColumn;
575 int currentWidth = 0;
576 while ((currentWidth < wrappedVisibleWidths) && (start < maxWidth))
579 .min(maxWidth, start + wrappedWidthInResidues - 1);
580 drawWrappedWidth(g, ypos, start, endColumn, canvasHeight);
581 ypos += wrappedRepeatHeightPx;
582 start += wrappedWidthInResidues;
586 drawWrappedDecorators(g, startColumn);
590 * Calculates and saves values needed when rendering a wrapped alignment.
591 * These depend on many factors, including
593 * <li>canvas width and height</li>
594 * <li>number of visible sequences, and height of annotations if shown</li>
595 * <li>font and character width</li>
596 * <li>whether scales are shown left, right or above the alignment</li>
600 * @param canvasHeight
601 * @return the number of residue columns in each width
603 protected int calculateWrappedGeometry(int canvasWidth, int canvasHeight)
605 int charHeight = av.getCharHeight();
608 * vertical space in pixels between wrapped widths of alignment
609 * - one character height, or two if scale above is drawn
611 wrappedSpaceAboveAlignment = charHeight
612 * (av.getScaleAboveWrapped() ? 2 : 1);
615 * height in pixels of the wrapped widths
617 wrappedRepeatHeightPx = wrappedSpaceAboveAlignment;
619 wrappedRepeatHeightPx += av.getAlignment().getHeight()
621 // add annotations panel height if shown
622 wrappedRepeatHeightPx += getAnnotationHeight();
625 * number of visible widths (the last one may be part height),
626 * ensuring a part height includes at least one sequence
628 ViewportRanges ranges = av.getRanges();
629 wrappedVisibleWidths = canvasHeight / wrappedRepeatHeightPx;
630 int remainder = canvasHeight % wrappedRepeatHeightPx;
631 if (remainder >= (wrappedSpaceAboveAlignment + charHeight))
633 wrappedVisibleWidths++;
637 * compute width in residues; this also sets East and West label widths
639 int wrappedWidthInResidues = getWrappedCanvasWidth(canvasWidth);
642 * limit visibleWidths to not exceed width of alignment
644 int xMax = ranges.getVisibleAlignmentWidth();
645 int startToEnd = xMax - ranges.getStartRes();
646 int maxWidths = startToEnd / wrappedWidthInResidues;
647 if (startToEnd % wrappedWidthInResidues > 0)
651 wrappedVisibleWidths = Math.min(wrappedVisibleWidths, maxWidths);
653 return wrappedWidthInResidues;
657 * Draws one width of a wrapped alignment, including sequences and
658 * annnotations, if shown, but not scales or hidden column markers
664 * @param canvasHeight
666 protected void drawWrappedWidth(Graphics g, int ypos, int startColumn,
667 int endColumn, int canvasHeight)
669 ViewportRanges ranges = av.getRanges();
670 int viewportWidth = ranges.getViewportWidth();
672 int endx = Math.min(startColumn + viewportWidth - 1, endColumn);
675 * move right before drawing by the width of the scale left (if any)
676 * plus column offset from left margin (usually zero, but may be non-zero
677 * when fast painting is drawing just a few columns)
679 int charWidth = av.getCharWidth();
680 int xOffset = labelWidthWest
681 + ((startColumn - ranges.getStartRes()) % viewportWidth)
684 // BH 2018 note: I have switched to using Graphics.create() here because it is
685 // more reliable (and simpler) to reset. The difference seems to be that SwingJS
686 // automatically sets a clipping region on an image to be the image dimensions, whereas
687 // Java sets no clip for an image. (A bug? Go figure!)
688 // Since we are using an off-screen BufferedImage here, the result is that g.getClip()
689 // returns non-null in JavaScript but not Java.
691 // Anyway, this works and, I suggest, is better design anyway.
693 // Graphics g = gg.create();
694 // mc 30/08/18 undone because gnu.jpdf.PDFGraphics doesn't handle
697 g.translate(xOffset, 0);
699 // When printing we have an extra clipped region,
700 // the Printable page which we need to account for here
701 Shape clip = g.getClip();
704 g.setClip(0, 0, viewportWidth * charWidth, canvasHeight);
708 g.setClip(0, (int) clip.getBounds().getY(),
709 viewportWidth * charWidth, (int) clip.getBounds().getHeight());
714 * white fill the region to be drawn (so incremental fast paint doesn't
715 * scribble over an existing image)
717 g.setColor(Color.white);
718 g.fillRect(0, ypos, (endx - startColumn + 1) * charWidth,
719 wrappedRepeatHeightPx);
721 drawPanel(g, startColumn, endx, 0, av.getAlignment().getHeight() - 1,
724 int cHeight = av.getAlignment().getHeight() * av.getCharHeight();
726 if (av.isShowAnnotation())
728 g.translate(0, cHeight + ypos + 3);
729 if (annotations == null)
731 annotations = new AnnotationPanel(av);
734 annotations.renderer.drawComponent(annotations, av, g, -1,
735 startColumn, endx + 1);
736 g.translate(0, -cHeight - ypos - 3);
739 g.translate(-xOffset, 0);
744 * Draws scales left, right and above (if shown), and any hidden column
745 * markers, on all widths of the wrapped alignment
750 protected void drawWrappedDecorators(Graphics g, final int startColumn)
752 int charWidth = av.getCharWidth();
754 g.setFont(av.getFont());
756 g.setColor(Color.black);
758 int ypos = wrappedSpaceAboveAlignment;
759 ViewportRanges ranges = av.getRanges();
760 int viewportWidth = ranges.getViewportWidth();
761 int maxWidth = ranges.getVisibleAlignmentWidth();
763 int startCol = startColumn;
765 while (widthsDrawn < wrappedVisibleWidths)
767 int endColumn = Math.min(maxWidth, startCol + viewportWidth - 1);
769 if (av.getScaleLeftWrapped())
771 drawVerticalScale(g, startCol, endColumn - 1, ypos, true);
774 if (av.getScaleRightWrapped())
776 int x = labelWidthWest + viewportWidth * charWidth;
779 drawVerticalScale(g, startCol, endColumn, ypos, false);
784 * white fill region of scale above and hidden column markers
785 * (to support incremental fast paint of image)
787 g.translate(labelWidthWest, 0);
788 g.setColor(Color.white);
789 g.fillRect(0, ypos - wrappedSpaceAboveAlignment, viewportWidth
790 * charWidth + labelWidthWest, wrappedSpaceAboveAlignment);
791 g.setColor(Color.black);
792 g.translate(-labelWidthWest, 0);
794 g.translate(labelWidthWest, 0);
796 if (av.getScaleAboveWrapped())
798 drawNorthScale(g, startCol, endColumn, ypos);
801 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
803 drawHiddenColumnMarkers(g, ypos, startCol, endColumn);
806 g.translate(-labelWidthWest, 0);
808 ypos += wrappedRepeatHeightPx;
809 startCol += viewportWidth;
815 * Draws markers (triangles) above hidden column positions between startColumn
823 protected void drawHiddenColumnMarkers(Graphics g, int ypos,
824 int startColumn, int endColumn)
826 int charHeight = av.getCharHeight();
827 int charWidth = av.getCharWidth();
829 g.setColor(Color.blue);
831 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
833 Iterator<Integer> it = hidden.getStartRegionIterator(startColumn,
837 res = it.next() - startColumn;
839 if (res < 0 || res > endColumn - startColumn + 1)
845 * draw a downward-pointing triangle at the hidden columns location
846 * (before the following visible column)
848 int xMiddle = res * charWidth;
849 int[] xPoints = new int[] { xMiddle - charHeight / 4,
850 xMiddle + charHeight / 4, xMiddle };
851 int yTop = ypos - (charHeight / 2);
852 int[] yPoints = new int[] { yTop, yTop, yTop + 8 };
853 g.fillPolygon(xPoints, yPoints, 3);
858 * Draw a selection group over a wrapped alignment
860 private void drawWrappedSelection(Graphics2D g, SequenceGroup group,
862 int canvasHeight, int startRes)
864 int charHeight = av.getCharHeight();
865 int charWidth = av.getCharWidth();
867 // height gap above each panel
868 int hgap = charHeight;
869 if (av.getScaleAboveWrapped())
874 int cWidth = (canvasWidth - labelWidthEast - labelWidthWest)
876 int cHeight = av.getAlignment().getHeight() * charHeight;
878 int startx = startRes;
880 int ypos = hgap; // vertical offset
881 int maxwidth = av.getAlignment().getWidth();
883 if (av.hasHiddenColumns())
885 maxwidth = av.getAlignment().getHiddenColumns()
886 .absoluteToVisibleColumn(maxwidth);
889 // chop the wrapped alignment extent up into panel-sized blocks and treat
890 // each block as if it were a block from an unwrapped alignment
891 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
892 BasicStroke.JOIN_ROUND, 3f, new float[]
894 g.setColor(Color.RED);
895 while ((ypos <= canvasHeight) && (startx < maxwidth))
897 // set end value to be start + width, or maxwidth, whichever is smaller
898 endx = startx + cWidth - 1;
905 g.translate(labelWidthWest, 0);
907 drawUnwrappedSelection(g, group, startx, endx, 0,
908 av.getAlignment().getHeight() - 1,
911 g.translate(-labelWidthWest, 0);
913 // update vertical offset
914 ypos += cHeight + getAnnotationHeight() + hgap;
916 // update horizontal offset
919 g.setStroke(new BasicStroke());
922 int getAnnotationHeight()
924 if (!av.isShowAnnotation())
929 if (annotations == null)
931 annotations = new AnnotationPanel(av);
934 return annotations.adjustPanelHeight();
938 * Draws the visible region of the alignment on the graphics context. If there
939 * are hidden column markers in the visible region, then each sub-region
940 * between the markers is drawn separately, followed by the hidden column
944 * the graphics context, positioned at the first residue to be drawn
946 * offset of the first column to draw (0..)
948 * offset of the last column to draw (0..)
950 * offset of the first sequence to draw (0..)
952 * offset of the last sequence to draw (0..)
954 * vertical offset at which to draw (for wrapped alignments)
956 public void drawPanel(Graphics g1, final int startRes, final int endRes,
957 final int startSeq, final int endSeq, final int yOffset)
959 int charHeight = av.getCharHeight();
960 int charWidth = av.getCharWidth();
962 if (!av.hasHiddenColumns())
964 draw(g1, startRes, endRes, startSeq, endSeq, yOffset);
972 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
973 VisibleContigsIterator regions = hidden
974 .getVisContigsIterator(startRes, endRes + 1, true);
976 while (regions.hasNext())
978 int[] region = regions.next();
979 blockEnd = region[1];
980 blockStart = region[0];
983 * draw up to just before the next hidden region, or the end of
984 * the visible region, whichever comes first
986 g1.translate(screenY * charWidth, 0);
988 draw(g1, blockStart, blockEnd, startSeq, endSeq, yOffset);
991 * draw the downline of the hidden column marker (ScalePanel draws the
992 * triangle on top) if we reached it
994 if (av.getShowHiddenMarkers()
995 && (regions.hasNext() || regions.endsAtHidden()))
997 g1.setColor(Color.blue);
999 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
1000 0 + yOffset, (blockEnd - blockStart + 1) * charWidth - 1,
1001 (endSeq - startSeq + 1) * charHeight + yOffset);
1004 g1.translate(-screenY * charWidth, 0);
1005 screenY += blockEnd - blockStart + 1;
1012 * Draws a region of the visible alignment
1016 * offset of the first column in the visible region (0..)
1018 * offset of the last column in the visible region (0..)
1020 * offset of the first sequence in the visible region (0..)
1022 * offset of the last sequence in the visible region (0..)
1024 * vertical offset at which to draw (for wrapped alignments)
1026 private void draw(Graphics g, int startRes, int endRes, int startSeq,
1027 int endSeq, int offset)
1029 int charHeight = av.getCharHeight();
1030 int charWidth = av.getCharWidth();
1032 g.setFont(av.getFont());
1033 seqRdr.prepare(g, av.isRenderGaps());
1037 // / First draw the sequences
1038 // ///////////////////////////
1039 for (int i = startSeq; i <= endSeq; i++)
1041 nextSeq = av.getAlignment().getSequenceAt(i);
1042 if (nextSeq == null)
1044 // occasionally, a race condition occurs such that the alignment row is
1048 seqRdr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
1049 startRes, endRes, offset + ((i - startSeq) * charHeight));
1051 if (av.isShowSequenceFeatures())
1053 fr.drawSequence(g, nextSeq, startRes, endRes,
1054 offset + ((i - startSeq) * charHeight), false);
1058 * highlight search Results once sequence has been drawn
1060 if (av.hasSearchResults())
1062 SearchResultsI searchResults = av.getSearchResults();
1063 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
1065 if (visibleResults != null)
1067 for (int r = 0; r < visibleResults.length; r += 2)
1069 seqRdr.drawHighlightedText(nextSeq, visibleResults[r],
1070 visibleResults[r + 1],
1071 (visibleResults[r] - startRes) * charWidth,
1072 offset + ((i - startSeq) * charHeight));
1078 if (av.getSelectionGroup() != null
1079 || av.getAlignment().getGroups().size() > 0)
1081 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
1087 * Draws the outlines of any groups defined on the alignment (excluding the
1088 * current selection group, if any)
1097 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
1098 int startSeq, int endSeq, int offset)
1100 Graphics2D g = (Graphics2D) g1;
1102 SequenceGroup group = null;
1103 int groupIndex = -1;
1105 if (av.getAlignment().getGroups().size() > 0)
1107 group = av.getAlignment().getGroups().get(0);
1115 g.setColor(group.getOutlineColour());
1116 drawPartialGroupOutline(g, group, startRes, endRes, startSeq,
1120 if (groupIndex >= av.getAlignment().getGroups().size())
1124 group = av.getAlignment().getGroups().get(groupIndex);
1125 } while (groupIndex < av.getAlignment().getGroups().size());
1130 * Draws the outline of the current selection group (if any)
1138 private void drawSelectionGroup(Graphics2D g, int startRes, int endRes,
1139 int startSeq, int endSeq)
1141 SequenceGroup group = av.getSelectionGroup();
1147 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
1148 BasicStroke.JOIN_ROUND, 3f, new float[]
1150 g.setColor(Color.RED);
1151 if (!av.getWrapAlignment())
1153 drawUnwrappedSelection(g, group, startRes, endRes, startSeq, endSeq,
1158 drawWrappedSelection(g, group, getWidth(), getHeight(),
1159 av.getRanges().getStartRes());
1161 g.setStroke(new BasicStroke());
1165 * Draw the cursor as a separate image and overlay
1168 * start residue of area to draw cursor in
1170 * end residue of area to draw cursor in
1172 * start sequence of area to draw cursor in
1174 * end sequence of are to draw cursor in
1175 * @return a transparent image of the same size as the sequence canvas, with
1176 * the cursor drawn on it, if any
1178 private void drawCursor(Graphics g, int startRes, int endRes,
1182 // convert the cursorY into a position on the visible alignment
1183 int cursor_ypos = cursorY;
1185 // don't do work unless we have to
1186 if (cursor_ypos >= startSeq && cursor_ypos <= endSeq)
1190 int startx = startRes;
1193 // convert the cursorX into a position on the visible alignment
1194 int cursor_xpos = av.getAlignment().getHiddenColumns()
1195 .absoluteToVisibleColumn(cursorX);
1197 if (av.getAlignment().getHiddenColumns().isVisible(cursorX))
1200 if (av.getWrapAlignment())
1202 // work out the correct offsets for the cursor
1203 int charHeight = av.getCharHeight();
1204 int charWidth = av.getCharWidth();
1205 int canvasWidth = getWidth();
1206 int canvasHeight = getHeight();
1208 // height gap above each panel
1209 int hgap = charHeight;
1210 if (av.getScaleAboveWrapped())
1215 int cWidth = (canvasWidth - labelWidthEast - labelWidthWest)
1217 int cHeight = av.getAlignment().getHeight() * charHeight;
1219 endx = startx + cWidth - 1;
1220 int ypos = hgap; // vertical offset
1222 // iterate down the wrapped panels
1223 while ((ypos <= canvasHeight) && (endx < cursor_xpos))
1225 // update vertical offset
1226 ypos += cHeight + getAnnotationHeight() + hgap;
1228 // update horizontal offset
1230 endx = startx + cWidth - 1;
1233 xoffset = labelWidthWest;
1236 // now check if cursor is within range for x values
1237 if (cursor_xpos >= startx && cursor_xpos <= endx)
1239 // get the character the cursor is drawn at
1240 SequenceI seq = av.getAlignment().getSequenceAt(cursorY);
1241 char s = seq.getCharAt(cursorX);
1243 seqRdr.drawCursor(g, s,
1244 xoffset + (cursor_xpos - startx) * av.getCharWidth(),
1245 yoffset + (cursor_ypos - startSeq) * av.getCharHeight());
1253 * Draw a selection group over an unwrapped alignment
1256 * graphics object to draw with
1260 * start residue of area to draw
1262 * end residue of area to draw
1264 * start sequence of area to draw
1266 * end sequence of area to draw
1268 * vertical offset (used when called from wrapped alignment code)
1270 private void drawUnwrappedSelection(Graphics2D g, SequenceGroup group,
1271 int startRes, int endRes, int startSeq, int endSeq, int offset)
1273 int charWidth = av.getCharWidth();
1275 if (!av.hasHiddenColumns())
1277 drawPartialGroupOutline(g, group, startRes, endRes, startSeq, endSeq,
1282 // package into blocks of visible columns
1287 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
1288 VisibleContigsIterator regions = hidden
1289 .getVisContigsIterator(startRes, endRes + 1, true);
1290 while (regions.hasNext())
1292 int[] region = regions.next();
1293 blockEnd = region[1];
1294 blockStart = region[0];
1296 g.translate(screenY * charWidth, 0);
1297 drawPartialGroupOutline(g, group,
1298 blockStart, blockEnd, startSeq, endSeq, offset);
1300 g.translate(-screenY * charWidth, 0);
1301 screenY += blockEnd - blockStart + 1;
1307 * Draws part of a selection group outline
1315 * @param verticalOffset
1317 private void drawPartialGroupOutline(Graphics2D g, SequenceGroup group,
1318 int startRes, int endRes, int startSeq, int endSeq,
1321 int charHeight = av.getCharHeight();
1322 int charWidth = av.getCharWidth();
1323 int visWidth = (endRes - startRes + 1) * charWidth;
1327 boolean inGroup = false;
1332 List<SequenceI> seqs = group.getSequences(null);
1334 // position of start residue of group relative to startRes, in pixels
1335 int sx = (group.getStartRes() - startRes) * charWidth;
1337 // width of group in pixels
1338 int xwidth = (((group.getEndRes() + 1) - group.getStartRes())
1341 if (!(sx + xwidth < 0 || sx > visWidth))
1343 for (i = startSeq; i <= endSeq; i++)
1345 sy = verticalOffset + (i - startSeq) * charHeight;
1347 if ((sx <= (endRes - startRes) * charWidth)
1348 && seqs.contains(av.getAlignment().getSequenceAt(i)))
1351 && !seqs.contains(av.getAlignment().getSequenceAt(i + 1)))
1353 bottom = sy + charHeight;
1358 if (((top == -1) && (i == 0)) || !seqs
1359 .contains(av.getAlignment().getSequenceAt(i - 1)))
1370 drawVerticals(g, sx, xwidth, visWidth, oldY, sy);
1371 drawHorizontals(g, sx, xwidth, visWidth, top, bottom);
1373 // reset top and bottom
1381 sy = verticalOffset + ((i - startSeq) * charHeight);
1382 drawVerticals(g, sx, xwidth, visWidth, oldY, sy);
1383 drawHorizontals(g, sx, xwidth, visWidth, top, bottom);
1389 * Draw horizontal selection group boundaries at top and bottom positions
1392 * graphics object to draw on
1398 * visWidth maximum available width
1400 * position to draw top of group at
1402 * position to draw bottom of group at
1404 private void drawHorizontals(Graphics2D g, int sx, int xwidth,
1405 int visWidth, int top, int bottom)
1415 // don't let width extend beyond current block, or group extent
1417 if (startx + width >= visWidth)
1419 width = visWidth - startx;
1424 g.drawLine(startx, top, startx + width, top);
1429 g.drawLine(startx, bottom - 1, startx + width, bottom - 1);
1434 * Draw vertical lines at sx and sx+xwidth providing they lie within
1438 * graphics object to draw on
1444 * visWidth maximum available width
1450 private void drawVerticals(Graphics2D g, int sx, int xwidth, int visWidth,
1453 // if start position is visible, draw vertical line to left of
1455 if (sx >= 0 && sx < visWidth)
1457 g.drawLine(sx, oldY, sx, sy);
1460 // if end position is visible, draw vertical line to right of
1462 if (sx + xwidth < visWidth)
1464 g.drawLine(sx + xwidth, oldY, sx + xwidth, sy);
1469 * Highlights search results in the visible region by rendering as white text
1470 * on a black background. Any previous highlighting is removed. Answers true
1471 * if any highlight was left on the visible alignment (so status bar should be
1472 * set to match), else false. This method does _not_ set the 'fastPaint' flag,
1473 * so allows the next repaint to update the whole display.
1478 public boolean highlightSearchResults(SearchResultsI results)
1480 return highlightSearchResults(results, false);
1485 * Highlights search results in the visible region by rendering as white text
1486 * on a black background. Any previous highlighting is removed. Answers true
1487 * if any highlight was left on the visible alignment (so status bar should be
1488 * set to match), else false.
1490 * Optionally, set the 'fastPaint' flag for a faster redraw if only the
1491 * highlighted regions are modified. This speeds up highlighting across linked
1494 * Currently fastPaint is not implemented for scrolled wrapped alignments. If
1495 * a wrapped alignment had to be scrolled to show the highlighted region, then
1496 * it should be fully redrawn, otherwise a fast paint can be performed. This
1497 * argument could be removed if fast paint of scrolled wrapped alignment is
1498 * coded in future (JAL-2609).
1501 * @param doFastPaint
1502 * if true, sets a flag so the next repaint only redraws the modified
1506 public boolean highlightSearchResults(SearchResultsI results,
1507 boolean doFastPaint)
1513 boolean wrapped = av.getWrapAlignment();
1516 fastPaint = doFastPaint;
1517 fastpainting = fastPaint;
1520 * to avoid redrawing the whole visible region, we instead
1521 * redraw just the minimal regions to remove previous highlights
1524 SearchResultsI previous = av.getSearchResults();
1525 av.setSearchResults(results);
1526 boolean redrawn = false;
1527 boolean drawn = false;
1530 redrawn = drawMappedPositionsWrapped(previous);
1531 drawn = drawMappedPositionsWrapped(results);
1536 redrawn = drawMappedPositions(previous);
1537 drawn = drawMappedPositions(results);
1542 * if highlights were either removed or added, repaint
1550 * return true only if highlights were added
1556 fastpainting = false;
1561 * Redraws the minimal rectangle in the visible region (if any) that includes
1562 * mapped positions of the given search results. Whether or not positions are
1563 * highlighted depends on the SearchResults set on the Viewport. This allows
1564 * this method to be called to either clear or set highlighting. Answers true
1565 * if any positions were drawn (in which case a repaint is still required),
1571 protected boolean drawMappedPositions(SearchResultsI results)
1573 if ((results == null) || (img == null)) // JAL-2784 check gg is not null
1579 * calculate the minimal rectangle to redraw that
1580 * includes both new and existing search results
1582 int firstSeq = Integer.MAX_VALUE;
1584 int firstCol = Integer.MAX_VALUE;
1586 boolean matchFound = false;
1588 ViewportRanges ranges = av.getRanges();
1589 int firstVisibleColumn = ranges.getStartRes();
1590 int lastVisibleColumn = ranges.getEndRes();
1591 AlignmentI alignment = av.getAlignment();
1592 if (av.hasHiddenColumns())
1594 firstVisibleColumn = alignment.getHiddenColumns()
1595 .visibleToAbsoluteColumn(firstVisibleColumn);
1596 lastVisibleColumn = alignment.getHiddenColumns()
1597 .visibleToAbsoluteColumn(lastVisibleColumn);
1600 for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
1601 .getEndSeq(); seqNo++)
1603 SequenceI seq = alignment.getSequenceAt(seqNo);
1605 int[] visibleResults = results.getResults(seq, firstVisibleColumn,
1607 if (visibleResults != null)
1609 for (int i = 0; i < visibleResults.length - 1; i += 2)
1611 int firstMatchedColumn = visibleResults[i];
1612 int lastMatchedColumn = visibleResults[i + 1];
1613 if (firstMatchedColumn <= lastVisibleColumn
1614 && lastMatchedColumn >= firstVisibleColumn)
1617 * found a search results match in the visible region -
1618 * remember the first and last sequence matched, and the first
1619 * and last visible columns in the matched positions
1622 firstSeq = Math.min(firstSeq, seqNo);
1623 lastSeq = Math.max(lastSeq, seqNo);
1624 firstMatchedColumn = Math.max(firstMatchedColumn,
1625 firstVisibleColumn);
1626 lastMatchedColumn = Math.min(lastMatchedColumn,
1628 firstCol = Math.min(firstCol, firstMatchedColumn);
1629 lastCol = Math.max(lastCol, lastMatchedColumn);
1637 if (av.hasHiddenColumns())
1639 firstCol = alignment.getHiddenColumns()
1640 .absoluteToVisibleColumn(firstCol);
1641 lastCol = alignment.getHiddenColumns().absoluteToVisibleColumn(lastCol);
1643 int transX = (firstCol - ranges.getStartRes()) * av.getCharWidth();
1644 int transY = (firstSeq - ranges.getStartSeq()) * av.getCharHeight();
1645 Graphics gg = img.getGraphics();
1646 gg.translate(transX, transY);
1647 drawPanel(gg, firstCol, lastCol, firstSeq, lastSeq, 0);
1648 gg.translate(-transX, -transY);
1656 public void propertyChange(PropertyChangeEvent evt)
1658 String eventName = evt.getPropertyName();
1660 if (eventName.equals(SequenceGroup.SEQ_GROUP_CHANGED))
1666 else if (eventName.equals(ViewportRanges.MOVE_VIEWPORT))
1674 if (eventName.equals(ViewportRanges.STARTRES)
1675 || eventName.equals(ViewportRanges.STARTRESANDSEQ))
1677 // Make sure we're not trying to draw a panel
1678 // larger than the visible window
1679 if (eventName.equals(ViewportRanges.STARTRES))
1681 scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
1685 scrollX = ((int[]) evt.getNewValue())[0]
1686 - ((int[]) evt.getOldValue())[0];
1688 ViewportRanges vpRanges = av.getRanges();
1690 int range = vpRanges.getEndRes() - vpRanges.getStartRes();
1691 if (scrollX > range)
1695 else if (scrollX < -range)
1700 // Both scrolling and resizing change viewport ranges: scrolling changes
1701 // both start and end points, but resize only changes end values.
1702 // Here we only want to fastpaint on a scroll, with resize using a normal
1703 // paint, so scroll events are identified as changes to the horizontal or
1704 // vertical start value.
1705 if (eventName.equals(ViewportRanges.STARTRES))
1707 if (av.getWrapAlignment())
1709 fastPaintWrapped(scrollX);
1713 fastPaint(scrollX, 0);
1716 else if (eventName.equals(ViewportRanges.STARTSEQ))
1719 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());
1721 else if (eventName.equals(ViewportRanges.STARTRESANDSEQ))
1723 if (av.getWrapAlignment())
1725 fastPaintWrapped(scrollX);
1729 fastPaint(scrollX, 0);
1732 else if (eventName.equals(ViewportRanges.STARTSEQ))
1735 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());
1737 else if (eventName.equals(ViewportRanges.STARTRESANDSEQ))
1739 if (av.getWrapAlignment())
1741 fastPaintWrapped(scrollX);
1747 * Does a minimal update of the image for a scroll movement. This method
1748 * handles scroll movements of up to one width of the wrapped alignment (one
1749 * click in the vertical scrollbar). Larger movements (for example after a
1750 * scroll to highlight a mapped position) trigger a full redraw instead.
1753 * number of positions scrolled (right if positive, left if negative)
1755 protected void fastPaintWrapped(int scrollX)
1757 ViewportRanges ranges = av.getRanges();
1759 if (Math.abs(scrollX) > ranges.getViewportWidth())
1762 * shift of more than one view width is
1763 * overcomplicated to handle in this method
1770 if (fastpainting || img == null)
1776 fastpainting = true;
1781 Graphics gg = img.getGraphics();
1783 calculateWrappedGeometry(getWidth(), getHeight());
1786 * relocate the regions of the alignment that are still visible
1788 shiftWrappedAlignment(-scrollX);
1791 * add new columns (sequence, annotation)
1792 * - at top left if scrollX < 0
1793 * - at right of last two widths if scrollX > 0
1797 int startRes = ranges.getStartRes();
1798 drawWrappedWidth(gg, wrappedSpaceAboveAlignment, startRes, startRes
1799 - scrollX - 1, getHeight());
1803 fastPaintWrappedAddRight(scrollX);
1807 * draw all scales (if shown) and hidden column markers
1809 drawWrappedDecorators(gg, ranges.getStartRes());
1816 fastpainting = false;
1821 * Draws the specified number of columns at the 'end' (bottom right) of a
1822 * wrapped alignment view, including sequences and annotations if shown, but
1823 * not scales. Also draws the same number of columns at the right hand end of
1824 * the second last width shown, if the last width is not full height (so
1825 * cannot simply be copied from the graphics image).
1829 protected void fastPaintWrappedAddRight(int columns)
1836 Graphics gg = img.getGraphics();
1838 ViewportRanges ranges = av.getRanges();
1839 int viewportWidth = ranges.getViewportWidth();
1840 int charWidth = av.getCharWidth();
1843 * draw full height alignment in the second last row, last columns, if the
1844 * last row was not full height
1846 int visibleWidths = wrappedVisibleWidths;
1847 int canvasHeight = getHeight();
1848 boolean lastWidthPartHeight = (wrappedVisibleWidths * wrappedRepeatHeightPx) > canvasHeight;
1850 if (lastWidthPartHeight)
1852 int widthsAbove = Math.max(0, visibleWidths - 2);
1853 int ypos = wrappedRepeatHeightPx * widthsAbove
1854 + wrappedSpaceAboveAlignment;
1855 int endRes = ranges.getEndRes();
1856 endRes += widthsAbove * viewportWidth;
1857 int startRes = endRes - columns;
1858 int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
1862 * white fill first to erase annotations
1866 gg.translate(xOffset, 0);
1867 gg.setColor(Color.white);
1868 gg.fillRect(labelWidthWest, ypos,
1869 (endRes - startRes + 1) * charWidth, wrappedRepeatHeightPx);
1870 gg.translate(-xOffset, 0);
1872 drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
1877 * draw newly visible columns in last wrapped width (none if we
1878 * have reached the end of the alignment)
1879 * y-offset for drawing last width is height of widths above,
1882 int widthsAbove = visibleWidths - 1;
1883 int ypos = wrappedRepeatHeightPx * widthsAbove
1884 + wrappedSpaceAboveAlignment;
1885 int endRes = ranges.getEndRes();
1886 endRes += widthsAbove * viewportWidth;
1887 int startRes = endRes - columns + 1;
1890 * white fill first to erase annotations
1892 int xOffset = ((startRes - ranges.getStartRes()) % viewportWidth)
1894 gg.translate(xOffset, 0);
1895 gg.setColor(Color.white);
1896 int width = viewportWidth * charWidth - xOffset;
1897 gg.fillRect(labelWidthWest, ypos, width, wrappedRepeatHeightPx);
1898 gg.translate(-xOffset, 0);
1900 gg.setFont(av.getFont());
1901 gg.setColor(Color.black);
1903 if (startRes < ranges.getVisibleAlignmentWidth())
1905 drawWrappedWidth(gg, ypos, startRes, endRes, canvasHeight);
1909 * and finally, white fill any space below the visible alignment
1911 int heightBelow = canvasHeight - visibleWidths * wrappedRepeatHeightPx;
1912 if (heightBelow > 0)
1914 gg.setColor(Color.white);
1915 gg.fillRect(0, canvasHeight - heightBelow, getWidth(), heightBelow);
1921 * Shifts the visible alignment by the specified number of columns - left if
1922 * negative, right if positive. Copies and moves sequences and annotations (if
1923 * shown). Scales, hidden column markers and any newly visible columns must be
1928 protected void shiftWrappedAlignment(int positions)
1935 Graphics gg = img.getGraphics();
1937 int charWidth = av.getCharWidth();
1939 int canvasHeight = getHeight();
1940 ViewportRanges ranges = av.getRanges();
1941 int viewportWidth = ranges.getViewportWidth();
1942 int widthToCopy = (ranges.getViewportWidth() - Math.abs(positions))
1944 int heightToCopy = wrappedRepeatHeightPx - wrappedSpaceAboveAlignment;
1945 int xMax = ranges.getVisibleAlignmentWidth();
1950 * shift right (after scroll left)
1951 * for each wrapped width (starting with the last), copy (width-positions)
1952 * columns from the left margin to the right margin, and copy positions
1953 * columns from the right margin of the row above (if any) to the
1954 * left margin of the current row
1958 * get y-offset of last wrapped width, first row of sequences
1960 int y = canvasHeight / wrappedRepeatHeightPx * wrappedRepeatHeightPx;
1961 y += wrappedSpaceAboveAlignment;
1962 int copyFromLeftStart = labelWidthWest;
1963 int copyFromRightStart = copyFromLeftStart + widthToCopy;
1967 gg.copyArea(copyFromLeftStart, y, widthToCopy, heightToCopy,
1968 positions * charWidth, 0);
1971 gg.copyArea(copyFromRightStart, y - wrappedRepeatHeightPx,
1972 positions * charWidth, heightToCopy, -widthToCopy,
1973 wrappedRepeatHeightPx);
1976 y -= wrappedRepeatHeightPx;
1982 * shift left (after scroll right)
1983 * for each wrapped width (starting with the first), copy (width-positions)
1984 * columns from the right margin to the left margin, and copy positions
1985 * columns from the left margin of the row below (if any) to the
1986 * right margin of the current row
1988 int xpos = av.getRanges().getStartRes();
1989 int y = wrappedSpaceAboveAlignment;
1990 int copyFromRightStart = labelWidthWest - positions * charWidth;
1992 while (y < canvasHeight)
1994 gg.copyArea(copyFromRightStart, y, widthToCopy, heightToCopy,
1995 positions * charWidth, 0);
1996 if (y + wrappedRepeatHeightPx < canvasHeight - wrappedRepeatHeightPx
1997 && (xpos + viewportWidth <= xMax))
1999 gg.copyArea(labelWidthWest, y + wrappedRepeatHeightPx, -positions
2000 * charWidth, heightToCopy, widthToCopy,
2001 -wrappedRepeatHeightPx);
2003 y += wrappedRepeatHeightPx;
2004 xpos += viewportWidth;
2012 * Redraws any positions in the search results in the visible region of a
2013 * wrapped alignment. Any highlights are drawn depending on the search results
2014 * set on the Viewport, not the <code>results</code> argument. This allows
2015 * this method to be called either to clear highlights (passing the previous
2016 * search results), or to draw new highlights.
2021 protected boolean drawMappedPositionsWrapped(SearchResultsI results)
2023 if ((results == null) || (img == null)) // JAL-2784 check gg is not null
2027 int charHeight = av.getCharHeight();
2029 boolean matchFound = false;
2031 calculateWrappedGeometry(getWidth(), getHeight());
2032 int wrappedWidth = av.getWrappedWidth();
2033 int wrappedHeight = wrappedRepeatHeightPx;
2035 ViewportRanges ranges = av.getRanges();
2036 int canvasHeight = getHeight();
2037 int repeats = canvasHeight / wrappedHeight;
2038 if (canvasHeight / wrappedHeight > 0)
2043 int firstVisibleColumn = ranges.getStartRes();
2044 int lastVisibleColumn = ranges.getStartRes() + repeats
2045 * ranges.getViewportWidth() - 1;
2047 AlignmentI alignment = av.getAlignment();
2048 if (av.hasHiddenColumns())
2050 firstVisibleColumn = alignment.getHiddenColumns()
2051 .visibleToAbsoluteColumn(firstVisibleColumn);
2052 lastVisibleColumn = alignment.getHiddenColumns()
2053 .visibleToAbsoluteColumn(lastVisibleColumn);
2056 int gapHeight = charHeight * (av.getScaleAboveWrapped() ? 2 : 1);
2059 Graphics gg = img.getGraphics();
2061 for (int seqNo = ranges.getStartSeq(); seqNo <= ranges
2062 .getEndSeq(); seqNo++)
2064 SequenceI seq = alignment.getSequenceAt(seqNo);
2066 int[] visibleResults = results.getResults(seq, firstVisibleColumn,
2068 if (visibleResults != null)
2070 for (int i = 0; i < visibleResults.length - 1; i += 2)
2072 int firstMatchedColumn = visibleResults[i];
2073 int lastMatchedColumn = visibleResults[i + 1];
2074 if (firstMatchedColumn <= lastVisibleColumn
2075 && lastMatchedColumn >= firstVisibleColumn)
2078 * found a search results match in the visible region
2080 firstMatchedColumn = Math.max(firstMatchedColumn,
2081 firstVisibleColumn);
2082 lastMatchedColumn = Math.min(lastMatchedColumn,
2086 * draw each mapped position separately (as contiguous positions may
2087 * wrap across lines)
2089 for (int mappedPos = firstMatchedColumn; mappedPos <= lastMatchedColumn; mappedPos++)
2091 int displayColumn = mappedPos;
2092 if (av.hasHiddenColumns())
2094 displayColumn = alignment.getHiddenColumns()
2095 .absoluteToVisibleColumn(displayColumn);
2099 * transX: offset from left edge of canvas to residue position
2101 int transX = labelWidthWest
2102 + ((displayColumn - ranges.getStartRes()) % wrappedWidth)
2103 * av.getCharWidth();
2106 * transY: offset from top edge of canvas to residue position
2108 int transY = gapHeight;
2109 transY += (displayColumn - ranges.getStartRes())
2110 / wrappedWidth * wrappedHeight;
2111 transY += (seqNo - ranges.getStartSeq()) * av.getCharHeight();
2114 * yOffset is from graphics origin to start of visible region
2116 int yOffset = 0;// (displayColumn / wrappedWidth) * wrappedHeight;
2117 if (transY < getHeight())
2120 gg.translate(transX, transY);
2121 drawPanel(gg, displayColumn, displayColumn, seqNo, seqNo,
2123 gg.translate(-transX, -transY);
2137 * Answers the width in pixels of the left scale labels (0 if not shown)
2141 int getLabelWidthWest()
2143 return labelWidthWest;