library(SJava) .JavaInit() panel <- .JNew("JPanel") panel$setLayout(.JNew("BorderLayout")) label <- .JNew("JLabel", "( , )") comp <- .JNew("JPanel") panel$add("North", label) panel$add("Center", comp) .JNew("GenericFrame", panel, T) comp$setBackground(.Java("Color", "red")) jdynamicCompile("java.awt.event.MouseMotionListener", "RMouseListener") mouseListener <- function(jlabel) { x <- 1 y <- 1 mouseMoved <- function(ev) { x <<- ev$getX() y <<- ev$getY() # print(c(x,y)) jlabel$setText(paste("(",x,", ",y,")", sep="")) } # If one clicks on the canvas and move the mouse, an error is generated # because we didn't provide a mouseDragged. So we duplicate the function # here so that the Java class can find the method. return(list(mouseMoved = mouseMoved, mouseDragged = mouseMoved, where=function(){c(x,y)})) } l <- mouseListener(label) r <- foreignReference(l) comp$addMouseMotionListener(.JavaConstructor("RMouseListener", r, .convert = FALSE))