how about changing to pixel array ?
//There are 3 classes, please give a *.gif to try, what u need to focus is the ImageData.java
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
public class ImageEditor extends JPanel {
public final static int LINE_WIDTH = 2;
protected ImageIcon icon;
protected Point start = new Point(0, 0);
protected Point finish = new Point(0, 0);
protected Point pastePoint;
protected JPopupMenu popupMenu;
protected AbstractAction cutAction;
protected AbstractAction copyAction;
protected AbstractAction pasteAction;
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("You must specify the name of an image file");
return;
}
ImageEditor editor = new ImageEditor(args[0]);
JFrame f = new JFrame(args[0]);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(editor);
f.setSize(400, 300);
f.setVisible(true);
}
public ImageEditor(String name) {
super();
buildPopupMenu();
setBackground(Color.black);
setLayout(new GridLayout(1, 1, 0, 0));
icon = new ImageIcon(name);
JLabel label = new JLabel(icon);
label.setHorizontalAlignment(SwingConstants.LEFT);
label.setVerticalAlignment(SwingConstants.TOP);
label.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent event) {
handleMouseDown(event);
}
});
label.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent event) {
handleMouseDrag(event);
}
});
JScrollPane jsp = new JScrollPane(label);
add(jsp);
}
protected void handleMouseDown(MouseEvent event) {
if ((event.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
start = event.getPoint();
finish = event.getPoint();
}
else if ((event.getModifiers() & InputEvent.BUTTON3_MASK) != 0) {
displayPopupMenu(event.getPoint());
pastePoint = event.getPoint();
}
}
protected void handleMouseDrag(MouseEvent event) {
finish = event.getPoint();
repaint();
}
protected void buildPopupMenu() {
popupMenu = new JPopupMenu();
copyAction = new AbstractAction("Copy") {
public void actionPerformed(ActionEvent event) {
performCopy();
}
};
popupMenu.add(copyAction);
cutAction = new AbstractAction("Cut") {
public void actionPerformed(ActionEvent event) {
performCut();
}
};
popupMenu.add(cutAction);
pasteAction = new AbstractAction("Paste") {
public void actionPerformed(ActionEvent event) {
performPaste();
}
};
popupMenu.add(pasteAction);
}
protected void displayPopupMenu(Point p) {
Clipboard cb = getToolkit().getSystemClipboard();
Transferable t = cb.getContents(this);
boolean isSelected = !(start.equals(finish));
cutAction.setEnabled(isSelected);
copyAction.setEnabled(isSelected);
boolean canPaste = ((t != null) &&
(t.isDataFlavorSupported(
ImageSelection.IMAGE_DATA_FLAVOR)));
pasteAction.setEnabled(canPaste);
popupMenu.show(this, p.x, p.y);
}
protected void performCopy() {
Rectangle r = getSelectedArea();
int[] pixels = getPixels(r);
ImageData data = new ImageData(r.width, r.height, pixels);
ImageSelection selection = new ImageSelection(data);
Clipboard cb = getToolkit().getSystemClipboard();
cb.setContents(selection, selection);
}
protected void performCut() {
Rectangle r = getSelectedArea();
int[] pixels = getPixels(r);
ImageData data = new ImageData(r.width, r.height, pixels);
ImageSelection selection = new ImageSelection(data);
Clipboard cb = getToolkit().getSystemClipboard();
cb.setContents(selection, selection);
for (int i = 0; i < pixels.length; i++) {
pixels = 0;
}
setPixels(pixels, r);
}
protected void performPaste() {
Clipboard cb = getToolkit().getSystemClipboard();
try {
Transferable t = cb.getContents(this);
if (t.isDataFlavorSupported(
ImageSelection.IMAGE_DATA_FLAVOR)) {
ImageData data = (ImageData)(t.getTransferData(
ImageSelection.IMAGE_DATA_FLAVOR));
Rectangle area = new Rectangle(start.x, start.y,
data.getWidth(), data.getHeight());
int[] pixels = data.getPixelData();
setPixels(pixels, area);
}
}
catch (Exception e) {
JOptionPane.showMessageDialog(this,
"Unable to paste clipboard data");
}
}
protected Rectangle getSelectedArea() {
int width = finish.x - start.x;
int height = finish.y - start.y;
return new Rectangle(start.x, start.y, width, height);
}
protected int[] getPixels(Rectangle area) {
int[] pixels = new int[area.width * area.height];
PixelGrabber pg = new PixelGrabber(icon.getImage(), area.x,
area.y, area.width,
area.height, pixels, 0,
area.width);
try {
pg.grabPixels();
} catch (Exception e) {}
;
return pixels;
}
protected void setPixels(int[] newPixels, Rectangle area) {
int pixel;
Image image = icon.getImage();
int imageWidth = icon.getIconWidth();
int imageHeight = icon.getIconHeight();
int[] oldPixels = new int[imageWidth * imageHeight];
PixelGrabber pg = new PixelGrabber(image, 0, 0, imageWidth,
imageHeight, oldPixels, 0,
imageWidth);
try {
pg.grabPixels();
} catch (Exception e) {}
;
for (int y = 0; y < area.height; y++) {
if (imageHeight <= area.y + y) {
break;
}
for (int x = 0; x < area.width; x++) {
if (imageWidth <= area.x + x) {
break;
}
oldPixels[((area.y + y) * imageWidth) + area.x + x] =
newPixels[(area.width * y) + x];
}
}
MemoryImageSource mis = new MemoryImageSource(imageWidth,
imageHeight, oldPixels, 0, imageWidth);
icon.setImage(createImage(mis));
repaint();
}
public void paint(Graphics g) {
super.paint(g);
int width = finish.x - start.x;
int height = finish.y - start.y;
if ((width > 0) && (height > 0)) {
g.setColor(Color.blue);
for (int i = 0; i < LINE_WIDTH; i++) {
g.drawRect(start.x + i, start.y + i, width, height);
}
}
}
}
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.image.*;
import java.io.*;
import com.sun.image.codec.jpeg.*;
public class ImageSelection implements Transferable, ClipboardOwner {
protected ImageData imageData;
public final static DataFlavor IMAGE_DATA_FLAVOR =
new DataFlavor(ImageData.class, "Image Data");
public final static DataFlavor JPEG_MIME_FLAVOR =
new DataFlavor("image/jpeg", "JPEG Image Data");
protected final static DataFlavor[] flavors = {
JPEG_MIME_FLAVOR, IMAGE_DATA_FLAVOR
};
public ImageSelection(ImageData data) {
imageData = data;
}
public Object getTransferData(DataFlavor flavor)
throws java.io.IOException, UnsupportedFlavorException {
if (flavor.equals(IMAGE_DATA_FLAVOR)) {
return imageData;
} else if (flavor.equals(JPEG_MIME_FLAVOR)) {
return getJPEGInputStream();
}
throw new UnsupportedFlavorException(flavor);
}
protected InputStream getJPEGInputStream() throws IOException {
int width = imageData.getWidth();
int height = imageData.getHeight();
MemoryImageSource mis = new MemoryImageSource(width, height,
imageData.getPixelData(), 0, width);
BufferedImage bi =
new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2d = bi.createGraphics();
Image img = Toolkit.getDefaultToolkit().createImage(mis);
g2d.drawImage(img, 0, 0, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JPEGImageEncoder jie = JPEGCodec.createJPEGEncoder(baos);
jie.encode(bi);
baos.close();
return new ByteArrayInputStream(baos.toByteArray());
}
public DataFlavor[] getTransferDataFlavors() {
return flavors;
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
for (int i = 0; i < flavors.length; i++) {
if (flavor.equals(flavors)) {
return true;
}
}
return false;
}
public void lostOwnership(Clipboard cb, Transferable t) {}
}
public class ImageData implements java.io.Serializable {
protected int width;
protected int height;
protected int[] pixelData;
public ImageData(int width, int height, int[] pixels) {
this.width = width;
this.height = height;
pixelData = (int[])(pixels.clone());
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int[] getPixelData() {
return pixelData;
}
}
yundi at 2007-7-4 20:13:00 >
