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();
122 private void drawNorthScale(Graphics g, int startx, int endx, int ypos)
125 int scalestartx = startx - (startx % 10) + 10;
127 g.setColor(Color.black);
129 for (int i = scalestartx; i < endx; i += 10)
132 if (av.hasHiddenColumns())
134 value = av.getColumnSelection().adjustForHiddenColumns(value);
137 g.drawString(String.valueOf(value), (i - startx - 1) * charWidth,
138 ypos - (charHeight / 2));
140 g.drawLine(((i - startx - 1) * charWidth) + (charWidth / 2),
141 (ypos + 2) - (charHeight / 2), ((i - startx - 1) * charWidth)
142 + (charWidth / 2), ypos - 2);
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)
561 new int[] { res * charWidth - charHeight / 4,
562 res * charWidth + charHeight / 4, res * charWidth },
563 new int[] { ypos - (charHeight / 2),
564 ypos - (charHeight / 2), ypos - (charHeight / 2) + 8 },
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, -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,
641 int startSeq, int endSeq, int offset)
644 if (!av.hasHiddenColumns())
646 draw(g1, startRes, endRes, startSeq, endSeq, offset);
650 List<int[]> regions = av.getColumnSelection().getHiddenColumns();
653 int blockStart = startRes;
654 int blockEnd = endRes;
656 for (int[] region : regions)
658 int hideStart = region[0];
659 int hideEnd = region[1];
661 if (hideStart <= blockStart)
663 blockStart += (hideEnd - hideStart) + 1;
667 blockEnd = hideStart - 1;
669 g1.translate(screenY * charWidth, 0);
671 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
673 if (av.getShowHiddenMarkers())
675 g1.setColor(Color.blue);
677 g1.drawLine((blockEnd - blockStart + 1) * charWidth - 1,
678 0 + offset, (blockEnd - blockStart + 1) * charWidth - 1,
679 (endSeq - startSeq) * charHeight + offset);
682 g1.translate(-screenY * charWidth, 0);
683 screenY += blockEnd - blockStart + 1;
684 blockStart = hideEnd + 1;
687 if (screenY <= (endRes - startRes))
689 blockEnd = blockStart + (endRes - startRes) - screenY;
690 g1.translate(screenY * charWidth, 0);
691 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
693 g1.translate(-screenY * charWidth, 0);
699 // int startRes, int endRes, int startSeq, int endSeq, int x, int y,
700 // int x1, int x2, int y1, int y2, int startx, int starty,
701 private void draw(Graphics g, int startRes, int endRes, int startSeq,
702 int endSeq, int offset)
704 g.setFont(av.getFont());
705 sr.prepare(g, av.isRenderGaps());
709 // / First draw the sequences
710 // ///////////////////////////
711 for (int i = startSeq; i < endSeq; i++)
713 nextSeq = av.getAlignment().getSequenceAt(i);
716 // occasionally, a race condition occurs such that the alignment row is
720 sr.drawSequence(nextSeq, av.getAlignment().findAllGroups(nextSeq),
721 startRes, endRes, offset + ((i - startSeq) * charHeight));
723 if (av.isShowSequenceFeatures())
725 fr.drawSequence(g, nextSeq, startRes, endRes, offset
726 + ((i - startSeq) * charHeight));
729 // / Highlight search Results once all sequences have been drawn
730 // ////////////////////////////////////////////////////////
731 if (searchResults != null)
733 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
735 if (visibleResults != null)
737 for (int r = 0; r < visibleResults.length; r += 2)
739 sr.drawHighlightedText(nextSeq, visibleResults[r],
740 visibleResults[r + 1], (visibleResults[r] - startRes)
742 + ((i - startSeq) * charHeight));
747 if (av.cursorMode && cursorY == i && cursorX >= startRes
748 && cursorX <= endRes)
750 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * charWidth,
751 offset + ((i - startSeq) * charHeight));
755 if (av.getSelectionGroup() != null
756 || av.getAlignment().getGroups().size() > 0)
758 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
763 void drawGroupsBoundaries(Graphics g1, int startRes, int endRes,
764 int startSeq, int endSeq, int offset)
766 Graphics2D g = (Graphics2D) g1;
768 // ///////////////////////////////////
769 // Now outline any areas if necessary
770 // ///////////////////////////////////
771 SequenceGroup group = av.getSelectionGroup();
777 int visWidth = (endRes - startRes + 1) * charWidth;
779 if ((group == null) && (av.getAlignment().getGroups().size() > 0))
781 group = av.getAlignment().getGroups().get(0);
791 boolean inGroup = false;
795 for (i = startSeq; i < endSeq; i++)
797 sx = (group.getStartRes() - startRes) * charWidth;
798 sy = offset + ((i - startSeq) * charHeight);
799 ex = (((group.getEndRes() + 1) - group.getStartRes()) * charWidth) - 1;
801 if (sx + ex < 0 || sx > visWidth)
806 if ((sx <= (endRes - startRes) * charWidth)
807 && group.getSequences(null).contains(
808 av.getAlignment().getSequenceAt(i)))
811 && !group.getSequences(null).contains(
812 av.getAlignment().getSequenceAt(i + 1)))
814 bottom = sy + charHeight;
819 if (((top == -1) && (i == 0))
820 || !group.getSequences(null).contains(
821 av.getAlignment().getSequenceAt(i - 1)))
829 if (group == av.getSelectionGroup())
831 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT,
832 BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f },
834 g.setColor(Color.RED);
838 g.setStroke(new BasicStroke());
839 g.setColor(group.getOutlineColour());
847 if (sx >= 0 && sx < visWidth)
849 g.drawLine(sx, oldY, sx, sy);
852 if (sx + ex < visWidth)
854 g.drawLine(sx + ex, oldY, sx + ex, sy);
863 if (sx + ex > visWidth)
868 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
870 ex = (endRes - startRes + 1) * charWidth;
875 g.drawLine(sx, top, sx + ex, top);
881 g.drawLine(sx, bottom, sx + ex, bottom);
892 sy = offset + ((i - startSeq) * charHeight);
893 if (sx >= 0 && sx < visWidth)
895 g.drawLine(sx, oldY, sx, sy);
898 if (sx + ex < visWidth)
900 g.drawLine(sx + ex, oldY, sx + ex, sy);
909 if (sx + ex > visWidth)
913 else if (sx + ex >= (endRes - startRes + 1) * charWidth)
915 ex = (endRes - startRes + 1) * charWidth;
920 g.drawLine(sx, top, sx + ex, top);
926 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
935 g.setStroke(new BasicStroke());
937 if (groupIndex >= av.getAlignment().getGroups().size())
942 group = av.getAlignment().getGroups().get(groupIndex);
944 } while (groupIndex < av.getAlignment().getGroups().size());
956 public void highlightSearchResults(SearchResults results)
960 searchResults = results;