.packageName <- "JavaGD"
JavaGD <- function(name="JavaGD", width=400, height=300, ps=12) {
  # if rJava is loaded, we use it to initialize the class path,
  # because it knows how to merge class path into a running VM - we don't
  if ("rJava" %in% .packages() && nchar(.javaGD.get.class.path())>0) {
    .jinit(.javaGD.get.class.path())
    .javaGD.set.class.path("")
  }
  invisible(.C("newJavaGD",as.character(name),as.numeric(width),as.numeric(height),as.numeric(ps),PACKAGE="JavaGD"))
}

.getJavaGDObject <- function(devNr) {
    a<-.Call("javaGDobjectCall",as.integer(devNr-1),PACKAGE="JavaGD")
    if (!is.null(a)) {
    	if (exists(".jmkref")) a <- .jmkref(a)
	else stop(".jmkref is not available. Please use rJava 0.3 or higher.")
    }
}
.javaGD.get.size <- function(devNr=dev.cur()) {
    par<-rep(0,6)
    l<-.C("javaGDgetSize",as.integer(devNr-1),as.double(par), PACKAGE="JavaGD")
    par<-l[[2]]
    if (par[6]==0) list() else list(x=par[1],y=par[2],width=(par[3]-par[1]),height=(par[4]-par[2]),dpiX=par[5],dpiY=par[6])
}

.javaGD.copy.device <- function(devNr=dev.cur(), device=pdf, width.diff=0, height.diff=0, ...) {
    s<-.javaGD.get.size(devNr)
    pd<-dev.cur()
    dev.set(devNr)
    dev.copy(device, width=par()$din[1]/0.72+width.diff, height=par()$din[2]/0.72+height.diff, ...)
    dev.off()
    dev.set(pd)
    invisible(devNr)
}

.javaGD.version <- function() {
	v<-.C("javaGDversion",as.integer(rep(0,4)), PACKAGE="JavaGD")[[1]]
	list(major=v[1]%/%65536, minor=(v[1]%/%256)%%256, patch=(v[1]%%256), numeric=v[1])
}

.javaGD.set.display.parameters <- function(dpiX=100, dpiY=100, aspect=1) {
	invisible(.C("javaGDsetDisplayParam",as.double(c(dpiX, dpiY, aspect)),PACKAGE="JavaGD"))
}

.javaGD.set.class.path <- function(cp) {
  if (length(cp)<1) stop("Invalid class path")
  invisible(.C("setJavaGDClassPath",as.character(cp),PACKAGE="JavaGD"))
}

.javaGD.get.class.path <- function() {
  .C("getJavaGDClassPath","",PACKAGE="JavaGD")[[1]]
}
.First.lib <-
function(libname, pkgname) {
    javahome <- Sys.getenv("JAVA_HOME")
    .putenv <- if (exists("Sys.setenv")) Sys.setenv else Sys.putenv
    if(!nchar(javahome)) {
	# let's try to fetch the paths from registry via WinRegistry.dll
	javahome <- NULL
	library.dynam("WinRegistry", pkgname, libname)
	key<-"Software\\JavaSoft\\Java Runtime Environment"
	jrever <- .Call("RegGetStrValue",c(key,"CurrentVersion"))
	if (is.null(jrever)) { # try JDK if JRE fails
	    key<-"Software\\JavaSoft\\Java Development Kit"
	    jrever <- .Call("RegGetStrValue",c(key,"CurrentVersion"))
	}
	if (!is.null(jrever)) {
	    dispver <- jrever
	    key<-paste(key,jrever,sep="\\")
	    micro <- .Call("RegGetStrValue", c(key,"MicroVersion"))
	    if (!is.null(micro)) dispver <- paste(dispver,micro,sep=".")
#	    cat("using Java Runtime version",dispver,"\n")
	    javahome <- .Call("RegGetStrValue",c(key,"JavaHome"))
	    if (!is.null(javahome)) { # ok, let's try to get the real lib path
		p <- .Call("RegGetStrValue",c(key,"RuntimeLib"))
		if (!is.null(p)) {
		    # the following assumes that the entry is of the form
		    # ...\jvm.dll - this should be ok since if it's not,
		    # then we won't find the DLL either.
		    # Note that we just add it to the PATH so if this fails
		    # we still fall back to the JavaHome entry.
		    .putenv(PATH=paste(Sys.getenv("PATH"),
					  substr(p,1,nchar(p)-8),sep=";"))
		}
	    }
	}
	if (is.null(javahome))
	    stop("JAVA_HOME is not set")
    }
    if(!nchar(javahome)) stop("JAVA_HOME is not set")
#    else cat("using JAVA_HOME =", javahome, "\n")
    .putenv(PATH=paste(Sys.getenv("PATH"),
               file.path(javahome, "bin", "client"), sep=";"))
    .javaGD.jar.file<-paste(libname,pkgname,"java","javaGD.jar",sep=.Platform$file.sep)
    library.dynam("JavaGD", pkgname, libname)
    # set internal classpath in case we really need to initialize the JVM
    .C("setJavaGDClassPath",paste(.javaGD.jar.file,Sys.getenv("CLASSPATH"),sep=";"))
}
