4 import java.awt.event.KeyEvent;
5 import java.awt.event.*;
7 import ij.plugin.frame.Recorder;
11 public class Toolbar extends JPanel implements MouseListener, MouseMotionListener {
13 public static final int RECTANGLE = 0;
14 public static final int OVAL = 1;
15 public static final int POLYGON = 2;
16 public static final int FREEROI = 3;
17 public static final int LINE = 4;
18 public static final int POLYLINE = 5;
19 public static final int FREELINE = 6;
20 public static final int WAND = 7;
21 public static final int TEXT = 8;
22 public static final int MAGNIFIER = 9;
23 public static final int HAND = 10;
24 public static final int DROPPER = 11;
26 public static final int FIRST_SELECT_TOOL = 0;
27 public static final int LAST_SELECT_TOOL = 8;
30 public static final int ANGLE = 999;
31 public static final int CROSSHAIR = 999;
34 public static final int NUM_TOOLS = 14;
36 public static final int DIVIDER = 100;
37 public static final int SPARE = 101;
39 public static final int[] BUTTON_SEQUENCE = {
56 private static final int SIZE = 22;
57 private static final int OFFSET = 3;
59 private Dimension ps =
new Dimension(SIZE*BUTTON_SEQUENCE.length, SIZE);
60 private boolean[] down;
61 private static int current;
64 private int xOffset, yOffset;
65 private long mouseDownTime;
67 private static Toolbar instance;
68 private int mpPrevious = RECTANGLE;
69 private String[] names =
new String[NUM_TOOLS];
70 private String[] icons =
new String[NUM_TOOLS];
77 private Color gray =
ImageJ.backgroundColor;
78 private Color brighter = gray.brighter();
79 private Color darker = gray.darker();
80 private Color evenDarker = darker.darker();
84 down =
new boolean[NUM_TOOLS];
87 setForeground(foregroundColor);
89 addMouseListener(
this);
90 addMouseMotionListener(
this);
105 private void drawButtons(Graphics g) {
106 int currentOffset = 1;
107 for (
int i=0; i<BUTTON_SEQUENCE.length; i++) {
108 drawButton(g, BUTTON_SEQUENCE[i], currentOffset);
109 currentOffset += getButtonWidth(BUTTON_SEQUENCE[i]);
113 private int getButtonWidth(
int tool) {
114 if (tool == DIVIDER) {
121 private void fill3DRect(Graphics g,
int x,
int y,
int width,
int height,
boolean raised) {
126 g.fillRect(x+1, y+1, width-2, height-2);
127 g.setColor(raised ? brighter : evenDarker);
128 g.drawLine(x, y, x, y + height - 1);
129 g.drawLine(x + 1, y, x + width - 2, y);
130 g.setColor(raised ? evenDarker : brighter);
131 g.drawLine(x + 1, y + height - 1, x + width - 1, y + height - 1);
132 g.drawLine(x + width - 1, y, x + width - 1, y + height - 2);
135 private void drawButton(Graphics g,
int tool) {
138 while ((i < BUTTON_SEQUENCE.length) && (BUTTON_SEQUENCE[i] != tool)) {
139 offset += getButtonWidth(BUTTON_SEQUENCE[i]);
142 drawButton(g, tool, offset);
146 private void drawButton(Graphics g,
int tool,
int currentOffset) {
147 if (tool == DIVIDER)
return;
149 fill3DRect(g, currentOffset, 1, 22, 21, !down[tool]);
150 g.setColor(Color.black);
151 int x = currentOffset + 2;
153 if (down[tool]) { x++; y++;}
155 if (icons[tool]!=null) {
156 drawIcon(g, icons[tool], x, y);
161 g.drawRect(x+1, y+2, 14, 11);
164 g.drawOval(x+1, y+3, 14, 11);
167 xOffset = x+1; yOffset = y+3;
168 m(4,0); d(14,0); d(14,1); d(10,5); d(10,6);
169 d(13,9); d(13,10); d(0,10); d(0,4); d(4,0);
172 xOffset = x+1; yOffset = y+3;
173 m(3,0); d(5,0); d(7,2); d(9,2); d(11,0); d(13,0); d(14,1); d(15,2);
174 d(15,4); d(14,5); d(14,6); d(12,8); d(11,8); d(10,9); d(9,9); d(8,10);
175 d(5,10); d(3,8); d(2,8); d(1,7); d(1,6); d(0,5); d(0,2); d(1,1); d(2,1);
178 xOffset = x; yOffset = y+5;
182 xOffset = x+1; yOffset = y+3;
183 m(0,3); d(3,0); d(13,0); d(13,1); d(8,6); d(12,10);
186 xOffset = x+1; yOffset = y+4;
187 m(0,1); d(2,3); d(4,3); d(7,0); d(8,0); d(10,4); d(14,8); d(15,8);
190 xOffset = x; yOffset = y;
191 m(1,8); d(6,8); d(6,6); d(10,6); d(10,10); d(6,10); d(6,9);
192 m(8,1); d(8,5); m(11,8); d(15,8); m(8,11); d(8,15);
196 xOffset = x+2; yOffset = y+2;
197 m(4,0); d(4,0); m(2,0); d(3,1); d(4,2); m(0,0); d(1,1);
198 m(0,2); d(1,3); d(2,4); m(0,4); d(0,4); m(3,3); d(12,12);
201 xOffset = x+2; yOffset = y+1;
203 m(1,12); d(7,0); d(12,13);
208 xOffset = x+2; yOffset = y+2;
209 m(3,0); d(3,0); d(5,0); d(8,3); d(8,5); d(7,6); d(7,7);
210 d(6,7); d(5,8); d(3,8); d(0,5); d(0,3); d(3,0);
211 m(8,8); d(9,8); d(13,12); d(13,13); d(12,13); d(8,9); d(8,8);
215 xOffset = x+1; yOffset = y+1;
216 m(5,14); d(2,11); d(2,10); d(0,8); d(0,7); d(1,6); d(2,6); d(4,8);
217 d(4,6); d(3,5); d(3,4); d(2,3); d(2,2); d(3,1); d(4,1); d(5,2); d(5,3);
218 m(6,5); d(6,1); d(7,0); d(8,0); d(9,1); d(9,5);
219 m(9,1); d(11,1); d(12,2); d(12,6);
220 m(13,4); d(14,3); d(15,4); d(15,7); d(14,8);
221 d(14,10); d(13,11); d(13,12); d(12,13); d(12,14);
224 xOffset = x; yOffset = y;
225 g.setColor(foregroundColor);
232 m(10,7); d(12,7); d(12,9);
233 m(8,7); d(2,13); d(2,15); d(4,15); d(11,8);
234 g.setColor(backgroundColor);
235 m(0,0); d(16,0); d(16,16); d(0,16); d(0,0);
236 g.setColor(Color.black);
241 void drawIcon(Graphics g, String icon,
int x,
int y) {
244 int length = icon.length();
248 char command = icon.charAt(pc++);
249 if (pc>=length)
break;
251 case 'B': x+=v(); y+=v();
break;
252 case 'R': g.drawRect(x+v(), y+v(), v(), v());
break;
253 case 'F': g.fillRect(x+v(), y+v(), v(), v());
break;
254 case 'O': g.drawOval(x+v(), y+v(), v(), v());
break;
255 case 'o': g.fillOval(x+v(), y+v(), v(), v());
break;
256 case 'C': g.setColor(
new Color(v()*16,v()*16,v()*16));
break;
257 case 'L': g.drawLine(x+v(), y+v(), x+v(), y+v());
break;
258 case 'D': g.drawLine(x1=x+v(), x2=y+v(), x1, x2);
break;
262 x2=v();
if (x2==0)
break;
263 y2=v();
if (y2==0)
break;
265 g.drawLine(x1, y1, x2, y2);
272 int size = v()*10+v();
273 char[] c =
new char[1];
274 c[0] = pc<icon.length()?icon.charAt(pc++):
'e';
275 g.setFont(
new Font(
"SansSerif", Font.BOLD, size));
276 g.drawString(
new String(c), x2, y2);
280 if (pc>=length)
break;
282 g.setColor(Color.black);
286 if (pc>=icon.length())
return 0;
287 char c = icon.charAt(pc++);
310 private void showMessage(
int tool) {
311 if (tool == -1)
return;
312 if ((tool < names.length) && (tool >= 0) && (names[tool]!=null)) {
313 IJ.showStatus(names[tool]);
318 IJ.showStatus(
"Rectangular selection tool");
321 IJ.showStatus(
"Oval selection tool");
324 IJ.showStatus(
"Polygon selection tool");
327 IJ.showStatus(
"Freehand selection tool");
330 IJ.showStatus(
"Straight line selection tool");
333 IJ.showStatus(
"Segmented line selection tool");
336 IJ.showStatus(
"Freehand line selection tool");
339 IJ.showStatus(
"Crosshair (mark and count) tool");
342 IJ.showStatus(
"Wand (tracing) tool: click an object to select it");
345 IJ.showStatus(
"Text tool: click and drag to create a text box");
348 IJ.showStatus(
"Zoom Tool: left-click to zoom in, right-click to zoom out");
351 IJ.showStatus(
"Scrolling tool");
354 IJ.showStatus(
"Color picker (" + foregroundColor.getRed() +
","
355 + foregroundColor.getGreen() +
"," + foregroundColor.getBlue() +
")");
363 private void m(
int x,
int y) {
368 private void d(
int x,
int y) {
371 g.drawLine(this.x, this.y, x, y);
376 private void resetButtons() {
377 for (
int i=0; i<NUM_TOOLS; i++)
381 public void paint(Graphics g) {
385 public void setTool(
int tool) {
386 if (tool==current || tool<0 || tool>=NUM_TOOLS) {
389 if ((FIRST_SELECT_TOOL <= tool) && (LAST_SELECT_TOOL >= tool)) {
390 IJ.getInstance().getImagePlus().killRoi();
393 down[current] =
true;
394 down[previous] =
false;
395 Graphics g = this.getGraphics();
396 drawButton(g, previous);
397 drawButton(g, current);
399 showMessage(current);
405 public static Color getForegroundColor() {
406 return foregroundColor;
409 public static void setForegroundColor(Color c) {
416 public static Color getBackgroundColor() {
417 return backgroundColor;
420 public static void setBackgroundColor(Color c) {
427 static void updateColors() {
429 Graphics g = tb.getGraphics();
430 tb.drawButton(g, DROPPER);
434 public void mousePressed(MouseEvent e) {
436 int newTool = getToolFromCoord(x);
437 if (newTool == -1)
return;
439 boolean doubleClick = newTool==current && (System.currentTimeMillis()-mouseDownTime)<=500;
440 mouseDownTime = System.currentTimeMillis();
442 mpPrevious = current;
445 ImagePlus imp = IJ.getInstance().getImagePlus();
448 IJ.doCommand(
"Set Measurements...");
452 if (imp!=null) imp.getCanvas().unzoom();
455 if (imp!=null) IJ.doCommand(
"Calibrate...");
458 case LINE:
case POLYLINE:
case FREELINE:
459 IJ.doCommand(
"Line Width...");
462 IJ.doCommand(
"Crosshair...");
465 IJ.doCommand(
"Fonts...");
468 IJ.doCommand(
"Color Picker...");
476 public void mouseReleased(MouseEvent e) {}
477 public void mouseExited(MouseEvent e) { IJ.showStatus(
""); }
478 public void mouseClicked(MouseEvent e) {}
479 public void mouseEntered(MouseEvent e) {}
480 public void mouseDragged(MouseEvent e) {}
482 public Dimension getPreferredSize(){
486 public Dimension getMinimumSize(){
490 public void mouseMoved(MouseEvent e) {
492 showMessage(getToolFromCoord(x));
495 private int getToolFromCoord(
int x) {
498 while ((i != BUTTON_SEQUENCE.length) && (offset < x)) {
499 offset += getButtonWidth(BUTTON_SEQUENCE[i]);
502 return BUTTON_SEQUENCE[i-1];