ImageSelection in Java

The ImageSelection class is simply a wrapper for Image objects.Objects that you want to transfer via the clipboard must implement the Transferable interface.We build an ImageSelection class that can be used to transfer images through the local clipboard.

 

import java.awt.*;

import java.io.*;

import java.awt.event.*;

import java.awt.datatransfer.*;

import javax.swing.*;

import javax.swing.JMenu;

public class ImageTransferTest

{

public static void main(String aa[])

{

JFrame frame1=new ImageTransferFramee();

JFrame frame2=new ImageTransferFramee();

frame1.setTitle("Frame One(1)");

frame2.setTitle("Frame Two(2)");

frame1.show();

frame2.show();

}

}

class ImageTransferFramee extends JFrame implements ActionListener

{

   public ImageTransferFramee()

    {

      setSize(300,300);

      Container contentPane=getContentPane();

      label=new JLabel();

      contentPane.add(label,"Center");

     JMenu fileMenu=new JMenu("File");

     openItem=new JMenuItem("Open");

    openItem.addActionListener(this);

    fileMenu.add(openItem);

    exitItem=new JMenuItem("Exit");

    exitItem.addActionListener(this);

    fileMenu.add(exitItem);

   JMenu editMenu=new JMenu("Edit");

   copyItem=new JMenuItem("Copy");

   copyItem.addActionListener(this);

    editMenu.add(copyItem);

    pasteItem=new JMenuItem("Paste");

    pasteItem.addActionListener(this);

   editMenu.add(pasteItem);

    JMenuBar menuBar=new JMenuBar();

    menuBar.add(fileMenu);

    menuBar.add(editMenu);

    setJMenuBar(menuBar);

   }

public void actionPerformed(ActionEvent acevt)

{

       Object source=acevt.getSource();

       if(source==openItem)

       {

        JFileChooser chooserr=new JFileChooser();

        chooserr.setCurrentDirectory(new File("."));

       chooserr.setFileFilter(new javax.swing.filechooser.FileFilter()

        {

            public boolean accept(File f)

             {

                 String name=f.getName().toLowerCase();

                 return name.endsWith("wel3.gif") || name.endsWith("wel.jpg")||name. endsWith      ("wel1.jpg")|| f.isDirectory();

              }

          public String getDescription()

            {

               return "Image files";

             }

        });

             int r=chooserr.showOpenDialog(this);

           if(r==JFileChooser.APPROVE_OPTION)

            {

               String name=chooserr.getSelectedFile().getAbsolutePath();

               setImage(Toolkit.getDefaultToolkit().getImage(name));

            }

       }

           else if(source==exitItem)

                   System.exit(0);

           else if(source==copyItem)

                     copy();

            else if(source==pasteItem)

                   paste();

            }

      private void copy()

          {

                 ImageSelection selection=new ImageSelection(theImage);

                  localClipboardd.setContents(selection,null);

         }

    private void paste()

         {

             Transferable contents=localClipboardd.getContents(this);

             if(contents==null)

                 return;

                try

                 {

                      Image image=(Image)contents.getTransferData(ImageSelection.imageFlavoor);

                       setImage(image);

                  }

          catch(Exception errr)

         {

         }

   }

       public void setImage(Image image)

        {

         theImage=image;

        label.setIcon(new ImageIcon(image));

       }

/*WE cannot use the system clipboard because, at this time, Java technology can transfer only text into the system clipboard*/

             private static Clipboard localClipboardd=new Clipboard("local");

              private Image theImage;

             private JLabel label;

              private JMenuItem openItem;

             private JMenuItem exitItem;

            private JMenuItem copyItem;

             private JMenuItem pasteItem;

       }

class ImageSelectioon implements Transferable

 {

        public ImageSelectioon(Image image)

        {

           theImage=image;

        }

  public DataFlavor[] getTransferDataFlavors()

   {

        return flavors;

   }

         public boolean isDataFlavorSupported(DataFlavor flavoor)/*check whether the requested flavor is equal to the imageFlavoor object*/

                        {

                              return flavoor.equals(imageFlavoor);

                        }

/*this returns the data, formatted in the requested flavor.Thrown an UnsupportedFlavorException if the flavor requested is not supported*/

public synchronized Object getTransferData(DataFlavor flavoor)throws UnsupportedFlavorException

    {

           if(flavoor.equals(imageFlavoor))

           {

            return theImage;

            }

         else

           {

             throw new UnsupportedFlavorException(flavoor);

           }

     }

/*defines the single supported flavor as a class java.awt.image, with a human-presentable name of “AWT Imagee”*/

          public static final DataFlavor imageFlavoor=new DataFlavor(java.awt.Image.class,"AWT Imagee");

         private static DataFlavor[] flavors={imageFlavoor};

        private Image theImage;

    }

ImageSelection in Java

Explanation…

1)Click on File menu and click on open menu

2)Select the image(.jpg or .gif any).

3)Click on  Edit menu and then click on copy menu.

4)Last go to other window(frame 1 to frame2 or frame2 to frame1)and click on paste(Edit menu).

In this program we creates two windows. We can load an image file into each window, or we can copy and paste between the windows, with the Edit|Copy and Edit|Paste menu options.

private void copy()

{

ImageSelection selection=new ImageSelection(theImage);

localClipboardd.setContents(selection,null);

}

private void paste()

{

Transferable contents=localClipboardd.getContents(this);

if(contents==null)

return;

try

{

Image image=(Image)contents.getTransferData(ImageSelection.imageFlavor);

setImage(image);

}

catch(Exception errr)

{

}

In this case,selection” is an ImageSelection instance.We also need to cast the return value of getTransferData to an Image this time .