Hi everyone. I just started learning Java and did a few application to familiarize myself with it. I wrote an application to output a 2D array table but i can't seem to do it in an applet. Can anyone guide me along here? Problem is when i tried to output it,the table don't show up. :'( I checked through some tutorials and found it is displayed in the Java Console but how to make it appear in the applet? Here's the original code:
import java.util.*;
public class RandomArray
{
public static void main(String[] args)
{
int size = Integer.parseInt(args[0]);
if(size < 4 || size > 7)
System.out.println("You have entered an invalid size.Please try again.");
else
{
System.out.println("A 2D Array with " + size + " rows and columns is created.");
System.out.println();
// declare the 2D array, which will hold the random numbers
int[][] randArray = new int [size][size];
int[][] randArrayRotated = new int [size][size];
//declare a ramdom variable
Random rand = new Random();
//initialise and put in random numbers into the 2D array
for (int row = 0; row < size; row++)
{ // this loop is for rows
for (int col=0; col < size; col++)
{ // this loop is for columns
// generate a new random number between 0 and 9 and put it into the 1st 2Darray
randArray[row][col] = rand.nextInt(10);
//copy data from 1st array into 2nd array with the rows changing to column and vice versa
randArrayRotated[col][row] = randArray[row][col];
// this ensures that all single-digit numbers will line up into columns
if(randArray[row][col] < 10) System.out.print(" ");
System.out.print(randArray[row][col] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("The new 2D Array with the rows becoming columns and vice versa.");
System.out.println();
for(int row = 0; row < size; row++)
{
for(int col = 0; col < size; col++)
{
// this ensures that all single-digit numbers will line up into columns
if(randArrayRotated[row][col] < 10) System.out.print(" ");
System.out.print(randArrayRotated[row][col] + " ");
}
System.out.println();
}
}
}
}
Here's the code i put i changed so as to run in applet
[COLOR=Red]import java.util.*;
import java.awt.*;
import javax.swing.*;
public class RandArrayApplet extends JApplet
{
int[][] randArray = new int [4][4];
int[][] randArrayRotated = new int [4][4];
public void init()
{
//declare a ramdom variable
Random rand = new Random();
//initialise and put in random numbers into the 2D array
for (int row = 0; row < 4; row++)
{ // this loop is for rows
for (int col=0; col < 4; col++)
{ // this loop is for columns
// generate a new random number between 0 and 9 and put it into the 1st 2Darray
randArray[row][col] = rand.nextInt(10);
//copy data from 1st array into 2nd array with the rows changing to column and vice versa
randArrayRotated[col][row] = randArray[row][col];
// this ensures that all single-digit numbers will line up into columns
if(randArray[row][col] < 10) System.out.print(" ");
System.out.print(randArray[row][col] + " ");
}
System.out.println();
}
for(int row = 0; row < 4; row++)
{
for(int col = 0; col < 4; col++)
{
// this ensures that all single-digit numbers will line up into columns
if(randArrayRotated[row][col] < 10) System.out.print(" ");
System.out.print(randArrayRotated[row][col] + " ");
}
System.out.println();
}
}
public void start()
{
}
public void paint(Graphics g)
{
// simple text displayed on applet
g.setColor(Color.white);
g.fillRect(0, 0, 200, 100);
g.setColor(Color.black);
g.drawString("Sample Applet", 20, 20);
g.setColor(Color.blue);
g.drawString("created by BlueJ", 20, 40);
g.drawString("A 2D Array with 4 rows and columns is created.", 20, 60);
g.drawString("The new 2D Array with the rows becoming columns and vice versa.", 20, 180);
g.drawString("This program is done by: Tam Weng Choon", 20, 300);
g.drawString("Student ID: H0706407", 20, 320);
}
public void destroy()
{
}
}
Appreciate if anyone can give me a little guidance here.:)