2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.SearchResults;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
28 import java.awt.BasicStroke;
29 import java.awt.BorderLayout;
30 import java.awt.Color;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
33 import java.awt.Graphics2D;
34 import java.awt.RenderingHints;
35 import java.awt.Shape;
36 import java.awt.image.BufferedImage;
38 import javax.swing.JComponent;
46 public class SeqCanvas extends JComponent
48 final FeatureRenderer fr;
50 final SequenceRenderer sr;
62 SearchResults searchResults = null;
64 boolean fastPaint = false;
75 * Creates a new SeqCanvas object.
80 public SeqCanvas(AlignmentPanel ap)
83 fr = new FeatureRenderer(ap);
84 sr = new SequenceRenderer(av);
85 setLayout(new BorderLayout());
86 PaintRefresher.Register(this, av.getSequenceSetId());
87 setBackground(Color.white);
90 public SequenceRenderer getSequenceRenderer()
95 public FeatureRenderer getFeatureRenderer()
112 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
114 int scalestartx = startx - (startx % 10) + 10;
116 g.setColor(Color.black);
119 for (int i = scalestartx; i < endx; i += 10)
122 if (av.hasHiddenColumns())
124 value = av.getColumnSelection().adjustForHiddenColumns(value);
127 g.drawString(String.valueOf(value), (i - startx - 1) * av.charWidth,
128 ypos - (av.charHeight / 2));
130 g.drawLine(((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
131 (ypos + 2) - (av.charHeight / 2),
132 ((i - startx - 1) * av.charWidth) + (av.charWidth / 2),
149 void drawWestScale(Graphics g, int startx, int endx, int ypos)
151 FontMetrics fm = getFontMetrics(av.getFont());
152 ypos += av.charHeight;
154 if (av.hasHiddenColumns())
156 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
157 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
160 int maxwidth = av.getAlignment().getWidth();
161 if (av.hasHiddenColumns())
163 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
167 for (int i = 0; i < av.getAlignment().getHeight(); i++)
169 SequenceI seq = av.getAlignment().getSequenceAt(i);
175 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
182 value = av.getAlignment().getSequenceAt(i).findPosition(index);
189 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
191 g.drawString(value + "", x, (ypos + (i * av.charHeight))
192 - (av.charHeight / 5));
209 void drawEastScale(Graphics g, int startx, int endx, int ypos)
211 ypos += av.charHeight;
213 if (av.hasHiddenColumns())
215 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
220 for (int i = 0; i < av.getAlignment().getHeight(); i++)
222 seq = av.getAlignment().getSequenceAt(i);
226 while (index > startx)
228 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
235 value = seq.findPosition(index);
242 g.drawString(String.valueOf(value), 0, (ypos + (i * av.charHeight))
243 - (av.charHeight / 5));
248 boolean fastpainting = false;
251 * need to make this thread safe move alignment rendering in response to
257 * shift up or down in repaint
259 public void fastPaint(int horizontal, int vertical)
261 if (fastpainting || gg == null)
268 gg.copyArea(horizontal * av.charWidth, vertical * av.charHeight,
269 imgWidth, imgHeight, -horizontal * av.charWidth, -vertical
272 int sr = av.startRes;
274 int ss = av.startSeq;
279 if (horizontal > 0) // scrollbar pulled right, image to the left
282 transX = (er - sr - horizontal) * av.charWidth;
283 sr = er - horizontal;
285 else if (horizontal < 0)
287 er = sr - horizontal - 1;
289 else if (vertical > 0) // scroll down
293 if (ss < av.startSeq)
294 { // ie scrolling too fast, more than a page at a time
299 transY = imgHeight - (vertical * av.charHeight);
302 else if (vertical < 0)
312 gg.translate(transX, transY);
313 drawPanel(gg, sr, er, ss, es, 0);
314 gg.translate(-transX, -transY);
317 fastpainting = false;
321 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
322 * towards! startx is the first residue (starting at 0) to display. endx is
323 * the last residue to display (starting at 0). starty is the first sequence
324 * to display (starting at 0). endy is the last sequence to display (starting
325 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
326 * adjustment listener in SeqPanel when the scrollbars move.
329 // Set this to false to force a full panel paint
330 public void paintComponent(Graphics g)
332 BufferedImage lcimg = img; // take reference since other threads may null
333 // img and call later.
334 super.paintComponent(g);
338 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
339 .getClipBounds().height)))
341 g.drawImage(lcimg, 0, 0, this);
346 // this draws the whole of the alignment
347 imgWidth = getWidth();
348 imgHeight = getHeight();
350 imgWidth -= (imgWidth % av.charWidth);
351 imgHeight -= (imgHeight % av.charHeight);
353 if ((imgWidth < 1) || (imgHeight < 1))
358 if (lcimg == null || imgWidth != lcimg.getWidth()
359 || imgHeight != lcimg.getHeight())
363 lcimg = img = new BufferedImage(imgWidth, imgHeight,
364 BufferedImage.TYPE_INT_RGB);
365 gg = (Graphics2D) img.getGraphics();
366 gg.setFont(av.getFont());
367 } catch (OutOfMemoryError er)
370 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
371 new OOMWarning("Creating alignment image for display", er);
379 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
380 RenderingHints.VALUE_ANTIALIAS_ON);
383 gg.setColor(Color.white);
384 gg.fillRect(0, 0, imgWidth, imgHeight);
386 if (av.getWrapAlignment())
388 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
392 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
395 g.drawImage(lcimg, 0, 0, this);
405 * @return DOCUMENT ME!
407 public int getWrappedCanvasWidth(int cwidth)
409 FontMetrics fm = getFontMetrics(av.getFont());
414 if (av.scaleRightWrapped)
416 LABEL_EAST = fm.stringWidth(getMask());
419 if (av.scaleLeftWrapped)
421 LABEL_WEST = fm.stringWidth(getMask());
424 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
428 * Generates a string of zeroes.
437 for (int i = 0; i < av.getAlignment().getHeight(); i++)
439 tmp = av.getAlignment().getSequenceAt(i).getEnd();
446 for (int i = maxWidth; i > 0; i /= 10)
460 * @param canvasHeight
465 public void drawWrappedPanel(Graphics g, int canvasWidth,
466 int canvasHeight, int startRes)
468 AlignmentI al = av.getAlignment();
470 FontMetrics fm = getFontMetrics(av.getFont());
472 if (av.scaleRightWrapped)
474 LABEL_EAST = fm.stringWidth(getMask());
477 if (av.scaleLeftWrapped)
479 LABEL_WEST = fm.stringWidth(getMask());
482 int hgap = av.charHeight;
483 if (av.scaleAboveWrapped)
485 hgap += av.charHeight;
488 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
489 int cHeight = av.getAlignment().getHeight() * av.charHeight;
491 av.setWrappedWidth(cWidth);
493 av.endRes = av.startRes + cWidth;
497 int maxwidth = av.getAlignment().getWidth() - 1;
499 if (av.hasHiddenColumns())
501 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
504 while ((ypos <= canvasHeight) && (startRes < maxwidth))
506 endx = startRes + cWidth - 1;
513 g.setFont(av.getFont());
514 g.setColor(Color.black);
516 if (av.scaleLeftWrapped)
518 drawWestScale(g, startRes, endx, ypos);
521 if (av.scaleRightWrapped)
523 g.translate(canvasWidth - LABEL_EAST, 0);
524 drawEastScale(g, startRes, endx, ypos);
525 g.translate(-(canvasWidth - LABEL_EAST), 0);
528 g.translate(LABEL_WEST, 0);
530 if (av.scaleAboveWrapped)
532 drawNorthScale(g, startRes, endx, ypos);
535 if (av.hasHiddenColumns() && av.showHiddenMarkers)
537 g.setColor(Color.blue);
539 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
542 res = av.getColumnSelection().findHiddenRegionPosition(i)
545 if (res < 0 || res > endx - startRes)
550 gg.fillPolygon(new int[]
551 { res * av.charWidth - av.charHeight / 4,
552 res * av.charWidth + av.charHeight / 4, res * av.charWidth },
554 { ypos - (av.charHeight / 2), ypos - (av.charHeight / 2),
555 ypos - (av.charHeight / 2) + 8 }, 3);
560 // When printing we have an extra clipped region,
561 // the Printable page which we need to account for here
562 Shape clip = g.getClip();
566 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
570 g.setClip(0, (int) clip.getBounds().getY(), cWidth * av.charWidth,
571 (int) clip.getBounds().getHeight());
574 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
576 if (av.isShowAnnotation())
578 g.translate(0, cHeight + ypos + 3);
579 if (annotations == null)
581 annotations = new AnnotationPanel(av);
584 annotations.renderer.drawComponent(annotations, av, g,
585 -1, startRes, endx + 1);
586 g.translate(0, -cHeight - ypos - 3);
589 g.translate(-LABEL_WEST, 0);
591 ypos += cHeight + getAnnotationHeight() + hgap;
597 AnnotationPanel annotations;
599 int getAnnotationHeight()
601 if (!av.isShowAnnotation())
606 if (annotations == null)
608 annotations = new AnnotationPanel(av);
611 return annotations.adjustPanelHeight();
630 public void drawPanel(Graphics g1, int startRes, int endRes,
632 int endSeq, int offset)
634 if (!av.hasHiddenColumns())
636 draw(g1, startRes, endRes, startSeq, endSeq, offset);
640 java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
643 int blockStart = startRes;
644 int blockEnd = endRes;
646 for (int i = 0; regions != null && i < regions.size(); i++)
648 int[] region = (int[]) regions.elementAt(i);
649 int hideStart = region[0];
650 int hideEnd = region[1];
652 if (hideStart <= blockStart)
654 blockStart += (hideEnd - hideStart) + 1;
658 blockEnd = hideStart - 1;
660 g1.translate(screenY * av.charWidth, 0);
662 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
664 if (av.getShowHiddenMarkers())
666 g1.setColor(Color.blue);
668 g1.drawLine((blockEnd - blockStart + 1) * av.charWidth - 1,
669 0 + offset, (blockEnd - blockStart + 1) * av.charWidth
670 - 1, (endSeq - startSeq) * av.charHeight + offset);
673 g1.translate(-screenY * av.charWidth, 0);
674 screenY += blockEnd - blockStart + 1;
675 blockStart = hideEnd + 1;
678 if (screenY <= (endRes - startRes))
680 blockEnd = blockStart + (endRes - startRes) - screenY;
681 g1.translate(screenY * av.charWidth, 0);
682 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
684 g1.translate(-screenY * av.charWidth, 0);
690 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
691 // int x1, int x2, int y1, int y2, int startx, int starty,
692 void draw(Graphics g, int startRes, int endRes, int startSeq, int endSeq,
695 g.setFont(av.getFont());
696 sr.prepare(g, av.renderGaps);
700 // / First draw the sequences
701 // ///////////////////////////
702 for (int i = startSeq; i < endSeq; i++)
704 nextSeq = av.getAlignment().getSequenceAt(i);
707 // occasionally, a race condition occurs such that the alignment row is
711 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
712 startRes, endRes, offset + ((i - startSeq) * av.charHeight));
714 if (av.showSequenceFeatures)
716 fr.drawSequence(g, nextSeq, startRes, endRes, offset
717 + ((i - startSeq) * av.charHeight));
720 // / Highlight search Results once all sequences have been drawn
721 // ////////////////////////////////////////////////////////
722 if (searchResults != null)
724 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
726 if (visibleResults != null)
728 for (int r = 0; r < visibleResults.length; r += 2)
730 sr.drawHighlightedText(nextSeq, visibleResults[r],
731 visibleResults[r + 1], (visibleResults[r] - startRes)
732 * av.charWidth, offset
733 + ((i - startSeq) * av.charHeight));
738 if (av.cursorMode && cursorY == i && cursorX >= startRes
739 && cursorX <= endRes)
741 sr.drawCursor(nextSeq, cursorX,
742 (cursorX - startRes) * av.charWidth, offset
743 + ((i - startSeq) * av.charHeight));
747 if (av.getSelectionGroup() != null
748 || av.getAlignment().getGroups().size() > 0)
750 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
755 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
756 int startSeq, int endSeq, int offset)
758 Graphics2D g = (Graphics2D) g1;
760 // ///////////////////////////////////
761 // Now outline any areas if necessary
762 // ///////////////////////////////////
763 SequenceGroup group = av.getSelectionGroup();
769 int visWidth = (endRes - startRes + 1) * av.charWidth;
771 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
773 group = av.getAlignment().getGroups().get(0);
783 boolean inGroup = false;
787 for (i = startSeq; i < endSeq; i++)
789 sx = (group.getStartRes() - startRes) * av.charWidth;
790 sy = offset + ((i - startSeq) * av.charHeight);
791 ex = (((group.getEndRes() + 1) - group.getStartRes()) * av.charWidth) - 1;
793 if (sx + ex < 0 || sx > visWidth)
798 if ((sx <= (endRes - startRes) * av.charWidth)
799 && group.getSequences(null).contains(
800 av.getAlignment().getSequenceAt(i)))
803 && !group.getSequences(null).contains(
804 av.getAlignment().getSequenceAt(i + 1)))
806 bottom = sy + av.charHeight;
811 if (((top == -1) && (i == 0))
812 || !group.getSequences(null).contains(
813 av.getAlignment().getSequenceAt(i - 1)))
821 if (group == av.getSelectionGroup())
823 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
824 BasicStroke.JOIN_ROUND, 3f, new float[]
826 g.setColor(Color.RED);
830 g.setStroke(new BasicStroke());
831 g.setColor(group.getOutlineColour());
839 if (sx >= 0 && sx < visWidth)
841 g.drawLine(sx, oldY, sx, sy);
844 if (sx + ex < visWidth)
846 g.drawLine(sx + ex, oldY, sx + ex, sy);
855 if (sx + ex > visWidth)
860 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
862 ex = (endRes - startRes + 1) * av.charWidth;
867 g.drawLine(sx, top, sx + ex, top);
873 g.drawLine(sx, bottom, sx + ex, bottom);
884 sy = offset + ((i - startSeq) * av.charHeight);
885 if (sx >= 0 && sx < visWidth)
887 g.drawLine(sx, oldY, sx, sy);
890 if (sx + ex < visWidth)
892 g.drawLine(sx + ex, oldY, sx + ex, sy);
901 if (sx + ex > visWidth)
905 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
907 ex = (endRes - startRes + 1) * av.charWidth;
912 g.drawLine(sx, top, sx + ex, top);
918 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
927 g.setStroke(new BasicStroke());
929 if (groupIndex >= av.getAlignment().getGroups().size())
934 group = av.getAlignment().getGroups()
937 } while (groupIndex < av.getAlignment().getGroups().size());
949 public void highlightSearchResults(SearchResults results)
953 searchResults = results;