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.Graphics;
30 import java.awt.Graphics2D;
31 import java.awt.event.ActionEvent;
32 import java.awt.event.ActionListener;
33 import java.awt.image.BufferedImage;
35 import javax.swing.JComponent;
36 import javax.swing.Timer;
38 public class OverviewCanvas extends JComponent
40 private static final long RUNNING_TIME = 2000;
42 private static final int SPEED = 40;
44 private static final Color TRANS_GREY = new Color(100, 100, 100, 25);
46 // This is set true if the alignment view changes whilst
47 // the overview is being calculated
48 private volatile boolean restart = false;
50 private volatile boolean updaterunning = false;
52 private BufferedImage miniMe;
54 private BufferedImage lastMiniMe = null;
56 private BufferedImage veryLastMiniMe = null;
58 // Can set different properties in this seqCanvas than
59 // main visible SeqCanvas
60 private SequenceRenderer sr;
62 private jalview.renderer.seqfeatures.FeatureRenderer fr;
64 private OverviewDimensions od;
66 private OverviewRenderer or = null;
68 private AlignViewportI av;
70 private float alpha = 0f;
72 private long startTime = -1;
74 private final Timer timer;
76 private ProgressPanel progressPanel;
78 public OverviewCanvas(OverviewDimensions overviewDims,
79 AlignViewportI alignvp, ProgressPanel pp)
85 sr = new SequenceRenderer(av);
86 sr.renderGaps = false;
87 sr.forOverview = true;
88 fr = new jalview.renderer.seqfeatures.FeatureRenderer(av);
90 setSize(od.getWidth(), od.getHeight());
92 timer = new Timer(SPEED, new ActionListener()
96 public void actionPerformed(ActionEvent e)
100 startTime = System.currentTimeMillis();
105 long time = System.currentTimeMillis();
106 long duration = time - startTime;
107 if (duration >= RUNNING_TIME)
110 ((Timer) e.getSource()).stop();
115 alpha = 1f - ((float) duration / (float) RUNNING_TIME);
124 * Update the overview dimensions object used by the canvas (e.g. if we change
125 * from showing hidden columns to hiding them or vice versa)
127 * @param overviewDims
129 public void resetOviewDims(OverviewDimensions overviewDims)
135 * Signals to drawing code that the associated alignment viewport has changed
136 * and a redraw will be required
138 public boolean restartDraw()
152 updaterunning = true;
159 * Draw the overview sequences
161 * @param showSequenceFeatures
162 * true if sequence features are to be shown
163 * @param showAnnotation
164 * true if the annotation is to be shown
165 * @param transferRenderer
166 * the renderer to transfer feature colouring from
168 public void draw(boolean showSequenceFeatures, boolean showAnnotation,
169 FeatureRenderer transferRenderer)
172 veryLastMiniMe = lastMiniMe;
174 if (showSequenceFeatures)
176 fr.transferSettings(transferRenderer);
179 or = new OverviewRenderer(sr, fr, od);
180 or.addPropertyChangeListener(progressPanel);
181 miniMe = or.draw(od.getRows(av.getAlignment()),
182 od.getColumns(av.getAlignment()));
184 Graphics mg = miniMe.getGraphics();
188 mg.translate(0, od.getSequencesHeight());
189 or.drawGraph(mg, av.getAlignmentConservationAnnotation(),
190 av.getCharWidth(), od.getGraphHeight(),
191 od.getColumns(av.getAlignment()));
192 mg.translate(0, -od.getSequencesHeight());
196 or.removePropertyChangeListener(progressPanel);
200 draw(showSequenceFeatures, showAnnotation, transferRenderer);
204 updaterunning = false;
212 public void paintComponent(Graphics g)
216 if (lastMiniMe == null)
218 g.setColor(Color.white);
219 g.fillRect(0, 0, getWidth(), getHeight());
223 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
225 g.setColor(TRANS_GREY);
226 g.fillRect(0, 0, getWidth(), getHeight());
228 else if (lastMiniMe != null)
231 if ((getWidth() > 0) && (getHeight() > 0)
232 && ((getWidth() != od.getWidth())
233 || (getHeight() != od.getHeight())))
235 // if there is annotation, scale the alignment and annotation separately
236 if (od.getGraphHeight() > 0)
238 BufferedImage topImage = lastMiniMe.getSubimage(0, 0,
239 od.getWidth(), od.getSequencesHeight());
240 BufferedImage bottomImage = lastMiniMe.getSubimage(0,
241 od.getSequencesHeight(), od.getWidth(),
242 od.getGraphHeight());
244 // must be done at this point as we rely on using old width/height
245 // above, and new width/height below
246 od.setWidth(getWidth());
247 od.setHeight(getHeight());
249 // stick the images back together so lastMiniMe is consistent in the
250 // event of a repaint - BUT probably not thread safe
251 lastMiniMe = new BufferedImage(od.getWidth(), od.getHeight(),
252 BufferedImage.TYPE_INT_RGB);
253 Graphics lg = lastMiniMe.getGraphics();
254 lg.drawImage(topImage, 0, 0, od.getWidth(),
255 od.getSequencesHeight(), null);
256 lg.drawImage(bottomImage, 0, od.getSequencesHeight(),
257 od.getWidth(), od.getGraphHeight(), this);
262 od.setWidth(getWidth());
263 od.setHeight(getHeight());
266 // scale lastMiniMe to the new size
267 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
269 // make sure the box is in the right place
270 od.setBoxPosition(av.getAlignment().getHiddenSequences(),
271 av.getAlignment().getHiddenColumns());
275 if (alpha != 0) // this is a timer triggered dissolve
277 Graphics2D g2d = (Graphics2D) g.create();
279 // draw the original image
280 g2d.drawImage(veryLastMiniMe, 0, 0, getWidth(), getHeight(),
283 // draw the new image on top with varying degrees of transparency
284 g2d.setComposite(AlphaComposite.SrcOver.derive(1f - alpha));
285 g2d.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
291 // fall back to normal behaviour
292 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
298 g.setColor(Color.red);