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.:)

Hi everyone. I managed to get the code working!! Yeah! anyway,it wasn't easy cos i ended up playing around with the for loop and g.drawString. Took me a while during work.Lucky my boss never saw what i was doin on the pc. Haha.Anyway,would really appreciate if someone can point out to me if what i did is correct? Cos i was tinking maybe there's a better way to code it. Somehow i am not sure if i can put the for lop in the init method or juz put EVERYTHING inside the paint method.Tried both and it still works. Anyway,here's the code after i manipulated it:

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];
    //declare a ramdom variable
    Random rand = new Random();

    public void init()
    {
        //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];
            }
        }
    }

    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);
        int x;
        int y = 0;
        for(int row = 0; row < 4; row++)
        {   
            x = 0;
            y += 20;
            for(int col = 0; col < 4; col++)
            {
                g.drawString(randArray[row][col] + " ", 20 + x, 60 + y);
                g.drawString(randArrayRotated[row][col] + " ", 20 + x, 180 + y);
                x += 20;
            }
        }
    }

    public void destroy()
    {
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.