fastPaint flag 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     if (av.getWrapAlignment())\r
117     {\r
118           // Draw the rest of the panels\r
119           int chunkHeight =  (av.alignment.getHeight() + 2)*av.charHeight;\r
120           int row = av.getStartRes() / av.chunkWidth ;\r
121           for(int ypos=2*av.charHeight;\r
122               ypos <= getHeight() && row*av.chunkWidth<av.alignment.getWidth();\r
123               ypos += chunkHeight, row++ )\r
124           {\r
125             for (int i = starty; i < av.alignment.getHeight(); i++)\r
126             {\r
127               SequenceI s = av.alignment.getSequenceAt(i);\r
128               drawIdString(gg, s, i, 0, ypos);\r
129             }\r
130           }\r
131 \r
132 \r
133     } else\r
134     {\r
135 \r
136       //Now draw the id strings\r
137       for (int i = starty; i < endy; i++)\r
138       {\r
139         // Selected sequence colours\r
140 \r
141         if (av.getSelectionGroup()!= null\r
142             && av.getSelectionGroup().sequences.contains(av.alignment.getSequenceAt(i)))\r
143         {\r
144        //   if(av.alignment.findGroup(al.getSequenceAt(i)).getEndRes()==\r
145           currentColor = Color.lightGray;\r
146           currentTextColor = Color.black;\r
147         }\r
148         else\r
149         {\r
150           currentColor = av.alignment.getSequenceAt(i).getColor();\r
151           currentTextColor = Color.black;\r
152         }\r
153 \r
154         gg.setColor(currentColor);\r
155 \r
156         gg.fillRect(0,\r
157                     AlignmentUtil.getPixelHeight(starty, i, av.charHeight),\r
158                     getWidth(),\r
159                     av.charHeight);\r
160 \r
161         gg.setColor(currentTextColor);\r
162         String string = av.alignment.getSequenceAt(i).getName();\r
163         if(av.getShowFullId())\r
164           string = av.alignment.getSequenceAt(i).getDisplayId();\r
165         gg.drawString(string, 0,\r
166                       AlignmentUtil.getPixelHeight(starty, i, av.charHeight) +\r
167                       av.charHeight - (av.charHeight / 5));\r
168       }\r
169 \r
170       // add a border\r
171       gg.setColor(Color.white);\r
172       gg.fillRect(getWidth()-4,0,4,getHeight());\r
173     }\r
174 \r
175   }\r
176 }\r