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)
357 // img is a cached version of the last view we drew
358 // selectImage will hold any selection we have
359 // lcimg is a local *copy* of img which we'll draw selectImage on top of
369 BufferedImage lcimg = new BufferedImage(img.getWidth(), img.getHeight(),
371 Graphics2D g2d = lcimg.createGraphics();
372 g2d.drawImage(img, 0, 0, null);
376 // BufferedImage lcimg = img; // take reference since other threads may null
377 // img and call later.
378 super.paintComponent(g);
380 BufferedImage selectImage = drawSelectionGroup();
384 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
385 .getClipBounds().height)))
387 Graphics2D g2 = (Graphics2D) lcimg.getGraphics();
388 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
390 // overlay selection group on lcimg
391 if (selectImage != null)
394 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
395 g2.drawImage(selectImage, 0, 0, this);
397 g.drawImage(lcimg, 0, 0, this);
403 /* lcimg = setupImage(lcimg);
411 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
412 RenderingHints.VALUE_ANTIALIAS_ON);
415 gg.setColor(Color.white);
416 gg.fillRect(0, 0, imgWidth, imgHeight);
418 ViewportRanges ranges = av.getRanges();
419 if (av.getWrapAlignment())
421 drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
425 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
426 ranges.getStartSeq(), ranges.getEndSeq(), 0);
429 Graphics2D g2 = (Graphics2D) lcimg.getGraphics();
431 // overlay selection group on lcimg
432 if (selectImage != null)
435 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
436 g2.drawImage(selectImage, 0, 0, this);
438 g.drawImage(lcimg, 0, 0, this);
441 private void paintSeqGroup()
447 private void setupImage()
449 // this draws the whole of the alignment
450 imgWidth = getWidth();
451 imgHeight = getHeight();
453 imgWidth -= (imgWidth % charWidth);
454 imgHeight -= (imgHeight % charHeight);
456 if ((imgWidth < 1) || (imgHeight < 1))
464 img = new BufferedImage(imgWidth, imgHeight,
465 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
466 gg = (Graphics2D) img.getGraphics();
467 gg.setFont(av.getFont());
468 } catch (OutOfMemoryError er)
471 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
472 new OOMWarning("Creating alignment image for display", er);
479 private BufferedImage setupSelectionImage()
481 BufferedImage lcimg = null;
483 int width = getWidth();
484 int height = getHeight();
486 width -= (width % charWidth);
487 height -= (height % charHeight);
489 if ((width < 1) || (height < 1))
496 lcimg = new BufferedImage(width, height,
497 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
498 } catch (OutOfMemoryError er)
502 "Selection Group image OutOfMemory Redraw Error.\n" + er);
503 new OOMWarning("Creating alignment image for display", er);
517 * @return DOCUMENT ME!
519 public int getWrappedCanvasWidth(int cwidth)
521 FontMetrics fm = getFontMetrics(av.getFont());
526 if (av.getScaleRightWrapped())
528 LABEL_EAST = fm.stringWidth(getMask());
531 if (av.getScaleLeftWrapped())
533 LABEL_WEST = fm.stringWidth(getMask());
536 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
540 * Generates a string of zeroes.
549 for (int i = 0; i < av.getAlignment().getHeight(); i++)
551 tmp = av.getAlignment().getSequenceAt(i).getEnd();
558 for (int i = maxWidth; i > 0; i /= 10)
572 * @param canvasHeight
577 public void drawWrappedPanel(Graphics g, int canvasWidth,
578 int canvasHeight, int startRes)
581 AlignmentI al = av.getAlignment();
583 FontMetrics fm = getFontMetrics(av.getFont());
588 if (av.getScaleRightWrapped())
590 LABEL_EAST = fm.stringWidth(getMask());
593 if (av.getScaleLeftWrapped())
595 LABEL_WEST = fm.stringWidth(getMask());
598 int hgap = charHeight;
599 if (av.getScaleAboveWrapped())
604 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
605 int cHeight = av.getAlignment().getHeight() * charHeight;
607 av.setWrappedWidth(cWidth);
609 av.getRanges().setViewportStartAndWidth(startRes, cWidth);
613 int maxwidth = av.getAlignment().getWidth();
615 if (av.hasHiddenColumns())
617 maxwidth = av.getAlignment().getHiddenColumns()
618 .findColumnPosition(maxwidth);
621 while ((ypos <= canvasHeight) && (startRes < maxwidth))
623 endx = startRes + cWidth - 1;
630 g.setFont(av.getFont());
631 g.setColor(Color.black);
633 if (av.getScaleLeftWrapped())
635 drawWestScale(g, startRes, endx, ypos);
638 if (av.getScaleRightWrapped())
640 g.translate(canvasWidth - LABEL_EAST, 0);
641 drawEastScale(g, startRes, endx, ypos);
642 g.translate(-(canvasWidth - LABEL_EAST), 0);
645 g.translate(LABEL_WEST, 0);
647 if (av.getScaleAboveWrapped())
649 drawNorthScale(g, startRes, endx, ypos);
652 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
654 g.setColor(Color.blue);
656 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
657 List<Integer> positions = hidden.findHiddenRegionPositions();
658 for (int pos : positions)
660 res = pos - startRes;
662 if (res < 0 || res > endx - startRes)
668 new int[] { res * charWidth - charHeight / 4,
669 res * charWidth + charHeight / 4, res * charWidth },
670 new int[] { ypos - (charHeight / 2),
671 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
677 // When printing we have an extra clipped region,
678 // the Printable page which we need to account for here
679 Shape clip = g.getClip();
683 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
687 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
688 (int) clip.getBounds().getHeight());
691 drawPanel(g, startRes, endx, 0, al.getHeight() - 1, ypos);
693 if (av.isShowAnnotation())
695 g.translate(0, cHeight + ypos + 3);
696 if (annotations == null)
698 annotations = new AnnotationPanel(av);
701 annotations.renderer.drawComponent(annotations, av, g, -1,
703 g.translate(0, -cHeight - ypos - 3);
706 g.translate(-LABEL_WEST, 0);
708 ypos += cHeight + getAnnotationHeight() + hgap;
714 AnnotationPanel annotations;
716 int getAnnotationHeight()
718 if (!av.isShowAnnotation())
723 if (annotations == null)
725 annotations = new AnnotationPanel(av);
728 return annotations.adjustPanelHeight();
747 public void drawPanel(Graphics g1, int startRes, int endRes,
748 int startSeq, int endSeq, int offset)
751 if (!av.hasHiddenColumns())
753 draw(g1, startRes, endRes, startSeq, endSeq, offset);
758 int blockStart = startRes;
759 int blockEnd = endRes;
761 for (int[] region : av.getAlignment().getHiddenColumns()
762 .getHiddenColumnsCopy())
764 int hideStart = region[0];
765 int hideEnd = region[1];
767 if (hideStart <= blockStart)
769 blockStart += (hideEnd - hideStart) + 1;
773 blockEnd = hideStart - 1;
775 g1.translate(screenY * charWidth, 0);
777 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
779 if (av.getShowHiddenMarkers())
781 g1.setColor(Color.blue);
783 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
784 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
785 (endSeq - startSeq + 1) * charHeight + offset);
788 g1.translate(-screenY * charWidth, 0);
789 screenY += blockEnd - blockStart + 1;
790 blockStart = hideEnd + 1;
792 if (screenY > (endRes - startRes))
794 // already rendered last block
799 if (screenY <= (endRes - startRes))
801 // remaining visible region to render
802 blockEnd = blockStart + (endRes - startRes) - screenY;
803 g1.translate(screenY * charWidth, 0);
804 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
806 g1.translate(-screenY * charWidth, 0);
812 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
813 // int x1, int x2, int y1, int y2, int startx, int starty,
814 private void draw(Graphics g, int startRes, int endRes, int startSeq,
815 int endSeq, int offset)
817 g.setFont(av.getFont());
818 sr.prepare(g, av.isRenderGaps());
822 // / First draw the sequences
823 // ///////////////////////////
824 for (int i = startSeq; i <= endSeq; i++)
826 nextSeq = av.getAlignment().getSequenceAt(i);
829 // occasionally, a race condition occurs such that the alignment row is
833 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
834 startRes, endRes, offset + ((i - startSeq) * charHeight));
836 if (av.isShowSequenceFeatures())
838 fr.drawSequence(g, nextSeq, startRes, endRes, offset
839 + ((i - startSeq) * charHeight), false);
842 // / Highlight search Results once all sequences have been drawn
843 // ////////////////////////////////////////////////////////
844 if (av.hasSearchResults())
846 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
848 if (visibleResults != null)
850 for (int r = 0; r < visibleResults.length; r += 2)
852 sr.drawHighlightedText(nextSeq, visibleResults[r],
853 visibleResults[r + 1], (visibleResults[r] - startRes)
855 + ((i - startSeq) * charHeight));
860 if (av.cursorMode && cursorY == i && cursorX >= startRes
861 && cursorX <= endRes)
863 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
864 offset + ((i - startSeq) * charHeight));
868 if (av.getSelectionGroup() != null
869 || av.getAlignment().getGroups().size() > 0)
871 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
876 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
877 int startSeq, int endSeq, int offset)
879 Graphics2D g = (Graphics2D) g1;
881 // ///////////////////////////////////
882 // Now outline any areas if necessary
883 // ///////////////////////////////////
885 SequenceGroup group = null;
891 int visWidth = (endRes - startRes + 1) * charWidth;
893 if (av.getAlignment().getGroups().size() > 0)
895 group = av.getAlignment().getGroups().get(0);
905 boolean inGroup = false;
909 for (i = startSeq; i <= endSeq; i++)
911 // position of start residue of group relative to startRes, in pixels
912 sx = (group.getStartRes() - startRes) * charWidth;
913 sy = offset + ((i - startSeq) * charHeight);
914 // width of group in pixels
915 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
917 if (sx + ex < 0 || sx > visWidth)
922 if ((sx <= (endRes - startRes) * charWidth)
923 && group.getSequences(null).contains(
924 av.getAlignment().getSequenceAt(i)))
927 && !group.getSequences(null).contains(
928 av.getAlignment().getSequenceAt(i + 1)))
930 bottom = sy + charHeight;
935 if (((top == -1) && (i == 0))
936 || !group.getSequences(null).contains(
937 av.getAlignment().getSequenceAt(i - 1)))
945 g.setStroke(new BasicStroke());
946 g.setColor(group.getOutlineColour());
953 // if start position is visible, draw vertical line to left of
955 if (sx >= 0 && sx < visWidth)
957 g.drawLine(sx, oldY, sx, sy);
960 // if end position is visible, draw vertical line to right of
962 if (sx + ex < visWidth)
964 g.drawLine(sx + ex, oldY, sx + ex, sy);
973 if (sx + ex > visWidth)
978 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
980 ex = (endRes - startRes + 1) * charWidth;
983 // draw horizontal line at top of group
986 g.drawLine(sx, top, sx + ex, top);
990 // draw horizontal line at bottom of group
993 g.drawLine(sx, bottom, sx + ex, bottom);
1004 sy = offset + ((i - startSeq) * charHeight);
1005 if (sx >= 0 && sx < visWidth)
1007 g.drawLine(sx, oldY, sx, sy);
1010 if (sx + ex < visWidth)
1012 g.drawLine(sx + ex, oldY, sx + ex, sy);
1021 if (sx + ex > visWidth)
1025 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
1027 ex = (endRes - startRes + 1) * charWidth;
1032 g.drawLine(sx, top, sx + ex, top);
1038 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
1047 g.setStroke(new BasicStroke());
1049 if (groupIndex >= av.getAlignment().getGroups().size())
1054 group = av.getAlignment().getGroups().get(groupIndex);
1056 } while (groupIndex < av.getAlignment().getGroups().size());
1063 * Draw the selection group as a separate image and overlay
1065 private BufferedImage drawSelectionGroup()
1067 // get a new image of the correct size
1068 BufferedImage selectionImage = setupSelectionImage();
1070 if (selectionImage == null)
1075 SequenceGroup group = av.getSelectionGroup();
1082 // set up drawing colour
1083 Graphics2D g = (Graphics2D) selectionImage.getGraphics();
1084 // set background to transparent
1085 g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
1086 g.fillRect(0, 0, selectionImage.getWidth(), selectionImage.getHeight());
1088 g.setComposite(AlphaComposite.Src);
1089 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
1090 BasicStroke.JOIN_ROUND, 3f, new float[]
1092 g.setColor(Color.RED);
1094 int visWidth = av.getRanges().getViewportWidth() * charWidth;
1096 int startRes = av.getRanges().getStartRes();
1098 // set x start and end positions of group
1099 int startx = (group.getStartRes() - startRes) * charWidth;
1100 int endx = (group.getEndRes() - startRes + 1) * charWidth;
1104 boolean inGroup = false;
1108 // get sequences to determine y positions of group
1109 int startSeq = av.getRanges().getStartSeq();
1110 for (i = startSeq; i <= av.getRanges().getEndSeq(); ++i)
1112 int sy = (i - startSeq) * charHeight;
1114 if (group.getSequences(null)
1115 .contains(av.getAlignment().getSequenceAt(i)))
1117 if ((bottom == -1) && !group.getSequences(null)
1118 .contains(av.getAlignment().getSequenceAt(i + 1)))
1120 bottom = sy + charHeight;
1125 if (((top == -1) && (i == 0)) || !group.getSequences(null)
1126 .contains(av.getAlignment().getSequenceAt(i - 1)))
1139 // if start position is visible, draw vertical line to left of
1141 if (startx >= 0 && startx < visWidth * charWidth)
1143 g.drawLine(startx, oldY, startx, sy);
1146 // if end position is visible, draw vertical line to right of
1148 if (endx <= visWidth * charWidth)
1150 g.drawLine(endx, oldY, endx, sy);
1153 if (endx > visWidth * charWidth)
1155 endx = visWidth * charWidth;
1158 // draw horizontal line at top of group
1161 g.drawLine(startx, top, endx, top);
1165 // draw horizontal line at bottom of group
1168 g.drawLine(startx, bottom, endx, bottom);
1178 int sy = (i - startSeq) * charHeight;
1179 if (startx >= 0 && startx < visWidth)
1181 g.drawLine(startx, oldY, startx, sy);
1184 if (endx < visWidth)
1186 g.drawLine(endx, oldY, endx, sy);
1189 if (endx > visWidth)
1196 g.drawLine(startx, top, endx, top);
1202 g.drawLine(startx, bottom - 1, endx, bottom - 1);
1209 return selectionImage;
1218 public void highlightSearchResults(SearchResultsI results)
1222 av.setSearchResults(results);
1228 public void propertyChange(PropertyChangeEvent evt)
1230 String eventName = evt.getPropertyName();
1232 if (eventName.equals(SequenceGroup.SEQ_GROUP_CHANGED))
1236 else if (av.getWrapAlignment())
1238 if (eventName.equals(ViewportRanges.STARTRES))
1246 if (eventName.equals(ViewportRanges.STARTRES))
1248 // Make sure we're not trying to draw a panel
1249 // larger than the visible window
1250 ViewportRanges vpRanges = av.getRanges();
1251 scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
1252 int range = vpRanges.getEndRes() - vpRanges.getStartRes();
1253 if (scrollX > range)
1257 else if (scrollX < -range)
1263 // Both scrolling and resizing change viewport ranges: scrolling changes
1264 // both start and end points, but resize only changes end values.
1265 // Here we only want to fastpaint on a scroll, with resize using a normal
1266 // paint, so scroll events are identified as changes to the horizontal or
1267 // vertical start value.
1268 if (eventName.equals(ViewportRanges.STARTRES))
1270 // scroll - startres and endres both change
1271 fastPaint(scrollX, 0);
1273 else if (eventName.equals(ViewportRanges.STARTSEQ))
1276 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());