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
19 package jalview.appletgui;
23 import jalview.datamodel.*;
24 import jalview.schemes.*;
26 public class SequenceRenderer
32 boolean renderGaps = true;
34 SequenceGroup currentSequenceGroup = null;
36 SequenceGroup[] allGroups = null;
42 boolean forOverview = false;
44 public SequenceRenderer(AlignViewport av)
55 public void prepare(Graphics g, boolean renderGaps)
58 fm = g.getFontMetrics();
60 this.renderGaps = renderGaps;
63 public Color getResidueBoxColour(SequenceI seq, int i)
65 allGroups = av.alignment.findAllGroups(seq);
67 if (inCurrentSequenceGroup(i))
69 if (currentSequenceGroup.getDisplayBoxes())
71 getBoxColour(currentSequenceGroup.cs, seq, i);
74 else if (av.getShowBoxes())
76 getBoxColour(av.globalColourScheme, seq, i);
82 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
86 resBoxColour = cs.findColour(seq.getCharAt(i), i);
89 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
91 resBoxColour = Color.lightGray;
95 resBoxColour = Color.white;
100 public Color findSequenceColour(SequenceI seq, int i)
102 allGroups = av.alignment.findAllGroups(seq);
103 drawBoxes(seq, i, i, 0);
107 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
117 drawBoxes(seq, start, end, y1);
119 if (av.validCharWidth)
121 drawText(seq, start, end, y1);
125 public void drawBoxes(SequenceI seq, int start, int end, int y1)
128 int length = seq.getLength();
131 int curWidth = av.charWidth;
133 Color tempColour = null;
136 resBoxColour = Color.white;
139 if (inCurrentSequenceGroup(i))
141 if (currentSequenceGroup.getDisplayBoxes())
143 getBoxColour(currentSequenceGroup.cs, seq, i);
146 else if (av.getShowBoxes())
148 getBoxColour(av.getGlobalColourScheme(), seq, i);
152 if (resBoxColour != tempColour)
154 if (tempColour != null)
156 graphics.fillRect(av.charWidth * (curStart - start), y1,
157 curWidth, av.charHeight);
159 graphics.setColor(resBoxColour);
162 curWidth = av.charWidth;
163 tempColour = resBoxColour;
168 curWidth += av.charWidth;
174 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
178 public void drawText(SequenceI seq, int start, int end, int y1)
180 Font boldFont = null;
181 boolean bold = false;
182 if (av.upperCasebold)
184 boldFont = new Font(av.getFont().getName(), Font.BOLD, av.charHeight);
186 graphics.setFont(av.getFont());
189 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
193 // Need to find the sequence position here.
194 if (end + 1 >= seq.getLength())
196 end = seq.getLength() - 1;
201 for (int i = start; i <= end; i++)
203 graphics.setColor(Color.black);
205 s = seq.getCharAt(i);
206 if (!renderGaps && jalview.util.Comparison.isGap(s))
211 if (inCurrentSequenceGroup(i))
213 if (!currentSequenceGroup.getDisplayText())
218 if (currentSequenceGroup.getColourText())
220 getBoxColour(currentSequenceGroup.cs, seq, i);
221 graphics.setColor(resBoxColour.darker());
226 if (!av.getShowText())
231 if (av.getColourText())
233 getBoxColour(av.getGlobalColourScheme(), seq, i);
234 if (av.getShowBoxes())
236 graphics.setColor(resBoxColour.darker());
240 graphics.setColor(resBoxColour);
245 if (av.upperCasebold)
247 fm = graphics.getFontMetrics();
248 if ('A' <= s && s <= 'Z')
253 graphics.setFont(boldFont);
259 graphics.setFont(av.font);
265 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
266 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
272 boolean inCurrentSequenceGroup(int res)
274 if (allGroups == null)
279 for (int i = 0; i < allGroups.length; i++)
281 if (allGroups[i].getStartRes() <= res
282 && allGroups[i].getEndRes() >= res)
284 currentSequenceGroup = allGroups[i];
292 public void drawHighlightedText(SequenceI seq, int start, int end,
295 int pady = av.charHeight / 5;
297 graphics.setColor(Color.black);
298 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
300 graphics.setColor(Color.white);
303 // Need to find the sequence position here.
304 if (av.validCharWidth)
306 for (int i = start; i <= end; i++)
308 if (i < seq.getLength())
310 s = seq.getCharAt(i);
313 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
314 graphics.drawString(String.valueOf(s), charOffset + x1
315 + av.charWidth * (i - start), y1 + av.charHeight - pady);
320 public void drawCursor(SequenceI seq, int res, int x1, int y1)
322 int pady = av.charHeight / 5;
324 graphics.setColor(Color.black);
325 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
326 graphics.setColor(Color.white);
328 graphics.setColor(Color.white);
330 char s = seq.getCharAt(res);
331 if (av.validCharWidth)
334 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
335 graphics.drawString(String.valueOf(s), charOffset + x1,
336 (y1 + av.charHeight) - pady);