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 setSize(od.getWidth(), od.getHeight());
93 timer = new Timer(SPEED, new ActionListener()
97 public void actionPerformed(ActionEvent e)
101 startTime = System.currentTimeMillis();
106 long time = System.currentTimeMillis();
107 long duration = time - startTime;
108 if (duration >= RUNNING_TIME)
111 ((Timer) e.getSource()).stop();
116 alpha = 1f - ((float) duration / (float) RUNNING_TIME);
125 * Update the overview dimensions object used by the canvas (e.g. if we change
126 * from showing hidden columns to hiding them or vice versa)
128 * @param overviewDims
130 public void resetOviewDims(OverviewDimensions overviewDims)
136 * Signals to drawing code that the associated alignment viewport has changed
137 * and a redraw will be required
139 public boolean restartDraw()
153 updaterunning = true;
160 * Draw the overview sequences
162 * @param showSequenceFeatures
163 * true if sequence features are to be shown
164 * @param showAnnotation
165 * true if the annotation is to be shown
166 * @param transferRenderer
167 * the renderer to transfer feature colouring from
169 public void draw(boolean showSequenceFeatures, boolean showAnnotation,
170 FeatureRenderer transferRenderer)
172 System.out.println(this.getHeight());
173 //od.setHeight(this.getHeight());
174 //od.setWidth(this.getWidth());
175 setPreferredSize(new Dimension(od.getWidth(), od.getHeight()));
178 veryLastMiniMe = lastMiniMe;
180 if (showSequenceFeatures)
182 fr.transferSettings(transferRenderer);
185 or = new OverviewRenderer(sr, fr, od);
186 or.addPropertyChangeListener(progressPanel);
187 miniMe = or.draw(od.getRows(av.getAlignment()),
188 od.getColumns(av.getAlignment()));
190 Graphics mg = miniMe.getGraphics();
194 mg.translate(0, od.getSequencesHeight());
195 or.drawGraph(mg, av.getAlignmentConservationAnnotation(),
196 av.getCharWidth(), od.getGraphHeight(),
197 od.getColumns(av.getAlignment()));
198 mg.translate(0, -od.getSequencesHeight());
202 or.removePropertyChangeListener(progressPanel);
206 draw(showSequenceFeatures, showAnnotation, transferRenderer);
210 updaterunning = false;
218 public void paintComponent(Graphics g)
223 if (lastMiniMe == null)
225 g.setColor(Color.white);
226 g.fillRect(0, 0, getWidth(), getHeight());
230 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
232 g.setColor(TRANS_GREY);
233 g.fillRect(0, 0, getWidth(), getHeight());
235 else if (lastMiniMe != null)
237 if ((getWidth() > 0) && (getHeight() > 0)
238 && ((getWidth() != od.getWidth())
239 || (getHeight() != od.getHeight())))
241 // scale the alignment and annotation separately *** if there is
243 if (od.getGraphHeight() > 0)
245 BufferedImage topImage = lastMiniMe.getSubimage(0, 0,
246 od.getWidth(), od.getSequencesHeight());
247 BufferedImage bottomImage = lastMiniMe.getSubimage(0,
248 od.getSequencesHeight(), od.getWidth(),
249 od.getGraphHeight());
251 // must be done at this point as we rely on using old width/height
252 // above, and new width/height below
253 od.setWidth(getWidth());
254 od.setHeight(getHeight());
256 // stick the images back together so lastMiniMe is consistent in the
257 // event of a repaint - BUT probably not thread safe
258 lastMiniMe = new BufferedImage(od.getWidth(), od.getHeight(),
259 BufferedImage.TYPE_INT_RGB);
260 Graphics lg = lastMiniMe.getGraphics();
261 lg.drawImage(topImage, 0, 0, od.getWidth(),
262 od.getSequencesHeight(), null);
263 lg.drawImage(bottomImage, 0, od.getSequencesHeight(),
264 od.getWidth(), od.getGraphHeight(), this);
269 od.setWidth(getWidth());
270 od.setHeight(getHeight());
273 // scale lastMiniMe to the new size
274 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
276 // make sure the box is in the right place
277 od.setBoxPosition(av.getAlignment().getHiddenSequences(),
278 av.getAlignment().getHiddenColumns());
282 if (alpha != 0) // this is a timer triggered dissolve
284 Graphics2D g2d = (Graphics2D) g.create();
286 // draw the original image
287 g2d.drawImage(veryLastMiniMe, 0, 0, getWidth(), getHeight(),
290 // draw the new image on top with varying degrees of transparency
291 g2d.setComposite(AlphaComposite.SrcOver.derive(1f - alpha));
292 g2d.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
296 /* else if (lastMiniMe != miniMe)
298 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
299 g.setColor(TRANS_GREY);
300 g.fillRect(0, 0, getWidth(), getHeight());
304 // fall back to normal behaviour
305 g.drawImage(lastMiniMe, 0, 0, getWidth(), getHeight(), this);
313 g.setColor(Color.red);