Re: [visad] Display alignment

Hi James,

As far as I know, there is no function to automatically scale based on
"all ink on screen" (though I agree such a feature would be useful).
However, you do have complete control over the display projection
using the ProjectionControl.setMatrix and ProjectionControl.getMatrix
commands. You may also be interested in that class's
setAspectCartesian and setAspect methods.

Below is an example class (modified from the one I sent you before)
that demonstrates how to control the projection. I have started the
display scaled slightly larger to nicely fill the whole window. I
suggest temporarily adding a way to dump the current projection matrix
for your app's display, to obtain the optimal matrix, then hardcoding
that matrix as the default starting projection.

Regarding the axes not rendering, I have never seen that problem. It
sounds like either a painting bug (try moving the display around), or
a nasty race condition, since it happens only intermittently. Have you
tried your app on a different machine? If you are running Windows,
have you tried with DirectX vs OpenGL (use the -Dj3d.rend=d3d or
-Dj3d.rend=ogl command line flags, respectively)? If you can concoct
an example that demonstrates the problem across multiple machines, it
would be very helpful for debugging.



import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import javax.swing.*;
import visad.*;
import visad.java3d.*;

public class PlotMatrix {

  private static double[] savedMatrix = {
    0.798, 0.0, 0.0, 0.109,
    0.0, 0.798, 0.0, 0.059,
    0.0, 0.0, 0.798, 0.0,
    0.0, 0.0, 0.0, 1.0

  public static void main(String[] args) throws Exception {
    // mock up some data
    System.out.println("Creating data...");
    final int w = 256, h = 256;
    float[][] dataSamples = new float[1][w * h];
    float min = Float.POSITIVE_INFINITY, max = Float.NEGATIVE_INFINITY;
    for (int y=0; y<h; y++) {
      for (int x=0; x<w; x++) {
        float v = (float)
          (Math.sin(Math.PI * x / w) + Math.cos(Math.PI * y / h));
        if (v < min) min = v;
        if (v > max) max = v;
        dataSamples[0][w * y + x] = v;
    RealType xType = RealType.getRealType("x");
    RealType yType = RealType.getRealType("y");
    RealTupleType xyType = new RealTupleType(xType, yType);
    RealType vType = RealType.getRealType("value");
    FunctionType fType = new FunctionType(xyType, vType);
    Integer2DSet dataSet = new Integer2DSet(xyType, w, h);
    FlatField data = new FlatField(fType, dataSet);

    // create references
    DataReferenceImpl dataRef = new DataReferenceImpl("data");

    // create display
    System.out.println("Plotting data...");
    final DisplayImpl d = new DisplayImplJ3D("display",
      new TwoDDisplayRendererJ3D());
    ScalarMap[] maps = data.getType().guessMaps(false);
    ScalarMap xMap = new ScalarMap(xType, Display.XAxis);
    ScalarMap yMap = new ScalarMap(yType, Display.YAxis);
    ScalarMap vMap = new ScalarMap(vType, Display.RGB);
    xMap.setRange(0, w);
    yMap.setRange(0, h);

    // create buttons
    JButton save = new JButton("Save matrix");
    save.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) { saveMatrix(d); }
    JButton load = new JButton("Restore matrix");
    load.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) { loadMatrix(d); }

    // show display onscreen
    JFrame f = new JFrame("Plot Matrix Test");
    JPanel p = new JPanel();
    p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
    JPanel buttons = new JPanel();
    buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
    f.setBounds(100, 100, 500, 500);

  public static void saveMatrix(DisplayImpl d) {
    savedMatrix = d.getProjectionControl().getMatrix();
    System.out.print("saving: matrix -> [");
    for (int i=0; i<savedMatrix.length; i++) {
      System.out.print(" " + savedMatrix[i]);
    System.out.println(" ]");

  public static void loadMatrix(DisplayImpl d) {
    try { d.getProjectionControl().setMatrix(savedMatrix); }
    catch (VisADException exc) { exc.printStackTrace(); }
    catch (RemoteException exc) { exc.printStackTrace(); }
    System.out.print("restored last saved matrix");


On 10/17/07, James Fishbaugh <jfishbaugh@xxxxxxxxx> wrote:
> Hello again,
> I was wondering if there is anyway I can tell visAD to make the best
> use of available space.  By default, it appears to align the box (I'm
> doing a 2D plot) in the center of the space at a reasonable size for
> the window.  However, when y-axis labels are long this forces the
> y-axis title off the screen.  Is there anyway I could get it to center
> based on all ink on screen?  Also, can I tell it to make the best use
> of space so it makes the plot bigger and puts everything in the
> center.  I've included a screenshot of the default behavior and a
> screenshot of what I'd like it to look like.
> Also, a separate but related note, I'm  having issues with things
> associated with the axes not rendering.  Sometimes I run it and there
> is nothing but a y-axis title -- no tick marks or labels, but the
> x-axis is fine.  Other times the opposite occurs.  Very rarely (it
> took me 7 "trys" to obtain the screenshots) does everything render as
> expected.  Has anyone encountered behavior like this?
> James
> _______________________________________________
> visad mailing list
> visad@xxxxxxxxxxxxxxxx
> For list information, to unsubscribe, visit: 

  • 2007 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the visad archives: