当前位置: 技术问答>java相关
[高分求救]怎么使用剪切板来进行图片的存入和取出?
来源: 互联网 发布时间:2015-10-25
本文导语: 好象,java.awt.datatransfer.Clipboard类只能存取类型为String的数据,如果是图象或者其他类型的数据,怎么样进行剪切板操作呢? 最好有一些例子代码,谢谢! | import java.awt.*; import java.awt.datatrans...
好象,java.awt.datatransfer.Clipboard类只能存取类型为String的数据,如果是图象或者其他类型的数据,怎么样进行剪切板操作呢?
最好有一些例子代码,谢谢!
最好有一些例子代码,谢谢!
|
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.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();
}
}
}
}
给分吧!!!
|
你可以试试:你可以先直接读取图象文件置入byte数组中(通过bufferedImage类),然后转换成String格式传给Clipboard,当做到粘贴动作时,再转换一次,对屏幕进行写操作。具体我没试过,但是这两天我一直在做TGA的IMAGE显示,应该是可行的。
|
up
|
这么快就写出来啦??
呵呵,上班肯定不务正业啊……:)
呵呵,上班肯定不务正业啊……:)
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。