Hi guys,


0 - Introduction:


Sorry about the grammar. I am not a native english speaker ( i'm brazillian )
I made a program that print the screen, open it in a frame and allows me to select an area of the image.
I added a (Save) button that when it is pressed the program write the selected area of image in a new file and save it.

Code:
public class ScreenCaptureRectangle {

    Rectangle captureRect;
    Point start = new Point();
    SimpleDateFormat sdf;

    ScreenCaptureRectangle(final BufferedImage screen) {
        sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        final BufferedImage screenCopy = new BufferedImage(
                screen.getWidth(),
                screen.getHeight(),
                screen.getType());
        final JLabel screenLabel = new JLabel(new ImageIcon(screenCopy));
        JScrollPane screenScroll = new JScrollPane(screenLabel);

        screenScroll.setPreferredSize(new Dimension(
                (int) (screen.getWidth() / 3),
                (int) (screen.getHeight() / 3)));

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(screenScroll, BorderLayout.CENTER);
        JButton btnSave = new JButton("SAVE");
        btnSave.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent ae) {
                double w = captureRect.getWidth();
                double h = captureRect.getHeight();
                double x = captureRect.getX();
                double y = captureRect.getY();

                int W = (int) w;
                int H = (int) h;
                int X = (int) x;
                int Y = (int) y;

                BufferedImage selectImg = screen.getSubimage(X, Y, W, H);
                try {
                    String fName = generateFileName();
                    if (fName != null) {
                        File f = new File(fName);
                        if (f.createNewFile()) {
                            ImageIO.write(selectImg, "jpg", f);
                        }
                    }
                } catch (IOException ex) {
                    Logger.getLogger(ScreenCaptureRectangle.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        panel.add(btnSave, BorderLayout.AFTER_LAST_LINE);

        final JLabel selectionLabel = new JLabel(
                "Drag a rectangle in the screen shot!");
        panel.add(selectionLabel, BorderLayout.SOUTH);

        repaint(screen, screenCopy);
        screenLabel.repaint();

        screenLabel.addMouseMotionListener(new MouseMotionAdapter() {

            @Override
            public void mouseDragged(MouseEvent me) {
                Point end = me.getPoint();
                captureRect = new Rectangle(start,
                        new Dimension(end.x - start.x, end.y - start.y));
                repaint(screen, screenCopy);
                screenLabel.repaint();
                selectionLabel.setText("Rectangle: " + captureRect);
            }

        });

        screenLabel.addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent me) {
                start = me.getPoint();
                repaint(screen, screenCopy);
                selectionLabel.setText("Start Point: " + start);
                screenLabel.repaint();
            }

            @Override
            public void mouseReleased(MouseEvent me) {
                int endX = me.getX();
                int endY = me.getY();
                if (endX > start.x && endY > start.y) {
                    captureRect = new Rectangle(start.x, start.y, endX-start.x, endY-start.y);
                    System.out.println("Rectangle of interest: " + captureRect);
                }
            }

        });

        JOptionPane.showMessageDialog(null, panel);
    }

    private String generateFileName() {
        return new StringBuilder("screencrop_").append(sdf.format(new Date())).append(".jpg").toString();
    }

    public void repaint(BufferedImage orig, BufferedImage copy) {
        Graphics2D g = copy.createGraphics();
        g.drawImage(orig, 0, 0, null);
        if (captureRect != null) {
            g.setColor(Color.RED);
            g.draw(captureRect);
            g.setColor(new Color(255, 255, 255, 150));
            g.fill(captureRect);
        }
        g.dispose();
    }

    public static void main(String[] args) throws Exception {
        Robot robot = new Robot();
        final Dimension screenSize = Toolkit.getDefaultToolkit().
                getScreenSize();
        final BufferedImage screen = robot.createScreenCapture(
                new Rectangle(screenSize));

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new ScreenCaptureRectangle(screen);
            }
        });
    }
}

1 - What i want it to do:


I want to use a list of rectangles to store all the selected images and when i pressed the (OK) button the program will concatenate it all in an single horizontal line (at the same file).
But i want more than this:
i want to: Create a new button that when it is pressed it starts to save the nexts rectangles (images) in another list and when i pressed [ (OK) button or this button again ] the program will concatenate this file under the first horizontal line (all at the same file).


2 - How to to this :


I think i'll have to compare the heigth of all images of the horizontal line above. For example the first image of the horizontal line below: drawImage(img, 0, biggest_height, null);


3 - What i did:


I tried to declare the list on tradicional way but it didnt work because it has limited length. then i tried to use this code:

ArrayList<Rectangle> al = new ArrayList<Rectangle>();

al.add(new Rectangle(x, y, w, h));

but it didn't work again.



4 - Can you help me to do this app ?


Thanks for your time.
And try to help me!!