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