/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

   RobotLine.java
   A class for drawing straight lines with the mouse, controlled by a robot
   Written by Barak Itkin <lightningismyname@gmail.com>

   To compile and run this program, do:

     javac RobotLine.java
     java RobotLine
*/

import java.awt.AWTException;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.BoxLayout;

/**
 *
 * @author Barak Itkin
 */
public class RobotLine {

    static Frame f;


    public static void main(String[] args) {
        f = new Frame("Move me");
        f.setLayout(new BoxLayout(f, BoxLayout.Y_AXIS));
        f.add(new Label("Move and resize me to the area in which you want to draw lines"));
        f.add(new Label("When the window is in place, close it and lines will be drawn."));

        f.setSize(500, 90);
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter() {

            int x,y,width,height;

            public void windowClosing(WindowEvent e) {
                x = f.getX();
                y = f.getY();
                width = f.getWidth();
                height = f.getHeight();

                /* Get the dialog out of the way */
                f.setVisible(false);
                try {
                    /* Create the robot for controlling the mouse */
                    Robot r = new Robot();
                    /* Draw several lines, depending on the height of the window */
                    for (int j = 0; j < height; j+=10)
                    {
                        r.mouseMove(x, y + j);
                        /* Small delays between drawing of lines */
                        Thread.sleep(200);
                        /* Click the mouse button */
                        r.mousePress(InputEvent.BUTTON1_MASK);
                        /* Move horizontally to the left, 1px at a time */
                        for (int i = 0; i < width; i+=1) {
                            r.mouseMove(x+i, y+j);
                        }
                        /* Release the mouse button */
                        r.mouseRelease(InputEvent.BUTTON1_MASK);
                    }
                } catch (Exception ex) {
                    System.out.println("An error occured..." + ex);
                }
                /* Now that we finished drawing, exit */
                System.exit(0);

            }
       });

       
    }

}
