2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.gui;
\r
22 import java.awt.image.*;
\r
23 import javax.swing.*;
\r
25 import jalview.analysis.*;
\r
26 import jalview.datamodel.*;
\r
28 public class SeqCanvas
\r
32 SequenceRenderer sr;
\r
38 boolean showScores = false;
\r
39 boolean displaySearch = false;
\r
40 int[] searchResults = null;
\r
43 boolean fastPaint = false;
\r
47 public SeqCanvas(AlignViewport av)
\r
50 fr = new FeatureRenderer(av);
\r
51 sr = new SequenceRenderer(av);
\r
52 setLayout(new BorderLayout());
\r
53 PaintRefresher.Register(this);
\r
56 void drawNorthScale(Graphics g, int startx, int endx, int ypos)
\r
58 int scalestartx = startx - (startx % 10) + 10;
\r
60 g.setColor(Color.black);
\r
63 for (int i = scalestartx; i < endx; i += 10)
\r
65 String string = String.valueOf(i);
\r
66 g.drawString(string, (i - startx - 1) * av.charWidth,
\r
67 ypos - (av.charHeight / 2));
\r
69 g.drawLine( ( (i - startx - 1) * av.charWidth) + (av.charWidth / 2),
\r
70 (ypos + 2) - (av.charHeight / 2),
\r
71 ( (i - startx - 1) * av.charWidth) + (av.charWidth / 2), ypos -
\r
76 void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
78 FontMetrics fm = getFontMetrics(av.getFont());
\r
79 ypos += av.charHeight;
\r
82 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
84 SequenceI seq = av.alignment.getSequenceAt(i);
\r
88 while (index < endx)
\r
90 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
97 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
104 int x = LABEL_WEST - fm.stringWidth(value + "");
\r
105 g.drawString(value + "", x,
\r
106 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
\r
111 void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
113 ypos += av.charHeight;
\r
116 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
118 SequenceI seq = av.alignment.getSequenceAt(i);
\r
122 while (index > startx)
\r
124 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
131 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
138 g.drawString(value + "", 0,
\r
139 (ypos + (i * av.charHeight)) - (av.charHeight / 5));
\r
144 public void fastPaint(int horizontal, int vertical)
\r
146 if ( ( (horizontal == 0) && (vertical == 0)) || (gg == null))
\r
151 gg.copyArea(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,
\r
152 -vertical * av.charHeight);
\r
154 int sr = av.startRes;
\r
155 int er = av.endRes;
\r
156 int ss = av.startSeq;
\r
157 int es = av.endSeq;
\r
161 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
163 transX = (er - sr - horizontal) * av.charWidth;
\r
164 sr = er - horizontal;
\r
166 else if (horizontal < 0)
\r
168 er = sr - horizontal;
\r
170 else if (vertical > 0) // scroll down
\r
172 ss = es - vertical;
\r
174 if (ss < av.startSeq)
\r
175 { // ie scrolling too fast, more than a page at a time
\r
180 transY = imgHeight - (vertical * av.charHeight);
\r
183 else if (vertical < 0)
\r
185 es = ss - vertical;
\r
187 if (es > av.endSeq)
\r
193 gg.translate(transX, transY);
\r
195 gg.setColor(Color.white);
\r
196 gg.fillRect(0, 0, (er - sr + 1) * av.charWidth,
\r
197 (es - ss) * av.charHeight);
\r
198 drawPanel(gg, sr, er, ss, es, sr, ss, 0);
\r
199 gg.translate( -transX, -transY);
\r
206 * Definitions of startx and endx (hopefully):
\r
207 * SMJS This is what I'm working towards!
\r
208 * startx is the first residue (starting at 0) to display.
\r
209 * endx is the last residue to display (starting at 0).
\r
210 * starty is the first sequence to display (starting at 0).
\r
211 * endy is the last sequence to display (starting at 0).
\r
212 * NOTE 1: The av limits are set in setFont in this class and
\r
213 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
215 public void paintComponent(Graphics g)
\r
217 g.setColor(Color.white);
\r
218 g.fillRect(0, 0, getWidth(), getHeight());
\r
222 g.drawImage(img, 0, 0, this);
\r
228 // this draws the whole of the alignment
\r
229 imgWidth = getWidth();
\r
230 imgHeight = getHeight();
\r
232 imgWidth -= (imgWidth % av.charWidth);
\r
233 imgHeight -= (imgHeight % av.charHeight);
\r
235 if ( (imgWidth < 1) || (imgHeight < 1))
\r
240 img = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_RGB);
\r
241 gg = (Graphics2D) img.getGraphics();
\r
242 gg.setFont(av.getFont());
\r
243 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
\r
244 RenderingHints.VALUE_ANTIALIAS_ON);
\r
246 gg.setColor(Color.white);
\r
247 gg.fillRect(0, 0, imgWidth, imgHeight);
\r
249 chunkWidth = getWrappedCanvasWidth(getWidth());
\r
250 chunkHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
252 av.setChunkHeight(chunkHeight);
\r
253 av.setChunkWidth(chunkWidth);
\r
255 if (av.getWrapAlignment())
\r
257 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
\r
261 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq,
\r
262 av.startRes, av.startSeq, 0);
\r
265 g.drawImage(img, 0, 0, this);
\r
268 public int getWrappedCanvasWidth(int cwidth)
\r
270 FontMetrics fm = getFontMetrics(av.getFont());
\r
275 if (av.scaleRightWrapped)
\r
277 LABEL_EAST = fm.stringWidth(av.alignment.getWidth() + "000");
\r
280 if (av.scaleLeftWrapped)
\r
282 LABEL_WEST = fm.stringWidth(av.alignment.getWidth() + "");
\r
285 return (cwidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
288 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight,
\r
291 AlignmentI al = av.getAlignment();
\r
293 FontMetrics fm = getFontMetrics(av.getFont());
\r
295 int LABEL_EAST = 0;
\r
297 if (av.scaleRightWrapped)
\r
299 LABEL_EAST = fm.stringWidth(al.getWidth() + "000");
\r
302 int LABEL_WEST = 0;
\r
304 if (av.scaleLeftWrapped)
\r
306 LABEL_WEST = fm.stringWidth(al.getWidth() + "0");
\r
309 int cWidth = (canvasWidth - LABEL_EAST - LABEL_WEST) / av.charWidth;
\r
310 int cHeight = (av.getAlignment().getHeight() + 2) * av.charHeight;
\r
312 av.endRes = av.startRes + cWidth;
\r
314 int endx = (startRes + cWidth) - 1;
\r
315 int ypos = 2 * av.charHeight;
\r
317 while ( (ypos <= canvasHeight) && (startRes < av.alignment.getWidth()))
\r
319 g.setColor(Color.black);
\r
321 if (av.scaleLeftWrapped)
\r
323 drawWestScale(g, startRes, endx, ypos);
\r
326 if (av.scaleRightWrapped)
\r
328 g.translate(canvasWidth - LABEL_EAST + av.charWidth, 0);
\r
329 drawEastScale(g, startRes, endx, ypos);
\r
330 g.translate( - (canvasWidth - LABEL_EAST + av.charWidth), 0);
\r
333 g.translate(LABEL_WEST, 0);
\r
335 if (av.scaleAboveWrapped)
\r
337 drawNorthScale(g, startRes, endx, ypos);
\r
340 // When printing we have an extra clipped region,
\r
341 // the Printable page which we need to account for here
\r
342 Shape clip = g.getClip();
\r
346 g.setClip(0, 0, cWidth * av.charWidth, canvasHeight);
\r
350 g.setClip(0, (int) clip.getBounds().getY(),
\r
351 cWidth * av.charWidth, (int) clip.getBounds().getHeight());
\r
354 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
356 g.translate( -LABEL_WEST, 0);
\r
359 startRes += cWidth;
\r
360 endx = (startRes + cWidth) - 1;
\r
362 if (endx > al.getWidth())
\r
364 endx = al.getWidth();
\r
369 synchronized public void drawPanel(Graphics g1, int x1, int x2, int y1,
\r
370 int y2, int startx, int starty, int offset)
\r
372 Graphics2D g = (Graphics2D) g1;
\r
373 g.setFont(av.getFont());
\r
374 sr.renderGaps(av.renderGaps);
\r
378 /// First draw the sequences
\r
379 /////////////////////////////
\r
380 for (int i = y1; i < y2; i++)
\r
382 nextSeq = av.alignment.getSequenceAt(i);
\r
384 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups(nextSeq),
\r
385 x1, x2, (x1 - startx) * av.charWidth,
\r
387 AlignmentUtil.getPixelHeight(starty, i, av.charHeight),
\r
388 av.charWidth, av.charHeight);
\r
390 if (av.showSequenceFeatures)
\r
392 fr.drawSequence(g, nextSeq,
\r
393 av.alignment.findAllGroups(nextSeq), x1, x2,
\r
394 (x1 - startx) * av.charWidth,
\r
396 AlignmentUtil.getPixelHeight(starty, i, av.charHeight),
\r
397 av.charWidth, av.charHeight);
\r
402 /////////////////////////////////////
\r
403 // Now outline any areas if necessary
\r
404 /////////////////////////////////////
\r
405 SequenceGroup group = av.getSelectionGroup();
\r
406 java.util.Vector groups = av.alignment.getGroups();
\r
411 int groupIndex = -1;
\r
413 if ( (group == null) && (groups.size() > 0))
\r
415 group = (SequenceGroup) groups.elementAt(0);
\r
425 boolean inGroup = false;
\r
429 for (i = y1; i < y2; i++)
\r
431 sx = (group.getStartRes() - startx) * av.charWidth;
\r
433 AlignmentUtil.getPixelHeight(starty, i, av.charHeight);
\r
434 ex = ( ( (group.getEndRes() + 1) - group.getStartRes()) *
\r
438 if ( (sx < getWidth()) && (ex > 0) &&
\r
439 group.sequences.contains(av.alignment.getSequenceAt(
\r
442 if ( (bottom == -1) &&
\r
443 !group.sequences.contains(
\r
444 av.alignment.getSequenceAt(i + 1)))
\r
446 bottom = sy + av.charHeight;
\r
451 if ( ( (top == -1) && (i == 0)) ||
\r
452 !group.sequences.contains(
\r
453 av.alignment.getSequenceAt(i - 1)))
\r
461 if (group == av.getSelectionGroup())
\r
463 g.setStroke(new BasicStroke(1,
\r
464 BasicStroke.CAP_BUTT,
\r
465 BasicStroke.JOIN_ROUND, 3f,
\r
468 g.setColor(Color.RED);
\r
472 g.setStroke(new BasicStroke());
\r
473 g.setColor(group.getOutlineColour());
\r
481 g.drawLine(sx, oldY, sx, sy);
\r
482 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
486 g.drawLine(sx, top, sx + ex, top);
\r
492 g.drawLine(sx, bottom, sx + ex, bottom);
\r
505 g.drawLine(sx, top, sx + ex, top);
\r
511 g.drawLine(sx, bottom - 1, sx + ex, bottom - 1);
\r
516 AlignmentUtil.getPixelHeight(starty, i, av.charHeight);
\r
517 g.drawLine(sx, oldY, sx, sy);
\r
518 g.drawLine(sx + ex, oldY, sx + ex, sy);
\r
524 if (groupIndex >= groups.size())
\r
529 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
531 while (groupIndex < groups.size());
\r
534 /// Highlight search Results once all sequences have been drawn
\r
535 //////////////////////////////////////////////////////////
\r
538 for (int r = 0; r < searchResults.length; r += 3)
\r
540 int searchSeq = searchResults[r];
\r
542 if ( (searchSeq >= y1) && (searchSeq < y2))
\r
544 SequenceI seq = av.getAlignment().getSequenceAt(searchSeq);
\r
546 int searchStart = seq.findIndex(searchResults[r + 1]) - 1;
\r
547 int searchEnd = seq.findIndex(searchResults[r + 2]) - 1;
\r
549 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
551 if (searchStart < x1)
\r
556 if (searchEnd > x2)
\r
561 ssr.drawHighlightedText(seq, searchStart, searchEnd,
\r
562 (searchStart - startx) * av.charWidth,
\r
564 AlignmentUtil.getPixelHeight(starty,
\r
566 av.charHeight), av.charWidth, av.charHeight);
\r
572 public void highlightSearchResults(int[] results)
\r
574 // results are in the order sequence, startRes, endRes
\r
575 if (results == null)
\r
577 displaySearch = false;
\r
581 displaySearch = true;
\r
584 searchResults = results;
\r