fastPaint modified incase scroll is more than page size
[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        ss = es - vertical;\r
62        if(ss<av.startSeq) // ie scrolling too fast, more than a page at a time\r
63          ss = av.startSeq;\r
64        else\r
65          transY = imgHeight - vertical * av.charHeight;\r
66      }\r
67      else if (vertical < 0)\r
68      {\r
69        es = ss - vertical;\r
70        if(es > av.endSeq)\r
71          es = av.endSeq;\r
72      }\r
73 \r
74 \r
75 \r
76         gg.translate(0, transY);\r
77 \r
78         drawIds(ss, es);\r
79 \r
80         gg.translate( 0, -transY );\r
81 \r
82 \r
83       fastPaint = true;\r
84       repaint();\r
85   }\r
86 \r
87   public void paintComponent(Graphics g)\r
88   {\r
89     g.setColor(Color.white);\r
90     g.fillRect(0, 0, getWidth(), getHeight());\r
91 \r
92     if (fastPaint)\r
93     {\r
94       fastPaint = false;\r
95       g.drawImage(image, 0, 0, this);\r
96       return;\r
97     }\r
98 \r
99     imgHeight = getHeight();\r
100     imgHeight -= imgHeight % av.charHeight;\r
101     image = new BufferedImage(getWidth(), imgHeight, BufferedImage.TYPE_INT_RGB);\r
102     gg = (Graphics2D) image.getGraphics();\r
103     //Fill in the background\r
104     gg.setColor(Color.white);\r
105     gg.fillRect(0, 0, getWidth(), imgHeight);\r
106     gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
107                         RenderingHints.VALUE_ANTIALIAS_ON);\r
108     Font italic = new Font(av.getFont().getName(), Font.ITALIC,\r
109                            av.getFont().getSize());\r
110     gg.setFont(italic);\r
111 \r
112     drawIds( av.getStartSeq(), av.endSeq);\r
113 \r
114     g.drawImage(image, 0, 0, this);\r
115   }\r
116 \r
117   void drawIds(int starty, int endy)\r
118   {\r
119     Color currentColor     = Color.white;\r
120     Color currentTextColor = Color.black;\r
121 \r
122     if (av.getWrapAlignment())\r
123     {\r
124 \r
125           int rowSize =  av.getEndRes() - av.getStartRes();\r
126           // Draw the rest of the panels\r
127 \r
128           for(int ypos=2*av.charHeight, row=av.getEndRes()-rowSize;\r
129               ypos <= getHeight() && row<av.alignment.getWidth();\r
130               ypos += av.chunkHeight, row+=rowSize )\r
131           {\r
132             for (int i = starty; i < av.alignment.getHeight(); i++)\r
133             {\r
134               SequenceI s = av.alignment.getSequenceAt(i);\r
135               drawIdString(gg, s, i, 0, ypos);\r
136             }\r
137           }\r
138 \r
139 \r
140     } else\r
141     {\r
142 \r
143       //Now draw the id strings\r
144       for (int i = starty; i < endy; i++)\r
145       {\r
146         // Selected sequence colours\r
147 \r
148         if (av.getSelectionGroup()!= null\r
149             && av.getSelectionGroup().sequences.contains(av.alignment.getSequenceAt(i)))\r
150         {\r
151        //   if(av.alignment.findGroup(al.getSequenceAt(i)).getEndRes()==\r
152           currentColor = Color.lightGray;\r
153           currentTextColor = Color.black;\r
154         }\r
155         else\r
156         {\r
157           currentColor = av.alignment.getSequenceAt(i).getColor();\r
158           currentTextColor = Color.black;\r
159         }\r
160 \r
161         gg.setColor(currentColor);\r
162 \r
163         gg.fillRect(0,\r
164                     AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
165                     getWidth(),\r
166                     av.charHeight);\r
167 \r
168         gg.setColor(currentTextColor);\r
169         String string = av.alignment.getSequenceAt(i).getName();\r
170         if(av.getShowFullId())\r
171           string = av.alignment.getSequenceAt(i).getDisplayId();\r
172         gg.drawString(string, 0,\r
173                       AlignmentUtil.getPixelHeight(starty, i, av.charHeight) +\r
174                       av.charHeight - (av.charHeight / 5));\r
175       }\r
176 \r
177       // add a border\r
178       gg.setColor(Color.white);\r
179       gg.fillRect(getWidth()-4,0,4,getHeight());\r
180     }\r
181 \r
182   }\r
183 }\r