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.SearchResults;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.util.ColorUtils;
29 import java.awt.BasicStroke;
30 import java.awt.BorderLayout;
31 import java.awt.Color;
32 import java.awt.FontMetrics;
33 import java.awt.Graphics;
34 import java.awt.Graphics2D;
35 import java.awt.RenderingHints;
36 import java.awt.Shape;
37 import java.awt.image.BufferedImage;
38 import java.util.List;
40 import javax.swing.JComponent;
48 public class SeqCanvas extends JComponent
50 final FeatureRenderer fr;
52 final SequenceRenderer sr;
64 SearchResults searchResults = null;
66 boolean fastPaint = false;
77 * Creates a new SeqCanvas object.
82 public SeqCanvas(AlignmentPanel ap)
86 fr = new FeatureRenderer(ap);
87 sr = new SequenceRenderer(av);
88 setLayout(new BorderLayout());
89 PaintRefresher.Register(this, av.getSequenceSetId());
90 setBackground(Color.white);
93 public SequenceRenderer getSequenceRenderer()
98 public FeatureRenderer getFeatureRenderer()
103 int charHeight = 0, charWidth = 0;
105 private void updateViewport()
107 charHeight = av.getCharHeight();
108 charWidth = av.getCharWidth();
123 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
126 int scalestartx = startx - (startx % 10) + 10;
128 g.setColor(Color.black);
130 for (int i = scalestartx; i < endx; i += 10)
133 if (av.hasHiddenColumns())
135 value = av.getColumnSelection().adjustForHiddenColumns(value);
138 g.drawString(String.valueOf(value), (i - startx - 1) * charWidth,
139 ypos - (charHeight / 2));
141 g.drawLine(((i - startx - 1) * charWidth) + (charWidth / 2),
142 (ypos + 2) - (charHeight / 2), ((i - startx - 1) * charWidth)
143 + (charWidth / 2), ypos - 2);
159 void drawWestScale(Graphics g, int startx, int endx, int ypos)
161 FontMetrics fm = getFontMetrics(av.getFont());
164 if (av.hasHiddenColumns())
166 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
167 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
170 int maxwidth = av.getAlignment().getWidth();
171 if (av.hasHiddenColumns())
173 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
177 for (int i = 0; i < av.getAlignment().getHeight(); i++)
179 SequenceI seq = av.getAlignment().getSequenceAt(i);
185 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
192 value = av.getAlignment().getSequenceAt(i).findPosition(index);
199 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
201 g.drawString(value + "", x, (ypos + (i * charHeight))
219 void drawEastScale(Graphics g, int startx, int endx, int ypos)
223 if (av.hasHiddenColumns())
225 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
230 for (int i = 0; i < av.getAlignment().getHeight(); i++)
232 seq = av.getAlignment().getSequenceAt(i);
236 while (index > startx)
238 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
245 value = seq.findPosition(index);
252 g.drawString(String.valueOf(value), 0, (ypos + (i * charHeight))
258 boolean fastpainting = false;
261 * need to make this thread safe move alignment rendering in response to
267 * shift up or down in repaint
269 public void fastPaint(int horizontal, int vertical)
271 if (fastpainting || gg == null)
278 gg.copyArea(horizontal * charWidth, vertical * charHeight, imgWidth,
279 imgHeight, -horizontal * charWidth, -vertical * charHeight);
281 int sr = av.startRes;
283 int ss = av.startSeq;
288 if (horizontal > 0) // scrollbar pulled right, image to the left
291 transX = (er - sr - horizontal) * charWidth;
292 sr = er - horizontal;
294 else if (horizontal < 0)
296 er = sr - horizontal - 1;
298 else if (vertical > 0) // scroll down
302 if (ss < av.startSeq)
303 { // ie scrolling too fast, more than a page at a time
308 transY = imgHeight - (vertical * charHeight);
311 else if (vertical < 0)
321 gg.translate(transX, transY);
322 drawPanel(gg, sr, er, ss, es, 0);
323 gg.translate(-transX, -transY);
326 fastpainting = false;
330 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
331 * towards! startx is the first residue (starting at 0) to display. endx is
332 * the last residue to display (starting at 0). starty is the first sequence
333 * to display (starting at 0). endy is the last sequence to display (starting
334 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
335 * adjustment listener in SeqPanel when the scrollbars move.
338 // Set this to false to force a full panel paint
340 public void paintComponent(Graphics g)
343 BufferedImage lcimg = img; // take reference since other threads may null
344 // img and call later.
345 super.paintComponent(g);
349 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
350 .getClipBounds().height)))
352 g.drawImage(lcimg, 0, 0, this);
357 // this draws the whole of the alignment
358 imgWidth = getWidth();
359 imgHeight = getHeight();
361 imgWidth -= (imgWidth % charWidth);
362 imgHeight -= (imgHeight % charHeight);
364 if ((imgWidth < 1) || (imgHeight < 1))
369 if (lcimg == null || imgWidth != lcimg.getWidth()
370 || imgHeight != lcimg.getHeight())
374 lcimg = img = new BufferedImage(imgWidth, imgHeight,
375 BufferedImage.TYPE_INT_RGB);
376 gg = (Graphics2D) img.getGraphics();
377 gg.setFont(av.getFont());
378 } catch (OutOfMemoryError er)
381 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
382 new OOMWarning("Creating alignment image for display", er);
390 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
391 RenderingHints.VALUE_ANTIALIAS_ON);
394 gg.setColor(Color.white);
395 gg.fillRect(0, 0, imgWidth, imgHeight);
397 if (av.getWrapAlignment())
399 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
403 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
406 g.drawImage(lcimg, 0, 0, this);
416 * @return DOCUMENT ME!
418 public int getWrappedCanvasWidth(int cwidth)
420 FontMetrics fm = getFontMetrics(av.getFont());
425 if (av.getScaleRightWrapped())
427 LABEL_EAST = fm.stringWidth(getMask());
430 if (av.getScaleLeftWrapped())
432 LABEL_WEST = fm.stringWidth(getMask());
435 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
439 * Generates a string of zeroes.
448 for (int i = 0; i < av.getAlignment().getHeight(); i++)
450 tmp = av.getAlignment().getSequenceAt(i).getEnd();
457 for (int i = maxWidth; i > 0; i /= 10)
471 * @param canvasHeight
476 public void drawWrappedPanel(Graphics g, int canvasWidth,
477 int canvasHeight, int startRes)
480 AlignmentI al = av.getAlignment();
482 FontMetrics fm = getFontMetrics(av.getFont());
484 if (av.getScaleRightWrapped())
486 LABEL_EAST = fm.stringWidth(getMask());
489 if (av.getScaleLeftWrapped())
491 LABEL_WEST = fm.stringWidth(getMask());
494 int hgap = charHeight;
495 if (av.getScaleAboveWrapped())
500 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
501 int cHeight = av.getAlignment().getHeight() * charHeight;
503 av.setWrappedWidth(cWidth);
505 av.endRes = av.startRes + cWidth;
509 int maxwidth = av.getAlignment().getWidth() - 1;
511 if (av.hasHiddenColumns())
513 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
516 while ((ypos <= canvasHeight) && (startRes < maxwidth))
518 endx = startRes + cWidth - 1;
525 g.setFont(av.getFont());
526 g.setColor(Color.black);
528 if (av.getScaleLeftWrapped())
530 drawWestScale(g, startRes, endx, ypos);
533 if (av.getScaleRightWrapped())
535 g.translate(canvasWidth - LABEL_EAST, 0);
536 drawEastScale(g, startRes, endx, ypos);
537 g.translate(-(canvasWidth - LABEL_EAST), 0);
540 g.translate(LABEL_WEST, 0);
542 if (av.getScaleAboveWrapped())
544 drawNorthScale(g, startRes, endx, ypos);
547 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
549 g.setColor(Color.blue);
551 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
554 res = av.getColumnSelection().findHiddenRegionPosition(i)
557 if (res < 0 || res > endx - startRes)
563 new int[] { res * charWidth - charHeight / 4,
564 res * charWidth + charHeight / 4, res * charWidth },
565 new int[] { ypos - (charHeight / 2),
566 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
572 // When printing we have an extra clipped region,
573 // the Printable page which we need to account for here
574 Shape clip = g.getClip();
578 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
582 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
583 (int) clip.getBounds().getHeight());
586 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
588 if (av.isShowAnnotation())
590 g.translate(0, cHeight + ypos + 3);
591 if (annotations == null)
593 annotations = new AnnotationPanel(av);
596 annotations.renderer.drawComponent(annotations, av, g, -1,
598 g.translate(0, -cHeight - ypos - 3);
601 g.translate(-LABEL_WEST, 0);
603 ypos += cHeight + getAnnotationHeight() + hgap;
609 AnnotationPanel annotations;
611 int getAnnotationHeight()
613 if (!av.isShowAnnotation())
618 if (annotations == null)
620 annotations = new AnnotationPanel(av);
623 return annotations.adjustPanelHeight();
642 public void drawPanel(Graphics g1, int startRes, int endRes,
643 int startSeq, int endSeq, int offset)
646 if (!av.hasHiddenColumns())
648 draw(g1, startRes, endRes, startSeq, endSeq, offset);
652 List<int[]> regions = av.getColumnSelection().getHiddenColumns();
655 int blockStart = startRes;
656 int blockEnd = endRes;
658 for (int[] region : regions)
660 int hideStart = region[0];
661 int hideEnd = region[1];
663 if (hideStart <= blockStart)
665 blockStart += (hideEnd - hideStart) + 1;
669 blockEnd = hideStart - 1;
671 g1.translate(screenY * charWidth, 0);
673 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
675 if (av.getShowHiddenMarkers())
677 g1.setColor(Color.blue);
679 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
680 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
681 (endSeq - startSeq) * charHeight + offset);
684 g1.translate(-screenY * charWidth, 0);
685 screenY += blockEnd - blockStart + 1;
686 blockStart = hideEnd + 1;
688 if (screenY > (endRes - startRes))
690 // already rendered last block
695 if (screenY <= (endRes - startRes))
697 // remaining visible region to render
698 blockEnd = blockStart + (endRes - startRes) - screenY;
699 g1.translate(screenY * charWidth, 0);
700 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
702 g1.translate(-screenY * charWidth, 0);
708 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
709 // int x1, int x2, int y1, int y2, int startx, int starty,
710 private void draw(Graphics g, int startRes, int endRes, int startSeq,
711 int endSeq, int offset)
713 g.setFont(av.getFont());
714 sr.prepare(g, av.isRenderGaps());
718 // / First draw the sequences
719 // ///////////////////////////
720 for (int i = startSeq; i < endSeq; i++)
722 nextSeq = av.getAlignment().getSequenceAt(i);
725 // occasionally, a race condition occurs such that the alignment row is
729 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
730 startRes, endRes, offset + ((i - startSeq) * charHeight));
732 if (av.isShowSequenceFeatures())
734 fr.drawSequence(g, nextSeq, startRes, endRes, offset
735 + ((i - startSeq) * charHeight));
738 // / Highlight search Results once all sequences have been drawn
739 // ////////////////////////////////////////////////////////
740 if (searchResults != null)
742 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
744 if (visibleResults != null)
746 for (int r = 0; r < visibleResults.length; r += 2)
748 sr.drawHighlightedText(nextSeq, visibleResults[r],
749 visibleResults[r + 1], (visibleResults[r] - startRes)
751 + ((i - startSeq) * charHeight));
756 if (av.cursorMode && cursorY == i && cursorX >= startRes
757 && cursorX <= endRes)
759 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
760 offset + ((i - startSeq) * charHeight));
764 if (av.getSelectionGroup() != null
765 || av.getAlignment().getGroups().size() > 0)
767 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
772 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
773 int startSeq, int endSeq, int offset)
775 Graphics2D g = (Graphics2D) g1;
777 // ///////////////////////////////////
778 // Now outline any areas if necessary
779 // ///////////////////////////////////
780 SequenceGroup group = av.getSelectionGroup();
786 int visWidth = (endRes - startRes + 1) * charWidth;
788 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
790 group = av.getAlignment().getGroups().get(0);
800 boolean inGroup = false;
804 for (i = startSeq; i < endSeq; i++)
806 sx = (group.getStartRes() - startRes) * charWidth;
807 sy = offset + ((i - startSeq) * charHeight);
808 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
810 if (sx + ex < 0 || sx > visWidth)
815 if ((sx <= (endRes - startRes) * charWidth)
816 && group.getSequences(null).contains(
817 av.getAlignment().getSequenceAt(i)))
820 && !group.getSequences(null).contains(
821 av.getAlignment().getSequenceAt(i + 1)))
823 bottom = sy + charHeight;
828 if (((top == -1) && (i == 0))
829 || !group.getSequences(null).contains(
830 av.getAlignment().getSequenceAt(i - 1)))
838 if (group == av.getSelectionGroup())
840 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
841 BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f },
843 g.setColor(Color.RED);
847 g.setStroke(new BasicStroke());
848 g.setColor(ColorUtils.getColor(group.getOutlineColour()));
856 if (sx >= 0 && sx < visWidth)
858 g.drawLine(sx, oldY, sx, sy);
861 if (sx + ex < visWidth)
863 g.drawLine(sx + ex, oldY, sx + ex, sy);
872 if (sx + ex > visWidth)
877 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
879 ex = (endRes - startRes + 1) * charWidth;
884 g.drawLine(sx, top, sx + ex, top);
890 g.drawLine(sx, bottom, sx + ex, bottom);
901 sy = offset + ((i - startSeq) * charHeight);
902 if (sx >= 0 && sx < visWidth)
904 g.drawLine(sx, oldY, sx, sy);
907 if (sx + ex < visWidth)
909 g.drawLine(sx + ex, oldY, sx + ex, sy);
918 if (sx + ex > visWidth)
922 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
924 ex = (endRes - startRes + 1) * charWidth;
929 g.drawLine(sx, top, sx + ex, top);
935 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
944 g.setStroke(new BasicStroke());
946 if (groupIndex >= av.getAlignment().getGroups().size())
951 group = av.getAlignment().getGroups().get(groupIndex);
953 } while (groupIndex < av.getAlignment().getGroups().size());
965 public void highlightSearchResults(SearchResults results)
969 searchResults = results;