2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 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.out.println(er + " making image, SeqCanvas");
339 javax.swing.SwingUtilities.invokeLater(new Runnable()
343 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.
345 "Out of memory creating alignment image!!"
347 "\nSee help files for increasing Java Virtual Machine memory."
349 javax.swing.JOptionPane.WARNING_MESSAGE);
359 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
360 RenderingHints.VALUE_ANTIALIAS_ON);
363 gg.setColor(Color.white);
364 gg.fillRect(0, 0, imgWidth, imgHeight);
366 if (av.getWrapAlignment())
368 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
372 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
375 g.drawImage(lcimg, 0, 0, this);
383 * @param cwidth DOCUMENT ME!
385 * @return DOCUMENT ME!
387 public int getWrappedCanvasWidth(int cwidth)
389 FontMetrics fm = getFontMetrics(av.getFont());
394 if (av.scaleRightWrapped)
396 LABEL_EAST = fm.stringWidth(getMask());
399 if (av.scaleLeftWrapped)
401 LABEL_WEST = fm.stringWidth(getMask());
404 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
408 * Generates a string of zeroes.
416 for (int i = 0; i < av.alignment.getHeight(); i++)
418 tmp = av.alignment.getSequenceAt(i).getEnd();
425 for (int i = maxWidth; i > 0; i /= 10)
435 * @param g DOCUMENT ME!
436 * @param canvasWidth DOCUMENT ME!
437 * @param canvasHeight DOCUMENT ME!
438 * @param startRes DOCUMENT ME!
440 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
443 AlignmentI al = av.getAlignment();
445 FontMetrics fm = getFontMetrics(av.getFont());
447 if (av.scaleRightWrapped)
449 LABEL_EAST = fm.stringWidth(getMask());
452 if (av.scaleLeftWrapped)
454 LABEL_WEST = fm.stringWidth(getMask());
457 int hgap = av.charHeight;
458 if (av.scaleAboveWrapped)
460 hgap += av.charHeight;
463 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
464 int cHeight = av.getAlignment().getHeight() * av.charHeight;
466 av.setWrappedWidth(cWidth);
468 av.endRes = av.startRes + cWidth;
472 int maxwidth = av.alignment.getWidth() - 1;
474 if (av.hasHiddenColumns)
476 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
479 while ( (ypos <= canvasHeight) && (startRes < maxwidth))
481 endx = startRes + cWidth - 1;
488 g.setFont(av.getFont());
489 g.setColor(Color.black);
491 if (av.scaleLeftWrapped)
493 drawWestScale(g, startRes, endx, ypos);
496 if (av.scaleRightWrapped)
498 g.translate(canvasWidth - LABEL_EAST, 0);
499 drawEastScale(g, startRes, endx, ypos);
500 g.translate( - (canvasWidth - LABEL_EAST), 0);
503 g.translate(LABEL_WEST, 0);
505 if (av.scaleAboveWrapped)
507 drawNorthScale(g, startRes, endx, ypos);
510 if (av.hasHiddenColumns && av.showHiddenMarkers)
512 g.setColor(Color.blue);
514 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size();
517 res = av.getColumnSelection().findHiddenRegionPosition(i) -
520 if (res < 0 || res > endx - startRes)
525 gg.fillPolygon(new int[]
526 {res * av.charWidth - av.charHeight / 4,
527 res * av.charWidth + av.charHeight / 4,
531 ypos - (av.charHeight / 2),
532 ypos - (av.charHeight / 2),
533 ypos - (av.charHeight / 2) + 8
539 // When printing we have an extra clipped region,
540 // the Printable page which we need to account for here
541 Shape clip = g.getClip();
545 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
549 g.setClip(0, (int) clip.getBounds().getY(),
550 cWidth * av.charWidth, (int) clip.getBounds().getHeight());
553 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
555 if (av.showAnnotation)
557 g.translate(0, cHeight + ypos + 3);
558 if (annotations == null)
560 annotations = new AnnotationPanel(av);
563 annotations.drawComponent( (Graphics2D) g, startRes, endx + 1);
564 g.translate(0, -cHeight - ypos - 3);
567 g.translate( -LABEL_WEST, 0);
569 ypos += cHeight + getAnnotationHeight() + hgap;
575 AnnotationPanel annotations;
576 int getAnnotationHeight()
578 if (!av.showAnnotation)
583 if (annotations == null)
585 annotations = new AnnotationPanel(av);
588 return annotations.adjustPanelHeight();
594 * @param g1 DOCUMENT ME!
595 * @param startRes DOCUMENT ME!
596 * @param endRes DOCUMENT ME!
597 * @param startSeq DOCUMENT ME!
598 * @param endSeq DOCUMENT ME!
599 * @param offset DOCUMENT ME!
601 void drawPanel(Graphics g1, int startRes, int endRes,
602 int startSeq, int endSeq, int offset)
604 if (!av.hasHiddenColumns)
606 draw(g1, startRes, endRes, startSeq, endSeq, offset);
610 java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
613 int blockStart = startRes;
614 int blockEnd = endRes;
616 for (int i = 0; i < regions.size(); i++)
618 int[] region = (int[]) regions.elementAt(i);
619 int hideStart = region[0];
620 int hideEnd = region[1];
622 if (hideStart <= blockStart)
624 blockStart += (hideEnd - hideStart) + 1;
628 blockEnd = hideStart - 1;
630 g1.translate(screenY * av.charWidth, 0);
632 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
634 if (av.getShowHiddenMarkers())
636 g1.setColor(Color.blue);
638 g1.drawLine( (blockEnd - blockStart + 1) * av.charWidth - 1,
640 (blockEnd - blockStart + 1) * av.charWidth - 1,
641 (endSeq - startSeq) * av.charHeight + offset);
644 g1.translate( -screenY * av.charWidth, 0);
645 screenY += blockEnd - blockStart + 1;
646 blockStart = hideEnd + 1;
649 if (screenY <= (endRes - startRes))
651 blockEnd = blockStart + (endRes - startRes) - screenY;
652 g1.translate(screenY * av.charWidth, 0);
653 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
655 g1.translate( -screenY * av.charWidth, 0);
661 //int startRes, int endRes, int startSeq, int endSeq, int x, int y,
662 // int x1, int x2, int y1, int y2, int startx, int starty,
663 void draw(Graphics g,
664 int startRes, int endRes,
665 int startSeq, int endSeq,
668 g.setFont(av.getFont());
669 sr.prepare(g, av.renderGaps);
673 /// First draw the sequences
674 /////////////////////////////
675 for (int i = startSeq; i < endSeq; i++)
677 nextSeq = av.alignment.getSequenceAt(i);
679 sr.drawSequence(nextSeq, av.alignment.findAllGroups(nextSeq),
681 offset + ( (i - startSeq) * av.charHeight));
683 if (av.showSequenceFeatures)
685 fr.drawSequence(g, nextSeq, startRes, endRes,
686 offset + ( (i - startSeq) * av.charHeight));
689 /// Highlight search Results once all sequences have been drawn
690 //////////////////////////////////////////////////////////
691 if (searchResults != null)
693 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
695 if (visibleResults != null)
697 for (int r = 0; r < visibleResults.length; r += 2)
699 sr.drawHighlightedText(nextSeq, visibleResults[r],
700 visibleResults[r + 1],
701 (visibleResults[r] - startRes) *
703 offset + ( (i - startSeq) * av.charHeight));
708 if (av.cursorMode && cursorY == i
709 && cursorX >= startRes && cursorX <= endRes)
711 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * av.charWidth,
712 offset + ( (i - startSeq) * av.charHeight));
716 if (av.getSelectionGroup() != null || av.alignment.getGroups().size() > 0)
718 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
723 void drawGroupsBoundaries(Graphics g1,
724 int startRes, int endRes,
725 int startSeq, int endSeq,
728 Graphics2D g = (Graphics2D) g1;
730 /////////////////////////////////////
731 // Now outline any areas if necessary
732 /////////////////////////////////////
733 SequenceGroup group = av.getSelectionGroup();
739 int visWidth = (endRes - startRes + 1) * av.charWidth;
741 if ( (group == null) && (av.alignment.getGroups().size() > 0))
743 group = (SequenceGroup) av.alignment.getGroups().elementAt(0);
753 boolean inGroup = false;
757 for (i = startSeq; i < endSeq; i++)
759 sx = (group.getStartRes() - startRes) * av.charWidth;
760 sy = offset + ( (i - startSeq) * av.charHeight);
761 ex = ( ( (group.getEndRes() + 1) - group.getStartRes()) *
765 if (sx + ex < 0 || sx > visWidth)
770 if ( (sx <= (endRes - startRes) * av.charWidth) &&
771 group.getSequences(null).
772 contains(av.alignment.getSequenceAt(i)))
774 if ( (bottom == -1) &&
775 !group.getSequences(null).contains(
776 av.alignment.getSequenceAt(i + 1)))
778 bottom = sy + av.charHeight;
783 if ( ( (top == -1) && (i == 0)) ||
784 !group.getSequences(null).contains(
785 av.alignment.getSequenceAt(i - 1)))
793 if (group == av.getSelectionGroup())
795 g.setStroke(new BasicStroke(1,
796 BasicStroke.CAP_BUTT,
797 BasicStroke.JOIN_ROUND, 3f,
800 g.setColor(Color.RED);
804 g.setStroke(new BasicStroke());
805 g.setColor(group.getOutlineColour());
813 if (sx >= 0 && sx < visWidth)
815 g.drawLine(sx, oldY, sx, sy);
818 if (sx + ex < visWidth)
820 g.drawLine(sx + ex, oldY, sx + ex, sy);
829 if (sx + ex > visWidth)
834 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
836 ex = (endRes - startRes + 1) * av.charWidth;
841 g.drawLine(sx, top, sx + ex, top);
847 g.drawLine(sx, bottom, sx + ex, bottom);
858 sy = offset + ( (i - startSeq) * av.charHeight);
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)
879 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
881 ex = (endRes - startRes + 1) * av.charWidth;
886 g.drawLine(sx, top, sx + ex, top);
892 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
901 g.setStroke(new BasicStroke());
903 if (groupIndex >= av.alignment.getGroups().size())
908 group = (SequenceGroup) av.alignment.getGroups().elementAt(groupIndex);
911 while (groupIndex < av.alignment.getGroups().size());
920 * @param results DOCUMENT ME!
922 public void highlightSearchResults(SearchResults results)
926 searchResults = results;