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,
300 img.getWidth(), img.getHeight(), -horizontal * charWidth,
301 -vertical * charHeight);
303 if (horizontal > 0) // scrollbar pulled right, image to the left
305 transX = (er - sr - horizontal) * charWidth;
306 sr = er - horizontal;
308 else if (horizontal < 0)
310 er = sr - horizontal;
312 else if (vertical > 0) // scroll down
316 if (ss < ranges.getStartSeq())
317 { // ie scrolling too fast, more than a page at a time
318 ss = ranges.getStartSeq();
322 transY = img.getHeight() - ((vertical + 1) * charHeight);
325 else if (vertical < 0)
329 if (es > ranges.getEndSeq())
331 es = ranges.getEndSeq();
335 gg.translate(transX, transY);
336 drawPanel(gg, sr, er, ss, es, 0);
337 gg.translate(-transX, -transY);
340 fastpainting = false;
344 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
345 * towards! startx is the first residue (starting at 0) to display. endx is
346 * the last residue to display (starting at 0). starty is the first sequence
347 * to display (starting at 0). endy is the last sequence to display (starting
348 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
349 * adjustment listener in SeqPanel when the scrollbars move.
352 // Set this to false to force a full panel paint
354 public void paintComponent(Graphics g)
356 super.paintComponent(g);
360 // img is a cached version of the last view we drew
361 // selectImage will hold any selection we have
362 // lcimg is a local *copy* of img which we'll draw selectImage on top of
364 BufferedImage selectImage = drawSelectionGroup();
366 if (fastPaint || (getVisibleRect().width != g.getClipBounds().width)
367 || (getVisibleRect().height != g.getClipBounds().height))
369 BufferedImage lcimg = buildLocalImage(selectImage);
370 g.drawImage(lcimg, 0, 0, this);
376 int width = getWidth();
377 int height = getHeight();
379 width -= (width % charWidth);
380 height -= (height % charHeight);
382 if ((width < 1) || (height < 1))
387 if (img == null || width != img.getWidth() || height != img.getHeight())
390 gg = (Graphics2D) img.getGraphics();
391 gg.setFont(av.getFont());
401 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
402 RenderingHints.VALUE_ANTIALIAS_ON);
405 gg.setColor(Color.white);
406 gg.fillRect(0, 0, img.getWidth(), img.getHeight());
408 ViewportRanges ranges = av.getRanges();
409 if (av.getWrapAlignment())
411 drawWrappedPanel(gg, getWidth(), getHeight(), ranges.getStartRes());
415 drawPanel(gg, ranges.getStartRes(), ranges.getEndRes(),
416 ranges.getStartSeq(), ranges.getEndSeq(), 0);
419 BufferedImage lcimg = buildLocalImage(selectImage);
420 g.drawImage(lcimg, 0, 0, this);
424 * Make a local image by combining the cached image img
427 private BufferedImage buildLocalImage(BufferedImage selectImage)
429 // clone the cached image
430 BufferedImage lcimg = new BufferedImage(img.getWidth(), img.getHeight(),
432 Graphics2D g2d = lcimg.createGraphics();
433 g2d.drawImage(img, 0, 0, null);
435 // overlay selection group on lcimg
436 if (selectImage != null)
439 AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
440 g2d.drawImage(selectImage, 0, 0, this);
447 private void paintSeqGroup()
453 /*private void setupImage()
455 // this draws the whole of the alignment
456 imgWidth = getWidth();
457 imgHeight = getHeight();
459 imgWidth -= (imgWidth % charWidth);
460 imgHeight -= (imgHeight % charHeight);
462 if ((imgWidth < 1) || (imgHeight < 1))
469 img = new BufferedImage(imgWidth, imgHeight,
470 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
471 gg = (Graphics2D) img.getGraphics();
472 gg.setFont(av.getFont());
473 } catch (OutOfMemoryError er)
476 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
477 new OOMWarning("Creating alignment image for display", er);
484 private BufferedImage setupImage()
486 BufferedImage lcimg = null;
488 int width = getWidth();
489 int height = getHeight();
491 width -= (width % charWidth);
492 height -= (height % charHeight);
494 if ((width < 1) || (height < 1))
501 lcimg = new BufferedImage(width, height,
502 BufferedImage.TYPE_INT_ARGB); // ARGB so alpha compositing works
503 } catch (OutOfMemoryError er)
507 "Selection Group image OutOfMemory Redraw Error.\n" + er);
508 new OOMWarning("Creating alignment image for display", er);
522 * @return DOCUMENT ME!
524 public int getWrappedCanvasWidth(int cwidth)
526 FontMetrics fm = getFontMetrics(av.getFont());
531 if (av.getScaleRightWrapped())
533 LABEL_EAST = fm.stringWidth(getMask());
536 if (av.getScaleLeftWrapped())
538 LABEL_WEST = fm.stringWidth(getMask());
541 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
545 * Generates a string of zeroes.
554 for (int i = 0; i < av.getAlignment().getHeight(); i++)
556 tmp = av.getAlignment().getSequenceAt(i).getEnd();
563 for (int i = maxWidth; i > 0; i /= 10)
577 * @param canvasHeight
582 public void drawWrappedPanel(Graphics g, int canvasWidth,
583 int canvasHeight, int startRes)
586 AlignmentI al = av.getAlignment();
588 FontMetrics fm = getFontMetrics(av.getFont());
593 if (av.getScaleRightWrapped())
595 LABEL_EAST = fm.stringWidth(getMask());
598 if (av.getScaleLeftWrapped())
600 LABEL_WEST = fm.stringWidth(getMask());
603 int hgap = charHeight;
604 if (av.getScaleAboveWrapped())
609 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
610 int cHeight = av.getAlignment().getHeight() * charHeight;
612 av.setWrappedWidth(cWidth);
614 av.getRanges().setViewportStartAndWidth(startRes, cWidth);
618 int maxwidth = av.getAlignment().getWidth();
620 if (av.hasHiddenColumns())
622 maxwidth = av.getAlignment().getHiddenColumns()
623 .findColumnPosition(maxwidth);
626 while ((ypos <= canvasHeight) && (startRes < maxwidth))
628 endx = startRes + cWidth - 1;
635 g.setFont(av.getFont());
636 g.setColor(Color.black);
638 if (av.getScaleLeftWrapped())
640 drawWestScale(g, startRes, endx, ypos);
643 if (av.getScaleRightWrapped())
645 g.translate(canvasWidth - LABEL_EAST, 0);
646 drawEastScale(g, startRes, endx, ypos);
647 g.translate(-(canvasWidth - LABEL_EAST), 0);
650 g.translate(LABEL_WEST, 0);
652 if (av.getScaleAboveWrapped())
654 drawNorthScale(g, startRes, endx, ypos);
657 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
659 g.setColor(Color.blue);
661 HiddenColumns hidden = av.getAlignment().getHiddenColumns();
662 List<Integer> positions = hidden.findHiddenRegionPositions();
663 for (int pos : positions)
665 res = pos - startRes;
667 if (res < 0 || res > endx - startRes)
673 new int[] { res * charWidth - charHeight / 4,
674 res * charWidth + charHeight / 4, res * charWidth },
675 new int[] { ypos - (charHeight / 2),
676 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
682 // When printing we have an extra clipped region,
683 // the Printable page which we need to account for here
684 Shape clip = g.getClip();
688 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
692 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
693 (int) clip.getBounds().getHeight());
696 drawPanel(g, startRes, endx, 0, al.getHeight() - 1, ypos);
698 if (av.isShowAnnotation())
700 g.translate(0, cHeight + ypos + 3);
701 if (annotations == null)
703 annotations = new AnnotationPanel(av);
706 annotations.renderer.drawComponent(annotations, av, g, -1,
708 g.translate(0, -cHeight - ypos - 3);
711 g.translate(-LABEL_WEST, 0);
713 ypos += cHeight + getAnnotationHeight() + hgap;
719 AnnotationPanel annotations;
721 int getAnnotationHeight()
723 if (!av.isShowAnnotation())
728 if (annotations == null)
730 annotations = new AnnotationPanel(av);
733 return annotations.adjustPanelHeight();
752 public void drawPanel(Graphics g1, int startRes, int endRes,
753 int startSeq, int endSeq, int offset)
756 if (!av.hasHiddenColumns())
758 draw(g1, startRes, endRes, startSeq, endSeq, offset);
763 int blockStart = startRes;
764 int blockEnd = endRes;
766 for (int[] region : av.getAlignment().getHiddenColumns()
767 .getHiddenColumnsCopy())
769 int hideStart = region[0];
770 int hideEnd = region[1];
772 if (hideStart <= blockStart)
774 blockStart += (hideEnd - hideStart) + 1;
778 blockEnd = hideStart - 1;
780 g1.translate(screenY * charWidth, 0);
782 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
784 if (av.getShowHiddenMarkers())
786 g1.setColor(Color.blue);
788 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
789 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
790 (endSeq - startSeq + 1) * charHeight + offset);
793 g1.translate(-screenY * charWidth, 0);
794 screenY += blockEnd - blockStart + 1;
795 blockStart = hideEnd + 1;
797 if (screenY > (endRes - startRes))
799 // already rendered last block
804 if (screenY <= (endRes - startRes))
806 // remaining visible region to render
807 blockEnd = blockStart + (endRes - startRes) - screenY;
808 g1.translate(screenY * charWidth, 0);
809 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
811 g1.translate(-screenY * charWidth, 0);
817 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
818 // int x1, int x2, int y1, int y2, int startx, int starty,
819 private void draw(Graphics g, int startRes, int endRes, int startSeq,
820 int endSeq, int offset)
822 g.setFont(av.getFont());
823 sr.prepare(g, av.isRenderGaps());
827 // / First draw the sequences
828 // ///////////////////////////
829 for (int i = startSeq; i <= endSeq; i++)
831 nextSeq = av.getAlignment().getSequenceAt(i);
834 // occasionally, a race condition occurs such that the alignment row is
838 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
839 startRes, endRes, offset + ((i - startSeq) * charHeight));
841 if (av.isShowSequenceFeatures())
843 fr.drawSequence(g, nextSeq, startRes, endRes, offset
844 + ((i - startSeq) * charHeight), false);
847 // / Highlight search Results once all sequences have been drawn
848 // ////////////////////////////////////////////////////////
849 if (av.hasSearchResults())
851 int[] visibleResults = av.getSearchResults().getResults(nextSeq,
853 if (visibleResults != null)
855 for (int r = 0; r < visibleResults.length; r += 2)
857 sr.drawHighlightedText(nextSeq, visibleResults[r],
858 visibleResults[r + 1], (visibleResults[r] - startRes)
860 + ((i - startSeq) * charHeight));
865 if (av.cursorMode && cursorY == i && cursorX >= startRes
866 && cursorX <= endRes)
868 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
869 offset + ((i - startSeq) * charHeight));
873 if (av.getSelectionGroup() != null
874 || av.getAlignment().getGroups().size() > 0)
876 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
881 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
882 int startSeq, int endSeq, int offset)
884 Graphics2D g = (Graphics2D) g1;
886 // ///////////////////////////////////
887 // Now outline any areas if necessary
888 // ///////////////////////////////////
890 SequenceGroup group = null;
896 int visWidth = (endRes - startRes + 1) * charWidth;
898 if (av.getAlignment().getGroups().size() > 0)
900 group = av.getAlignment().getGroups().get(0);
910 boolean inGroup = false;
914 for (i = startSeq; i <= endSeq; i++)
916 // position of start residue of group relative to startRes, in pixels
917 sx = (group.getStartRes() - startRes) * charWidth;
918 sy = offset + ((i - startSeq) * charHeight);
919 // width of group in pixels
920 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
922 if (sx + ex < 0 || sx > visWidth)
927 if ((sx <= (endRes - startRes) * charWidth)
928 && group.getSequences(null).contains(
929 av.getAlignment().getSequenceAt(i)))
932 && !group.getSequences(null).contains(
933 av.getAlignment().getSequenceAt(i + 1)))
935 bottom = sy + charHeight;
940 if (((top == -1) && (i == 0))
941 || !group.getSequences(null).contains(
942 av.getAlignment().getSequenceAt(i - 1)))
950 g.setStroke(new BasicStroke());
951 g.setColor(group.getOutlineColour());
958 // if start position is visible, draw vertical line to left of
960 if (sx >= 0 && sx < visWidth)
962 g.drawLine(sx, oldY, sx, sy);
965 // if end position is visible, draw vertical line to right of
967 if (sx + ex < visWidth)
969 g.drawLine(sx + ex, oldY, sx + ex, sy);
978 if (sx + ex > visWidth)
983 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
985 ex = (endRes - startRes + 1) * charWidth;
988 // draw horizontal line at top of group
991 g.drawLine(sx, top, sx + ex, top);
995 // draw horizontal line at bottom of group
998 g.drawLine(sx, bottom, sx + ex, bottom);
1009 sy = offset + ((i - startSeq) * charHeight);
1010 if (sx >= 0 && sx < visWidth)
1012 g.drawLine(sx, oldY, sx, sy);
1015 if (sx + ex < visWidth)
1017 g.drawLine(sx + ex, oldY, sx + ex, sy);
1026 if (sx + ex > visWidth)
1030 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
1032 ex = (endRes - startRes + 1) * charWidth;
1037 g.drawLine(sx, top, sx + ex, top);
1043 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
1052 g.setStroke(new BasicStroke());
1054 if (groupIndex >= av.getAlignment().getGroups().size())
1059 group = av.getAlignment().getGroups().get(groupIndex);
1061 } while (groupIndex < av.getAlignment().getGroups().size());
1068 * Draw the selection group as a separate image and overlay
1070 private BufferedImage drawSelectionGroup()
1072 // get a new image of the correct size
1073 BufferedImage selectionImage = setupImage();
1075 if (selectionImage == null)
1080 SequenceGroup group = av.getSelectionGroup();
1087 // set up drawing colour
1088 Graphics2D g = (Graphics2D) selectionImage.getGraphics();
1089 // set background to transparent
1090 g.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
1091 g.fillRect(0, 0, selectionImage.getWidth(), selectionImage.getHeight());
1093 g.setComposite(AlphaComposite.Src);
1094 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
1095 BasicStroke.JOIN_ROUND, 3f, new float[]
1097 g.setColor(Color.RED);
1099 int visWidth = av.getRanges().getViewportWidth() * charWidth;
1101 int startRes = av.getRanges().getStartRes();
1103 // set x start and end positions of group
1104 int startx = (group.getStartRes() - startRes) * charWidth;
1105 int endx = (group.getEndRes() - startRes + 1) * charWidth;
1109 boolean inGroup = false;
1113 // get sequences to determine y positions of group
1114 int startSeq = av.getRanges().getStartSeq();
1115 for (i = startSeq; i <= av.getRanges().getEndSeq(); ++i)
1117 int sy = (i - startSeq) * charHeight;
1119 if (group.getSequences(null)
1120 .contains(av.getAlignment().getSequenceAt(i)))
1122 if ((bottom == -1) && !group.getSequences(null)
1123 .contains(av.getAlignment().getSequenceAt(i + 1)))
1125 bottom = sy + charHeight;
1130 if (((top == -1) && (i == 0)) || !group.getSequences(null)
1131 .contains(av.getAlignment().getSequenceAt(i - 1)))
1144 // if start position is visible, draw vertical line to left of
1146 if (startx >= 0 && startx < visWidth * charWidth)
1148 g.drawLine(startx, oldY, startx, sy);
1151 // if end position is visible, draw vertical line to right of
1153 if (endx <= visWidth * charWidth)
1155 g.drawLine(endx, oldY, endx, sy);
1158 if (endx > visWidth * charWidth)
1160 endx = visWidth * charWidth;
1163 // draw horizontal line at top of group
1166 g.drawLine(startx, top, endx, top);
1170 // draw horizontal line at bottom of group
1173 g.drawLine(startx, bottom, endx, bottom);
1183 int sy = (i - startSeq) * charHeight;
1184 if (startx >= 0 && startx < visWidth)
1186 g.drawLine(startx, oldY, startx, sy);
1189 if (endx < visWidth)
1191 g.drawLine(endx, oldY, endx, sy);
1194 if (endx > visWidth)
1201 g.drawLine(startx, top, endx, top);
1207 g.drawLine(startx, bottom - 1, endx, bottom - 1);
1214 return selectionImage;
1223 public void highlightSearchResults(SearchResultsI results)
1227 av.setSearchResults(results);
1233 public void propertyChange(PropertyChangeEvent evt)
1235 String eventName = evt.getPropertyName();
1237 if (eventName.equals(SequenceGroup.SEQ_GROUP_CHANGED))
1241 else if (av.getWrapAlignment())
1243 if (eventName.equals(ViewportRanges.STARTRES))
1251 if (eventName.equals(ViewportRanges.STARTRES))
1253 // Make sure we're not trying to draw a panel
1254 // larger than the visible window
1255 ViewportRanges vpRanges = av.getRanges();
1256 scrollX = (int) evt.getNewValue() - (int) evt.getOldValue();
1257 int range = vpRanges.getEndRes() - vpRanges.getStartRes();
1258 if (scrollX > range)
1262 else if (scrollX < -range)
1268 // Both scrolling and resizing change viewport ranges: scrolling changes
1269 // both start and end points, but resize only changes end values.
1270 // Here we only want to fastpaint on a scroll, with resize using a normal
1271 // paint, so scroll events are identified as changes to the horizontal or
1272 // vertical start value.
1273 if (eventName.equals(ViewportRanges.STARTRES))
1275 // scroll - startres and endres both change
1276 fastPaint(scrollX, 0);
1278 else if (eventName.equals(ViewportRanges.STARTSEQ))
1281 fastPaint(0, (int) evt.getNewValue() - (int) evt.getOldValue());