// ArrayTwoDimensionFill.java
// Fills a 3 row by 6 column array with consectutive integers
// Also fills a 3 row by 6 column array with element subscripts
// APPLET CODE="ArrayTwoDimensionFill" HEIGHT=200 WIDTH=800
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ArrayTwoDimensionFill extends JApplet implements ActionListener {
int a [ ] [ ]; // creates reference to the two dimensional array
// GUI Components
Container c;
JTextArea outputArea;
JButton b;
public void init( ) {
a = new int [ 3 ] [ 6 ]; // allocates the array
c = getContentPane( );
c.setLayout( new FlowLayout( ) );
outputArea = new JTextArea( 10, 60 );
c.add( outputArea );
b = new JButton( "Array, Array");
b.addActionListener( this );
c.add( b );
}
public void actionPerformed( ActionEvent e ) {
printFillOrder( );
printSubscripts( );
}
public void printFillOrder( ) {
// initialize outputArea
outputArea.setText( "" );
// print table caption
outputArea.append( "The order in which cells are filled\n" );
// print column labels
for ( int c = 0; c < a[ 0 ].length; c++ )
outputArea.append( "\tCol " + c );
// print the table
for ( int r = 0; r < a.length; r++ ) {
outputArea.append( "\nRow " + r );
for ( int c = 0; c < a[ r ].length; c++ )
outputArea.append( "\t" + ( r * a[ r ].length + c ) );
}
}
public void printSubscripts( ) {
// print table caption
outputArea.append( "\n\nThe cell subscripts in \"r, c\" format\n" );
// print column labels
for ( int c = 0; c < a[ 0 ].length; c++ )
outputArea.append( "\tCol " + c );
// print the table
for ( int r = 0; r < a.length; r++ ) {
outputArea.append( "\nRow " + r );
for ( int c = 0; c < a[ r ].length; c++ )
outputArea.append( "\t" + r + ", " + c );
}
}
}