2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
23 import jalview.api.AlignViewportI;
24 import jalview.renderer.OverviewRenderer;
25 import jalview.viewmodel.OverviewDimensions;
27 import java.awt.AlphaComposite;
28 import java.awt.Color;
29 import java.awt.Dimension;
30 import java.awt.Graphics;
31 import java.awt.Graphics2D;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.awt.image.BufferedImage;
36 import javax.swing.JComponent;
37 import javax.swing.Timer;
39 public class OverviewCanvas extends JComponent
41 private static final long RUNNING_TIME = 2000;
43 private static final int SPEED = 40;
45 private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
47 // This is set true if the alignment view changes whilst
48 // the overview is being calculated
49 private volatile boolean restart = false;
51 private volatile boolean updaterunning = false;
53 private BufferedImage miniMe;
55 private BufferedImage lastMiniMe = null;
57 private BufferedImage veryLastMiniMe = null;
59 // Can set different properties in this seqCanvas than
60 // main visible SeqCanvas
61 private SequenceRenderer sr;
63 private jalview.renderer.seqfeatures.FeatureRenderer fr;
65 private OverviewDimensions od;
67 private OverviewRenderer or = null;
69 private AlignViewportI av;
71 private float alpha = 0f;
73 private long startTime = -1;
75 private final Timer timer;
77 private ProgressPanel progressPanel;
79 public OverviewCanvas(OverviewDimensions overviewDims,
80 AlignViewportI alignvp, ProgressPanel pp)
86 sr = new SequenceRenderer(av);
87 sr.renderGaps = false;
88 sr.forOverview = true;
89 fr = new jalview.renderer.seqfeatures.FeatureRenderer(av);
91 timer = new Timer(SPEED, new ActionListener()
95 public void actionPerformed(ActionEvent e)
99 startTime = System.currentTimeMillis();
104 long time = System.currentTimeMillis();
105 long duration = time - startTime;
106 if (duration >= RUNNING_TIME)
109 ((Timer) e.getSource()).stop();
114 alpha = 1f - ((float) duration / (float) RUNNING_TIME);
123 * Update the overview dimensions object used by the canvas (e.g. if we change
124 * from showing hidden columns to hiding them or vice versa)
126 * @param overviewDims
128 public void resetOviewDims(OverviewDimensions overviewDims)
134 * Signals to drawing code that the associated alignment viewport has changed
135 * and a redraw will be required
137 public boolean restartDraw()
151 updaterunning = true;
158 * Draw the overview sequences
160 * @param showSequenceFeatures
161 * true if sequence features are to be shown
162 * @param showAnnotation
163 * true if the annotation is to be shown
164 * @param transferRenderer
165 * the renderer to transfer feature colouring from
167 public void draw(boolean showSequenceFeatures, boolean showAnnotation,
168 FeatureRenderer transferRenderer)
171 veryLastMiniMe = lastMiniMe;
173 if (showSequenceFeatures)
175 fr.transferSettings(transferRenderer);
178 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
180 or = new OverviewRenderer(sr, fr, od);
181 or.addPropertyChangeListener(progressPanel);
182 miniMe = or.draw(od.getRows(av.getAlignment()),
183 od.getColumns(av.getAlignment()));
185 Graphics mg = miniMe.getGraphics();
189 mg.translate(0, od.getSequencesHeight());
190 or.drawGraph(mg, av.getAlignmentConservationAnnotation(),
191 av.getCharWidth(), od.getGraphHeight(),
192 od.getColumns(av.getAlignment()));
193 mg.translate(0, -od.getSequencesHeight());
197 or.removePropertyChangeListener(progressPanel);
201 draw(showSequenceFeatures, showAnnotation, transferRenderer);
205 updaterunning = false;
213 public void paintComponent(Graphics g)
218 if (lastMiniMe == null)
220 g.setColor(Color.white);
221 g.fillRect(0, 0, getWidth(), getHeight());
225 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
227 g.setColor(TRANS_GREY);
228 g.fillRect(0, 0, getWidth(), getHeight());
230 else if (lastMiniMe != null)
232 if ((getWidth() > 0) && (getHeight() > 0)
233 && ((getWidth() != od.getWidth())
234 || (getHeight() != od.getHeight())))
236 // scale the alignment and annotation separately *** if there is
238 if (od.getGraphHeight() > 0)
240 BufferedImage topImage = lastMiniMe.getSubimage(0, 0,
241 od.getWidth(), od.getSequencesHeight());
242 BufferedImage bottomImage = lastMiniMe.getSubimage(0,
243 od.getSequencesHeight(), od.getWidth(),
244 od.getGraphHeight());
246 // must be done at this point as we rely on using old width/height
247 // above, and new width/height below
248 od.setWidth(getWidth());
249 od.setHeight(getHeight());
251 // stick the images back together so lastMiniMe is consistent in the
252 // event of a repaint - BUT probably not thread safe
253 lastMiniMe = new BufferedImage(od.getWidth(), od.getHeight(),
254 BufferedImage.TYPE_INT_RGB);
255 Graphics lg = lastMiniMe.getGraphics();
256 lg.drawImage(topImage, 0, 0, od.getWidth(),
257 od.getSequencesHeight(), null);
258 lg.drawImage(bottomImage, 0, od.getSequencesHeight(),
259 od.getWidth(), od.getGraphHeight(), this);
264 od.setWidth(getWidth());
265 od.setHeight(getHeight());
268 // scale lastMiniMe to the new size
269 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
271 // make sure the box is in the right place
272 od.setBoxPosition(av.getAlignment().getHiddenSequences(),
273 av.getAlignment().getHiddenColumns());
277 if (alpha != 0) // this is a timer triggered dissolve
279 Graphics2D g2d = (Graphics2D) g.create();
281 // draw the original image
282 g2d.drawImage(veryLastMiniMe, 0, 0, getWidth(), getHeight(),
285 // draw the new image on top with varying degrees of transparency
286 g2d.setComposite(AlphaComposite.SrcOver.derive(1f - alpha));
287 g2d.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
291 else if (lastMiniMe != miniMe)
293 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
294 g.setColor(TRANS_GREY);
295 g.fillRect(0, 0, getWidth(), getHeight());
299 // fall back to normal behaviour
300 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
308 g.setColor(Color.red);