Skip to content

Instantly share code, notes, and snippets.

@kishida
Last active April 8, 2023 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kishida/c4ff37ea57f133bce66fe897fdfe7deb to your computer and use it in GitHub Desktop.
Save kishida/c4ff37ea57f133bce66fe897fdfe7deb to your computer and use it in GitHub Desktop.
JGraphXで画像をドロップするサンプル
package neoki.slm;
import com.mxgraph.model.mxCell;
import com.mxgraph.model.mxGeometry;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.*;
import java.io.File;
import java.util.List;
import java.util.stream.Stream;
public class JGraphXFileDropExample {
public static void main(String[] args) {
JFrame frame = new JFrame("JGraphX Example");
frame.setLayout(new BorderLayout());
mxGraph graph = new mxGraph();
graph.getModel().beginUpdate();
mxCell vertex1 = (mxCell) graph.insertVertex(graph.getDefaultParent(), null, "Hello", 20, 20, 80, 30);
mxGraphComponent graphComponent = new mxGraphComponent(graph);
graph.getModel().endUpdate();
frame.add(graphComponent, BorderLayout.CENTER);
var oldDropTarget = graphComponent.getDropTarget();
graphComponent.setDropTarget(new DropTarget(graphComponent, DnDConstants.ACTION_COPY, new DropTargetAdapter() {
@Override
public void dragEnter(DropTargetDragEvent dtde) {
if (!dtde.getTransferable().isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
oldDropTarget.dragEnter(dtde);
}
}
@Override
public void drop(DropTargetDropEvent dtde) {
try {
Transferable tr = dtde.getTransferable();
if (tr.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
List<File> fileList = (List<File>) tr.getTransferData(DataFlavor.javaFileListFlavor);
for (File file : fileList) {
String filePath = file.getAbsolutePath();
if (Stream.of(".jpg", ".jpeg", ".png", ".gif").anyMatch(p -> filePath.toLowerCase().endsWith(p))) {
ImageIcon imageIcon = new ImageIcon(filePath);
mxGeometry geometry = new mxGeometry(
dtde.getLocation().x - imageIcon.getIconWidth() / 2,
dtde.getLocation().y - imageIcon.getIconHeight() / 2,
imageIcon.getIconWidth(), imageIcon.getIconHeight());
String fileURL = file.toURI().toURL().toString();
mxCell cell = new mxCell(imageIcon.getImage(), geometry, "shape=image;image=" + fileURL);
cell.setVertex(true);
cell.setValue("");
cell.setConnectable(true);
graph.getModel().beginUpdate();
try {
graph.addCell(cell, graph.getDefaultParent());
} finally {
graph.getModel().endUpdate();
}
}
}
dtde.dropComplete(true);
} else {
if (oldDropTarget != null) {
oldDropTarget.drop(dtde);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}));
frame.setSize(800, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>naoki</groupId>
<artifactId>SmallLanguageModel</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.github.vlsi.mxgraph</groupId>
<artifactId>jgraphx</artifactId>
<version>4.2.2</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>20</maven.compiler.source>
<maven.compiler.target>20</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment