Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Image Cut, Copy and Paste

Posted on 2004-11-08
2
Medium Priority
?
233 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:ashsaber99
2 Comments
 
LVL 2

Expert Comment

by:ThummalaRaghuveer
ID: 12528761
Did not get your question......

either draw a box or get the image in the box
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 12528790
Use a BufferedImage and its getSubimage() method.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question