Image Cut, Copy and Paste

Hi,

I'm working on an image editor much like Microsoft's Photo Editor.  I set it up like this:  I hava a JScrollPane with a JLabel inside of it.  I set the JLabels icon to the picture I'm currently editing.  I have a Select operation that allows the user to drag a selection box over the image.  I know need to know how to cut what is inside the selection box, copy to the system clipboard, and paste back onto the image.  My Select operation:

 private class SelectionArea extends JLabel
 {
        Rectangle currentRect = null;
        Rectangle rectToDraw = null;
        Rectangle previousRectDrawn = new Rectangle();
       
       
        public SelectionArea()
        {
           
         
            setOpaque( true );
            setMinimumSize( new Dimension( 10, 10 ) ); //don't hog space

            MyListener myListener = new MyListener();
            addMouseListener( myListener );
            addMouseMotionListener( myListener );
        }

        private class MyListener extends MouseInputAdapter
        {
            public void mousePressed( MouseEvent e )
            {
                int x, y;
                System.out.print("press");
                // check for left mouse button press
                if ( SwingUtilities.isLeftMouseButton( e ) )
                {
                    x = e.getX();
                    y = e.getY();
                    currentRect = new Rectangle( x, y, 0, 0 );
                    updateDrawableRect( getWidth(), getHeight() );
                    repaint();
                }

               
            }

            public void mouseDragged( MouseEvent e )
            {
                if ( SwingUtilities.isLeftMouseButton( e ) )
                    updateSize( e );
            }

            public void mouseReleased( MouseEvent e )
            {
                if ( SwingUtilities.isLeftMouseButton( e ) )
                    updateSize( e );
            }

            /* Update the size of the current rectangle and call repaint.
             * Because currentRect always has the same origin,
             * translate it if the width or height is negative.
             *
             * For efficiency, specify the painting region using arguments
             * to the repaint() call.
             */
            void updateSize( MouseEvent e )
            {
                int x = e.getX();
                int y = e.getY();
                currentRect.setSize( x - currentRect.x, y - currentRect.y );
                updateDrawableRect( getWidth(), getHeight() );
                Rectangle totalRepaint = rectToDraw.union( previousRectDrawn );
                repaint( totalRepaint.x, totalRepaint.y, totalRepaint.width, totalRepaint.height );
            }
        }

        // Tell Java how to repaint the Swing component.
        protected void paintComponent( Graphics g )
        {
            super.paintComponent( g );      //paint the background and image

            // if currentRect exists, paint a box on top
            if ( currentRect != null )
            {
                // Draw a rectangle on top of the image. XORing draws nondestructively.
                g.setXORMode( Color.white );
                g.drawRect( rectToDraw.x, rectToDraw.y, rectToDraw.width - 1, rectToDraw.height - 1 );

             
            }
        }

        private void updateDrawableRect( int compWidth, int compHeight )
        {
            int x = currentRect.x;
            int y = currentRect.y;
            int width = currentRect.width;
            int height = currentRect.height;

            // make width and height positive, if necessary
            if ( width < 0 )
            {
                width = 0 - width;
                x = x - width + 1;
                if ( x < 0 )
                {
                    width += x;
                    x = 0;
                }
            }
            if ( height < 0 )
            {
                height = 0 - height;
                y = y - height + 1;
                if ( y < 0 )
                {
                    height += y;
                    y = 0;
                }
            }

            // rectangle shouldn't extend past the drawing area
            if ( ( x + width ) > compWidth )
            {
                width = compWidth - x;
            }
            if ( ( y + height ) > compHeight )
            {
                height = compHeight - y;
            }

            // update rectToDraw after saving old value
            if ( rectToDraw != null )
            {
                previousRectDrawn.setBounds( rectToDraw.x, rectToDraw.y, rectToDraw.width, rectToDraw.height );
                rectToDraw.setBounds( x, y, width, height );
            }
            else
            {
                rectToDraw = new Rectangle( x, y, width, height );
            }
        }
    }

Any ideas? Sample code?

Thank you for your time.
ashsaber99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ThummalaRaghuveerCommented:
Did not get your question......

either draw a box or get the image in the box
0
objectsCommented:
Use a BufferedImage and its getSubimage() method.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.