7 import java.awt.event.*;
12 static final String[] locations = {
"Upper Right",
"Lower Right",
"Lower Left",
"Upper Left",
"At Selection"};
13 static final int UPPER_RIGHT=0, LOWER_RIGHT=1, LOWER_LEFT=2, UPPER_LEFT=3, AT_SELECTION=4;
14 static final String[] colors = {
"White",
"Black",
"Light Gray",
"Gray",
"Dark Gray",
"Red",
"Green",
"Blue",
"Yellow"};
15 static final String[] checkboxLabels = {
"Bold Text",
"Hide Text"};
16 static double barWidth;
17 static int defaultBarHeight = 3;
18 static int barHeightInPixels = defaultBarHeight;
19 static String location = locations[UPPER_RIGHT];
20 static String color = colors[0];
21 static boolean boldText =
true;
22 static boolean hideText;
24 static boolean labelAll;
30 int roiX=-1, roiY, roiWidth, roiHeight;
31 boolean[] checkboxStates =
new boolean[2];
34 public void run(String arg) {
43 Roi roi = imp.getRoi();
45 Rectangle r = roi.getBounds();
50 location = locations[AT_SELECTION];
51 }
else if (location.equals(locations[AT_SELECTION]))
52 location = locations[UPPER_RIGHT];
55 mag = (imp!=null)?imp.getCanvas().getMagnification():1.0;
58 if (fontSize<(12/mag))
59 fontSize = (int)(12/mag);
60 String units = cal.getUnits();
61 double pixelWidth = cal.pixelWidth;
64 double scale = 1.0/pixelWidth;
65 imageWidth = imp.getWidth()*pixelWidth;
66 if (roiX>0 && roiWidth>10)
67 barWidth = roiWidth*pixelWidth;
68 else if (barWidth==0.0 || barWidth>0.67*imageWidth) {
69 barWidth = (80.0*pixelWidth)/mag;
70 if (barWidth>0.67*imageWidth)
71 barWidth = 0.67*imageWidth;
73 barWidth = (int)barWidth;
75 int digits = (int)barWidth==barWidth?0:1;
78 int percent = (int)(barWidth*100.0/imageWidth);
79 if (mag<1.0 && barHeightInPixels<defaultBarHeight/mag)
80 barHeightInPixels = (int)(defaultBarHeight/mag);
83 GenericDialog gd =
new BarDialog(
"Scale Bar");
84 gd.addNumericField(
"Width in "+units+
": ", barWidth, digits);
85 gd.addNumericField(
"Height in pixels: ", barHeightInPixels, 0);
86 gd.addNumericField(
"Font Size: ", fontSize, 0);
87 gd.addChoice(
"Color: ", colors, color);
88 gd.addChoice(
"Location: ", locations, location);
89 checkboxStates[0] = boldText; checkboxStates[1] = hideText;
90 gd.addCheckboxGroup(1, 2, checkboxLabels, checkboxStates);
92 if (gd.wasCanceled()) {
97 barWidth = gd.getNextNumber();
98 barHeightInPixels = (int)gd.getNextNumber();
99 fontSize = (int)gd.getNextNumber();
100 color = gd.getNextChoice();
101 location = gd.getNextChoice();
102 boldText = gd.getNextBoolean();
103 hideText = gd.getNextBoolean();
107 void drawScaleBar(ImagePlus imp) {
108 if (!updateLocation())
110 ImageProcessor ip = imp.getProcessor();
111 Undo.setup(Undo.FILTER, imp);
112 Color color = getColor();
120 ip.setRoi(x, y, barWidthInPixels, barHeightInPixels);
124 int fontType = boldText?Font.BOLD:Font.PLAIN;
125 ip.setFont(
new Font(
"SansSerif", fontType, fontSize));
126 ip.setAntialiasedText(
true);
127 int digits = (int)barWidth==barWidth?0:1;
130 String label = IJ.d2s(barWidth, digits) +
" "+ imp.getCalibration().getUnits();
131 x = x +(barWidthInPixels - ip.getStringWidth(label))/2;
132 y = y + barHeightInPixels + fontSize + fontSize/4;
134 ip.drawString(label, x, y);
138 boolean updateLocation() {
139 Calibration cal = imp.getCalibration();
140 barWidthInPixels = (int)(barWidth/cal.pixelWidth);
141 int width = imp.getWidth();
142 int height = imp.getHeight();
144 int x = width - width/fraction - barWidthInPixels;
146 if (location.equals(locations[UPPER_RIGHT]))
148 else if (location.equals(locations[LOWER_RIGHT]))
149 y = height - height/fraction - barHeightInPixels - fontSize;
150 else if (location.equals(locations[UPPER_LEFT])) {
153 }
else if (location.equals(locations[LOWER_LEFT])) {
155 y = height - height/fraction - barHeightInPixels - fontSize;
168 Color c = Color.white;
169 if (color.equals(colors[1])) c = Color.black;
170 else if (color.equals(colors[2])) c = Color.lightGray;
171 else if (color.equals(colors[3])) c = Color.gray;
172 else if (color.equals(colors[4])) c = Color.darkGray;
173 else if (color.equals(colors[5])) c = Color.red;
174 else if (color.equals(colors[6])) c = Color.green;
175 else if (color.equals(colors[7])) c = Color.blue;
176 else if (color.equals(colors[8])) c = Color.yellow;
180 void updateScalebar() {
182 imp.getProcessor().reset();
186 class BarDialog
extends GenericDialog {
188 BarDialog(String title) {
192 public void textValueChanged(TextEvent e) {
193 TextField widthField = ((TextField)numberField.elementAt(0));
194 Double d = getValue(widthField.getText());
197 barWidth = d.doubleValue();
198 TextField heightField = ((TextField)numberField.elementAt(1));
199 d = getValue(heightField.getText());
202 barHeightInPixels = (int)d.doubleValue();
203 TextField fontSizeField = ((TextField)numberField.elementAt(2));
204 d = getValue(fontSizeField.getText());
207 int size = (int)d.doubleValue();
213 public void itemStateChanged(ItemEvent e) {
214 Choice col = (Choice)(choice.elementAt(0));
215 color = col.getSelectedItem();
216 Choice loc = (Choice)(choice.elementAt(1));
217 location = loc.getSelectedItem();
218 boldText = ((Checkbox)(checkbox.elementAt(0))).getState();
219 hideText = ((Checkbox)(checkbox.elementAt(1))).getState();