Fast paint method implemented
[jalview.git] / src / jalview / gui / SeqCanvas.java
1 package jalview.gui;\r
2 \r
3 import java.awt.*;\r
4 import javax.swing.*;\r
5 import jalview.datamodel.*;\r
6 import jalview.analysis.*;\r
7 \r
8 \r
9 public class SeqCanvas extends JPanel\r
10 {\r
11      FeatureRenderer fr;\r
12     Image             img;\r
13     Graphics2D          gg;\r
14     int               imgWidth;\r
15     int               imgHeight;\r
16 \r
17     AlignViewport     av;\r
18 \r
19     public boolean paintFlag = false;\r
20 \r
21     boolean showScores = false;\r
22     boolean displaySearch = false;\r
23     int [] searchResults = null;\r
24 \r
25     int chunkHeight;\r
26     int chunkWidth;\r
27 \r
28 \r
29     public SeqCanvas(AlignViewport av)\r
30     {\r
31         this.av         = av;\r
32        fr = new FeatureRenderer(av);\r
33        setLayout(new BorderLayout());\r
34        PaintRefresher.Register(this);\r
35 \r
36     }\r
37 \r
38   public void drawScale(Graphics g, int startx, int endx,int ypos) {\r
39       int scalestartx = startx - startx%10 + 10;\r
40 \r
41       g.setColor(Color.black);\r
42 \r
43       for (int i=scalestartx;i < endx;i+= 10)\r
44       {\r
45           String string = String.valueOf(i);\r
46           g.drawString(string,(i-startx-1)*av.charWidth,ypos - av.charHeight/2);\r
47 \r
48           g.drawLine( (i-startx-1)*av.charWidth +av.charWidth/2, ypos+2 - av.charHeight/2,\r
49                     (i-startx-1)*av.charWidth +av.charWidth/2, ypos-2 );\r
50 \r
51       }\r
52 \r
53   }\r
54 \r
55 \r
56 public void fastPaint(int horizontal, int vertical)\r
57 {\r
58     if(horizontal==0 && vertical==0)\r
59    {\r
60      return;\r
61    }\r
62 \r
63 \r
64    if (img==null || paintFlag)\r
65    {\r
66       repaint();\r
67       return;\r
68    }\r
69 \r
70     gg.copyArea( 0,0, imgWidth, imgHeight, -horizontal*av.charWidth, -vertical*av.charHeight );\r
71 \r
72  int sr=av.startRes, er=av.endRes+1, ss = av.startSeq, es = av.endSeq, transX=0, transY = 0;\r
73  if(horizontal>0) // scrollbar pulled right, image to the left\r
74  {\r
75    transX = (er-sr-horizontal)*av.charWidth;\r
76    sr = er - horizontal ;\r
77  }\r
78  else if(horizontal<0)\r
79  {\r
80    er = sr-horizontal;\r
81  }\r
82 \r
83 \r
84 \r
85 \r
86  if(vertical>0)    // scroll down\r
87  {\r
88    transY = imgHeight - vertical*av.charHeight;\r
89    ss = es - vertical;\r
90  }\r
91  else if(vertical<0)\r
92  {\r
93 \r
94      es = ss-vertical;\r
95 \r
96  }\r
97 \r
98     gg.translate(transX, transY);\r
99 \r
100     drawPanel(gg, sr,er,ss,es,sr,ss,0);\r
101 \r
102     gg.translate( -transX, -transY );\r
103     getGraphics().drawImage(img,0,0,this);\r
104 \r
105 }\r
106 \r
107 /**\r
108  * Definitions of startx and endx (hopefully):\r
109  * SMJS This is what I'm working towards!\r
110  *   startx is the first residue (starting at 0) to display.\r
111  *   endx   is the last residue to display (starting at 0).\r
112  *   starty is the first sequence to display (starting at 0).\r
113  *   endy   is the last sequence to display (starting at 0).\r
114  * NOTE 1: The av limits are set in setFont in this class and\r
115  * in the adjustment listener in SeqPanel when the scrollbars move.\r
116  */\r
117 \r
118   public void paintComponent(Graphics g)\r
119   {\r
120     // this draws the whole of the alignment\r
121 \r
122       imgWidth  = getWidth();\r
123       imgHeight = getHeight();\r
124 \r
125       imgWidth -= imgWidth%av.charWidth;\r
126       imgHeight-= imgHeight%av.charHeight;\r
127 \r
128       img = createImage(imgWidth,imgHeight);\r
129       gg  = (Graphics2D)img.getGraphics();\r
130       gg.setFont(av.getFont());\r
131       gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);\r
132       gg.setClip(0,0,imgWidth, imgHeight);\r
133       paintFlag = false;\r
134 \r
135 \r
136     chunkWidth  =   getWidth()/av.charWidth;\r
137     chunkHeight =  (av.getAlignment().getHeight() + 2)*av.charHeight;\r
138 \r
139     av.setChunkHeight(chunkHeight);\r
140     av.setChunkWidth(chunkWidth);\r
141 \r
142     g.setColor(Color.WHITE);\r
143     g.fillRect(0,0,getWidth(), getHeight());\r
144 \r
145     if (av.getWrapAlignment())\r
146       drawWrappedPanel(gg, getWidth(), getHeight(), av.startRes);\r
147     else\r
148       drawPanel(gg, av.startRes, av.endRes, av.startSeq, av.endSeq, av.startRes, av.startSeq, 0);\r
149 \r
150 \r
151     g.drawImage(img,0,0,this);\r
152 \r
153   }\r
154 \r
155   public void drawWrappedPanel(Graphics g, int canvasWidth, int canvasHeight, int startRes)\r
156   {\r
157       AlignmentI da = av.getAlignment();\r
158 \r
159       int cWidth  =   canvasWidth/av.charWidth;\r
160       int cHeight =  (av.getAlignment().getHeight() + 2)*av.charHeight;\r
161 \r
162       int  endx   = startRes+cWidth-1;\r
163       int ypos  = 2*av.charHeight;\r
164 \r
165       while (ypos <= canvasHeight)\r
166       {\r
167         drawScale(g, startRes, endx, ypos);\r
168         drawPanel(g, startRes, endx, 0, da.getHeight(), startRes, 0, ypos);\r
169 \r
170         ypos += cHeight;\r
171         startRes += cWidth;\r
172         endx = startRes + cWidth - 1;\r
173 \r
174         if (endx > da.getWidth())\r
175           endx = da.getWidth();\r
176       }\r
177 \r
178   }\r
179 \r
180 \r
181   public void drawPanel(Graphics g1,int x1,int x2, int y1, int y2,int startx, int starty,int offset) {\r
182 \r
183 \r
184    Graphics2D g = (Graphics2D)g1;\r
185     g.setFont(av.getFont());\r
186     RendererI sr = av.getRenderer();\r
187 \r
188     /*Vector    pid    = av.getConsensus(false);\r
189     Vector tmpseq = new Vector();\r
190     for (int i = 0; i < av.getAlignment().getHeight(); i++)\r
191         if (!av.getSelection().contains(av.getAlignment().getSequenceAt(i)))\r
192             tmpseq.addElement(av.getAlignment().getSequenceAt(i));\r
193 \r
194     if (sr instanceof SequenceRenderer)\r
195         pid    = AAFrequency.calculate(tmpseq,x1,x2);\r
196 \r
197     else if (sr instanceof GraphRenderer)\r
198         pid = AAFrequency.calculatePID(av.getAlignment().getSequenceAt(0),\r
199                                        av.getAlignment().getSequences(),\r
200                                        av.getPIDWindow(),x1,x2);\r
201 \r
202 */\r
203 \r
204    /* if (y2 > starty && y1 < av.getEndSeq())\r
205     {\r
206        fillBackground(g,\r
207                    Color.red,\r
208                    (x1-startx)*charWidth,\r
209                    offset + AlignmentUtil.getPixelHeight(starty,y1,av.getCharHeight()),\r
210                    (x2-x1+1)*charWidth,\r
211                    offset + AlignmentUtil.getPixelHeight(y1,y2,av.getCharHeight()));\r
212     }*/\r
213 \r
214     SequenceI nextSeq;\r
215 \r
216     /// First draw the sequences\r
217     /////////////////////////////\r
218     for (int i = y1 ; i < y2 ;i++)\r
219     {\r
220      nextSeq = av.getAlignment().getSequenceAt(i);\r
221 \r
222      sr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ),x1,x2,\r
223                  (x1 - startx) * av.charWidth,\r
224                  offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
225                  av.charWidth,av.charHeight,null, i);\r
226 \r
227      if(av.showSequenceFeatures)\r
228      {\r
229        fr.drawSequence(g, nextSeq, av.alignment.findAllGroups( nextSeq ), x1, x2,\r
230                        (x1 - startx) * av.charWidth,\r
231                        offset +\r
232                        AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
233                        av.charWidth, av.charHeight, null, i);\r
234      }\r
235     }\r
236     //\r
237     /////////////////////////////////////\r
238 \r
239     // Now outline any areas if necessary\r
240     /////////////////////////////////////\r
241     SequenceGroup group = av.getSelectionGroup();\r
242     java.util.Vector groups = av.alignment.getGroups();\r
243 \r
244     int sx = -1, sy = -1, ex = -1;\r
245     int groupIndex = -1;\r
246     if (group == null && groups.size() > 0)\r
247     {\r
248       group = (SequenceGroup) groups.elementAt(0);\r
249       groupIndex = 0;\r
250     }\r
251 \r
252     if (group != null)\r
253       do\r
254       {\r
255         int oldY = -1;\r
256         int i = 0;\r
257         boolean inGroup = false;\r
258         for (i = y1; i < y2; i++)\r
259         {\r
260           sx = (group.getStartRes() - startx) * av.charWidth;\r
261           sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
262           ex = (group.getEndRes() + 1 - group.getStartRes()) * av.charWidth;\r
263 \r
264           if (group.sequences.contains(av.alignment.getSequenceAt(i))\r
265               && sx < getWidth()\r
266               && ex > 0)\r
267           {\r
268             if (!inGroup)\r
269             {\r
270               oldY = sy;\r
271               inGroup = true;\r
272               if (group == av.getSelectionGroup())\r
273               {\r
274             //    g.setStroke(new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 3f, new float[]{5f,2f,2f}, 0f ));\r
275                 g.setColor(Color.RED);\r
276               }\r
277               else\r
278               {\r
279                 g.setStroke(new BasicStroke());\r
280                 g.setColor(group.getOutlineColour());\r
281               }\r
282             }\r
283           }\r
284           else\r
285           {\r
286             if (inGroup)\r
287             {\r
288               g.drawRect(sx, oldY, ex, (sy - oldY));\r
289               inGroup = false;\r
290             }\r
291           }\r
292         }\r
293         if (inGroup)\r
294         {\r
295           sy = offset + AlignmentUtil.getPixelHeight(starty, i, av.charHeight);\r
296           g.drawRect(sx, oldY, ex, (sy - oldY));\r
297           inGroup = false;\r
298         }\r
299 \r
300         groupIndex++;\r
301         if (groupIndex >= groups.size())\r
302           break;\r
303 \r
304         group = (SequenceGroup) groups.elementAt(groupIndex);\r
305 \r
306       }\r
307       while (groupIndex < groups.size());\r
308 \r
309 \r
310     /// Highlight search Results once all sequences have been drawn\r
311     //////////////////////////////////////////////////////////\r
312     if(displaySearch)\r
313     {\r
314       for(int r=0; r<searchResults.length; r+=3)\r
315       {\r
316         int searchSeq = searchResults[r];\r
317         int searchStart = searchResults[r+1];\r
318         int searchEnd = searchResults[r+2];\r
319 \r
320         if (searchSeq >= y1 && searchSeq < y2)\r
321         {\r
322           SequenceRenderer ssr = (SequenceRenderer) sr;\r
323           ssr.drawHighlightedText(av.getAlignment().getSequenceAt(searchSeq),\r
324                                   searchStart,\r
325                                   searchEnd,\r
326                                   (searchStart - startx) * av.charWidth,\r
327                                   offset +\r
328                                   AlignmentUtil.getPixelHeight(starty, searchSeq,\r
329               av.charHeight),\r
330                                   av.charWidth,\r
331                                   av.charHeight);\r
332         }\r
333       }\r
334     }\r
335 \r
336   }\r
337 \r
338 \r
339   public int getChunkWidth() {\r
340     return chunkWidth;\r
341   }\r
342 \r
343   public void highlightSearchResults(int [] results)\r
344   {\r
345     // results are in the order sequence, startRes, endRes\r
346     if(results==null)\r
347       displaySearch = false;\r
348     else\r
349       displaySearch = true;\r
350 \r
351     searchResults = results;\r
352 \r
353     paintFlag = true;\r
354     repaint();\r
355   }\r
356 \r
357 \r
358 }\r