2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 import java.awt.image.*;
25 import jalview.datamodel.*;
33 public class SeqCanvas
36 final FeatureRenderer fr;
37 final SequenceRenderer sr;
43 SearchResults searchResults = null;
44 boolean fastPaint = false;
52 * Creates a new SeqCanvas object.
54 * @param av DOCUMENT ME!
56 public SeqCanvas(AlignmentPanel ap)
59 fr = new FeatureRenderer(ap);
60 sr = new SequenceRenderer(av);
61 setLayout(new BorderLayout());
62 PaintRefresher.Register(this, av.getSequenceSetId());
63 setBackground(Color.white);
66 public SequenceRenderer getSequenceRenderer()
71 public FeatureRenderer getFeatureRenderer()
80 * @param g DOCUMENT ME!
81 * @param startx DOCUMENT ME!
82 * @param endx DOCUMENT ME!
83 * @param ypos DOCUMENT ME!
85 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
87 int scalestartx = startx - (startx % 10) + 10;
89 g.setColor(Color.black);
92 for (int i = scalestartx; i < endx; i += 10)
95 if (av.hasHiddenColumns)
97 value = av.getColumnSelection().adjustForHiddenColumns(value);
100 g.drawString(String.valueOf(value), (i - startx - 1) * av.charWidth,
101 ypos - (av.charHeight / 2));
103 g.drawLine( ( (i - startx - 1) * av.charWidth) + (av.charWidth / 2),
104 (ypos + 2) - (av.charHeight / 2),
105 ( (i - startx - 1) * av.charWidth) + (av.charWidth / 2), ypos -
113 * @param g DOCUMENT ME!
114 * @param startx DOCUMENT ME!
115 * @param endx DOCUMENT ME!
116 * @param ypos DOCUMENT ME!
118 void drawWestScale(Graphics g, int startx, int endx, int ypos)
120 FontMetrics fm = getFontMetrics(av.getFont());
121 ypos += av.charHeight;
123 if (av.hasHiddenColumns)
125 startx = av.getColumnSelection().adjustForHiddenColumns(startx);
126 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
129 int maxwidth = av.alignment.getWidth();
130 if (av.hasHiddenColumns)
132 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
136 for (int i = 0; i < av.alignment.getHeight(); i++)
138 SequenceI seq = av.alignment.getSequenceAt(i);
144 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
151 value = av.alignment.getSequenceAt(i).findPosition(index);
158 int x = LABEL_WEST - fm.stringWidth(String.valueOf(value)) -
160 g.drawString(value + "", x,
161 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
169 * @param g DOCUMENT ME!
170 * @param startx DOCUMENT ME!
171 * @param endx DOCUMENT ME!
172 * @param ypos DOCUMENT ME!
174 void drawEastScale(Graphics g, int startx, int endx, int ypos)
176 ypos += av.charHeight;
178 if (av.hasHiddenColumns)
180 endx = av.getColumnSelection().adjustForHiddenColumns(endx);
185 for (int i = 0; i < av.alignment.getHeight(); i++)
187 seq = av.alignment.getSequenceAt(i);
191 while (index > startx)
193 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
200 value = seq.findPosition(index);
207 g.drawString(String.valueOf(value), 0,
208 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
212 boolean fastpainting=false;
214 * need to make this thread safe
215 * move alignment rendering in response to slider adjustment
216 * @param horizontal shift along
217 * @param vertical shift up or down in repaint
219 public void fastPaint(int horizontal, int vertical)
233 gg.copyArea(horizontal * av.charWidth,
234 vertical * av.charHeight,
237 -horizontal * av.charWidth,
238 -vertical * av.charHeight);
240 int sr = av.startRes;
242 int ss = av.startSeq;
247 if (horizontal > 0) // scrollbar pulled right, image to the left
250 transX = (er - sr - horizontal) * av.charWidth;
251 sr = er - horizontal;
253 else if (horizontal < 0)
255 er = sr - horizontal - 1;
257 else if (vertical > 0) // scroll down
261 if (ss < av.startSeq)
262 { // ie scrolling too fast, more than a page at a time
267 transY = imgHeight - (vertical * av.charHeight);
270 else if (vertical < 0)
280 gg.translate(transX, transY);
281 drawPanel(gg, sr, er, ss, es, 0);
282 gg.translate( -transX, -transY);
289 * Definitions of startx and endx (hopefully):
290 * SMJS This is what I'm working towards!
291 * startx is the first residue (starting at 0) to display.
292 * endx is the last residue to display (starting at 0).
293 * starty is the first sequence to display (starting at 0).
294 * endy is the last sequence to display (starting at 0).
295 * NOTE 1: The av limits are set in setFont in this class and
296 * in the adjustment listener in SeqPanel when the scrollbars move.
299 // Set this to false to force a full panel paint
300 public void paintComponent(Graphics g)
302 BufferedImage lcimg = img; // take reference since other threads may null img and call later.
303 super.paintComponent(g);
305 if (lcimg != null && (fastPaint
306 || (getVisibleRect().width != g.getClipBounds().width)
307 || (getVisibleRect().height != g.getClipBounds().height)))
309 g.drawImage(lcimg, 0, 0, this);
314 // this draws the whole of the alignment
315 imgWidth = getWidth();
316 imgHeight = getHeight();
318 imgWidth -= (imgWidth % av.charWidth);
319 imgHeight -= (imgHeight % av.charHeight);
321 if ( (imgWidth < 1) || (imgHeight < 1))
326 if (lcimg == null || imgWidth != lcimg.getWidth() || imgHeight != lcimg.getHeight())
330 lcimg = img = new BufferedImage(imgWidth, imgHeight,
331 BufferedImage.TYPE_INT_RGB);
332 gg = (Graphics2D) img.getGraphics();
333 gg.setFont(av.getFont());
335 catch (OutOfMemoryError er)
338 System.err.println("SeqCanvas OutOfMemory Redraw Error.\n"+er);
339 new OOMWarning("Creating alignment image for display",er);
347 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
348 RenderingHints.VALUE_ANTIALIAS_ON);
351 gg.setColor(Color.white);
352 gg.fillRect(0, 0, imgWidth, imgHeight);
354 if (av.getWrapAlignment())
356 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
360 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
363 g.drawImage(lcimg, 0, 0, this);
371 * @param cwidth DOCUMENT ME!
373 * @return DOCUMENT ME!
375 public int getWrappedCanvasWidth(int cwidth)
377 FontMetrics fm = getFontMetrics(av.getFont());
382 if (av.scaleRightWrapped)
384 LABEL_EAST = fm.stringWidth(getMask());
387 if (av.scaleLeftWrapped)
389 LABEL_WEST = fm.stringWidth(getMask());
392 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
396 * Generates a string of zeroes.
404 for (int i = 0; i < av.alignment.getHeight(); i++)
406 tmp = av.alignment.getSequenceAt(i).getEnd();
413 for (int i = maxWidth; i > 0; i /= 10)
423 * @param g DOCUMENT ME!
424 * @param canvasWidth DOCUMENT ME!
425 * @param canvasHeight DOCUMENT ME!
426 * @param startRes DOCUMENT ME!
428 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
431 AlignmentI al = av.getAlignment();
433 FontMetrics fm = getFontMetrics(av.getFont());
435 if (av.scaleRightWrapped)
437 LABEL_EAST = fm.stringWidth(getMask());
440 if (av.scaleLeftWrapped)
442 LABEL_WEST = fm.stringWidth(getMask());
445 int hgap = av.charHeight;
446 if (av.scaleAboveWrapped)
448 hgap += av.charHeight;
451 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
452 int cHeight = av.getAlignment().getHeight() * av.charHeight;
454 av.setWrappedWidth(cWidth);
456 av.endRes = av.startRes + cWidth;
460 int maxwidth = av.alignment.getWidth() - 1;
462 if (av.hasHiddenColumns)
464 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
467 while ( (ypos <= canvasHeight) && (startRes < maxwidth))
469 endx = startRes + cWidth - 1;
476 g.setFont(av.getFont());
477 g.setColor(Color.black);
479 if (av.scaleLeftWrapped)
481 drawWestScale(g, startRes, endx, ypos);
484 if (av.scaleRightWrapped)
486 g.translate(canvasWidth - LABEL_EAST, 0);
487 drawEastScale(g, startRes, endx, ypos);
488 g.translate( - (canvasWidth - LABEL_EAST), 0);
491 g.translate(LABEL_WEST, 0);
493 if (av.scaleAboveWrapped)
495 drawNorthScale(g, startRes, endx, ypos);
498 if (av.hasHiddenColumns && av.showHiddenMarkers)
500 g.setColor(Color.blue);
502 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size();
505 res = av.getColumnSelection().findHiddenRegionPosition(i) -
508 if (res < 0 || res > endx - startRes)
513 gg.fillPolygon(new int[]
514 {res * av.charWidth - av.charHeight / 4,
515 res * av.charWidth + av.charHeight / 4,
519 ypos - (av.charHeight / 2),
520 ypos - (av.charHeight / 2),
521 ypos - (av.charHeight / 2) + 8
527 // When printing we have an extra clipped region,
528 // the Printable page which we need to account for here
529 Shape clip = g.getClip();
533 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
537 g.setClip(0, (int) clip.getBounds().getY(),
538 cWidth * av.charWidth, (int) clip.getBounds().getHeight());
541 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
543 if (av.showAnnotation)
545 g.translate(0, cHeight + ypos + 3);
546 if (annotations == null)
548 annotations = new AnnotationPanel(av);
551 annotations.drawComponent( (Graphics2D) g, startRes, endx + 1);
552 g.translate(0, -cHeight - ypos - 3);
555 g.translate( -LABEL_WEST, 0);
557 ypos += cHeight + getAnnotationHeight() + hgap;
563 AnnotationPanel annotations;
564 int getAnnotationHeight()
566 if (!av.showAnnotation)
571 if (annotations == null)
573 annotations = new AnnotationPanel(av);
576 return annotations.adjustPanelHeight();
582 * @param g1 DOCUMENT ME!
583 * @param startRes DOCUMENT ME!
584 * @param endRes DOCUMENT ME!
585 * @param startSeq DOCUMENT ME!
586 * @param endSeq DOCUMENT ME!
587 * @param offset DOCUMENT ME!
589 void drawPanel(Graphics g1, int startRes, int endRes,
590 int startSeq, int endSeq, int offset)
592 if (!av.hasHiddenColumns)
594 draw(g1, startRes, endRes, startSeq, endSeq, offset);
598 java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
601 int blockStart = startRes;
602 int blockEnd = endRes;
604 for (int i = 0; i < regions.size(); i++)
606 int[] region = (int[]) regions.elementAt(i);
607 int hideStart = region[0];
608 int hideEnd = region[1];
610 if (hideStart <= blockStart)
612 blockStart += (hideEnd - hideStart) + 1;
616 blockEnd = hideStart - 1;
618 g1.translate(screenY * av.charWidth, 0);
620 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
622 if (av.getShowHiddenMarkers())
624 g1.setColor(Color.blue);
626 g1.drawLine( (blockEnd - blockStart + 1) * av.charWidth - 1,
628 (blockEnd - blockStart + 1) * av.charWidth - 1,
629 (endSeq - startSeq) * av.charHeight + offset);
632 g1.translate( -screenY * av.charWidth, 0);
633 screenY += blockEnd - blockStart + 1;
634 blockStart = hideEnd + 1;
637 if (screenY <= (endRes - startRes))
639 blockEnd = blockStart + (endRes - startRes) - screenY;
640 g1.translate(screenY * av.charWidth, 0);
641 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
643 g1.translate( -screenY * av.charWidth, 0);
649 //int startRes, int endRes, int startSeq, int endSeq, int x, int y,
650 // int x1, int x2, int y1, int y2, int startx, int starty,
651 void draw(Graphics g,
652 int startRes, int endRes,
653 int startSeq, int endSeq,
656 g.setFont(av.getFont());
657 sr.prepare(g, av.renderGaps);
661 /// First draw the sequences
662 /////////////////////////////
663 for (int i = startSeq; i < endSeq; i++)
665 nextSeq = av.alignment.getSequenceAt(i);
667 sr.drawSequence(nextSeq, av.alignment.findAllGroups(nextSeq),
669 offset + ( (i - startSeq) * av.charHeight));
671 if (av.showSequenceFeatures)
673 fr.drawSequence(g, nextSeq, startRes, endRes,
674 offset + ( (i - startSeq) * av.charHeight));
677 /// Highlight search Results once all sequences have been drawn
678 //////////////////////////////////////////////////////////
679 if (searchResults != null)
681 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
683 if (visibleResults != null)
685 for (int r = 0; r < visibleResults.length; r += 2)
687 sr.drawHighlightedText(nextSeq, visibleResults[r],
688 visibleResults[r + 1],
689 (visibleResults[r] - startRes) *
691 offset + ( (i - startSeq) * av.charHeight));
696 if (av.cursorMode && cursorY == i
697 && cursorX >= startRes && cursorX <= endRes)
699 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * av.charWidth,
700 offset + ( (i - startSeq) * av.charHeight));
704 if (av.getSelectionGroup() != null || av.alignment.getGroups().size() > 0)
706 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
711 void drawGroupsBoundaries(Graphics g1,
712 int startRes, int endRes,
713 int startSeq, int endSeq,
716 Graphics2D g = (Graphics2D) g1;
718 /////////////////////////////////////
719 // Now outline any areas if necessary
720 /////////////////////////////////////
721 SequenceGroup group = av.getSelectionGroup();
727 int visWidth = (endRes - startRes + 1) * av.charWidth;
729 if ( (group == null) && (av.alignment.getGroups().size() > 0))
731 group = (SequenceGroup) av.alignment.getGroups().elementAt(0);
741 boolean inGroup = false;
745 for (i = startSeq; i < endSeq; i++)
747 sx = (group.getStartRes() - startRes) * av.charWidth;
748 sy = offset + ( (i - startSeq) * av.charHeight);
749 ex = ( ( (group.getEndRes() + 1) - group.getStartRes()) *
753 if (sx + ex < 0 || sx > visWidth)
758 if ( (sx <= (endRes - startRes) * av.charWidth) &&
759 group.getSequences(null).
760 contains(av.alignment.getSequenceAt(i)))
762 if ( (bottom == -1) &&
763 !group.getSequences(null).contains(
764 av.alignment.getSequenceAt(i + 1)))
766 bottom = sy + av.charHeight;
771 if ( ( (top == -1) && (i == 0)) ||
772 !group.getSequences(null).contains(
773 av.alignment.getSequenceAt(i - 1)))
781 if (group == av.getSelectionGroup())
783 g.setStroke(new BasicStroke(1,
784 BasicStroke.CAP_BUTT,
785 BasicStroke.JOIN_ROUND, 3f,
788 g.setColor(Color.RED);
792 g.setStroke(new BasicStroke());
793 g.setColor(group.getOutlineColour());
801 if (sx >= 0 && sx < visWidth)
803 g.drawLine(sx, oldY, sx, sy);
806 if (sx + ex < visWidth)
808 g.drawLine(sx + ex, oldY, sx + ex, sy);
817 if (sx + ex > visWidth)
822 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
824 ex = (endRes - startRes + 1) * av.charWidth;
829 g.drawLine(sx, top, sx + ex, top);
835 g.drawLine(sx, bottom, sx + ex, bottom);
846 sy = offset + ( (i - startSeq) * av.charHeight);
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)
867 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
869 ex = (endRes - startRes + 1) * av.charWidth;
874 g.drawLine(sx, top, sx + ex, top);
880 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
889 g.setStroke(new BasicStroke());
891 if (groupIndex >= av.alignment.getGroups().size())
896 group = (SequenceGroup) av.alignment.getGroups().elementAt(groupIndex);
899 while (groupIndex < av.alignment.getGroups().size());
908 * @param results DOCUMENT ME!
910 public void highlightSearchResults(SearchResults results)
914 searchResults = results;