Tag Archives: Programming

Using JavaGD in Java applications

Goal: Create a Java application with a JFrame that contains a JGDPanel from JavaGD and plot something via JRI.

Solution: The following code is a derivate of JGR code snipplets under GPL-2:

import java.awt.Component;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;

import org.rosuda.JRI.Rengine;
import org.rosuda.javaGD.GDInterface;
import org.rosuda.javaGD.JGDBufferedPanel;

public class JavaGDExample extends GDInterface implements WindowListener {

    JFrame f;
   
    public void gdOpen(double w, double h) {
        if (f!=null) gdClose();
        f = new JFrame("JavaGD Example");
        f.addWindowListener(this);      
        c = new JGDBufferedPanel(w, h);
        f.getContentPane().add((Component) c);
        f.pack();
        f.setVisible(true);
    }
   
    public void gdClose() {
        super.gdClose();
        if (f!=null) {
            c=null;
            f.removeAll();
            f.dispose();
            f=null;
        }
    }

    public static void main(String[] args) {
        Rengine engine = new Rengine(new String[] {"--vanilla"}, true, null);      
        engine.eval(".setenv <- if (exists("Sys.setenv")) Sys.setenv else Sys.putenv");
        engine.eval(".setenv("JAVAGD_CLASS_NAME"="JavaGDExample")");
        engine.eval("library(JavaGD)");
        engine.eval("JavaGD()");
        engine.eval("plot(rnorm(100))");       
    }
   
    /** listener response to "Close" - effectively invokes <code>dev.off()</code> on the device */
    public void windowClosing(WindowEvent e) {
        if (c!=null) executeDevOff();
    }
    public void windowClosed(WindowEvent e) {}
    public void windowOpened(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}
    public void windowDeactivated(WindowEvent e) {}
}

Et voilà:

JavaGDExample

Of course you have to make sure that the JRI native library is in a directory listed in java.library.path, for example on my machine via:

java -Djava.library.path=/usr/local/lib/R/site-library/rJava/jri/ JavaGDExample

Also you have to provide the correct path for the environment variable R_HOME such as:

R_HOME=/usr/lib64/R/

And of course you need the two jars javaGD.jar and JRIEngine.jar in your CLASSPATH.

Because it is autumn

autumn

autumn

library(colorspace)
branchvar <- 1
tree <- function(x, y, branch) {
  lines(x,y,col=hex(RGB(0.1,0.4,0)))
  wadd <- 0.7
  if(branch>0) {
    alpha <- atan2((y[2]-y[1]),(x[2]-x[1]))
    len <- sqrt((y[2]-y[1])^2+(x[2]-x[1])^2)*0.6
    tree(c(x[2],x[2]+abs(rnorm(1,1,branchvar))*len*cos(alpha)),
          c(y[2],y[2]+abs(rnorm(1,1,branchvar))*len*sin(alpha)),
          branch-1)
    tree(c(x[2],x[2]+len*cos(alpha+wadd)),
          c(y[2],y[2]+len*sin(alpha+wadd)),branch-1)
    tree(c(x[2],x[2]+len*cos(alpha-wadd)),
          c(y[2],y[2]+len*sin(alpha-wadd)),branch-1)    
  } else {
    points(x[2],y[2],pch=16,
             col=hex(RGB(rbeta(1,2.6,0.5),rbeta(1,0.5,3),0)))
  }
}

plot.new()
tree(c(0,0.20),c(0,0.20),7)

You need library colorspace.

Shortcuts in R under Unix from the readline library

Under Unix you can use in R the the advanced features for command editing and command history that the GNU Readline Library provides.

Both Emacs and vi editing modes are available and the Emacs-like keybindings are installed by default. Here some Emacs keybindings that also work in R (from the documentation of the GNU Readline Library):

Copy and Paste:
Ctrl-u Cut from the cursor to the beginning of the line.
Ctrl-k Cut from the cursor to the end of the line.
Ctrl-w Cut from the cursor to the start of the word.
Ctrl-y Pastes text from the clipboard.
Alt-y Rotate the kill-ring, and paste the new top. You can only do this if the prior command was Ctrl-y or Alt-y.

Moving:
Ctrl-a Move to the start of the line.
Ctrl-e Move to the end of the line.
Alt-b Move back one word.
Alt-f Move forward one word.
Ctrl-b Move back one character.
Ctrl-f Move forward one character.

Undo:
Ctrl-_ Undo the last changes.
Alt-r Undo all changes to the line.

Miscellaneous:
Ctrl-l Clear the screen leaving the current line at the top of the screen.

History:
Ctrl-r Incremental reverse search of history.
Alt-p Non-incremental reverse search of history.

If you want to use the vi-mode just press Ctrl+Alt+j and you can use the usual vi modes and commands (for example take a look at this vi-editing-mode-cheat-sheet).

If you want to start in the vi mode by default, put the following line in your ~/.inputrc (which of course will also effect your bash etc.):

set editing-mode vi