2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.datamodel.AlignmentI;
24 import jalview.datamodel.HiddenColumns;
25 import jalview.datamodel.SearchResultsI;
26 import jalview.datamodel.SequenceGroup;
27 import jalview.datamodel.SequenceI;
28 import jalview.renderer.ScaleRenderer;
29 import jalview.renderer.ScaleRenderer.ScaleMark;
30 import jalview.viewmodel.ViewportListenerI;
31 import jalview.viewmodel.ViewportRanges;
33 import java.awt.AlphaComposite;
34 import java.awt.BasicStroke;
35 import java.awt.BorderLayout;
36 import java.awt.Color;
37 import java.awt.FontMetrics;
38 import java.awt.Graphics;
39 import java.awt.Graphics2D;
40 import java.awt.RenderingHints;
41 import java.awt.Shape;
42 import java.awt.image.BufferedImage;
43 import java.beans.PropertyChangeEvent;
44 import java.util.List;
46 import javax.swing.JComponent;
54 public class SeqCanvas extends JComponent implements ViewportListenerI
56 final FeatureRenderer fr;
58 final SequenceRenderer sr;
70 boolean fastPaint = false;
81 * Creates a new SeqCanvas object.
86 public SeqCanvas(AlignmentPanel ap)
90 fr = new FeatureRenderer(ap);
91 sr = new SequenceRenderer(av);
92 setLayout(new BorderLayout());
93 PaintRefresher.Register(this, av.getSequenceSetId());
94 setBackground(Color.white);
96 av.getRanges().addPropertyChangeListener(this);
99 public SequenceRenderer getSequenceRenderer()
104 public FeatureRenderer getFeatureRenderer()
109 int charHeight = 0, charWidth = 0;
111 private void updateViewport()
113 charHeight = av.getCharHeight();
114 charWidth = av.getCharWidth();
129 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
132 for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
135 int mpos = mark.column; // (i - startx - 1)
140 String mstring = mark.text;
146 g.drawString(mstring, mpos * charWidth, ypos - (charHeight / 2));
148 g.drawLine((mpos * charWidth) + (charWidth / 2), (ypos + 2)
149 - (charHeight / 2), (mpos * charWidth) + (charWidth / 2),
167 void drawWestScale(Graphics g, int startx, int endx, int ypos)
169 FontMetrics fm = getFontMetrics(av.getFont());
172 if (av.hasHiddenColumns())
174 startx = av.getAlignment().getHiddenColumns()
175 .adjustForHiddenColumns(startx);
176 endx = av.getAlignment().getHiddenColumns()
177 .adjustForHiddenColumns(endx);
180 int maxwidth = av.getAlignment().getWidth();
181 if (av.hasHiddenColumns())
183 maxwidth = av.getAlignment().getHiddenColumns()
184 .findColumnPosition(maxwidth) - 1;
188 for (int i = 0; i < av.getAlignment().getHeight(); i++)
190 SequenceI seq = av.getAlignment().getSequenceAt(i);
196 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
203 value = av.getAlignment().getSequenceAt(i).findPosition(index);
210 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
212 g.drawString(value + "", x, (ypos + (i * charHeight))
230 void drawEastScale(Graphics g, int startx, int endx, int ypos)
234 if (av.hasHiddenColumns())
236 endx = av.getAlignment().getHiddenColumns()
237 .adjustForHiddenColumns(endx);
242 for (int i = 0; i < av.getAlignment().getHeight(); i++)
244 seq = av.getAlignment().getSequenceAt(i);
248 while (index > startx)
250 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
257 value = seq.findPosition(index);
264 g.drawString(String.valueOf(value), 0, (ypos + (i * charHeight))
270 boolean fastpainting = false;
273 * need to make this thread safe move alignment rendering in response to
279 * shift up or down in repaint
281 public void fastPaint(int horizontal, int vertical)
283 if (fastpainting || gg == null)
291 ViewportRanges ranges = av.getRanges();
292 int sr = ranges.getStartRes();
293 int er = ranges.getEndRes();
294 int ss = ranges.getStartSeq();
295 int es = ranges.getEndSeq();
299 gg.copyArea(horizontal * charWidth, vertical * charHeight, imgWidth,
300 imgHeight, -horizontal * charWidth, -vertical * charHeight);
302 if (horizontal > 0) // scrollbar pulled right, image to the left
304 transX = (er - sr - horizontal) * charWidth;
305 sr = er - horizontal;
307 else if (horizontal < 0)
309 er = sr - horizontal;
311 else if (vertical > 0) // scroll down
315 if (ss < ranges.getStartSeq())
316 { // ie scrolling too fast, more than a page at a time
317 ss = ranges.getStartSeq();
321 transY = imgHeight - ((vertical + 1) * charHeight);
324 else if (vertical < 0)
328 if (es > ranges.getEndSeq())
330 es = ranges.getEndSeq();
334 gg.translate(transX, transY);
335 drawPanel(gg, sr, er, ss, es, 0);
336 gg.translate(-transX, -transY);
339 fastpainting = false;
343 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
344 * towards! startx is the first residue (starting at 0) to display. endx is
345 * the last residue to display (starting at 0). starty is the first sequence
346 * to display (starting at 0). endy is the last sequence to display (starting
347 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
348 * adjustment listener in SeqPanel when the scrollbars move.
351 // Set this to false to force a full panel paint
353 public void paintComponent(Graphics g)
355 super.paintComponent(g);
359 // img is a cached version of the last view we drew
360 // selectImage will hold any selection we have
361 // lcimg is a local *copy* of img which we'll draw selectImage on top of
372 BufferedImage selectImage = drawSelectionGroup();
374 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
375 || (getVisibleRect().height != g.getClipBounds().height))
377 BufferedImage lcimg = buildLocalImage(selectImage);
378 g.drawImage(lcimg, 0, 0, this);
386 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
387 RenderingHints.VALUE_ANTIALIAS_ON);
390 gg.setColor(Color.white);
391 gg.fillRect(0, 0, imgWidth, imgHeight);
393 ViewportRanges ranges = av.getRanges();
394 if (av.getWrapAlignment())
396 drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
400 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
401 ranges.getStartSeq(), ranges.getEndSeq(), 0);
404 BufferedImage lcimg = buildLocalImage(selectImage);
405 g.drawImage(lcimg, 0, 0, this);
409 * Make a local image by combining the cached image img
412 private BufferedImage buildLocalImage(BufferedImage selectImage)
414 // clone the cached image
415 BufferedImage lcimg = new BufferedImage(img.getWidth(), img.getHeight(),
417 Graphics2D g2d = lcimg.createGraphics();
418 g2d.drawImage(img, 0, 0, null);
420 // overlay selection group on lcimg
421 if (selectImage != null)
424 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
425 g2d.drawImage(selectImage, 0, 0, this);
432 private void paintSeqGroup()
438 private void setupImage()
440 // this draws the whole of the alignment
441 imgWidth = getWidth();
442 imgHeight = getHeight();
444 imgWidth -= (imgWidth % charWidth);
445 imgHeight -= (imgHeight % charHeight);
447 if ((imgWidth < 1) || (imgHeight < 1))
455 img = new BufferedImage(imgWidth, imgHeight,
456 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
457 gg = (Graphics2D) img.getGraphics();
458 gg.setFont(av.getFont());
459 } catch (OutOfMemoryError er)
462 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
463 new OOMWarning("Creating alignment image for display", er);
470 private BufferedImage setupSelectionImage()
472 BufferedImage lcimg = null;
474 int width = getWidth();
475 int height = getHeight();
477 width -= (width % charWidth);
478 height -= (height % charHeight);
480 if ((width < 1) || (height < 1))
487 lcimg = new BufferedImage(width, height,
488 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
489 } catch (OutOfMemoryError er)
493 "Selection Group image OutOfMemory Redraw Error.\n" + er);
494 new OOMWarning("Creating alignment image for display", er);
508 * @return DOCUMENT ME!
510 public int getWrappedCanvasWidth(int cwidth)
512 FontMetrics fm = getFontMetrics(av.getFont());
517 if (av.getScaleRightWrapped())
519 LABEL_EAST = fm.stringWidth(getMask());
522 if (av.getScaleLeftWrapped())
524 LABEL_WEST = fm.stringWidth(getMask());
527 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
531 * Generates a string of zeroes.
540 for (int i = 0; i < av.getAlignment().getHeight(); i++)
542 tmp = av.getAlignment().getSequenceAt(i).getEnd();
549 for (int i = maxWidth; i > 0; i /= 10)
563 * @param canvasHeight
568 public void drawWrappedPanel(Graphics g, int canvasWidth,
569 int canvasHeight, int startRes)
572 AlignmentI al = av.getAlignment();
574 FontMetrics fm = getFontMetrics(av.getFont());
579 if (av.getScaleRightWrapped())
581 LABEL_EAST = fm.stringWidth(getMask());
584 if (av.getScaleLeftWrapped())
586 LABEL_WEST = fm.stringWidth(getMask());
589 int hgap = charHeight;
590 if (av.getScaleAboveWrapped())
595 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
596 int cHeight = av.getAlignment().getHeight() * charHeight;
598 av.setWrappedWidth(cWidth);
600 av.getRanges().setViewportStartAndWidth(startRes, cWidth);
604 int maxwidth = av.getAlignment().getWidth();
606 if (av.hasHiddenColumns())
608 maxwidth = av.getAlignment().getHiddenColumns()
609 .findColumnPosition(maxwidth);
612 while ((ypos <= canvasHeight) && (startRes < maxwidth))
614 endx = startRes + cWidth - 1;
621 g.setFont(av.getFont());
622 g.setColor(Color.black);
624 if (av.getScaleLeftWrapped())
626 drawWestScale(g, startRes, endx, ypos);
629 if (av.getScaleRightWrapped())
631 g.translate(canvasWidth - LABEL_EAST, 0);
632 drawEastScale(g, startRes, endx, ypos);
633 g.translate(-(canvasWidth - LABEL_EAST), 0);
636 g.translate(LABEL_WEST, 0);
638 if (av.getScaleAboveWrapped())
640 drawNorthScale(g, startRes, endx, ypos);
643 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
645 g.setColor(Color.blue);
647 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
648 List<Integer> positions = hidden.findHiddenRegionPositions();
649 for (int pos : positions)
651 res = pos - startRes;
653 if (res < 0 || res > endx - startRes)
659 new int[] { res * charWidth - charHeight / 4,
660 res * charWidth + charHeight / 4, res * charWidth },
661 new int[] { ypos - (charHeight / 2),
662 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
668 // When printing we have an extra clipped region,
669 // the Printable page which we need to account for here
670 Shape clip = g.getClip();
674 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
678 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
679 (int) clip.getBounds().getHeight());
682 drawPanel(g, startRes, endx, 0, al.getHeight() - 1, ypos);
684 if (av.isShowAnnotation())
686 g.translate(0, cHeight + ypos + 3);
687 if (annotations == null)
689 annotations = new AnnotationPanel(av);
692 annotations.renderer.drawComponent(annotations, av, g, -1,
694 g.translate(0, -cHeight - ypos - 3);
697 g.translate(-LABEL_WEST, 0);
699 ypos += cHeight + getAnnotationHeight() + hgap;
705 AnnotationPanel annotations;
707 int getAnnotationHeight()
709 if (!av.isShowAnnotation())
714 if (annotations == null)
716 annotations = new AnnotationPanel(av);
719 return annotations.adjustPanelHeight();
738 public void drawPanel(Graphics g1, int startRes, int endRes,
739 int startSeq, int endSeq, int offset)
742 if (!av.hasHiddenColumns())
744 draw(g1, startRes, endRes, startSeq, endSeq, offset);
749 int blockStart = startRes;
750 int blockEnd = endRes;
752 for (int[] region : av.getAlignment().getHiddenColumns()
753 .getHiddenColumnsCopy())
755 int hideStart = region[0];
756 int hideEnd = region[1];
758 if (hideStart <= blockStart)
760 blockStart += (hideEnd - hideStart) + 1;
764 blockEnd = hideStart - 1;
766 g1.translate(screenY * charWidth, 0);
768 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
770 if (av.getShowHiddenMarkers())
772 g1.setColor(Color.blue);
774 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
775 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
776 (endSeq - startSeq + 1) * charHeight + offset);
779 g1.translate(-screenY * charWidth, 0);
780 screenY += blockEnd - blockStart + 1;
781 blockStart = hideEnd + 1;
783 if (screenY > (endRes - startRes))
785 // already rendered last block
790 if (screenY <= (endRes - startRes))
792 // remaining visible region to render
793 blockEnd = blockStart + (endRes - startRes) - screenY;
794 g1.translate(screenY * charWidth, 0);
795 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
797 g1.translate(-screenY * charWidth, 0);
803 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
804 // int x1, int x2, int y1, int y2, int startx, int starty,
805 private void draw(Graphics g, int startRes, int endRes, int startSeq,
806 int endSeq, int offset)
808 g.setFont(av.getFont());
809 sr.prepare(g, av.isRenderGaps());
813 // / First draw the sequences
814 // ///////////////////////////
815 for (int i = startSeq; i <= endSeq; i++)
817 nextSeq = av.getAlignment().getSequenceAt(i);
820 // occasionally, a race condition occurs such that the alignment row is
824 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
825 startRes, endRes, offset + ((i - startSeq) * charHeight));
827 if (av.isShowSequenceFeatures())
829 fr.drawSequence(g, nextSeq, startRes, endRes, offset
830 + ((i - startSeq) * charHeight), false);
833 // / Highlight search Results once all sequences have been drawn
834 // ////////////////////////////////////////////////////////
835 if (av.hasSearchResults())
837 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
839 if (visibleResults != null)
841 for (int r = 0; r < visibleResults.length; r += 2)
843 sr.drawHighlightedText(nextSeq, visibleResults[r],
844 visibleResults[r + 1], (visibleResults[r] - startRes)
846 + ((i - startSeq) * charHeight));
851 if (av.cursorMode && cursorY == i && cursorX >= startRes
852 && cursorX <= endRes)
854 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
855 offset + ((i - startSeq) * charHeight));
859 if (av.getSelectionGroup() != null
860 || av.getAlignment().getGroups().size() > 0)
862 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
867 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
868 int startSeq, int endSeq, int offset)
870 Graphics2D g = (Graphics2D) g1;
872 // ///////////////////////////////////
873 // Now outline any areas if necessary
874 // ///////////////////////////////////
876 SequenceGroup group = null;
882 int visWidth = (endRes - startRes + 1) * charWidth;
884 if (av.getAlignment().getGroups().size() > 0)
886 group = av.getAlignment().getGroups().get(0);
896 boolean inGroup = false;
900 for (i = startSeq; i <= endSeq; i++)
902 // position of start residue of group relative to startRes, in pixels
903 sx = (group.getStartRes() - startRes) * charWidth;
904 sy = offset + ((i - startSeq) * charHeight);
905 // width of group in pixels
906 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
908 if (sx + ex < 0 || sx > visWidth)
913 if ((sx <= (endRes - startRes) * charWidth)
914 && group.getSequences(null).contains(
915 av.getAlignment().getSequenceAt(i)))
918 && !group.getSequences(null).contains(
919 av.getAlignment().getSequenceAt(i + 1)))
921 bottom = sy + charHeight;
926 if (((top == -1) && (i == 0))
927 || !group.getSequences(null).contains(
928 av.getAlignment().getSequenceAt(i - 1)))
936 g.setStroke(new BasicStroke());
937 g.setColor(group.getOutlineColour());
944 // if start position is visible, draw vertical line to left of
946 if (sx >= 0 && sx < visWidth)
948 g.drawLine(sx, oldY, sx, sy);
951 // if end position is visible, draw vertical line to right of
953 if (sx + ex < visWidth)
955 g.drawLine(sx + ex, oldY, sx + ex, sy);
964 if (sx + ex > visWidth)
969 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
971 ex = (endRes - startRes + 1) * charWidth;
974 // draw horizontal line at top of group
977 g.drawLine(sx, top, sx + ex, top);
981 // draw horizontal line at bottom of group
984 g.drawLine(sx, bottom, sx + ex, bottom);
995 sy = offset + ((i - startSeq) * charHeight);
996 if (sx >= 0 && sx < visWidth)
998 g.drawLine(sx, oldY, sx, sy);
1001 if (sx + ex < visWidth)
1003 g.drawLine(sx + ex, oldY, sx + ex, sy);
1012 if (sx + ex > visWidth)
1016 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
1018 ex = (endRes - startRes + 1) * charWidth;
1023 g.drawLine(sx, top, sx + ex, top);
1029 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
1038 g.setStroke(new BasicStroke());
1040 if (groupIndex >= av.getAlignment().getGroups().size())
1045 group = av.getAlignment().getGroups().get(groupIndex);
1047 } while (groupIndex < av.getAlignment().getGroups().size());
1054 * Draw the selection group as a separate image and overlay
1056 private BufferedImage drawSelectionGroup()
1058 // get a new image of the correct size
1059 BufferedImage selectionImage = setupSelectionImage();
1061 if (selectionImage == null)
1066 SequenceGroup group = av.getSelectionGroup();
1073 // set up drawing colour
1074 Graphics2D g = (Graphics2D) selectionImage.getGraphics();
1075 // set background to transparent
1076 g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
1077 g.fillRect(0, 0, selectionImage.getWidth(), selectionImage.getHeight());
1079 g.setComposite(AlphaComposite.Src);
1080 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
1081 BasicStroke.JOIN_ROUND, 3f, new float[]
1083 g.setColor(Color.RED);
1085 int visWidth = av.getRanges().getViewportWidth() * charWidth;
1087 int startRes = av.getRanges().getStartRes();
1089 // set x start and end positions of group
1090 int startx = (group.getStartRes() - startRes) * charWidth;
1091 int endx = (group.getEndRes() - startRes + 1) * charWidth;
1095 boolean inGroup = false;
1099 // get sequences to determine y positions of group
1100 int startSeq = av.getRanges().getStartSeq();
1101 for (i = startSeq; i <= av.getRanges().getEndSeq(); ++i)
1103 int sy = (i - startSeq) * charHeight;
1105 if (group.getSequences(null)
1106 .contains(av.getAlignment().getSequenceAt(i)))
1108 if ((bottom == -1) && !group.getSequences(null)
1109 .contains(av.getAlignment().getSequenceAt(i + 1)))
1111 bottom = sy + charHeight;
1116 if (((top == -1) && (i == 0)) || !group.getSequences(null)
1117 .contains(av.getAlignment().getSequenceAt(i - 1)))
1130 // if start position is visible, draw vertical line to left of
1132 if (startx >= 0 && startx < visWidth * charWidth)
1134 g.drawLine(startx, oldY, startx, sy);
1137 // if end position is visible, draw vertical line to right of
1139 if (endx <= visWidth * charWidth)
1141 g.drawLine(endx, oldY, endx, sy);
1144 if (endx > visWidth * charWidth)
1146 endx = visWidth * charWidth;
1149 // draw horizontal line at top of group
1152 g.drawLine(startx, top, endx, top);
1156 // draw horizontal line at bottom of group
1159 g.drawLine(startx, bottom, endx, bottom);
1169 int sy = (i - startSeq) * charHeight;
1170 if (startx >= 0 && startx < visWidth)
1172 g.drawLine(startx, oldY, startx, sy);
1175 if (endx < visWidth)
1177 g.drawLine(endx, oldY, endx, sy);
1180 if (endx > visWidth)
1187 g.drawLine(startx, top, endx, top);
1193 g.drawLine(startx, bottom - 1, endx, bottom - 1);
1200 return selectionImage;
1209 public void highlightSearchResults(SearchResultsI results)
1213 av.setSearchResults(results);
1219 public void propertyChange(PropertyChangeEvent evt)
1221 String eventName = evt.getPropertyName();
1223 if (eventName.equals(SequenceGroup.SEQ_GROUP_CHANGED))
1227 else if (av.getWrapAlignment())
1229 if (eventName.equals(ViewportRanges.STARTRES))
1237 if (eventName.equals(ViewportRanges.STARTRES))
1239 // Make sure we're not trying to draw a panel
1240 // larger than the visible window
1241 ViewportRanges vpRanges = av.getRanges();
1242 scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
1243 int range = vpRanges.getEndRes() - vpRanges.getStartRes();
1244 if (scrollX > range)
1248 else if (scrollX < -range)
1254 // Both scrolling and resizing change viewport ranges: scrolling changes
1255 // both start and end points, but resize only changes end values.
1256 // Here we only want to fastpaint on a scroll, with resize using a normal
1257 // paint, so scroll events are identified as changes to the horizontal or
1258 // vertical start value.
1259 if (eventName.equals(ViewportRanges.STARTRES))
1261 // scroll - startres and endres both change
1262 fastPaint(scrollX, 0);
1264 else if (eventName.equals(ViewportRanges.STARTSEQ))
1267 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());