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;
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;
37 import java.util.List;
39 import javax.swing.JComponent;
47 public class SeqCanvas extends JComponent
49 final FeatureRenderer fr;
51 final SequenceRenderer sr;
63 SearchResults searchResults = null;
65 boolean fastPaint = false;
76 * Creates a new SeqCanvas object.
81 public SeqCanvas(AlignmentPanel ap)
85 fr = new FeatureRenderer(ap);
86 sr = new SequenceRenderer(av);
87 setLayout(new BorderLayout());
88 PaintRefresher.Register(this, av.getSequenceSetId());
89 setBackground(Color.white);
92 public SequenceRenderer getSequenceRenderer()
97 public FeatureRenderer getFeatureRenderer()
102 int charHeight = 0, charWidth = 0;
104 private void updateViewport()
106 charHeight = av.getCharHeight();
107 charWidth = av.getCharWidth();
121 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
124 int scalestartx = startx - (startx % 10) + 10;
126 g.setColor(Color.black);
128 for (int i = scalestartx; i < endx; i += 10)
131 if (av.hasHiddenColumns())
133 value = av.getColumnSelection().adjustForHiddenColumns(value);
136 g.drawString(String.valueOf(value), (i - startx - 1) * charWidth,
137 ypos - (charHeight / 2));
139 g.drawLine(((i - startx - 1) * charWidth) + (charWidth / 2),
140 (ypos + 2) - (charHeight / 2), ((i - startx - 1) * charWidth)
158 void drawWestScale(Graphics g, int startx, int endx, int ypos)
160 FontMetrics fm = getFontMetrics(av.getFont());
163 if (av.hasHiddenColumns())
165 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
166 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
169 int maxwidth = av.getAlignment().getWidth();
170 if (av.hasHiddenColumns())
172 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
176 for (int i = 0; i < av.getAlignment().getHeight(); i++)
178 SequenceI seq = av.getAlignment().getSequenceAt(i);
184 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
191 value = av.getAlignment().getSequenceAt(i).findPosition(index);
198 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value))
200 g.drawString(value + "", x, (ypos + (i * charHeight))
218 void drawEastScale(Graphics g, int startx, int endx, int ypos)
222 if (av.hasHiddenColumns())
224 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
229 for (int i = 0; i < av.getAlignment().getHeight(); i++)
231 seq = av.getAlignment().getSequenceAt(i);
235 while (index > startx)
237 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
244 value = seq.findPosition(index);
251 g.drawString(String.valueOf(value), 0, (ypos + (i * charHeight))
257 boolean fastpainting = false;
260 * need to make this thread safe move alignment rendering in response to
266 * shift up or down in repaint
268 public void fastPaint(int horizontal, int vertical)
270 if (fastpainting || gg == null)
277 gg.copyArea(horizontal * charWidth, vertical * charHeight, imgWidth,
278 imgHeight, -horizontal * charWidth, -vertical * charHeight);
280 int sr = av.startRes;
282 int ss = av.startSeq;
287 if (horizontal > 0) // scrollbar pulled right, image to the left
290 transX = (er - sr - horizontal) * charWidth;
291 sr = er - horizontal;
293 else if (horizontal < 0)
295 er = sr - horizontal - 1;
297 else if (vertical > 0) // scroll down
301 if (ss < av.startSeq)
302 { // ie scrolling too fast, more than a page at a time
307 transY = imgHeight - (vertical * charHeight);
310 else if (vertical < 0)
320 gg.translate(transX, transY);
321 drawPanel(gg, sr, er, ss, es, 0);
322 gg.translate(-transX, -transY);
325 fastpainting = false;
329 * Definitions of startx and endx (hopefully): SMJS This is what I'm working
330 * towards! startx is the first residue (starting at 0) to display. endx is
331 * the last residue to display (starting at 0). starty is the first sequence
332 * to display (starting at 0). endy is the last sequence to display (starting
333 * at 0). NOTE 1: The av limits are set in setFont in this class and in the
334 * adjustment listener in SeqPanel when the scrollbars move.
337 // Set this to false to force a full panel paint
338 public void paintComponent(Graphics g)
341 BufferedImage lcimg = img; // take reference since other threads may null
342 // img and call later.
343 super.paintComponent(g);
347 || (getVisibleRect().width != g.getClipBounds().width) || (getVisibleRect().height != g
348 .getClipBounds().height)))
350 g.drawImage(lcimg, 0, 0, this);
355 // this draws the whole of the alignment
356 imgWidth = getWidth();
357 imgHeight = getHeight();
359 imgWidth -= (imgWidth % charWidth);
360 imgHeight -= (imgHeight % charHeight);
362 if ((imgWidth < 1) || (imgHeight < 1))
367 if (lcimg == null || imgWidth != lcimg.getWidth()
368 || imgHeight != lcimg.getHeight())
372 lcimg = img = new BufferedImage(imgWidth, imgHeight,
373 BufferedImage.TYPE_INT_RGB);
374 gg = (Graphics2D) img.getGraphics();
375 gg.setFont(av.getFont());
376 } catch (OutOfMemoryError er)
379 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n" + er);
380 new OOMWarning("Creating alignment image for display", er);
388 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
389 RenderingHints.VALUE_ANTIALIAS_ON);
392 gg.setColor(Color.white);
393 gg.fillRect(0, 0, imgWidth, imgHeight);
395 if (av.getWrapAlignment())
397 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
401 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
404 g.drawImage(lcimg, 0, 0, this);
414 * @return DOCUMENT ME!
416 public int getWrappedCanvasWidth(int cwidth)
418 FontMetrics fm = getFontMetrics(av.getFont());
423 if (av.getScaleRightWrapped())
425 LABEL_EAST = fm.stringWidth(getMask());
428 if (av.getScaleLeftWrapped())
430 LABEL_WEST = fm.stringWidth(getMask());
433 return (cwidth - LABEL_EAST - LABEL_WEST) / charWidth;
437 * Generates a string of zeroes.
446 for (int i = 0; i < av.getAlignment().getHeight(); i++)
448 tmp = av.getAlignment().getSequenceAt(i).getEnd();
455 for (int i = maxWidth; i > 0; i /= 10)
469 * @param canvasHeight
474 public void drawWrappedPanel(Graphics g, int canvasWidth,
475 int canvasHeight, int startRes)
478 AlignmentI al = av.getAlignment();
480 FontMetrics fm = getFontMetrics(av.getFont());
482 if (av.getScaleRightWrapped())
484 LABEL_EAST = fm.stringWidth(getMask());
487 if (av.getScaleLeftWrapped())
489 LABEL_WEST = fm.stringWidth(getMask());
492 int hgap = charHeight;
493 if (av.getScaleAboveWrapped())
498 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / charWidth;
499 int cHeight = av.getAlignment().getHeight() * charHeight;
501 av.setWrappedWidth(cWidth);
503 av.endRes = av.startRes + cWidth;
507 int maxwidth = av.getAlignment().getWidth() - 1;
509 if (av.hasHiddenColumns())
511 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
514 while ((ypos <= canvasHeight) && (startRes < maxwidth))
516 endx = startRes + cWidth - 1;
523 g.setFont(av.getFont());
524 g.setColor(Color.black);
526 if (av.getScaleLeftWrapped())
528 drawWestScale(g, startRes, endx, ypos);
531 if (av.getScaleRightWrapped())
533 g.translate(canvasWidth - LABEL_EAST, 0);
534 drawEastScale(g, startRes, endx, ypos);
535 g.translate(-(canvasWidth - LABEL_EAST), 0);
538 g.translate(LABEL_WEST, 0);
540 if (av.getScaleAboveWrapped())
542 drawNorthScale(g, startRes, endx, ypos);
545 if (av.hasHiddenColumns() && av.getShowHiddenMarkers())
547 g.setColor(Color.blue);
549 for (int i = 0; i < av.getColumnSelection().getHiddenColumns()
552 res = av.getColumnSelection().findHiddenRegionPosition(i)
555 if (res < 0 || res > endx - startRes)
560 gg.fillPolygon(new int[]
561 { res * charWidth - charHeight / 4,
562 res * charWidth + charHeight / 4, res * charWidth },
564 { ypos - (charHeight / 2), ypos - (charHeight / 2),
565 ypos - (charHeight / 2) + 8 }, 3);
570 // When printing we have an extra clipped region,
571 // the Printable page which we need to account for here
572 Shape clip = g.getClip();
576 g.setClip(0, 0, cWidth * charWidth, canvasHeight);
580 g.setClip(0, (int) clip.getBounds().getY(), cWidth * charWidth,
581 (int) clip.getBounds().getHeight());
584 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
586 if (av.isShowAnnotation())
588 g.translate(0, cHeight + ypos + 3);
589 if (annotations == null)
591 annotations = new AnnotationPanel(av);
594 annotations.renderer.drawComponent(annotations, av, g,
595 -1, startRes, endx + 1);
596 g.translate(0, -cHeight - ypos - 3);
599 g.translate(-LABEL_WEST, 0);
601 ypos += cHeight + getAnnotationHeight() + hgap;
607 AnnotationPanel annotations;
609 int getAnnotationHeight()
611 if (!av.isShowAnnotation())
616 if (annotations == null)
618 annotations = new AnnotationPanel(av);
621 return annotations.adjustPanelHeight();
640 public void drawPanel(Graphics g1, int startRes, int endRes,
642 int endSeq, int offset)
645 if (!av.hasHiddenColumns())
647 draw(g1, startRes, endRes, startSeq, endSeq, offset);
651 List<int[]> regions = av.getColumnSelection().getHiddenColumns();
654 int blockStart = startRes;
655 int blockEnd = endRes;
657 for (int[] region : regions)
659 int hideStart = region[0];
660 int hideEnd = region[1];
662 if (hideStart <= blockStart)
664 blockStart += (hideEnd - hideStart) + 1;
668 blockEnd = hideStart - 1;
670 g1.translate(screenY * charWidth, 0);
672 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
674 if (av.getShowHiddenMarkers())
676 g1.setColor(Color.blue);
678 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
679 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
680 (endSeq - startSeq) * charHeight + offset);
683 g1.translate(-screenY * charWidth, 0);
684 screenY += blockEnd - blockStart + 1;
685 blockStart = hideEnd + 1;
688 if (screenY <= (endRes - startRes))
690 blockEnd = blockStart + (endRes - startRes) - screenY;
691 g1.translate(screenY * charWidth, 0);
692 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
694 g1.translate(-screenY * charWidth, 0);
700 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
701 // int x1, int x2, int y1, int y2, int startx, int starty,
702 private void draw(Graphics g, int startRes, int endRes, int startSeq,
706 g.setFont(av.getFont());
707 sr.prepare(g, av.isRenderGaps());
711 // / First draw the sequences
712 // ///////////////////////////
713 for (int i = startSeq; i < endSeq; i++)
715 nextSeq = av.getAlignment().getSequenceAt(i);
718 // occasionally, a race condition occurs such that the alignment row is
722 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
723 startRes, endRes, offset + ((i - startSeq) * charHeight));
725 if (av.isShowSequenceFeatures())
727 fr.drawSequence(g, nextSeq, startRes, endRes, offset
728 + ((i - startSeq) * charHeight));
731 // / Highlight search Results once all sequences have been drawn
732 // ////////////////////////////////////////////////////////
733 if (searchResults != null)
735 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
737 if (visibleResults != null)
739 for (int r = 0; r < visibleResults.length; r += 2)
741 sr.drawHighlightedText(nextSeq, visibleResults[r],
742 visibleResults[r + 1], (visibleResults[r] - startRes)
744 + ((i - startSeq) * charHeight));
749 if (av.cursorMode && cursorY == i && cursorX >= startRes
750 && cursorX <= endRes)
752 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
753 offset + ((i - startSeq) * charHeight));
757 if (av.getSelectionGroup() != null
758 || av.getAlignment().getGroups().size() > 0)
760 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
765 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
766 int startSeq, int endSeq, int offset)
768 Graphics2D g = (Graphics2D) g1;
770 // ///////////////////////////////////
771 // Now outline any areas if necessary
772 // ///////////////////////////////////
773 SequenceGroup group = av.getSelectionGroup();
779 int visWidth = (endRes - startRes + 1) * charWidth;
781 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
783 group = av.getAlignment().getGroups().get(0);
793 boolean inGroup = false;
797 for (i = startSeq; i < endSeq; i++)
799 sx = (group.getStartRes() - startRes) * charWidth;
800 sy = offset + ((i - startSeq) * charHeight);
801 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
803 if (sx + ex < 0 || sx > visWidth)
808 if ((sx <= (endRes - startRes) * charWidth)
809 && group.getSequences(null).contains(
810 av.getAlignment().getSequenceAt(i)))
813 && !group.getSequences(null).contains(
814 av.getAlignment().getSequenceAt(i + 1)))
816 bottom = sy + charHeight;
821 if (((top == -1) && (i == 0))
822 || !group.getSequences(null).contains(
823 av.getAlignment().getSequenceAt(i - 1)))
831 if (group == av.getSelectionGroup())
833 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
834 BasicStroke.JOIN_ROUND, 3f, new float[]
836 g.setColor(Color.RED);
840 g.setStroke(new BasicStroke());
841 g.setColor(group.getOutlineColour());
849 if (sx >= 0 && sx < visWidth)
851 g.drawLine(sx, oldY, sx, sy);
854 if (sx + ex < visWidth)
856 g.drawLine(sx + ex, oldY, sx + ex, sy);
865 if (sx + ex > visWidth)
870 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
872 ex = (endRes - startRes + 1) * charWidth;
877 g.drawLine(sx, top, sx + ex, top);
883 g.drawLine(sx, bottom, sx + ex, bottom);
894 sy = offset + ((i - startSeq) * charHeight);
895 if (sx >= 0 && sx < visWidth)
897 g.drawLine(sx, oldY, sx, sy);
900 if (sx + ex < visWidth)
902 g.drawLine(sx + ex, oldY, sx + ex, sy);
911 if (sx + ex > visWidth)
915 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
917 ex = (endRes - startRes + 1) * charWidth;
922 g.drawLine(sx, top, sx + ex, top);
928 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
937 g.setStroke(new BasicStroke());
939 if (groupIndex >= av.getAlignment().getGroups().size())
944 group = av.getAlignment().getGroups()
947 } while (groupIndex < av.getAlignment().getGroups().size());
959 public void highlightSearchResults(SearchResults results)
963 searchResults = results;