I am trying to make a Java applet on Eclipse that will print an array of bars of random lengths, then sort them by length and print the new array. However, when I run my program, it says that my applet is not initialized. My code is below. Can anyone help me? Thank you so much!
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.applet.Applet;
public abstract class Proj1_1 extends Applet implements ActionListener
{
private static int[] numbers = new int[10];
public void init()
{
Button startButton = new Button("Sort");
startButton.addActionListener(this);
add(startButton);
setSize(300,300);
setVisible(true);
}
public void paint(Graphics screen)
{
numbers = Proj1_1.myRandom(numbers);
int i;
for (i = 0; i <= numbers.length - 1; i++)
{
screen.fillRect(20, 20 + 10 * i, numbers[i] + 30, 6);
}
}
public static int[] myRandom(int[] numbers)
{
Random random = new Random();
for(int i = 0; i < numbers.length; i++)
numbers[i] = random.nextInt(20);
return numbers;
}
public static int[] selectionSort (int[] numbers)
{
MySort sort = new MySort();
int[] numbers2 = sort.selectionSort(numbers);
return numbers2;
}
public void actionPerformed(ActionEvent e)
{
int[] numbers2 = Proj1_1.selectionSort(numbers);
int i;
for (i = 0; i <= numbers2.length - 1; i++)
{
Graphics screen = null;
screen.fillRect(20, 20 + 10 * i, numbers2[i] + 30, 6);
}
}
}
public class MySort {
int [] numbers;
public MySort()
{
}
public MySort(int[] numbers)
{
selectionSort(numbers);
}
public int[] selectionSort (int[] numbers)
{
for(int i=0; i<numbers.length; i++)
{
for(int j=0; j<numbers.length; j++)
{
if(numbers[i] < numbers[j])
{
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
}
return numbers;
}
}