#!/usr/bin/env jython
# VisAD Tutorial
# Copyright (C) 2000 Ugo Taddei
#
# Jython translation
# Copyright (C) 2002 Frank Gibbons.

try:
    import java
    from javax.swing import *
    from visad import *
    from visad.python.JPythonMethods import *
    from visad.java2d import DisplayImplJ2D
except:
    pass

false = 0
true = not false
#  Enhanced version of program P1_01.
#  Draw function height = 45 - 5 * time^2
#  Rename RealTypes and attach units to them.
#  Draw scales on display to show the RealTypes names.
#  Run program with java P2_01

class p2_01:
    def __init__(self):
        # Create the quantities to be displayed in x- and y-axes                
        # time and height are measured in SI seconds and SI meters, respectively
        # Use RealType(String name, Unit u,  Set set), set is null              
        time = getRealType("time")
        height = getRealType("height")
                                                                                
        # Create a FunctionType, that is the class which represents the function
        # height = f(time), that is the MathType ( time -> height )             
        # Use FunctionType(MathType domain, MathType range)                     
        func_t_h = FunctionType(time, height)                                   
                                                                                
        # These are our actual data values                                      
        # Our Data values for time are represented by the set                   
        # Create the time_set, with 5 integer values, ranging from 0 to 4.      
        # That means, that there should be 5 values for height.                 
        # Use Integer1DSet(MathType type, int length)                           
        time_set = Integer1DSet(time, 5)                                        
                                                                                
        # ...the height values                                                  
        h_vals = [[0.0, 33.75, 45.0, 33.75, 0.0]]                               
                                                                                
        # The Data class FlatField, which will hold time and height data.       
        # time data are implicitly given by the Set time_set                    
        # Create a FlatField, that is the class for the samples                 
        # Use FlatField(FunctionType type, Set domain_set)                      
        h_vals_ff = FlatField( func_t_h, time_set)                              
                                                                                
        # and put the height values above in it                                 
        h_vals_ff.setSamples( h_vals )                                          
                                                                                
        # Create a 2D display                                                   
        display = DisplayImplJ2D("display1")                                    
                                                                                
        # Get display's graphics mode control and draw scales                   
        dispGMC = display.getGraphicsModeControl()                              
        dispGMC.setScaleEnable(true)                                            
                                                                                
        # Create the ScalarMaps: quantity time is to be displayed along XAxis   
        # and height along YAxis                                                
        # Use ScalarMap(ScalarType scalar, DisplayRealType display_scalar)      
        timeMap = ScalarMap( time, Display.XAxis )                              
        heightMap = ScalarMap( height, Display.YAxis )                          
                                                                                
        # Add maps to display                                                   
        display.addMap( timeMap )                                               
        display.addMap( heightMap )                                             
                                                                                
        # Create a data reference and set the FlatField as our data             
        data_ref = DataReferenceImpl("data_ref")                                
        data_ref.setData( h_vals_ff )                                           
                                                                                
        # Add reference to display                                              
        display.addReference( data_ref )                                        
                                                                                
        # Create application window, put display into it                        
        jframe = JFrame("My second VisAD application")                          
        jframe.getContentPane().add(display.getComponent())                     
                                                                                
        # Set window size and make it visible                                   
        jframe.setSize(300, 300)                                                
        jframe.setVisible(true)                                                 


p2_01()



