Code to Generate Terrain Viewer UI ( Java )
/* TerrainViewerUI.java
Written February 2004 by John Bell for CS 526
This module is the the "View" portion of the MVC.
It notifies the Model ( written in C++ ) when events occur on the UI
*/
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.filechooser.*;
public class TerrainViewerUI extends JFrame implements ActionListener, ChangeListener {
int DataMin = 0, DataMax = 255, Delta8;
JButton ElevationBrowseButton, TextureBrowseButton, Quit;
JTextField RedDisplay, GreenDisplay, BlueDisplay, YellowDisplay, ScaleDisplay;
JSlider Red, Green, Blue, Yellow, Scale;
JFileChooser ElevationChooser, TextureChooser;
JTextField ElevationFileDisplay, TextureFileDisplay;
JRadioButton GreyScale, ColorMap, TextureMap;
JFrame Frame;
native void setElevationFile( String filename );
native void setTextureFile( String filename );
native void setGrayScale( );
native void setColorMap( );
native void setTextureMap( );
native void setScaling( double scaling );
native void setRed( int red );
native void setGreen( int green );
native void setBlue( int blue );
native void setYellow( int yellow );
native int getDataMin( );
native int getDataMax( );
TerrainViewerUI( String title ) {
super( title );
getContentPane().setLayout( new FlowLayout( ) );
JPanel p = new JPanel( new GridLayout( 2, 1 ) );
JPanel p2 = new JPanel( new FlowLayout( ) );
p2.add( new JLabel( "Elevation File:" ) );
ElevationFileDisplay = new JTextField( "canyon_elev.raw", 20 );
ElevationFileDisplay.addActionListener( this );
p2.add( ElevationFileDisplay );
ElevationBrowseButton = new JButton( "Browse" );
ElevationBrowseButton.addActionListener( this );
p2.add( ElevationBrowseButton );
p.add( p2 );
JPanel p3 = new JPanel( new FlowLayout( ) );
p3.add( new JLabel( "Texture File: " ) );
TextureFileDisplay = new JTextField( "canyon_rgb.jpg", 20 );
TextureFileDisplay.addActionListener( this );
p3.add( TextureFileDisplay );
TextureBrowseButton = new JButton( "Browse" );
TextureBrowseButton.addActionListener( this );
p3.add( TextureBrowseButton );
p.add( p3 );
p.setBorder( new EtchedBorder( ) );
getContentPane( ).add( p );
JPanel p4 = new JPanel( new GridLayout( 1, 2 ) );
p4.add( new JLabel( "Color Mode:" ) );
JPanel p5 = new JPanel( new GridLayout( 3, 1 ) );
ButtonGroup bg = new ButtonGroup( );
GreyScale = new JRadioButton( "Grey Scale", true );
bg.add( GreyScale );
p5.add( GreyScale );
ColorMap = new JRadioButton( "Color Map" );
bg.add( ColorMap );
p5.add( ColorMap );
TextureMap = new JRadioButton( "Texture Map" );
bg.add( TextureMap );
p5.add( TextureMap );
p4.add( p5 );
p4.setBorder( new EtchedBorder( EtchedBorder.RAISED ) );
getContentPane( ).add( p4 );
JPanel p6 = new JPanel( new FlowLayout( ) );
p6.add( new JLabel( "Height Scaling:" ) );
Scale = new JSlider( -100, 100, -10 );
Scale.addChangeListener( this );
p6.add( Scale );
ScaleDisplay = new JTextField( "-1.0", 5 );
ScaleDisplay.addActionListener( this );
p6.add( ScaleDisplay );
p6.setBorder( new EtchedBorder( EtchedBorder.LOWERED ) );
getContentPane( ).add( p6 );
DataMin = getDataMin( );
DataMax = getDataMax( );
Delta8 = ( DataMax - DataMin ) / 8;
JPanel p7 = new JPanel( new GridLayout( 4, 4 ) );
p7.setBorder( new TitledBorder( new EtchedBorder( ),
"Contours - Range = " + DataMin + " to " + DataMax ) );
JPanel p7a = new JPanel( new FlowLayout( ) );
p7a.add( new JLabel( "Red: " ) );
int r = DataMin + Delta8;
Red = new JSlider( DataMin, DataMax, r );
Red.addChangeListener( this );
p7a.add( Red );
RedDisplay = new JTextField( "" + r, 5 );
RedDisplay.addActionListener( this );
p7a.add( RedDisplay );
p7.add( p7a );
JPanel p7b = new JPanel( new FlowLayout( ) );
p7b.add( new JLabel( "Blue: " ) );
int b = DataMin + 3 * Delta8;
Blue = new JSlider( DataMin, DataMax, b );
Blue.addChangeListener( this );
p7b.add( Blue );
BlueDisplay = new JTextField( "" + b, 5 );
BlueDisplay.addActionListener( this );
p7b.add( BlueDisplay );
p7.add( p7b );
JPanel p7c = new JPanel( new FlowLayout( ) );
p7c.add( new JLabel( "Green: " ) );
int g = DataMin + 5 * Delta8;
Green = new JSlider( DataMin, DataMax, g );
Green.addChangeListener( this );
p7c.add( Green );
GreenDisplay = new JTextField( "" + g, 5 );
GreenDisplay.addActionListener( this );
p7c.add( GreenDisplay );
p7.add( p7c );
JPanel p7d = new JPanel( new FlowLayout( ) );
p7d.add( new JLabel( "Yellow:" ) );
int y = DataMin + 7 * Delta8;
Yellow = new JSlider( DataMin, DataMax, y );
Yellow.addChangeListener( this );
p7d.add( Yellow );
YellowDisplay = new JTextField( "" + y, 5 );
YellowDisplay.addActionListener( this );
p7d.add( YellowDisplay );
p7.add( p7d );
getContentPane( ).add( p7 );
addWindowListener( new windowCloser( ) );
return;
}
public void actionPerformed( ActionEvent e ) {
Object o = e.getSource( );
// Note: A switch does not work here:
if( o == ElevationBrowseButton ) {
//Create a file chooser
ElevationChooser = new JFileChooser();
ElevationChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
ElevationChooser.addActionListener( this );
ElevationChooser.setFileFilter( new RawFilter( ) );
Frame = new JFrame( );
Frame.getContentPane( ).add( ElevationChooser );
Frame.setSize( 350, 350 );
Frame.show( );
} else if ( o == TextureBrowseButton ) {
//Create a file chooser
TextureChooser = new JFileChooser();
TextureChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
TextureChooser.addActionListener( this );
TextureChooser.setFileFilter( new ImageFilter( ) );
Frame = new JFrame( );
Frame.getContentPane( ).add( TextureChooser );
Frame.setSize( 300, 300 );
Frame.show( );
} else if ( o == TextureFileDisplay )
setTextureFile( TextureFileDisplay.getText( ) );
else if ( o == ElevationFileDisplay )
setElevationFile( ElevationFileDisplay.getText( ) );
else if( o == ElevationChooser ) {
if( e.getActionCommand( ) == "CancelSelection" )
Frame.dispose( );
else if ( e.getActionCommand( ) == "ApproveSelection" ) {
ElevationFileDisplay.setText( ElevationChooser.getSelectedFile( ).getName( ) );
setElevationFile( ElevationFileDisplay.getText( ) );
Frame.dispose( );
}
} else if( o == TextureChooser ) {
if( e.getActionCommand( ) == "CancelSelection" )
Frame.dispose( );
else if ( e.getActionCommand( ) == "ApproveSelection" ) {
TextureFileDisplay.setText( TextureChooser.getSelectedFile( ).getName( ) );
setTextureFile( TextureFileDisplay.getText( ) );
Frame.dispose( );
}
} else if ( o == RedDisplay ) {
int r = Integer.parseInt( RedDisplay.getText( ) );
if( r >= Red.getMinimum( ) && r <= Red.getMaximum( ) ) {
Red.setValue( r );
setRed( r );
} else
RedDisplay.setText( "" + Red.getValue( ) );
} else if ( o == GreenDisplay ) {
int g = Integer.parseInt( GreenDisplay.getText( ) );
if( g >= Green.getMinimum( ) && g <= Green.getMaximum( ) ) {
Green.setValue( g );
setGreen( g );
} else
GreenDisplay.setText( "" + Green.getValue( ) );
} else if ( o == BlueDisplay ) {
int b = Integer.parseInt( BlueDisplay.getText( ) );
if( b >= Blue.getMinimum( ) && b <= Blue.getMaximum( ) ) {
Blue.setValue( b );
setBlue( b );
} else
BlueDisplay.setText( "" + Blue.getValue( ) );
} else if ( o == YellowDisplay ) {
int y = Integer.parseInt( YellowDisplay.getText( ) );
if( y >= Yellow.getMinimum( ) && y <= Yellow.getMaximum( ) ) {
Yellow.setValue( y );
setYellow( y );
} else
YellowDisplay.setText( "" + Yellow.getValue( ) );
} else if ( o == ScaleDisplay ) {
int s = ( int ) ( 10.0 * Double.parseDouble( ScaleDisplay.getText( ) ) );
if( s >= Scale.getMinimum( ) && s <= Scale.getMaximum( ) ) {
Scale.setValue( s );
setScaling( s / 10.0 );
} else
ScaleDisplay.setText( "" + Scale.getValue( ) / 10.0 );
}
else {
System.out.println( e.getActionCommand( ) );
}
return;
}
public void stateChanged( ChangeEvent e ) {
JSlider j = ( JSlider ) e.getSource( );
if( j == Scale ) {
ScaleDisplay.setText( "" + j.getValue( ) / 10.0 );
setScaling( j.getValue( ) / 10.0 );
} else if( j == Red ) {
RedDisplay.setText( "" + j.getValue( ) );
setRed( j.getValue( ) );
} else if( j == Green ) {
GreenDisplay.setText( "" + j.getValue( ) );
setGreen( j.getValue( ) );
} else if( j == Blue ) {
BlueDisplay.setText( "" + j.getValue( ) );
setBlue( j.getValue( ) );
} else if( j == Yellow ) {
YellowDisplay.setText( "" + j.getValue( ) );
setYellow( j.getValue( ) );
}
return;
}
// void displayText( String s ) {
// Display.append( s );
// return;
// }
} // End of TerrainViewerUI class
class windowCloser extends WindowAdapter {
public void windowClosing( WindowEvent e ) {
System.exit( 0 );
}
} // End of windowCloser