4 import java.awt.image.*;
\r
5 import javax.swing.*;
\r
6 import jalview.datamodel.*;
\r
7 import jalview.analysis.*;
\r
10 public class SeqCanvas extends JComponent
\r
20 boolean showScores = false;
\r
21 boolean displaySearch = false;
\r
22 int [] searchResults = null;
\r
27 boolean fastPaint = false;
\r
30 public SeqCanvas(AlignViewport av)
\r
33 fr = new FeatureRenderer(av);
\r
34 setLayout(new BorderLayout());
\r
35 PaintRefresher.Register(this);
\r
39 void drawNorthScale(Graphics g, int startx, int endx,int ypos) {
\r
40 int scalestartx = startx - startx % 10 + 10;
\r
42 g.setColor(Color.black);
\r
45 for (int i = scalestartx; i < endx; i += 10)
\r
47 String string = String.valueOf(i);
\r
48 g.drawString(string, (i - startx - 1) * av.charWidth,
\r
49 ypos - av.charHeight / 2);
\r
51 g.drawLine( (i - startx - 1) * av.charWidth + av.charWidth / 2,
\r
52 ypos + 2 - av.charHeight / 2,
\r
53 (i - startx - 1) * av.charWidth + av.charWidth / 2, ypos - 2);
\r
58 void drawWestScale(Graphics g, int startx, int endx, int ypos)
\r
60 FontMetrics fm = getFontMetrics(av.getFont());
\r
61 ypos+= av.charHeight;
\r
63 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
65 SequenceI seq = av.alignment.getSequenceAt(i);
\r
68 while (index < endx)
\r
70 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
76 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
81 int x = LABEL_WEST - fm.stringWidth(value+"");
\r
82 g.drawString(value + "", x, ypos + i*av.charHeight - av.charHeight/5);
\r
87 void drawEastScale(Graphics g, int startx, int endx, int ypos)
\r
89 ypos+= av.charHeight;
\r
91 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
93 SequenceI seq = av.alignment.getSequenceAt(i);
\r
96 while (index > startx)
\r
98 if (jalview.util.Comparison.isGap(seq.getCharAt(index)))
\r
104 value = av.alignment.getSequenceAt(i).findPosition(index);
\r
108 g.drawString(value + "", 0, ypos + i*av.charHeight - av.charHeight/5);
\r
117 public void fastPaint(int horizontal, int vertical)
\r
119 if (horizontal == 0 && vertical == 0 || gg==null)
\r
122 gg.copyArea(0, 0, imgWidth, imgHeight, -horizontal * av.charWidth,
\r
123 -vertical * av.charHeight);
\r
125 int sr = av.startRes, er = av.endRes, ss = av.startSeq, es = av.endSeq,
\r
126 transX = 0, transY = 0;
\r
127 if (horizontal > 0) // scrollbar pulled right, image to the left
\r
129 transX = (er - sr - horizontal) * av.charWidth;
\r
130 sr = er - horizontal;
\r
132 else if (horizontal < 0)
\r
133 er = sr - horizontal;
\r
135 else if (vertical > 0) // scroll down
\r
137 ss = es - vertical;
\r
138 if(ss<av.startSeq) // ie scrolling too fast, more than a page at a time
\r
141 transY = imgHeight - vertical * av.charHeight;
\r
143 else if (vertical < 0)
\r
145 es = ss - vertical;
\r
151 gg.translate(transX, transY);
\r
153 gg.setColor(Color.white);
\r
154 gg.fillRect(0,0, (er-sr)*av.charWidth, (es-ss)*av.charHeight);
\r
155 drawPanel(gg, sr, er, ss, es, sr, ss, 0);
\r
156 gg.translate( -transX, -transY);
\r
164 * Definitions of startx and endx (hopefully):
\r
165 * SMJS This is what I'm working towards!
\r
166 * startx is the first residue (starting at 0) to display.
\r
167 * endx is the last residue to display (starting at 0).
\r
168 * starty is the first sequence to display (starting at 0).
\r
169 * endy is the last sequence to display (starting at 0).
\r
170 * NOTE 1: The av limits are set in setFont in this class and
\r
171 * in the adjustment listener in SeqPanel when the scrollbars move.
\r
174 public void paintComponent(Graphics g)
\r
176 g.setColor(Color.white);
\r
177 g.fillRect(0, 0, getWidth(), getHeight());
\r
181 g.drawImage(img, 0, 0, this);
\r
186 // this draws the whole of the alignment
\r
187 imgWidth = getWidth();
\r
188 imgHeight = getHeight();
\r
189 if(imgWidth==0 || imgHeight==0)
\r
192 imgWidth -= imgWidth%av.charWidth;
\r
193 imgHeight-= imgHeight%av.charHeight;
\r
195 img = new BufferedImage(imgWidth,imgHeight,BufferedImage.TYPE_INT_RGB);
\r
196 gg = (Graphics2D)img.getGraphics();
\r
197 gg.setFont(av.getFont());
\r
198 gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
\r
200 gg.setColor(Color.white);
\r
201 gg.fillRect(0,0,imgWidth,imgHeight);
\r
203 chunkWidth = getWrappedCanvasWidth( getWidth() );
\r
204 chunkHeight = (av.getAlignment().getHeight() + 2)*av.charHeight;
\r
206 av.setChunkHeight(chunkHeight);
\r
207 av.setChunkWidth(chunkWidth);
\r
210 if (av.getWrapAlignment())
\r
211 drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);
\r
213 drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes, av.startSeq, 0);
\r
215 g.drawImage(img, 0, 0, this);
\r
219 int LABEL_WEST, LABEL_EAST;
\r
220 public int getWrappedCanvasWidth(int cwidth)
\r
222 FontMetrics fm = getFontMetrics(av.getFont());
\r
227 if(av.scaleRightWrapped)
\r
228 LABEL_EAST = fm.stringWidth( av.alignment.getWidth()+"000" );
\r
230 if(av.scaleLeftWrapped)
\r
231 LABEL_WEST = fm.stringWidth( av.alignment.getWidth()+"" );
\r
233 return (cwidth - LABEL_EAST -LABEL_WEST)/av.charWidth;
\r
236 public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight, int startRes)
\r
238 AlignmentI al = av.getAlignment();
\r
240 FontMetrics fm = getFontMetrics(av.getFont());
\r
242 int LABEL_EAST = 0;
\r
243 if(av.scaleRightWrapped)
\r
244 LABEL_EAST = fm.stringWidth( al.getWidth()+"000" );
\r
245 int LABEL_WEST = 0;
\r
246 if(av.scaleLeftWrapped)
\r
247 LABEL_WEST = fm.stringWidth(al.getWidth()+"0");
\r
250 int cWidth = (canvasWidth - LABEL_EAST -LABEL_WEST)/av.charWidth;
\r
251 int cHeight = (av.getAlignment().getHeight() + 2)*av.charHeight;
\r
253 int endx = startRes+cWidth-1;
\r
254 int ypos = 2*av.charHeight;
\r
256 while (ypos <= canvasHeight && startRes<av.alignment.getWidth() )
\r
258 g.setColor(Color.black);
\r
260 if(av.scaleLeftWrapped)
\r
261 drawWestScale(g, startRes, endx, ypos);
\r
263 if(av.scaleRightWrapped)
\r
265 g.translate(canvasWidth - LABEL_EAST +av.charWidth, 0);
\r
266 drawEastScale(g, startRes, endx, ypos);
\r
267 g.translate( - (canvasWidth - LABEL_EAST+av.charWidth), 0);
\r
270 g.translate(LABEL_WEST,0);
\r
271 if(av.scaleAboveWrapped)
\r
272 drawNorthScale(g, startRes, endx, ypos);
\r
275 // When printing we have an extra clipped region,
\r
276 // the Printable page which we need to account for here
\r
277 Shape clip = g.getClip();
\r
279 g.setClip(0, 0, cWidth*av.charWidth, canvasHeight);
\r
282 (int)clip.getBounds().getY(),
\r
283 cWidth*av.charWidth,
\r
284 (int)clip.getBounds().getHeight()
\r
287 drawPanel(g, startRes, endx, 0, al.getHeight(), startRes, 0, ypos);
\r
289 g.translate(-LABEL_WEST,0);
\r
292 startRes += cWidth;
\r
293 endx = startRes + cWidth - 1;
\r
295 if (endx > al.getWidth())
\r
296 endx = al.getWidth();
\r
302 synchronized public void drawPanel(Graphics g1,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {
\r
304 Graphics2D g = (Graphics2D)g1;
\r
305 g.setFont(av.getFont());
\r
306 RendererI sr = av.getRenderer();
\r
310 /// First draw the sequences
\r
311 /////////////////////////////
\r
312 for (int i = y1 ; i < y2 ;i++)
\r
314 nextSeq = av.alignment.getSequenceAt(i);
\r
316 sr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ),x1,x2,
\r
317 (x1 - startx) * av.charWidth,
\r
318 offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight),
\r
319 av.charWidth,av.charHeight,null, i);
\r
321 if(av.showSequenceFeatures)
\r
323 fr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ), x1, x2,
\r
324 (x1 - startx) * av.charWidth,
\r
326 AlignmentUtil.getPixelHeight(starty, i, av.charHeight),
\r
327 av.charWidth, av.charHeight, null, i);
\r
331 /////////////////////////////////////
\r
333 // Now outline any areas if necessary
\r
334 /////////////////////////////////////
\r
335 SequenceGroup group = av.getSelectionGroup();
\r
336 java.util.Vector groups = av.alignment.getGroups();
\r
338 int sx = -1, sy = -1, ex = -1;
\r
339 int groupIndex = -1;
\r
340 if (group == null && groups.size() > 0)
\r
342 group = (SequenceGroup) groups.elementAt(0);
\r
351 boolean inGroup = false;
\r
352 int top=-1, bottom =-1;
\r
353 for (i = y1; i < y2; i++)
\r
355 sx = (group.getStartRes() - startx) * av.charWidth;
\r
356 sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);
\r
357 ex = (group.getEndRes() + 1 - group.getStartRes()) * av.charWidth -1;
\r
359 if (sx < getWidth()
\r
361 && group.sequences.contains(av.alignment.getSequenceAt(i)))
\r
363 if (bottom == -1 &&
\r
364 !group.sequences.contains(av.alignment.getSequenceAt(i + 1)))
\r
365 bottom = sy + av.charHeight ;
\r
369 if (top == -1 && i==0 ||
\r
370 !group.sequences.contains(av.alignment.getSequenceAt(i - 1)))
\r
376 if (group == av.getSelectionGroup())
\r
378 g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3f, new float[]{5f,3f}, 0f ));
\r
379 g.setColor(Color.RED);
\r
383 g.setStroke(new BasicStroke());
\r
384 g.setColor(group.getOutlineColour());
\r
392 g.drawLine(sx, oldY, sx, sy );
\r
393 g.drawLine(sx+ex, oldY, sx+ex, sy );
\r
397 g.drawLine(sx, top, sx + ex, top);
\r
402 g.drawLine(sx, bottom, sx + ex, bottom);
\r
417 g.drawLine(sx, top, sx + ex, top);
\r
422 g.drawLine(sx, bottom-1, sx + ex, bottom-1);
\r
426 sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);
\r
427 g.drawLine(sx, oldY, sx, sy );
\r
428 g.drawLine(sx+ex, oldY, sx+ex, sy );
\r
432 if (groupIndex >= groups.size())
\r
435 group = (SequenceGroup) groups.elementAt(groupIndex);
\r
438 while (groupIndex < groups.size());
\r
441 /// Highlight search Results once all sequences have been drawn
\r
442 //////////////////////////////////////////////////////////
\r
445 for(int r=0; r<searchResults.length; r+=3)
\r
447 int searchSeq = searchResults[r];
\r
448 int searchStart = searchResults[r+1];
\r
449 int searchEnd = searchResults[r+2];
\r
451 if (searchSeq >= y1 && searchSeq < y2)
\r
453 SequenceRenderer ssr = (SequenceRenderer) sr;
\r
458 ssr.drawHighlightedText(av.getAlignment().getSequenceAt(searchSeq),
\r
461 (searchStart - startx) * av.charWidth,
\r
463 AlignmentUtil.getPixelHeight(starty, searchSeq,
\r
475 public void highlightSearchResults(int [] results)
\r
477 // results are in the order sequence, startRes, endRes
\r
479 displaySearch = false;
\r
481 displaySearch = true;
\r
483 searchResults = results;
\r