⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clipboarddemo4.java

📁 java 完全探索的随书源码
💻 JAVA
字号:
// ClipboardDemo4.java

import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.awt.image.*;

class ClipboardDemo4
{
   static Clipboard c = new Clipboard ("My clipboard");

   public static void main (String [] args)
   {
      int width = 100;
      int height = 100;

      int [] pixels = new int [width * height];

      int index = 0;

      for (int y = 0; y < height; y++)
      {
           int numerator = y * 255;
           int b = numerator / height;
           int r = 255 - numerator / height;

           for (int x = 0; x < width; x++)
           {
                int g = x * 255 / width;
                pixels [index++] = (255 << 24) | (r << 16) | (g << 8)
                                   | b;
           }
      }

      Toolkit tk = Toolkit.getDefaultToolkit ();
      Image im = tk.createImage (new MemoryImageSource (width,
                                                        height,
                                                        pixels,
                                                        0,
                                                        width));

      ImageSelection is = new ImageSelection (im);
      c.setContents (is, is);

      Frame f = new Frame ("Clipboard Demo4");
      
      f.addWindowListener (new WindowAdapter ()
                           {
                             public void windowClosing (WindowEvent e)
                             {
                                System.exit (0);
                             }
                           });

      picture p = new picture (f);

      f.add (p);

      Transferable t = c.getContents (new ClipboardDemo4 ());
      if (t == null)
          return;

      try
      {
          p.draw ((Image)
                  t.getTransferData (ImageSelection.imageFlavor));
      }
      catch (java.io.IOException e) {}
      catch (UnsupportedFlavorException e) {}

      f.pack ();

      f.setVisible (true);
   }
}

class ImageSelection implements Transferable, ClipboardOwner
{
    public static final DataFlavor imageFlavor =
                                 new DataFlavor (java.awt.Image.class,
                                                 "AWT Image");

    private DataFlavor [] flavors =
    {
       imageFlavor
    };

    private Image picture;

    public ImageSelection (Image picture)
    {
       this.picture = picture;
    }

    public DataFlavor [] getTransferDataFlavors ()
    {
       return flavors;
    }
  
    public boolean isDataFlavorSupported (DataFlavor df)
    {
       return imageFlavor.equals (df);
    }
  
    public Object getTransferData (DataFlavor df)
                  throws UnsupportedFlavorException
    {
       if (df.equals (imageFlavor))
           return picture;
       else
           throw new UnsupportedFlavorException (df);
    }
  
    public void lostOwnership (Clipboard c, Transferable contents)
    {
       System.out.println ("lost ownership");
    }
} 

class picture extends Canvas
{
   private Frame observer;  // The frame window that monitors an image.
   private Image image;     // The current image.

   picture (Frame observer)
   {
      this.observer = observer; // Save the current observer.
      setBackground (Color.lightGray);  
   }

   public void draw (Image image)
   {
      this.image = image;           // Save the current image.
      repaint ();                   // Draw the image.
   }

   public Dimension getPreferredSize ()
   {
      // When the layout manager calls picture's getPreferredSize ()
      // method, this method will return a Dimension object that
      // tells the layout manager how much room to reserve for the
      // picture component.  Because images are already present in
      // memory (by virtue of being on the clipboard), it's okay to
      // call the Image's getWidth and getHeight methods.  However,
      // an 80 pixel border is also provided.

      return new Dimension (image.getWidth (observer) + 80,
                            image.getHeight (observer) + 80);
   }

   public void paint (Graphics g)
   {
      // Determine the upper-left image corner (x, y) coordinates so
      // that the image will be centered.

      int x = (getSize ().width - image.getWidth (observer)) / 2;
      int y = (getSize ().height - image.getHeight (observer)) / 2;

      // Draw the image.

      g.drawImage (image, x, y, observer);
   }

   public void update (Graphics g)
   {
      // The inherited update method clears the background prior
      // to calling paint.  This results in flicker.  The flicker
      // is eliminated by overriding update.

      paint (g);
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -