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
23 import jalview.datamodel.*;
24 import jalview.schemes.*;
32 public class SequenceRenderer
38 boolean renderGaps = true;
40 SequenceGroup currentSequenceGroup = null;
42 SequenceGroup[] allGroups = null;
48 boolean monospacedFont;
50 boolean forOverview = false;
53 * Creates a new SequenceRenderer object.
58 public SequenceRenderer(AlignViewport av)
69 public void prepare(Graphics g, boolean renderGaps)
72 fm = g.getFontMetrics();
74 // If EPS graphics, stringWidth will be a double, not an int
75 double dwidth = fm.getStringBounds("M", g).getWidth();
77 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && (float) av.charWidth == dwidth);
79 this.renderGaps = renderGaps;
82 public Color getResidueBoxColour(SequenceI seq, int i)
84 allGroups = av.alignment.findAllGroups(seq);
86 if (inCurrentSequenceGroup(i))
88 if (currentSequenceGroup.getDisplayBoxes())
90 getBoxColour(currentSequenceGroup.cs, seq, i);
93 else if (av.getShowBoxes())
95 getBoxColour(av.globalColourScheme, seq, i);
111 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
115 resBoxColour = cs.findColour(seq.getCharAt(i), i);
118 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
120 resBoxColour = Color.lightGray;
124 resBoxColour = Color.white;
150 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
155 drawBoxes(seq, start, end, y1);
157 if (av.validCharWidth)
159 drawText(seq, start, end, y1);
181 public synchronized void drawBoxes(SequenceI seq, int start, int end,
185 int length = seq.getLength();
188 int curWidth = av.charWidth;
190 Color tempColour = null;
194 resBoxColour = Color.white;
198 if (inCurrentSequenceGroup(i))
200 if (currentSequenceGroup.getDisplayBoxes())
202 getBoxColour(currentSequenceGroup.cs, seq, i);
205 else if (av.getShowBoxes())
207 getBoxColour(av.globalColourScheme, seq, i);
212 if (resBoxColour != tempColour)
214 if (tempColour != null)
216 graphics.fillRect(av.charWidth * (curStart - start), y1,
217 curWidth, av.charHeight);
220 graphics.setColor(resBoxColour);
223 curWidth = av.charWidth;
224 tempColour = resBoxColour;
228 curWidth += av.charWidth;
234 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
257 public void drawText(SequenceI seq, int start, int end, int y1)
259 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
263 if (end + 1 >= seq.getLength())
265 end = seq.getLength() - 1;
267 graphics.setColor(av.textColour);
269 if (monospacedFont && av.showText && allGroups.length == 0
270 && !av.getColourText() && av.thresholdTextColour == 0)
274 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
278 char gap = av.getGapCharacter();
279 graphics.drawString(seq.getSequenceAsString(start, end + 1)
280 .replace(gap, ' '), 0, y1);
285 boolean getboxColour = false;
286 for (int i = start; i <= end; i++)
288 graphics.setColor(av.textColour);
289 getboxColour = false;
290 s = seq.getCharAt(i);
291 if (!renderGaps && jalview.util.Comparison.isGap(s))
296 if (inCurrentSequenceGroup(i))
298 if (!currentSequenceGroup.getDisplayText())
303 if (currentSequenceGroup.thresholdTextColour > 0
304 || currentSequenceGroup.getColourText())
307 getBoxColour(currentSequenceGroup.cs, seq, i);
309 if (currentSequenceGroup.getColourText())
311 graphics.setColor(resBoxColour.darker());
314 if (currentSequenceGroup.thresholdTextColour > 0)
316 if (resBoxColour.getRed() + resBoxColour.getBlue()
317 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
319 graphics.setColor(currentSequenceGroup.textColour2);
325 graphics.setColor(currentSequenceGroup.textColour);
331 if (!av.getShowText())
336 if (av.getColourText())
339 getBoxColour(av.globalColourScheme, seq, i);
341 if (av.getShowBoxes())
343 graphics.setColor(resBoxColour.darker());
347 graphics.setColor(resBoxColour);
351 if (av.thresholdTextColour > 0)
355 getBoxColour(av.globalColourScheme, seq, i);
358 if (resBoxColour.getRed() + resBoxColour.getBlue()
359 + resBoxColour.getGreen() < av.thresholdTextColour)
361 graphics.setColor(av.textColour2);
367 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
368 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
381 * @return DOCUMENT ME!
383 boolean inCurrentSequenceGroup(int res)
385 if (allGroups == null)
390 for (int i = 0; i < allGroups.length; i++)
392 if ((allGroups[i].getStartRes() <= res)
393 && (allGroups[i].getEndRes() >= res))
395 currentSequenceGroup = allGroups[i];
422 public void drawHighlightedText(SequenceI seq, int start, int end,
425 int pady = av.charHeight / 5;
427 graphics.setColor(Color.BLACK);
428 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
430 graphics.setColor(Color.white);
434 // Need to find the sequence position here.
435 if (av.validCharWidth)
437 for (int i = start; i <= end; i++)
439 if (i < seq.getLength())
441 s = seq.getCharAt(i);
444 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
446 .drawString(String.valueOf(s), charOffset + x1
447 + (av.charWidth * (i - start)),
448 (y1 + av.charHeight) - pady);
453 public void drawCursor(SequenceI seq, int res, int x1, int y1)
455 int pady = av.charHeight / 5;
457 graphics.setColor(Color.black);
458 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
460 if (av.validCharWidth)
462 graphics.setColor(Color.white);
464 char s = seq.getCharAt(res);
466 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
467 graphics.drawString(String.valueOf(s), charOffset + x1,
468 (y1 + av.charHeight) - pady);