当前位置: 技术问答>java相关
java中的clipboard是不是只能操作文本数据呀,其他数据格式如何操作?
来源: 互联网 发布时间:2017-04-01
本文导语: java中的clipboard是不是只能操作文本数据呀,其他数据格式如何操作? | 一个用clipboard操作图片的例子你看: import java.awt.*; import java.awt.datatransfer.*; import java.io.*; public class ImageSelection implem...
java中的clipboard是不是只能操作文本数据呀,其他数据格式如何操作?
|
一个用clipboard操作图片的例子你看:
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;
public class ImageSelection implements Transferable,
ClipboardOwner {
static public DataFlavor ImageFlavor;
private DataFlavor[] flavors = {ImageFlavor};
private Image image;
static {
try {
ImageFlavor = new DataFlavor(
Class.forName(“java.awt.Image”), “AWT Image”);
}
catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(java.util.mime.MimeTypeParseException e) {
e.printStackTrace();
}
}
public ImageSelection(Image image) {
this.image = image;
}
public synchronized DataFlavor[] getTransferDataFlavors() {
return flavors;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(ImageFlavor);
}
public synchronized Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if(flavor.equals(ImageFlavor)) {
return image;
}
else {
throw new UnsupportedFlavorException(flavor);
}
}
public void lostOwnership(Clipboard c, Transferable t) {
}
}
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
public class ClipboardTest2 extends Applet
implements ClipboardOwner {
private Clipboard clipboard;
private ImageCanvas copyFrom = new ImageCanvas();
private ImageCanvas copyTo = new ImageCanvas();
private Button copy = new Button(“Copy”);
private Button paste = new Button(“Paste”);
public void init() {
clipboard = new Clipboard(“image clipboard”);
copyFrom.setImage(getImage(getCodeBase(),”skelly.gif”));
add(copyFrom);
add(copyTo);
add(copy);
add(paste);
copy.addActionListener (new CopyListener());
paste.addActionListener(new PasteListener());
}
class CopyListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
ImageSelection contents =
new ImageSelection(copyFrom.getImage());
clipboard.setContents(contents, ClipboardTest2.this);
}
}
class PasteListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
Transferable contents = clipboard.getContents(this);
if(contents != null &&
contents.isDataFlavorSupported(
ImageSelection.ImageFlavor)) {
try {
Image image;
image = (Image) contents.getTransferData(
ImageSelection.ImageFlavor);
copyTo.setImage(image);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
public void lostOwnership(Clipboard clip,
Transferable transferable) {
System.out.println(“Lost ownership”);
}
}
class ImageCanvas extends Panel {
private Image image;
public ImageCanvas() {
this(null);
}
public ImageCanvas(Image image) {
if(image != null)
setImage(image);
}
public void paint(Graphics g) {
g.setColor(Color.lightGray);
g.draw3DRect(0,0,getSize().width-1,
getSize().height-1,true);
if(image != null) {
g.drawImage(image, 1, 1, this);
}
}
public void update(Graphics g) {
paint(g);
}
public void setImage(Image image) {
this.image = image;
try {
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image, 0);
tracker.waitForID(0);
}
catch(Exception e) { e.printStackTrace(); }
if(isShowing()) {
repaint();
}
}
public Image getImage() {
return image;
}
public Dimension getPreferredSize() {
return new Dimension(100,100);
}
}
import java.awt.*;
import java.awt.image.*;
import java.awt.datatransfer.*;
import java.io.*;
public class ImageSelection2 implements Transferable,
ClipboardOwner {
static public DataFlavor ImageFlavor;
static public DataFlavor ImageArrayFlavor;
private DataFlavor[] flavors = {ImageFlavor,
ImageArrayFlavor};
private Image image;
private int width, height;
static {
try {
ImageFlavor = new DataFlavor(
Class.forName(“java.awt.Image”),
“AWT Image”);
ImageArrayFlavor = new DataFlavor(“image/gif”,
“GIF Image”);
}
catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(java.util.mime.MimeTypeParseException e) {
e.printStackTrace();
}
}
public ImageSelection2(Image image, int width, int height) {
this.image = image;
this.width = width;
this.height = height;
}
public synchronized DataFlavor[] getTransferDataFlavors() {
return flavors;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(ImageFlavor) ||
flavor.equals(ImageArrayFlavor);
}
public synchronized Object getTransferData(
DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if(flavor.equals(ImageFlavor)) {
return image;
}
else if(flavor.equals(ImageArrayFlavor)) {
return imageToArray();
}
else
throw new UnsupportedFlavorException(flavor);
}
public void lostOwnership(Clipboard c, Transferable t) {
}
private int[] imageToArray() {
int[] pixels = new int[width*height];
PixelGrabber pg = new PixelGrabber(image,0,0,
width,height,pixels,0,width);
try { pg.grabPixels(); }
catch(InterruptedException e) { e.printStackTrace(); }
return pixels;
}
}
class PasteListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
Transferable contents = clipboard.getContents(this);
if(contents != null) {
try {
int[] array = (int[])
contents.getTransferData(
ImageSelection2.ImageArrayFlavor);
copyTo.setImage(waveThis(array,width,height));
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
给分吧!!!
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.*;
public class ImageSelection implements Transferable,
ClipboardOwner {
static public DataFlavor ImageFlavor;
private DataFlavor[] flavors = {ImageFlavor};
private Image image;
static {
try {
ImageFlavor = new DataFlavor(
Class.forName(“java.awt.Image”), “AWT Image”);
}
catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(java.util.mime.MimeTypeParseException e) {
e.printStackTrace();
}
}
public ImageSelection(Image image) {
this.image = image;
}
public synchronized DataFlavor[] getTransferDataFlavors() {
return flavors;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(ImageFlavor);
}
public synchronized Object getTransferData(DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if(flavor.equals(ImageFlavor)) {
return image;
}
else {
throw new UnsupportedFlavorException(flavor);
}
}
public void lostOwnership(Clipboard c, Transferable t) {
}
}
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
public class ClipboardTest2 extends Applet
implements ClipboardOwner {
private Clipboard clipboard;
private ImageCanvas copyFrom = new ImageCanvas();
private ImageCanvas copyTo = new ImageCanvas();
private Button copy = new Button(“Copy”);
private Button paste = new Button(“Paste”);
public void init() {
clipboard = new Clipboard(“image clipboard”);
copyFrom.setImage(getImage(getCodeBase(),”skelly.gif”));
add(copyFrom);
add(copyTo);
add(copy);
add(paste);
copy.addActionListener (new CopyListener());
paste.addActionListener(new PasteListener());
}
class CopyListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
ImageSelection contents =
new ImageSelection(copyFrom.getImage());
clipboard.setContents(contents, ClipboardTest2.this);
}
}
class PasteListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
Transferable contents = clipboard.getContents(this);
if(contents != null &&
contents.isDataFlavorSupported(
ImageSelection.ImageFlavor)) {
try {
Image image;
image = (Image) contents.getTransferData(
ImageSelection.ImageFlavor);
copyTo.setImage(image);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
public void lostOwnership(Clipboard clip,
Transferable transferable) {
System.out.println(“Lost ownership”);
}
}
class ImageCanvas extends Panel {
private Image image;
public ImageCanvas() {
this(null);
}
public ImageCanvas(Image image) {
if(image != null)
setImage(image);
}
public void paint(Graphics g) {
g.setColor(Color.lightGray);
g.draw3DRect(0,0,getSize().width-1,
getSize().height-1,true);
if(image != null) {
g.drawImage(image, 1, 1, this);
}
}
public void update(Graphics g) {
paint(g);
}
public void setImage(Image image) {
this.image = image;
try {
MediaTracker tracker = new MediaTracker(this);
tracker.addImage(image, 0);
tracker.waitForID(0);
}
catch(Exception e) { e.printStackTrace(); }
if(isShowing()) {
repaint();
}
}
public Image getImage() {
return image;
}
public Dimension getPreferredSize() {
return new Dimension(100,100);
}
}
import java.awt.*;
import java.awt.image.*;
import java.awt.datatransfer.*;
import java.io.*;
public class ImageSelection2 implements Transferable,
ClipboardOwner {
static public DataFlavor ImageFlavor;
static public DataFlavor ImageArrayFlavor;
private DataFlavor[] flavors = {ImageFlavor,
ImageArrayFlavor};
private Image image;
private int width, height;
static {
try {
ImageFlavor = new DataFlavor(
Class.forName(“java.awt.Image”),
“AWT Image”);
ImageArrayFlavor = new DataFlavor(“image/gif”,
“GIF Image”);
}
catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(java.util.mime.MimeTypeParseException e) {
e.printStackTrace();
}
}
public ImageSelection2(Image image, int width, int height) {
this.image = image;
this.width = width;
this.height = height;
}
public synchronized DataFlavor[] getTransferDataFlavors() {
return flavors;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return flavor.equals(ImageFlavor) ||
flavor.equals(ImageArrayFlavor);
}
public synchronized Object getTransferData(
DataFlavor flavor)
throws UnsupportedFlavorException, IOException {
if(flavor.equals(ImageFlavor)) {
return image;
}
else if(flavor.equals(ImageArrayFlavor)) {
return imageToArray();
}
else
throw new UnsupportedFlavorException(flavor);
}
public void lostOwnership(Clipboard c, Transferable t) {
}
private int[] imageToArray() {
int[] pixels = new int[width*height];
PixelGrabber pg = new PixelGrabber(image,0,0,
width,height,pixels,0,width);
try { pg.grabPixels(); }
catch(InterruptedException e) { e.printStackTrace(); }
return pixels;
}
}
class PasteListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
Transferable contents = clipboard.getContents(this);
if(contents != null) {
try {
int[] array = (int[])
contents.getTransferData(
ImageSelection2.ImageArrayFlavor);
copyTo.setImage(waveThis(array,width,height));
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
给分吧!!!