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.renderer.ScaleRenderer;
28 import jalview.renderer.ScaleRenderer.ScaleMark;
30 import java.awt.BasicStroke;
31 import java.awt.BorderLayout;
32 import java.awt.Color;
33 import java.awt.FontMetrics;
34 import java.awt.Graphics;
35 import java.awt.Graphics2D;
36 import java.awt.RenderingHints;
37 import java.awt.Shape;
38 import java.awt.image.BufferedImage;
39 import java.util.List;
41 import javax.swing.JComponent;
49 public class SeqCanvas extends JComponent
51 final FeatureRenderer fr;
53 final SequenceRenderer sr;
65 SearchResults searchResults = null;
67 boolean fastPaint = false;
78 * Creates a new SeqCanvas object.
83 public SeqCanvas(AlignmentPanel ap)
87 fr = new FeatureRenderer(ap);
88 sr = new SequenceRenderer(av);
89 setLayout(new BorderLayout());
90 PaintRefresher.Register(this, av.getSequenceSetId());
91 setBackground(Color.white);
94 public SequenceRenderer getSequenceRenderer()
99 public FeatureRenderer getFeatureRenderer()
104 int charHeight = 0, charWidth = 0;
106 private void updateViewport()
108 charHeight = av.getCharHeight();
109 charWidth = av.getCharWidth();
124 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
127 for (ScaleMark mark : new ScaleRenderer().calculateMarks(av, startx,
130 int mpos = mark.column; // (i - startx - 1)
135 String mstring = mark.text;
141 g.drawString(mstring, mpos * charWidth, ypos - (charHeight / 2));
143 g.drawLine((mpos * charWidth) + (charWidth / 2), (ypos + 2)
144 - (charHeight / 2), (mpos * charWidth) + (charWidth / 2),
162 void drawWestScale(Graphics g, int startx, int endx, int ypos)
164 FontMetrics fm = getFontMetrics(av.getFont());
167 if (av.hasHiddenColumns())
169 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
170 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
173 int maxwidth = av.getAlignment().getWidth();
174 if (av.hasHiddenColumns())
176 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
180 for (int i = 0; i < av.getAlignment().getHeight(); i++)
182 SequenceI seq = av.getAlignment().getSequenceAt(i);
188 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
195 value = av.getAlignment().getSequenceAt(i).findPosition(index);
202 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
204 g.drawString(value + "", x, (ypos + (i * charHeight))
222 void drawEastScale(Graphics g, int startx, int endx, int ypos)
226 if (av.hasHiddenColumns())
228 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
233 for (int i = 0; i < av.getAlignment().getHeight(); i++)
235 seq = av.getAlignment().getSequenceAt(i);
239 while (index > startx)
241 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
248 value = seq.findPosition(index);
255 g.drawString(String.valueOf(value), 0, (ypos + (i * charHeight))
261 boolean fastpainting = false;
264 * need to make this thread safe move alignment rendering in response to
270 * shift up or down in repaint
272 public void fastPaint(int horizontal, int vertical)
274 if (fastpainting || gg == null)
281 gg.copyArea(horizontal * charWidth, vertical * charHeight, imgWidth,
282 imgHeight, -horizontal * charWidth, -vertical * charHeight);
284 int sr = av.startRes;
286 int ss = av.startSeq;
291 if (horizontal > 0) // scrollbar pulled right, image to the left
294 transX = (er - sr - horizontal) * charWidth;
295 sr = er - horizontal;
297 else if (horizontal < 0)
299 er = sr - horizontal - 1;
301 else if (vertical > 0) // scroll down
305 if (ss < av.startSeq)
306 { // ie scrolling too fast, more than a page at a time
311 transY = imgHeight - (vertical * charHeight);
314 else if (vertical < 0)
324 gg.translate(transX, transY);
325 drawPanel(gg, sr, er, ss, es, 0);
326 gg.translate(-transX, -transY);
329 fastpainting = false;
333 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
334 * towards! startx is the first residue (starting at 0) to display. endx is
335 * the last residue to display (starting at 0). starty is the first sequence
336 * to display (starting at 0). endy is the last sequence to display (starting
337 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
338 * adjustment listener in SeqPanel when the scrollbars move.
341 // Set this to false to force a full panel paint
343 public void paintComponent(Graphics g)
346 BufferedImage lcimg = img; // take reference since other threads may null
347 // img and call later.
348 super.paintComponent(g);
352 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
353 .getClipBounds().height)))
355 g.drawImage(lcimg, 0, 0, this);
360 // this draws the whole of the alignment
361 imgWidth = getWidth();
362 imgHeight = getHeight();
364 imgWidth -= (imgWidth % charWidth);
365 imgHeight -= (imgHeight % charHeight);
367 if ((imgWidth < 1) || (imgHeight < 1))
372 if (lcimg == null || imgWidth != lcimg.getWidth()
373 || imgHeight != lcimg.getHeight())
377 lcimg = img = new BufferedImage(imgWidth, imgHeight,
378 BufferedImage.TYPE_INT_RGB);
379 gg = (Graphics2D) img.getGraphics();
380 gg.setFont(av.getFont());
381 } catch (OutOfMemoryError er)
384 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
385 new OOMWarning("Creating alignment image for display", er);
393 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
394 RenderingHints.VALUE_ANTIALIAS_ON);
397 gg.setColor(Color.white);
398 gg.fillRect(0, 0, imgWidth, imgHeight);
400 if (av.getWrapAlignment())
402 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
406 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
409 g.drawImage(lcimg, 0, 0, this);
419 * @return DOCUMENT ME!
421 public int getWrappedCanvasWidth(int cwidth)
423 FontMetrics fm = getFontMetrics(av.getFont());
428 if (av.getScaleRightWrapped())
430 LABEL_EAST = fm.stringWidth(getMask());
433 if (av.getScaleLeftWrapped())
435 LABEL_WEST = fm.stringWidth(getMask());
438 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
442 * Generates a string of zeroes.
451 for (int i = 0; i < av.getAlignment().getHeight(); i++)
453 tmp = av.getAlignment().getSequenceAt(i).getEnd();
460 for (int i = maxWidth; i > 0; i /= 10)
474 * @param canvasHeight
479 public void drawWrappedPanel(Graphics g, int canvasWidth,
480 int canvasHeight, int startRes)
483 AlignmentI al = av.getAlignment();
485 FontMetrics fm = getFontMetrics(av.getFont());
487 if (av.getScaleRightWrapped())
489 LABEL_EAST = fm.stringWidth(getMask());
492 if (av.getScaleLeftWrapped())
494 LABEL_WEST = fm.stringWidth(getMask());
497 int hgap = charHeight;
498 if (av.getScaleAboveWrapped())
503 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
504 int cHeight = av.getAlignment().getHeight() * charHeight;
506 av.setWrappedWidth(cWidth);
508 av.endRes = av.startRes + cWidth;
512 int maxwidth = av.getAlignment().getWidth() - 1;
514 if (av.hasHiddenColumns())
516 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
519 while ((ypos <= canvasHeight) && (startRes < maxwidth))
521 endx = startRes + cWidth - 1;
528 g.setFont(av.getFont());
529 g.setColor(Color.black);
531 if (av.getScaleLeftWrapped())
533 drawWestScale(g, startRes, endx, ypos);
536 if (av.getScaleRightWrapped())
538 g.translate(canvasWidth - LABEL_EAST, 0);
539 drawEastScale(g, startRes, endx, ypos);
540 g.translate(-(canvasWidth - LABEL_EAST), 0);
543 g.translate(LABEL_WEST, 0);
545 if (av.getScaleAboveWrapped())
547 drawNorthScale(g, startRes, endx, ypos);
550 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
552 g.setColor(Color.blue);
554 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
557 res = av.getColumnSelection().findHiddenRegionPosition(i)
560 if (res < 0 || res > endx - startRes)
566 new int[] { res * charWidth - charHeight / 4,
567 res * charWidth + charHeight / 4, res * charWidth },
568 new int[] { ypos - (charHeight / 2),
569 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
575 // When printing we have an extra clipped region,
576 // the Printable page which we need to account for here
577 Shape clip = g.getClip();
581 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
585 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
586 (int) clip.getBounds().getHeight());
589 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
591 if (av.isShowAnnotation())
593 g.translate(0, cHeight + ypos + 3);
594 if (annotations == null)
596 annotations = new AnnotationPanel(av);
599 annotations.renderer.drawComponent(annotations, av, g, -1,
601 g.translate(0, -cHeight - ypos - 3);
604 g.translate(-LABEL_WEST, 0);
606 ypos += cHeight + getAnnotationHeight() + hgap;
612 AnnotationPanel annotations;
614 int getAnnotationHeight()
616 if (!av.isShowAnnotation())
621 if (annotations == null)
623 annotations = new AnnotationPanel(av);
626 return annotations.adjustPanelHeight();
645 public void drawPanel(Graphics g1, int startRes, int endRes,
646 int startSeq, int endSeq, int offset)
649 if (!av.hasHiddenColumns())
651 draw(g1, startRes, endRes, startSeq, endSeq, offset);
655 List<int[]> regions = av.getColumnSelection().getHiddenColumns();
658 int blockStart = startRes;
659 int blockEnd = endRes;
661 for (int[] region : regions)
663 int hideStart = region[0];
664 int hideEnd = region[1];
666 if (hideStart <= blockStart)
668 blockStart += (hideEnd - hideStart) + 1;
672 blockEnd = hideStart - 1;
674 g1.translate(screenY * charWidth, 0);
676 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
678 if (av.getShowHiddenMarkers())
680 g1.setColor(Color.blue);
682 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
683 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
684 (endSeq - startSeq) * charHeight + offset);
687 g1.translate(-screenY * charWidth, 0);
688 screenY += blockEnd - blockStart + 1;
689 blockStart = hideEnd + 1;
691 if (screenY > (endRes - startRes))
693 // already rendered last block
698 if (screenY <= (endRes - startRes))
700 // remaining visible region to render
701 blockEnd = blockStart + (endRes - startRes) - screenY;
702 g1.translate(screenY * charWidth, 0);
703 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
705 g1.translate(-screenY * charWidth, 0);
711 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
712 // int x1, int x2, int y1, int y2, int startx, int starty,
713 private void draw(Graphics g, int startRes, int endRes, int startSeq,
714 int endSeq, int offset)
716 g.setFont(av.getFont());
717 sr.prepare(g, av.isRenderGaps());
721 // / First draw the sequences
722 // ///////////////////////////
723 for (int i = startSeq; i < endSeq; i++)
725 nextSeq = av.getAlignment().getSequenceAt(i);
728 // occasionally, a race condition occurs such that the alignment row is
732 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
733 startRes, endRes, offset + ((i - startSeq) * charHeight));
735 if (av.isShowSequenceFeatures())
737 fr.drawSequence(g, nextSeq, startRes, endRes, offset
738 + ((i - startSeq) * charHeight));
741 // / Highlight search Results once all sequences have been drawn
742 // ////////////////////////////////////////////////////////
743 if (searchResults != null)
745 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
747 if (visibleResults != null)
749 for (int r = 0; r < visibleResults.length; r += 2)
751 sr.drawHighlightedText(nextSeq, visibleResults[r],
752 visibleResults[r + 1], (visibleResults[r] - startRes)
754 + ((i - startSeq) * charHeight));
759 if (av.cursorMode && cursorY == i && cursorX >= startRes
760 && cursorX <= endRes)
762 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
763 offset + ((i - startSeq) * charHeight));
767 if (av.getSelectionGroup() != null
768 || av.getAlignment().getGroups().size() > 0)
770 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
775 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
776 int startSeq, int endSeq, int offset)
778 Graphics2D g = (Graphics2D) g1;
780 // ///////////////////////////////////
781 // Now outline any areas if necessary
782 // ///////////////////////////////////
783 SequenceGroup group = av.getSelectionGroup();
789 int visWidth = (endRes - startRes + 1) * charWidth;
791 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
793 group = av.getAlignment().getGroups().get(0);
803 boolean inGroup = false;
807 for (i = startSeq; i < endSeq; i++)
809 sx = (group.getStartRes() - startRes) * charWidth;
810 sy = offset + ((i - startSeq) * charHeight);
811 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
813 if (sx + ex < 0 || sx > visWidth)
818 if ((sx <= (endRes - startRes) * charWidth)
819 && group.getSequences(null).contains(
820 av.getAlignment().getSequenceAt(i)))
823 && !group.getSequences(null).contains(
824 av.getAlignment().getSequenceAt(i + 1)))
826 bottom = sy + charHeight;
831 if (((top == -1) && (i == 0))
832 || !group.getSequences(null).contains(
833 av.getAlignment().getSequenceAt(i - 1)))
841 if (group == av.getSelectionGroup())
843 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
844 BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f },
846 g.setColor(Color.RED);
850 g.setStroke(new BasicStroke());
851 g.setColor(group.getOutlineColour());
859 if (sx >= 0 && sx < visWidth)
861 g.drawLine(sx, oldY, sx, sy);
864 if (sx + ex < visWidth)
866 g.drawLine(sx + ex, oldY, sx + ex, sy);
875 if (sx + ex > visWidth)
880 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
882 ex = (endRes - startRes + 1) * charWidth;
887 g.drawLine(sx, top, sx + ex, top);
893 g.drawLine(sx, bottom, sx + ex, bottom);
904 sy = offset + ((i - startSeq) * charHeight);
905 if (sx >= 0 && sx < visWidth)
907 g.drawLine(sx, oldY, sx, sy);
910 if (sx + ex < visWidth)
912 g.drawLine(sx + ex, oldY, sx + ex, sy);
921 if (sx + ex > visWidth)
925 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
927 ex = (endRes - startRes + 1) * charWidth;
932 g.drawLine(sx, top, sx + ex, top);
938 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
947 g.setStroke(new BasicStroke());
949 if (groupIndex >= av.getAlignment().getGroups().size())
954 group = av.getAlignment().getGroups().get(groupIndex);
956 } while (groupIndex < av.getAlignment().getGroups().size());
968 public void highlightSearchResults(SearchResults results)
972 searchResults = results;