import ij.plugin.frame.*; import ij.*; import ij.gui.*; import java.net.*; import java.awt.*; import java.awt.event.*; //This applet is somewhat less clean than it could be for a couple of //reasons. Note the 3 flags declared below: // "rubberbanding" is a flag used to let the paint() function know // when it should be drawing in XOR mode. It's // normally false, gets set to true when a // mouse-down event occurs, and gets reset to // false when a mouse-up event occurs // "draw_final" is a flag used to let the paint method know // when to draw a final rectangle (not in XOR mode). // Unless you're drawing rectangles for the heck of it, // you can probably get rid of this. // "refresh" is a flag used to tell the applet when to redraw // the background image. (Redrawing the background // image while rubberbanding interferes with the // XOR drawing mode). It's initially set to true, gets // set to false in the mouse-drag event (initially, I'd // set it to false in the mouse-down event, but I got // a weird behavior (the image disappeared for the first, // but only the first, rubberbanded rectangle), so I // moved it to the mouse-drag event. Finally, when // a mouse-up event occurs, it's set to true again. public class Rubberband extends PlugInFrame implements MouseListener, MouseMotionListener{ Image i = null; int start_x, start_y, curr_x, curr_y, xor_x, xor_y; boolean rubberbanding = false; boolean draw_final = false; boolean refresh = true; Graphics g; public Rubberband() { super("Rubberband"); WindowManager.addWindow(this); setSize(320, 200); GUI.center(this); setVisible(true); init(); } public void init() { String name = "http://rsb.info.nih.gov/ij/images/clown.jpg"; URL url = null; try { url = new URL(name); } catch (Exception e) {} IJ.log(""+url); i = Toolkit.getDefaultToolkit().getImage(url); addMouseListener(this); addMouseMotionListener(this); start_x = start_y = curr_x = curr_y = 0; g = getGraphics(); } public void paint(Graphics g) { if (refresh) { g.drawImage(i, 0, 0, this); //IJ.log("drawImage"); } if (rubberbanding) { g.setXORMode(Color.green); // g.drawRect(start_x, start_y, xor_x - start_x, xor_y - start_y); g.drawLine(start_x, start_y, xor_x, xor_y); xor_x = curr_x; xor_y = curr_y; // g.drawRect(start_x, start_y, xor_x - start_x, xor_y - start_y); g.drawLine(start_x, start_y, xor_x, xor_y); } else if (draw_final) { draw_final = false; // g.drawRect(start_x, start_y, curr_x - start_x, curr_y - start_y); g.setColor(Color.yellow); g.drawLine(start_x, start_y, curr_x, curr_y); } } public void update (Graphics g) { paint(g); } public void mousePressed(MouseEvent e) { rubberbanding = true; start_x = curr_x = xor_x = e.getX(); start_y = curr_y = xor_y = e.getY(); } public void mouseReleased(MouseEvent e) { rubberbanding = false; draw_final = true; refresh = true; repaint(); } public void mouseDragged(MouseEvent e) { refresh = false; curr_x = e.getX(); curr_y = e.getY(); //repaint(); paint(g); } public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} }