Re: drawing text in a window

Donna:

Sorry I've not had time to answer sooner. The only things I can suggest right now (not having had a chance to go thru your Java code) are to look at how Don did the axes labels (visad.AxisScale), or to put your color bar into a separate JPanel and just display that along with the VisAD JPanel. The advantage of the latter would be you would not have to worry about placing it on top of some "real data"...not to mention that in your own JPanel, you can very easily control the placement and sizes of things.

tom

Donna L Gresh wrote:



Well, first I have to thank you for the attached code; I'd never have
figured this out. I'm making progress on my ultimate mission of creating a
simple colorbar legend, but have run into a snag. Here's the picture I've
managed to create; I'd like to get rid of the white line surrounding the
text (though even then it's not really that great). I've tried other
things, like using "aspect" to make my colorbar long and skinny (instead of
the default aspect ratio of 1) but then my fonts get squished too. I've
attached my code as well. Is there an easier way to do this? This seems
unnaturally difficult. Have not others wanted a colorbar legend?

(Embedded image moved to file: pic30296.jpg)

(See attached file: ColorBar.java)

Donna L. Gresh, Ph.D.
IBM T.J. Watson Research Center
(914) 945-2472
http://www.research.ibm.com/people/g/donnagresh
gresh@xxxxxxxxxx




Tom Whittaker <tomw@xxxxxxxxxxx To: Donna L Gresh/Watson/IBM@IBMUS du> cc: Subject: Re: drawing text in a window 01/10/2003 02:31 PM Please respond to tomw



As far as I know, all shapes are VisADGeometryArrays.  You make a
ScalarMap of some RealType to Display.Shape.  The RealType is then also
used to create a 'sampling set' (a single Display.Shape can have many
individual Shapes, and they are indexed through the sampling set).  Your
ScalarMap also has a ShapeControl, to which to specify both the sampling
set and the array of VisADGeometryArrays.  You then have to create
location RealTuples for each shape, that contains it's coordinate and
it's 'index' value.  You add a reference to this into your Display, and
wala...there it is or they are!

I found the whole Shape thing too complicated to use for 'simple'
things, which is why I tried to streamline it all in Jython.

I've attached an example that should help.  Otherwise, you might look at
the code in subs.py, since it uses Shapes "underneath"...

Too bad about the Java -- I'm on a mission to get folks using
Jython...can't you tell?

tom

Donna L Gresh wrote:



Sorry to be dense, but how do I "add" the Shape to the display? I've

tried

what I thought were the obvious things, but the compiler complains. (Yes

I

have to use Java :-)

Donna L. Gresh, Ph.D.
IBM T.J. Watson Research Center
(914) 945-2472
http://www.research.ibm.com/people/g/donnagresh
gresh@xxxxxxxxxx







                     Tom Whittaker


                     <tomw@xxxxxxxxxxx        To:       Donna L

Gresh/Watson/IBM@IBMUS

                     du>                      cc:       visad-list

<visad-list@xxxxxxxxxxxxx>

                                              Subject:  Re: drawing text

in a window

                     01/10/2003 01:35


                     PM


                     Please respond to


                     tomw






Hi Donna...

Donna L Gresh wrote:


I'd like to just be able to draw some text at particular values in
the data set (i.e. x = datamin, y=-1, and x=datamax, y=-1 for my

horizontal


colorbar). Is this a big deal, or is there a simple way to do it?



In jython, you can use the display.drawString() method in subs.py.  This
allows you to specify the location of your string in terms of the types
mapped to the 'visad box' coordinates.  The signature is:

def drawString(self, string, position, color=None, center=0,
  font="futural",start=[0.,0.,0.], base=[.1,0.,0.],
  up=[0.,.1,0.],size=None ):

  """
  Draw a string of characters on this display.  <string> is the
  string of characters.  <position> is the starting location for the
  string drawing, expressed as a list/tuple of 2 or 3 values (x,y,z).
  <color> is the color (e.g., "red" or java.awt.Color; default
  white).  <center> if true will center the string.  <font> defiles
  the HersheyFont name to use.  <start> defines the relative location
  of the starting point (x,y,z; default = 0,0,0).  <base> defines the
  direction that the base of the string should point (x,y,z; default
  along x-axis).  <up> defines the direction of the vertical stroke
  for each character (default = along y-axis).  <size> is the relative
  size.  Returns the index to the shape of the text (useful for
  moving).

so, for example:

  disp.drawString("My string",(2.3, -2.1),color='green', center=1)

would put the "My string" characters in green starting at x=2.3 and
y=-2.1 in the display "disp".

If you have to use Java ;-) then you may want to create a visad.Shape
and use the PlotText.render_label() [or ...render_font()] method to
create a Shape that you can then add to the display whenever you want it.

Hope that helps.

tom

--
Tom Whittaker (tomw@xxxxxxxxxxxxx)
University of Wisconsin-Madison
Space Science and Engineering Center
Cooperative Institute for Meteorological Satellite Studies
Phone/VoiceMail: 608.262.2759





--
Tom Whittaker (tomw@xxxxxxxxxxxxx)
University of Wisconsin-Madison
Space Science and Engineering Center
Cooperative Institute for Meteorological Satellite Studies
Phone/VoiceMail: 608.262.2759
/*
VisAD system for interactive analysis and visualization of numerical
data.  Copyright (C) 1996 - 2001 Bill Hibbard, Curtis Rueden, Tom
Rink, Dave Glowacki, Steve Emmerson, Tom Whittaker, Don Murray, and
Tommy Jasmin.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/

// import needed classes
import visad.*;
import visad.java3d.DisplayImplJ3D;
import visad.java3d.DirectManipulationRendererJ3D;
import visad.java2d.DisplayImplJ2D;
import visad.java2d.DirectManipulationRendererJ2D;
import java.rmi.RemoteException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import visad.util.HersheyFont;

public class ShapeTest {

  // type 'java ShapeTest' to run this application
  public static void main(String args[])
         throws VisADException, RemoteException {

    final RealType x = new RealType("x");
    final RealType y = new RealType("y");
    final RealType shape = new RealType("shape");

    DisplayImpl disp = new DisplayImplJ3D("display");

    ScalarMap sx = new ScalarMap(x, Display.XAxis);
    sx.setRange(-1.0, 1.0);
    disp.addMap(sx);

    ScalarMap sy = new ScalarMap(y, Display.YAxis);
    sy.setRange(-1.0, 1.0);
    disp.addMap(sy);

    ScalarMap shape_map = new ScalarMap(shape, Display.Shape);
    disp.addMap(shape_map);

    final RealTupleType coord_type = new RealTupleType(x, y, shape);
    RealTuple coord_tuple
             new RealTuple(coord_type, new double[] {0.7, -0.3, 0});
    RealTuple coord_tuple2
             new RealTuple(coord_type, new double[] {-0.7, 0.3, 1});

    // make some shapes..
    VisADLineArray cross = new VisADLineArray();
    cross.coordinates = new float[]
      {0.2f,  0.2f, 0.0f,    -0.2f, -0.2f, 0.0f,
       0.2f, -0.2f, 0.0f,    -0.2f,  0.2f, 0.0f};
    cross.vertexCount = cross.coordinates.length / 3;

    double[] start = {0.0, 0.0, 0.0}; // text at origin
    double[] base = {0.3, 0.0, 0.0};  // text out along XAxis
    double[] up = {0.0, 0.3, 0.0};    // character up along YAxis
    boolean center = true;            // center text

    HersheyFont hf = new HersheyFont();
    VisADLineArray tom = PlotText.render_font("The quick brown fox jumped
over the lazy dogs back", hf, start, base, up, center);

    VisADGeometryArray[] shapes = new VisADGeometryArray[] {cross, tom};

    ShapeControl shape_control = (ShapeControl) shape_map.getControl();
    shape_control.setShapeSet(new Integer1DSet(2));
    shape_control.setShapes(shapes);

    // now attach to the display

    DataReferenceImpl ref_coord_tuple = new DataReferenceImpl("ref_c_t");
    ref_coord_tuple.setData(coord_tuple);

    DataReferenceImpl ref_coord_tuple2 = new DataReferenceImpl("ref_c_t2");
    ref_coord_tuple2.setData(coord_tuple2);

    ConstantMap[] yellow = new ConstantMap[] {
      new ConstantMap(1., Display.Red),
      new ConstantMap(1., Display.Green),
      new ConstantMap(0., Display.Blue),
    };
    ConstantMap[] cyan  = new ConstantMap[] {
      new ConstantMap(0., Display.Red),
      new ConstantMap(1., Display.Green),
      new ConstantMap(1., Display.Blue),
    };


    // for simple dipslay, use
    //disp.addReference(ref_coord_tuple);
    //disp.addReference(ref_coord_tuple2);

    // ...or...for interactive display, use
    disp.addReferences(new DirectManipulationRendererJ3D(),
ref_coord_tuple, cyan);
    disp.addReferences(new DirectManipulationRendererJ3D(),
ref_coord_tuple2, yellow);

    // to list out the values at the cursor
    final DisplayRenderer dr = disp.getDisplayRenderer();

    CellImpl cell = new CellImpl() {
      public void doAction() throws VisADException {
        double xcoord = dr.getDirectAxisValue(x);
        double ycoord = dr.getDirectAxisValue(y);
        System.out.println("x,y ="+xcoord+"  "+ycoord);
      }
    };
    cell.addReference(ref_coord_tuple);
    cell.addReference(ref_coord_tuple2);
    // end of list out values addition...


    JFrame frame = new JFrame("Dot Application");
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {System.exit(0);}
    });

    // create JPanel in JFrame
    JPanel panel = new JPanel();
    frame.getContentPane().add(disp.getComponent());

    // set size of JFrame and make it visible
    frame.setSize(300, 300);
    frame.setVisible(true);
  }
}


------------------------------------------------------------------------


--
Tom Whittaker (tomw@xxxxxxxxxxxxx)
University of Wisconsin-Madison
Space Science and Engineering Center
Cooperative Institute for Meteorological Satellite Studies
Phone/VoiceMail: 608.262.2759