scale above, left right added
[jalview.git] / src / jalview / gui / IdCanvas.java
1 package jalview.gui;\r
2 \r
3 import java.awt.*;\r
4 import java.awt.Graphics2D.*;\r
5 import java.awt.image.*;\r
6 import javax.swing.*;\r
7 import jalview.datamodel.*;\r
8 import jalview.analysis.*;\r
9 public class IdCanvas extends JPanel\r
10 {\r
11   protected AlignViewport av;\r
12 \r
13   protected boolean showScores  = true;\r
14 \r
15   protected int     maxIdLength = -1;\r
16   protected String  maxIdStr    = null;\r
17   BufferedImage image;\r
18   Graphics2D gg;\r
19   int imgHeight=0;\r
20   boolean fastPaint = false;\r
21 \r
22   public IdCanvas(AlignViewport av)\r
23   {\r
24     setLayout(new BorderLayout());\r
25     this.av         = av;\r
26     PaintRefresher.Register(this);\r
27   }\r
28 \r
29   public void drawIdString(Graphics2D gg,SequenceI s,int i, int starty, int ypos) {\r
30       int charHeight = av.getCharHeight();\r
31 \r
32 \r
33       if (av.getSelectionGroup()!=null && av.getSelectionGroup().sequences.contains(s)) {\r
34           gg.setColor(Color.lightGray);\r
35           gg.fillRect(0,AlignmentUtil.getPixelHeight(starty,i,charHeight)+ ypos,getWidth(),charHeight);\r
36           gg.setColor(Color.white);\r
37       } else {\r
38           gg.setColor(s.getColor());\r
39           gg.fillRect(0,AlignmentUtil.getPixelHeight(starty,i,charHeight)+ ypos,getWidth(),charHeight);\r
40           gg.setColor(Color.black);\r
41       }\r
42 \r
43       String string = s.getName();\r
44       if(av.getShowFullId())\r
45         string = s.getDisplayId();\r
46 \r
47       gg.drawString(string,0,AlignmentUtil.getPixelHeight(starty,i,charHeight) + ypos + charHeight-   (charHeight/5));\r
48 \r
49   }\r
50 \r
51   public void fastPaint(int vertical)\r
52   {\r
53     if(gg==null)\r
54     {  repaint(); return;}\r
55 \r
56     gg.copyArea( 0,0, getWidth(), imgHeight, 0, -vertical*av.charHeight );\r
57 \r
58     int ss=av.startSeq, es=av.endSeq, transY = 0;\r
59      if(vertical>0)    // scroll down\r
60      {\r
61        transY = imgHeight - vertical*av.charHeight;\r
62        ss = es - vertical;\r
63      }\r
64      else if(vertical<0)\r
65      {\r
66        es = ss-vertical;\r
67      }\r
68 \r
69         gg.translate(0, transY);\r
70 \r
71         drawIds(ss, es);\r
72 \r
73         gg.translate( 0, -transY );\r
74 \r
75 \r
76       fastPaint = true;\r
77       repaint();\r
78   }\r
79 \r
80   public void paintComponent(Graphics g)\r
81   {\r
82     g.setColor(Color.white);\r
83     g.fillRect(0, 0, getWidth(), getHeight());\r
84 \r
85     if (fastPaint)\r
86     {\r
87       g.drawImage(image, 0, 0, this);\r
88       fastPaint = false;\r
89       return;\r
90     }\r
91 \r
92     imgHeight = getHeight();\r
93     imgHeight -= imgHeight % av.charHeight;\r
94     image = new BufferedImage(getWidth(), imgHeight, BufferedImage.TYPE_INT_RGB);\r
95     gg = (Graphics2D) image.getGraphics();\r
96     //Fill in the background\r
97     gg.setColor(Color.white);\r
98     gg.fillRect(0, 0, getWidth(), imgHeight);\r
99     gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
100                         RenderingHints.VALUE_ANTIALIAS_ON);\r
101     Font italic = new Font(av.getFont().getName(), Font.ITALIC,\r
102                            av.getFont().getSize());\r
103     gg.setFont(italic);\r
104 \r
105     drawIds( av.getStartSeq(), av.endSeq);\r
106 \r
107     g.drawImage(image, 0, 0, this);\r
108   }\r
109 \r
110   void drawIds(int starty, int endy)\r
111   {\r
112 \r
113     Color currentColor     = Color.white;\r
114     Color currentTextColor = Color.black;\r
115 \r
116 \r
117     if (av.getWrapAlignment())\r
118     {\r
119 \r
120           int rowSize =  av.getEndRes() - av.getStartRes();\r
121           // Draw the rest of the panels\r
122 \r
123           for(int ypos=2*av.charHeight, row=av.getEndRes();\r
124               ypos <= getHeight() && row<av.alignment.getWidth();\r
125               ypos += av.chunkHeight, row+=rowSize )\r
126           {\r
127             for (int i = starty; i < av.alignment.getHeight(); i++)\r
128             {\r
129               SequenceI s = av.alignment.getSequenceAt(i);\r
130               drawIdString(gg, s, i, 0, ypos);\r
131             }\r
132           }\r
133 \r
134 \r
135     } else\r
136     {\r
137 \r
138       //Now draw the id strings\r
139       for (int i = starty; i < endy; i++)\r
140       {\r
141         // Selected sequence colours\r
142 \r
143         if (av.getSelectionGroup()!= null\r
144             && av.getSelectionGroup().sequences.contains(av.alignment.getSequenceAt(i)))\r
145         {\r
146        //   if(av.alignment.findGroup(al.getSequenceAt(i)).getEndRes()==\r
147           currentColor = Color.lightGray;\r
148           currentTextColor = Color.black;\r
149         }\r
150         else\r
151         {\r
152           currentColor = av.alignment.getSequenceAt(i).getColor();\r
153           currentTextColor = Color.black;\r
154         }\r
155 \r
156         gg.setColor(currentColor);\r
157 \r
158         gg.fillRect(0,\r
159                     AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
160                     getWidth(),\r
161                     av.charHeight);\r
162 \r
163         gg.setColor(currentTextColor);\r
164         String string = av.alignment.getSequenceAt(i).getName();\r
165         if(av.getShowFullId())\r
166           string = av.alignment.getSequenceAt(i).getDisplayId();\r
167         gg.drawString(string, 0,\r
168                       AlignmentUtil.getPixelHeight(starty, i, av.charHeight) +\r
169                       av.charHeight - (av.charHeight / 5));\r
170       }\r
171 \r
172       // add a border\r
173       gg.setColor(Color.white);\r
174       gg.fillRect(getWidth()-4,0,4,getHeight());\r
175     }\r
176 \r
177   }\r
178 }\r