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));
216 * @param horizontal DOCUMENT ME!
217 * @param vertical DOCUMENT ME!
219 public void fastPaint(int horizontal, int vertical)
228 gg.copyArea(horizontal * av.charWidth,
229 vertical * av.charHeight,
232 -horizontal * av.charWidth,
233 -vertical * av.charHeight);
235 int sr = av.startRes;
237 int ss = av.startSeq;
242 if (horizontal > 0) // scrollbar pulled right, image to the left
245 transX = (er - sr - horizontal) * av.charWidth;
246 sr = er - horizontal;
248 else if (horizontal < 0)
250 er = sr - horizontal - 1;
252 else if (vertical > 0) // scroll down
256 if (ss < av.startSeq)
257 { // ie scrolling too fast, more than a page at a time
262 transY = imgHeight - (vertical * av.charHeight);
265 else if (vertical < 0)
275 gg.translate(transX, transY);
276 drawPanel(gg, sr, er, ss, es, 0);
277 gg.translate( -transX, -transY);
283 * Definitions of startx and endx (hopefully):
284 * SMJS This is what I'm working towards!
285 * startx is the first residue (starting at 0) to display.
286 * endx is the last residue to display (starting at 0).
287 * starty is the first sequence to display (starting at 0).
288 * endy is the last sequence to display (starting at 0).
289 * NOTE 1: The av limits are set in setFont in this class and
290 * in the adjustment listener in SeqPanel when the scrollbars move.
293 // Set this to false to force a full panel paint
294 public void paintComponent(Graphics g)
296 BufferedImage lcimg = img; // take reference since other threads may null img and call later.
297 super.paintComponent(g);
299 if (lcimg != null && (fastPaint
300 || (getVisibleRect().width != g.getClipBounds().width)
301 || (getVisibleRect().height != g.getClipBounds().height)))
303 g.drawImage(lcimg, 0, 0, this);
308 // this draws the whole of the alignment
309 imgWidth = getWidth();
310 imgHeight = getHeight();
312 imgWidth -= (imgWidth % av.charWidth);
313 imgHeight -= (imgHeight % av.charHeight);
315 if ( (imgWidth < 1) || (imgHeight < 1))
320 if (lcimg == null || imgWidth != lcimg.getWidth() || imgHeight != lcimg.getHeight())
324 lcimg = img = new BufferedImage(imgWidth, imgHeight,
325 BufferedImage.TYPE_INT_RGB);
326 gg = (Graphics2D) img.getGraphics();
327 gg.setFont(av.getFont());
329 catch (OutOfMemoryError er)
332 System.out.println(er + " making image, SeqCanvas");
333 javax.swing.SwingUtilities.invokeLater(new Runnable()
337 javax.swing.JOptionPane.showInternalMessageDialog(Desktop.
339 "Out of memory creating alignment image!!"
341 "\nSee help files for increasing Java Virtual Machine memory."
343 javax.swing.JOptionPane.WARNING_MESSAGE);
353 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
354 RenderingHints.VALUE_ANTIALIAS_ON);
357 gg.setColor(Color.white);
358 gg.fillRect(0, 0, imgWidth, imgHeight);
360 if (av.getWrapAlignment())
362 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
366 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, 0);
369 g.drawImage(lcimg, 0, 0, this);
377 * @param cwidth DOCUMENT ME!
379 * @return DOCUMENT ME!
381 public int getWrappedCanvasWidth(int cwidth)
383 FontMetrics fm = getFontMetrics(av.getFont());
388 if (av.scaleRightWrapped)
390 LABEL_EAST = fm.stringWidth(getMask());
393 if (av.scaleLeftWrapped)
395 LABEL_WEST = fm.stringWidth(getMask());
398 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
402 * Generates a string of zeroes.
410 for (int i = 0; i < av.alignment.getHeight(); i++)
412 tmp = av.alignment.getSequenceAt(i).getEnd();
419 for (int i = maxWidth; i > 0; i /= 10)
429 * @param g DOCUMENT ME!
430 * @param canvasWidth DOCUMENT ME!
431 * @param canvasHeight DOCUMENT ME!
432 * @param startRes DOCUMENT ME!
434 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
437 AlignmentI al = av.getAlignment();
439 FontMetrics fm = getFontMetrics(av.getFont());
441 if (av.scaleRightWrapped)
443 LABEL_EAST = fm.stringWidth(getMask());
446 if (av.scaleLeftWrapped)
448 LABEL_WEST = fm.stringWidth(getMask());
451 int hgap = av.charHeight;
452 if (av.scaleAboveWrapped)
454 hgap += av.charHeight;
457 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
458 int cHeight = av.getAlignment().getHeight() * av.charHeight;
460 av.setWrappedWidth(cWidth);
462 av.endRes = av.startRes + cWidth;
466 int maxwidth = av.alignment.getWidth() - 1;
468 if (av.hasHiddenColumns)
470 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
473 while ( (ypos <= canvasHeight) && (startRes < maxwidth))
475 endx = startRes + cWidth - 1;
482 g.setFont(av.getFont());
483 g.setColor(Color.black);
485 if (av.scaleLeftWrapped)
487 drawWestScale(g, startRes, endx, ypos);
490 if (av.scaleRightWrapped)
492 g.translate(canvasWidth - LABEL_EAST, 0);
493 drawEastScale(g, startRes, endx, ypos);
494 g.translate( - (canvasWidth - LABEL_EAST), 0);
497 g.translate(LABEL_WEST, 0);
499 if (av.scaleAboveWrapped)
501 drawNorthScale(g, startRes, endx, ypos);
504 if (av.hasHiddenColumns && av.showHiddenMarkers)
506 g.setColor(Color.blue);
508 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size();
511 res = av.getColumnSelection().findHiddenRegionPosition(i) -
514 if (res < 0 || res > endx - startRes)
519 gg.fillPolygon(new int[]
520 {res * av.charWidth - av.charHeight / 4,
521 res * av.charWidth + av.charHeight / 4,
525 ypos - (av.charHeight / 2),
526 ypos - (av.charHeight / 2),
527 ypos - (av.charHeight / 2) + 8
533 // When printing we have an extra clipped region,
534 // the Printable page which we need to account for here
535 Shape clip = g.getClip();
539 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
543 g.setClip(0, (int) clip.getBounds().getY(),
544 cWidth * av.charWidth, (int) clip.getBounds().getHeight());
547 drawPanel(g, startRes, endx, 0, al.getHeight(), ypos);
549 if (av.showAnnotation)
551 g.translate(0, cHeight + ypos + 3);
552 if (annotations == null)
554 annotations = new AnnotationPanel(av);
557 annotations.drawComponent( (Graphics2D) g, startRes, endx + 1);
558 g.translate(0, -cHeight - ypos - 3);
561 g.translate( -LABEL_WEST, 0);
563 ypos += cHeight + getAnnotationHeight() + hgap;
569 AnnotationPanel annotations;
570 int getAnnotationHeight()
572 if (!av.showAnnotation)
577 if (annotations == null)
579 annotations = new AnnotationPanel(av);
582 return annotations.adjustPanelHeight();
588 * @param g1 DOCUMENT ME!
589 * @param startRes DOCUMENT ME!
590 * @param endRes DOCUMENT ME!
591 * @param startSeq DOCUMENT ME!
592 * @param endSeq DOCUMENT ME!
593 * @param offset DOCUMENT ME!
595 void drawPanel(Graphics g1, int startRes, int endRes,
596 int startSeq, int endSeq, int offset)
598 if (!av.hasHiddenColumns)
600 draw(g1, startRes, endRes, startSeq, endSeq, offset);
604 java.util.Vector regions = av.getColumnSelection().getHiddenColumns();
607 int blockStart = startRes;
608 int blockEnd = endRes;
610 for (int i = 0; i < regions.size(); i++)
612 int[] region = (int[]) regions.elementAt(i);
613 int hideStart = region[0];
614 int hideEnd = region[1];
616 if (hideStart <= blockStart)
618 blockStart += (hideEnd - hideStart) + 1;
622 blockEnd = hideStart - 1;
624 g1.translate(screenY * av.charWidth, 0);
626 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
628 if (av.getShowHiddenMarkers())
630 g1.setColor(Color.blue);
632 g1.drawLine( (blockEnd - blockStart + 1) * av.charWidth - 1,
634 (blockEnd - blockStart + 1) * av.charWidth - 1,
635 (endSeq - startSeq) * av.charHeight + offset);
638 g1.translate( -screenY * av.charWidth, 0);
639 screenY += blockEnd - blockStart + 1;
640 blockStart = hideEnd + 1;
643 if (screenY <= (endRes - startRes))
645 blockEnd = blockStart + (endRes - startRes) - screenY;
646 g1.translate(screenY * av.charWidth, 0);
647 draw(g1, blockStart, blockEnd, startSeq, endSeq, offset);
649 g1.translate( -screenY * av.charWidth, 0);
655 //int startRes, int endRes, int startSeq, int endSeq, int x, int y,
656 // int x1, int x2, int y1, int y2, int startx, int starty,
657 void draw(Graphics g,
658 int startRes, int endRes,
659 int startSeq, int endSeq,
662 g.setFont(av.getFont());
663 sr.prepare(g, av.renderGaps);
667 /// First draw the sequences
668 /////////////////////////////
669 for (int i = startSeq; i < endSeq; i++)
671 nextSeq = av.alignment.getSequenceAt(i);
673 sr.drawSequence(nextSeq, av.alignment.findAllGroups(nextSeq),
675 offset + ( (i - startSeq) * av.charHeight));
677 if (av.showSequenceFeatures)
679 fr.drawSequence(g, nextSeq, startRes, endRes,
680 offset + ( (i - startSeq) * av.charHeight));
683 /// Highlight search Results once all sequences have been drawn
684 //////////////////////////////////////////////////////////
685 if (searchResults != null)
687 int[] visibleResults = searchResults.getResults(nextSeq, startRes,
689 if (visibleResults != null)
691 for (int r = 0; r < visibleResults.length; r += 2)
693 sr.drawHighlightedText(nextSeq, visibleResults[r],
694 visibleResults[r + 1],
695 (visibleResults[r] - startRes) *
697 offset + ( (i - startSeq) * av.charHeight));
702 if (av.cursorMode && cursorY == i
703 && cursorX >= startRes && cursorX <= endRes)
705 sr.drawCursor(nextSeq, cursorX, (cursorX - startRes) * av.charWidth,
706 offset + ( (i - startSeq) * av.charHeight));
710 if (av.getSelectionGroup() != null || av.alignment.getGroups().size() > 0)
712 drawGroupsBoundaries(g, startRes, endRes, startSeq, endSeq, offset);
717 void drawGroupsBoundaries(Graphics g1,
718 int startRes, int endRes,
719 int startSeq, int endSeq,
722 Graphics2D g = (Graphics2D) g1;
724 /////////////////////////////////////
725 // Now outline any areas if necessary
726 /////////////////////////////////////
727 SequenceGroup group = av.getSelectionGroup();
733 int visWidth = (endRes - startRes + 1) * av.charWidth;
735 if ( (group == null) && (av.alignment.getGroups().size() > 0))
737 group = (SequenceGroup) av.alignment.getGroups().elementAt(0);
747 boolean inGroup = false;
751 for (i = startSeq; i < endSeq; i++)
753 sx = (group.getStartRes() - startRes) * av.charWidth;
754 sy = offset + ( (i - startSeq) * av.charHeight);
755 ex = ( ( (group.getEndRes() + 1) - group.getStartRes()) *
759 if (sx + ex < 0 || sx > visWidth)
764 if ( (sx <= (endRes - startRes) * av.charWidth) &&
765 group.getSequences(null).
766 contains(av.alignment.getSequenceAt(i)))
768 if ( (bottom == -1) &&
769 !group.getSequences(null).contains(
770 av.alignment.getSequenceAt(i + 1)))
772 bottom = sy + av.charHeight;
777 if ( ( (top == -1) && (i == 0)) ||
778 !group.getSequences(null).contains(
779 av.alignment.getSequenceAt(i - 1)))
787 if (group == av.getSelectionGroup())
789 g.setStroke(new BasicStroke(1,
790 BasicStroke.CAP_BUTT,
791 BasicStroke.JOIN_ROUND, 3f,
794 g.setColor(Color.RED);
798 g.setStroke(new BasicStroke());
799 g.setColor(group.getOutlineColour());
807 if (sx >= 0 && sx < visWidth)
809 g.drawLine(sx, oldY, sx, sy);
812 if (sx + ex < visWidth)
814 g.drawLine(sx + ex, oldY, sx + ex, sy);
823 if (sx + ex > visWidth)
828 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
830 ex = (endRes - startRes + 1) * av.charWidth;
835 g.drawLine(sx, top, sx + ex, top);
841 g.drawLine(sx, bottom, sx + ex, bottom);
852 sy = offset + ( (i - startSeq) * av.charHeight);
853 if (sx >= 0 && sx < visWidth)
855 g.drawLine(sx, oldY, sx, sy);
858 if (sx + ex < visWidth)
860 g.drawLine(sx + ex, oldY, sx + ex, sy);
869 if (sx + ex > visWidth)
873 else if (sx + ex >= (endRes - startRes + 1) * av.charWidth)
875 ex = (endRes - startRes + 1) * av.charWidth;
880 g.drawLine(sx, top, sx + ex, top);
886 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
895 g.setStroke(new BasicStroke());
897 if (groupIndex >= av.alignment.getGroups().size())
902 group = (SequenceGroup) av.alignment.getGroups().elementAt(groupIndex);
905 while (groupIndex < av.alignment.getGroups().size());
914 * @param results DOCUMENT ME!
916 public void highlightSearchResults(SearchResults results)
920 searchResults = results;