returns color.white if residueBoxColour is null
[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 \r
6 import java.awt.*;\r
7 import java.util.*;\r
8 \r
9 public class SequenceRenderer implements RendererI\r
10 {\r
11   FontMetrics fm;\r
12 \r
13   public Color getResidueBoxColour(ColourSchemeI cs, SequenceI seq, int i)\r
14   {\r
15    Color c = Color.white;\r
16     try{\r
17        c = cs.findColour(seq, seq.getSequence(i, i + 1), i, null);\r
18     }catch(Exception ex){}\r
19 \r
20     return c;\r
21   }\r
22 \r
23 \r
24   public void drawSequence(Graphics g,ColourSchemeI cs,SequenceI seq,int start, int end, int x1, int y1, double width, int height,boolean showScores, boolean displayBoxes, boolean displayText, boolean colourText,Vector freq, int seqnum) {\r
25 \r
26 \r
27     if (displayBoxes)\r
28         drawBoxes(g,cs,seq,start,end,x1,y1,(int)width, height,freq);\r
29 \r
30     if (displayText)\r
31     {\r
32         fm = g.getFontMetrics();\r
33         drawText(g,cs,colourText,seq,start,end,x1,y1,(int)width,height);\r
34     }\r
35   }\r
36 \r
37   public void drawBoxes(Graphics g,ColourSchemeI cs, SequenceI seq,int start, int end, int x1, int y1, int width, int height,Vector freq) {\r
38     int i      = start;\r
39     int length = seq.getLength();\r
40 \r
41     Color currentColor = Color.WHITE;\r
42 \r
43     int curStart = x1;\r
44     int curWidth = width;\r
45 \r
46   //  int threshold = 80;\r
47 \r
48     while (i <= end && i < length) {\r
49       Color c = getResidueBoxColour(cs,seq,i);\r
50 \r
51     //  Hashtable hash  = (Hashtable)freq.elementAt(i-start);\r
52     //  String    s     = (String)hash.get("maxResidue");\r
53     //  int       count = ((Integer)hash.get("maxCount")).intValue();\r
54   //    int       max   = ((Integer)hash.get("size")).intValue();\r
55    //   int       nongap = ((Integer)hash.get("nongap")).intValue();\r
56    //   float     frac  = (float)(count*1.0/(1.0*nongap));\r
57 \r
58       //System.out.println("Frac/count/nongap " + frac + " " + count + " " + nongap);\r
59     /*  if (!seq.getSequence().substring(i,i+1).equals(s) ||\r
60           s.equals("-") ||\r
61           s.equals(".") ||\r
62           s.equals(" "))\r
63       {\r
64         c = Color.white;\r
65       } else {\r
66         if (frac > 0.9) {\r
67           c = Color.red;\r
68         } else if (frac > 0.8) {\r
69           c = Color.orange;\r
70         } else if (frac > 0.7) {\r
71           c = Color.pink;\r
72         } else if (frac > 0.5) {\r
73           c = Color.yellow;\r
74         } else if (frac> 0.3) {\r
75             c = Color.lightGray;\r
76         }\r
77       }*/\r
78 \r
79       if (c != currentColor || c != null)\r
80       {\r
81         g.fillRect(x1+width*(curStart-start),y1,curWidth,height);\r
82 \r
83         currentColor = c;\r
84         g.setColor(c);\r
85 \r
86         curStart = i;\r
87         curWidth = width;\r
88       }\r
89       else\r
90         curWidth += width;\r
91 \r
92       i++;\r
93     }\r
94     g.fillRect(x1+width*(curStart-start),y1,curWidth,height);\r
95   }\r
96 \r
97   public void drawText(Graphics g, ColourSchemeI cs, boolean colText, SequenceI seq,int start, int end, int x1, int y1, int width, int height)\r
98   {\r
99     int pady = height/5;\r
100     int charOffset=0;\r
101     g.setColor(Color.black);\r
102 \r
103     char s;\r
104     // Need to find the sequence position here.\r
105     for (int i = start; i <= end; i++)\r
106     {\r
107         if (i < end && i < seq.getLength())\r
108           s = seq.getSequence().charAt(i);\r
109         else if(i<seq.getLength())\r
110           s = seq.getSequence().charAt(i);\r
111         else\r
112           s = ' ';\r
113 \r
114         if (colText)\r
115         {\r
116             Color c = getResidueBoxColour(cs, seq, i);\r
117             g.setColor(c.darker());\r
118         }\r
119 \r
120       charOffset =  (width - fm.charWidth(s))/2;\r
121 \r
122 \r
123       g.drawString(String.valueOf(s), charOffset + x1 + width * (i - start), y1 + height - pady);\r
124     }\r
125   }\r
126 \r
127 }\r