import java.applet.Applet;
import java.awt.Graphics;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.Event;
import java.awt.Image;
import java.awt.Color;


//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 Applet
{
        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;

        public void init()
        {
        String imageName = getParameter("image");
        try
                {
                URL imageURL= new URL(getCodeBase(), imageName);
                i = getImage(imageURL);
                }
        catch (MalformedURLException mue)
                {
                System.out.println("Malformed URL exception when trying to load image " + imageName);
                }
        start_x = start_y = curr_x = curr_y = 0;
        }




        public void paint(Graphics g)
                {
                if (refresh)
                        {
                        g.drawImage(i, 0, 0, this);
                        }
                if (rubberbanding)
                        {
                        g.setXORMode(Color.green);
                        g.drawRect(start_x, start_y, xor_x - start_x, xor_y - start_y);
                        xor_x = curr_x;
                        xor_y = curr_y;
                        g.drawRect(start_x, start_y, xor_x - start_x, xor_y - start_y);
                        }

                //draw rectangle in xor mode
                else if (draw_final)
                        {
                        draw_final = false;
                        g.drawRect(start_x, start_y, curr_x - start_x, curr_y - start_y);


                        }

                }

        public void update (Graphics g)
                {
                paint(g);
                }
        public boolean handleEvent(Event e)
                {
                switch(e.id)
                        {
                        case Event.MOUSE_DOWN:
                                rubberbanding = true;
                                start_x = curr_x = xor_x = e.x;
                                start_y = curr_y = xor_y = e.y;
                                System.out.println("mouse_down at (" +e.x+ "," +e.y+ ")");
                                break;
                        case Event.MOUSE_DRAG:
                                refresh = false;
                                curr_x = e.x;
                                curr_y = e.y;
                                repaint();
                                System.out.println("mouse_drag at (" +e.x+ "," +e.y+ ")");
                                break;
                        case Event.MOUSE_UP:
                                rubberbanding = false;
                                draw_final = true;
                                System.out.println("rectangle from (" +start_x+ "," +start_y+ ") to (" +curr_x+ "," +curr_y+ ")");

                                //you would put code in here to process the coordinates of your rubberbanded box
                                refresh = true;
                                repaint();
                                break;
                                
                        }

                return super.handleEvent(e);
                }

}
