fixed improper generic parameter use in Tree.duplicateAs > Map, necessary
[trakem2.git] / TrakEM2_ / src / main / java / ini / trakem2 / display / SnapshotPanel.java
blob26b5a7c9e4ecc7f98802eaf60f51c6a688131a6e
1 /**
3 TrakEM2 plugin for ImageJ(C).
4 Copyright (C) 2005-2009 Albert Cardona and Rodney Douglas.
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation (http://www.gnu.org/licenses/gpl.txt )
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 You may contact Albert Cardona at acardona at ini.phys.ethz.ch
20 Institute of Neuroinformatics, University of Zurich / ETH, Switzerland.
21 **/
23 package ini.trakem2.display;
26 import ini.trakem2.persistence.FSLoader;
27 import ini.trakem2.utils.IJError;
28 import ini.trakem2.utils.Utils;
30 import java.awt.Color;
31 import java.awt.Dimension;
32 import java.awt.Graphics;
33 import java.awt.Graphics2D;
34 import java.awt.event.MouseEvent;
35 import java.awt.event.MouseListener;
36 import java.awt.image.BufferedImage;
38 import javax.swing.JPanel;
40 public class SnapshotPanel extends JPanel implements MouseListener {
42 private static final long serialVersionUID = 1L;
43 private Display display;
44 private Displayable d;
45 static public final int SIDE = 50;
46 static public final Color GREY = new Color(215, 215, 215);
48 public SnapshotPanel(Display display, Displayable d) {
49 this.display = display;
50 this.d = d;
51 // Always a square
52 Dimension dim = new Dimension(SIDE, SIDE);
53 setMinimumSize(dim);
54 setMaximumSize(dim);
55 setPreferredSize(dim);
58 public void set(final Displayable d) {
59 this.d = d;
60 repaint();
63 public void update(Graphics g) {
64 paint(g);
67 private BufferedImage img = null;
69 private void fillBackground(Graphics g, double lw, double lh, int slw, int slh) {
70 if (lw != lh) {
71 g.setColor(Color.black);
72 g.fillRect(0, 0, slw, slh);
73 g.setColor(GREY);
74 g.fillRect(slw, 0, SIDE - slw, SIDE);
75 g.fillRect(0, slh, slw, SIDE - slh);
76 } else {
77 g.setColor(Color.black);
78 g.fillRect(0, 0, SIDE, SIDE);
82 /** Paint the snapshot image over a black background that represents a scaled Layer. */
83 public void paint(final Graphics g) {
84 if (null == g) return; // happens if not visible
85 synchronized (this) {
86 if (null != img) {
87 // Paint and flush
88 g.drawImage(img, 0, 0, null);
89 this.img.flush();
90 this.img = null;
91 return;
94 // Else, repaint background to avoid flickering
95 final Layer la = display.getLayer();
96 if (null == la) {
97 Utils.log2("SnapshotPanel: null layer?");
98 return;
101 final double lw = la.getLayerWidth();
102 final double lh = la.getLayerHeight();
103 final double scale = Math.min(SIDE / lw,
104 SIDE / lh);
105 final int slw = (int)(lw * scale);
106 final int slh = (int)(lh * scale);
108 fillBackground(g, lw, lh, slw, slh);
110 // ... and create the image in a separate thread and repaint again
111 FSLoader.repainter.submit(new Runnable() { public void run() {
112 try {
113 if (!display.isPartiallyWithinViewport(d)) return;
114 BufferedImage img = new BufferedImage(SIDE, SIDE, BufferedImage.TYPE_INT_RGB);
115 Graphics2D g2 = img.createGraphics();
117 fillBackground(g2, lw, lh, slw, slh);
119 g2.scale(scale, scale);
121 try {
122 // Avoid painting images that have an alpha mask: takes forever.
123 //if (d.getClass() == Patch.class && ((Patch)d).hasAlphaChannel()) {
124 // d.paintAsBox(g2);
125 //} else {
126 final Layer la = display.getLayer();
127 d.paintSnapshot(g2, la, la.getParent().getColorCueLayerRange(la), d.getLayerSet().get2DBounds(), scale);
129 } catch (Exception e) {
130 d.paintAsBox(g2);
132 synchronized (this) {
133 if (null != SnapshotPanel.this.img) SnapshotPanel.this.img.flush();
134 SnapshotPanel.this.img = img;
136 repaint();
137 } catch (Throwable t) {
138 IJError.print(t);
140 }});
143 public void mousePressed(MouseEvent me) {
144 //must enable cancel!//if (display.isTransforming()) return;
145 display.setActive(d);
146 if (me.isPopupTrigger() || (ij.IJ.isMacOSX() && me.isControlDown()) || MouseEvent.BUTTON2 == me.getButton()) {
147 display.showPopup(this, me.getX(), me.getY());
150 public void mouseReleased(MouseEvent me) {}
151 public void mouseEntered(MouseEvent me) {}
152 public void mouseExited (MouseEvent me) {}
153 public void mouseClicked(MouseEvent me) {}