Skip to main content

Is there a trick to pasting a GIF file?

4 replies [Last post]
viravan
Offline
Joined: 2003-06-23
Points: 0

I am using 1.4.2 and the following little program is lifted from one of the TechTip articles ( http://developer.java.sun.com/developer/JDCTechTips/2003/tt0318.html#1 ).

The problem is that GIF image pasted from the system clipboard is completely distorted... (it doesn't matter whether the image is originally copied to the clipboard from MS Paint or Adobe PhotoShop).

Here is a link to what I'm talking about:

http://home.comcast.net/~viravan/Capture.png

here is the code:

DrawImage.java

import java.awt.*;<br />
import java.awt.event.*;<br />
import java.awt.datatransfer.*;<br />
import javax.swing.*;<br />
public class DragImage {<br />
   public static void main(String args[])  {<br />
      JFrame frame = new JFrame("Drag Image");<br />
      frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);<br />
      Container contentPane = frame.getContentPane();<br />
      final Clipboard clipboard = frame.getToolkit().getSystemClipboard();<br />
      final JLabel label = new JLabel();<br />
      if (args.length > 0)  {<br />
         Icon icon = new ImageIcon(args[0]);<br />
         label.setIcon(icon);<br />
      }<br />
      label.setTransferHandler(new ImageSelection());<br />
      MouseListener mouseListener = new MouseAdapter()  {<br />
         public void mousePressed(MouseEvent e)  {<br />
            JComponent comp = (JComponent) e.getSource();<br />
            TransferHandler handler = comp.getTransferHandler();<br />
            handler.exportAsDrag( comp, e, TransferHandler.COPY);<br />
         }<br />
      };<br />
      label.addMouseListener(mouseListener);<br />
      JScrollPane pane = new JScrollPane(label);<br />
      contentPane.add(pane, BorderLayout.CENTER);<br />
      JButton copy = new JButton("Copy");<br />
      copy.addActionListener(new ActionListener()  {<br />
         public void actionPerformed(ActionEvent e)  {<br />
            Action copyAction = TransferHandler.getCopyAction();<br />
            copyAction.actionPerformed( new ActionEvent( label, ActionEvent.ACTION_PERFORMED, (String) copyAction.getValue(Action.NAME), EventQueue.getMostRecentEventTime(), 0));<br />
         }<br />
      });<br />
      JButton clear = new JButton("Clear");<br />
      clear.addActionListener(new ActionListener()  {<br />
         public void actionPerformed( ActionEvent actionEvent)  {<br />
            label.setIcon(null);<br />
         }<br />
      });<br />
      JButton paste = new JButton("Paste");<br />
      paste.addActionListener(new ActionListener()  {<br />
         public void actionPerformed( ActionEvent actionEvent)  {<br />
            Action pasteAction = TransferHandler.getPasteAction();<br />
            pasteAction.actionPerformed( new ActionEvent(label, ActionEvent.ACTION_PERFORMED, (String) pasteAction.getValue(Action.NAME), EventQueue.getMostRecentEventTime(), 0));<br />
         }<br />
      });<br />
      JPanel p = new JPanel();<br />
      p.add(copy);<br />
      p.add(clear);<br />
      p.add(paste);<br />
      contentPane.add(p, BorderLayout.SOUTH);<br />
      frame.setSize(300, 300);<br />
      frame.show();<br />
   }<br />
}<br />

ImageSelection.java
[code]
import java.awt.*;
import java.awt.image.*;
import java.awt.datatransfer.*;
import java.io.*;
import javax.swing.*;
public class ImageSelection extends TransferHandler {
private static final DataFlavor flavors[] = {
DataFlavor.imageFlavor
};
public int getSourceActions(JComponent c) {
return TransferHandler.COPY;
}
public boolean canImport( JComponent comp, DataFlavor flavor[]) {
if (!(comp instanceof JLabel)) {
return false;
}
for (int i=0, n=flavor.length;i

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Anonymous

It seems that the Java datatransfer subsystem translates
256-color device-independent bitmaps (DIBs) into Java
Image incorrectly. Actually a GIF file opened in MS Paint
is transferred via the clipboard in the clipboard format
DIB. If you are interested, please read
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/w...
Is this issue critical for you?

Thanks,
Alexander

viravan
Offline
Joined: 2003-06-23
Points: 0

>Is this issue critical for you?

As indicated in the bug report, it is [b]not[/b] that critical but if you could give me a pointer on how to override the existing reader with my own, I could probably tackle the problem myself instead of waiting for a bug fix.

Thanks again.

V.V.

Anonymous

It may be a bug in JDK. Would you submit a bug against
Java 2 SDK/drag&drop about it? We'll investigate the
issue.

As a poor workaround one could comment the line "DIB=..."
in the file jre\lib\flavormap.properties, it will affect
all Java apps run with this JRE. The consequence is that
one could paste an image in the scenario you have but
one couldn't transfer an image in some other cases.

Another workaround that affects only one Java app is
to add the following piece of code to the initialization
part of the app. The consequence is the same but applies
only to that Java app.

[code]
if (System.getProperty("os.name").startsWith("Windows")) {
FlavorMap fm = SystemFlavorMap.getDefaultFlavorMap();
if (fm instanceof SystemFlavorMap) {
SystemFlavorMap sfm = (SystemFlavorMap)fm;
sfm.setFlavorsForNative("DIB", new DataFlavor[0]);
}
}
[/code]

Thanks,
Alexander

> I am using 1.4.2 and the following little program is
> lifted from one of the TechTip articles (
> http://developer.java.sun.com/developer/JDCTechTips/20
> 03/tt0318.html#1 ).
>
> The problem is that GIF image pasted from the system
> clipboard is completely distorted... (it doesn't
> matter whether the image is originally copied to the
> clipboard from MS Paint or Adobe PhotoShop).

viravan
Offline
Joined: 2003-06-23
Points: 0

>Another workaround that affects only one Java app is
to add the following piece of code to the initialization
part of the app. The consequence is the same but applies
only to that Java app.

[code]
if (System.getProperty("os.name").startsWith("Windows")) {
FlavorMap fm = SystemFlavorMap.getDefaultFlavorMap();
if (fm instanceof SystemFlavorMap) {
SystemFlavorMap sfm = (SystemFlavorMap)fm;
sfm.setFlavorsForNative("DIB", new DataFlavor[0]);
}
}
[/code]

You're right in both counts:

1) The workaround shown above will let me paste the GIF image.
2) I can no longer drag the image and drop it elsewhere.

I have submitted a bug report per your suggestion and the report has been assigned an [B]internal review ID of: 227305[/B]

Thanks!

V.V.

Message was edited by: viravan