5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

processing3系でドラッグアンドドロップする

Last updated at Posted at 2017-12-23

はじめに

processing3系でドラッグアンドドロップを実装した所詰まった所を記録します。

この記事で紹介するコードの多くはたーせる氏のProcessingアプリケーションでファイルのドラッグ&ドロップをサポートの記事を参考に書かれています。

# コード

dragdrop.pde
import java.awt.datatransfer.*;  
import java.awt.dnd.*;  
import java.io.File;  
import java.io.IOException;  
import java.awt.Component;
import java.util.List;

DropTarget dropTarget;  
Component component;

void dragDropFile() {
  component = (Component)this.surface.getNative();
  
  dropTarget = new DropTarget(component, new DropTargetListener() {  
    public void dragEnter(DropTargetDragEvent dtde) {
    }  
    public void dragOver(DropTargetDragEvent dtde) {
    }  
    public void dropActionChanged(DropTargetDragEvent dtde) {
    }  
    public void dragExit(DropTargetEvent dte) {
    }  
    public void drop(DropTargetDropEvent dtde) {  
      dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);  
      Transferable trans = dtde.getTransferable();  
      List<File> fileNameList = null;  
      if (trans.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {  
        try {  
          fileNameList = (List<File>)  
            trans.getTransferData(DataFlavor.javaFileListFlavor);
        } 
        catch (UnsupportedFlavorException ex) {  
          println(ex);
        } 
        catch (IOException ex) {  
          println(ex);
        }
      }  
      if (fileNameList == null) return;  
      
      for (File f : fileNameList) {
        println(f.getAbsolutePath());
      }
    }
  }
  );
}

悩んだ所

processing3系でDropTargetの引数にスケッチを指すthisを入れようとすると、The constructor "dnd.DropTarget(sketch_171223a. DropTargetListner(){})" does not eistsと怒られてしまいます。
これはthisがそこに引数として入るべきであるComponent型では無いため怒られています。

そこで、java.awt.Componnet にスケッチを入れるためにthis.surface.getNative()をcomponent型として取って引数にする必要があります。

参考

http://tercel-sakuragaoka.blogspot.jp/2011/10/processing.html
https://qiita.com/progfay/items/a81e2c4dc4785c3492f4

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?