1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
package ij.plugin.frame;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import ij.*;
import ij.plugin.*;
import ij.process.*;
import ij.gui.*;
import ij.measure.*;
import ij.plugin.frame.Recorder;
import ij.util.Tools;
/** Adjusts the width of line selections. */
public class LineWidthAdjuster extends PlugInFrame implements PlugIn,
Runnable, AdjustmentListener, TextListener, ItemListener {
public static final String LOC_KEY = "line.loc";
int sliderRange = 300;
Scrollbar slider;
int value;
boolean setText;
static LineWidthAdjuster instance;
Thread thread;
boolean done;
TextField tf;
Checkbox checkbox;
public LineWidthAdjuster() {
super("Line Width");
if (instance!=null) {
WindowManager.toFront(instance);
return;
}
WindowManager.addWindow(this);
instance = this;
slider = new Scrollbar(Scrollbar.HORIZONTAL, Line.getWidth(), 1, 1, sliderRange+1);
slider.setFocusable(false); // prevents blinking on Windows
Panel panel = new Panel();
int margin = IJ.isMacOSX()?5:0;
GridBagLayout grid = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
panel.setLayout(grid);
c.gridx = 0; c.gridy = 0;
c.gridwidth = 1;
c.ipadx = 100;
c.insets = new Insets(margin, 15, margin, 5);
c.anchor = GridBagConstraints.CENTER;
grid.setConstraints(slider, c);
panel.add(slider);
c.ipadx = 0; // reset
c.gridx = 1;
c.insets = new Insets(margin, 5, margin, 15);
tf = new TextField(""+Line.getWidth(), 4);
tf.addTextListener(this);
grid.setConstraints(tf, c);
panel.add(tf);
c.gridx = 2;
c.insets = new Insets(margin, 25, margin, 5);
checkbox = new Checkbox("Spline fit", isSplineFit());
checkbox.addItemListener(this);
panel.add(checkbox);
add(panel, BorderLayout.CENTER);
slider.addAdjustmentListener(this);
slider.setUnitIncrement(1);
pack();
Point loc = Prefs.getLocation(LOC_KEY);
if (loc!=null)
setLocation(loc);
else
GUI.center(this);
setResizable(false);
show();
thread = new Thread(this, "LineWidthAdjuster");
thread.start();
setup();
addKeyListener(IJ.getInstance());
}
public synchronized void adjustmentValueChanged(AdjustmentEvent e) {
value = slider.getValue();
setText = true;
notify();
}
public synchronized void textValueChanged(TextEvent e) {
int width = (int)Tools.parseDouble(tf.getText(), -1);
//IJ.log(""+width);
if (width==-1) return;
if (width<0) width=1;
if (width!=Line.getWidth()) {
slider.setValue(width);
value = width;
notify();
}
}
void setup() {
}
// Separate thread that does the potentially time-consuming processing
public void run() {
while (!done) {
synchronized(this) {
try {wait();}
catch(InterruptedException e) {}
if (done) return;
Line.setWidth(value);
if (setText) tf.setText(""+value);
setText = false;
updateRoi();
}
}
}
private static void updateRoi() {
ImagePlus imp = WindowManager.getCurrentImage();
if (imp!=null) {
Roi roi = imp.getRoi();
if (roi!=null && roi.isLine()) {
roi.updateWideLine(Line.getWidth());
imp.draw();
return;
}
}
if (Roi.previousRoi==null) return;
int id = Roi.previousRoi.getImageID();
if (id>=0) return;
imp = WindowManager.getImage(id);
if (imp==null) return;
Roi roi = imp.getRoi();
if (roi!=null && roi.isLine()) {
roi.updateWideLine(Line.getWidth());
imp.draw();
}
}
boolean isSplineFit() {
ImagePlus imp = WindowManager.getCurrentImage();
if (imp==null) return false;
Roi roi = imp.getRoi();
if (roi==null) return false;
if (!(roi instanceof PolygonRoi)) return false;
return ((PolygonRoi)roi).isSplineFit();
}
/** Overrides close() in PlugInFrame. */
public void close() {
super.close();
instance = null;
done = true;
Prefs.saveLocation(LOC_KEY, getLocation());
synchronized(this) {notify();}
}
public void windowActivated(WindowEvent e) {
super.windowActivated(e);
checkbox.setState(isSplineFit());
}
public void itemStateChanged(ItemEvent e) {
boolean selected = e.getStateChange()==ItemEvent.SELECTED;
ImagePlus imp = WindowManager.getCurrentImage();
if (imp==null)
{checkbox.setState(false); return;};
Roi roi = imp.getRoi();
int type = roi!=null?roi.getType():null;
if (roi==null || !(roi instanceof PolygonRoi) || type==Roi.FREEROI || type==Roi.FREELINE || type==Roi.ANGLE) {
checkbox.setState(false);
return;
};
PolygonRoi poly = (PolygonRoi)roi;
boolean splineFit = poly.isSplineFit();
if (selected && !splineFit) {
poly.fitSpline();
Prefs.splineFitLines = true;
imp.draw();
} else if (!selected && splineFit) {
poly.removeSplineFit();
Prefs.splineFitLines = false;
imp.draw();
}
}
public static void update() {
if (instance==null) return;
instance.checkbox.setState(instance.isSplineFit());
int sliderWidth = instance.slider.getValue();
int lineWidth = Line.getWidth();
if (lineWidth!=sliderWidth && lineWidth<=200) {
instance.slider.setValue(lineWidth);
instance.tf.setText(""+lineWidth);
}
}
}
|