GetResidueBoxColour altered for faster rendering
[jalview.git] / src / jalview / gui / SequenceRenderer.java
1 package jalview.gui;\r
2 \r
3 import jalview.datamodel.*;\r
4 import jalview.schemes.*;\r
5 import java.awt.*;\r
6 import java.util.*;\r
7 \r
8 public class SequenceRenderer implements RendererI\r
9 {\r
10   AlignViewport av;\r
11   FontMetrics fm;\r
12   boolean renderGaps = true;\r
13   SequenceGroup currentSequenceGroup = null;\r
14   Color resBoxColour = Color.white;\r
15   Graphics graphics;\r
16 \r
17   public SequenceRenderer(AlignViewport av)\r
18   {\r
19     this.av = av;\r
20   }\r
21 \r
22 \r
23   public void renderGaps(boolean b)\r
24   {\r
25     renderGaps = b;\r
26   }\r
27 \r
28   public Color getResidueBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
29   {\r
30     getBoxColour(cs, seq, i);\r
31     return resBoxColour;\r
32   }\r
33 \r
34   void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
35   {\r
36 \r
37    if (cs != null)\r
38        resBoxColour = cs.findColour(seq.getSequence(i, i + 1), i, av.getConsensus(false));\r
39    else\r
40        resBoxColour = Color.white;\r
41   }\r
42 \r
43   public void drawSequence(Graphics g,SequenceI seq,SequenceGroup sg, int start, int end, int x1, int y1, int width, int height, Vector pid, int seqnum)\r
44   {\r
45     currentSequenceGroup = sg;\r
46 \r
47     graphics = g;\r
48 \r
49     drawBoxes(seq, start, end, x1, y1, (int) width, height, pid);\r
50 \r
51     fm = g.getFontMetrics();\r
52     drawText(seq,start,end,x1,y1,(int)width,height);\r
53 \r
54   }\r
55 \r
56   public void drawBoxes(SequenceI seq,int start, int end, int x1, int y1, int width, int height,Vector freq) {\r
57     int i      = start;\r
58     int length = seq.getLength();\r
59 \r
60     int curStart = x1;\r
61     int curWidth = width;\r
62 \r
63     Color tempColour = Color.red;\r
64     while (i <= end && i < length)\r
65     {\r
66       if(inCurrentSequenceGroup(i))\r
67       {\r
68         if( currentSequenceGroup.getDisplayBoxes())\r
69              getBoxColour(currentSequenceGroup.cs, seq, i);\r
70       }\r
71       else if(av.getShowBoxes())\r
72            getBoxColour(av.getGlobalColourScheme(), seq, i);\r
73 \r
74       if (resBoxColour != tempColour)\r
75       {\r
76         graphics.fillRect(x1+width*(curStart-start),y1,curWidth,height);\r
77         graphics.setColor(resBoxColour);\r
78 \r
79         curStart = i;\r
80         curWidth = width;\r
81         tempColour = resBoxColour;\r
82 \r
83       }\r
84       else\r
85         curWidth += width;\r
86 \r
87       i++;\r
88     }\r
89     graphics.fillRect(x1+width*(curStart-start),y1,curWidth,height);\r
90   }\r
91 \r
92   public void drawText(SequenceI seq,int start, int end, int x1, int y1, int width, int height)\r
93   {\r
94     int pady = height/5;\r
95     int charOffset=0;\r
96     char s;\r
97     // Need to find the sequence position here.\r
98     for (int i = start; i <= end; i++)\r
99     {\r
100         if(i<seq.getLength())\r
101           s = seq.getSequence().charAt(i);\r
102         else\r
103           s = ' ';\r
104 \r
105         if(!renderGaps && jalview.util.Comparison.isGap(s))\r
106           continue;\r
107 \r
108         graphics.setColor(Color.black);\r
109 \r
110         if (inCurrentSequenceGroup(i))\r
111         {\r
112           if(!currentSequenceGroup.getDisplayText())\r
113             continue;\r
114 \r
115           if(currentSequenceGroup.getColourText())\r
116             graphics.setColor(resBoxColour.darker());\r
117         }\r
118         else\r
119         {\r
120           if(!av.getShowText())\r
121             continue;\r
122 \r
123           if(av.getColourText())\r
124             graphics.setColor(resBoxColour.darker());\r
125         }\r
126 \r
127       charOffset =  (width - fm.charWidth(s))/2;\r
128       graphics.drawString(String.valueOf(s), charOffset + x1 + width * (i - start), y1 + height - pady);\r
129     }\r
130 \r
131 \r
132   }\r
133 \r
134   boolean inCurrentSequenceGroup(int res)\r
135   {\r
136     if(currentSequenceGroup==null)\r
137       return false;\r
138 \r
139     return (currentSequenceGroup.getStartRes()<=res && currentSequenceGroup.getEndRes()>=res)?true:false;\r
140   }\r
141 \r
142   public void drawHighlightedText(SequenceI seq,int start, int end, int x1, int y1, int width, int height)\r
143   {\r
144     int pady = height/5;\r
145     int charOffset=0;\r
146     graphics.setColor(Color.BLACK);\r
147     graphics.fillRect(x1,y1,width*(end-start+1),height);\r
148     graphics.setColor(Color.white);\r
149 \r
150     char s='~';\r
151     // Need to find the sequence position here.\r
152     for (int i = start; i <= end; i++)\r
153     {\r
154        if(i<seq.getLength())\r
155           s = seq.getSequence().charAt(i);\r
156 \r
157       charOffset =  (width - fm.charWidth(s))/2;\r
158       graphics.drawString(String.valueOf(s), charOffset + x1 + width * (i - start), y1 + height - pady);\r
159     }\r
160   }\r
161 \r
162 \r
163 }\r