TheKebab 0 Newbie Poster

I've just started learning some Java, and now I've stumbled upon what may be my biggest enemy so far, Applets.

To begin with, they are inconvenient to code and and test. You have to build them in the IDE, (Using Netbeans as of now) create an external HTML which can then open the compiled classes in a browser, then place and run it from the output directory. Attaching a debugger means more work than the Applet itself. Once you run them, more problems occur: Sometimes the browser will refuse to render them, sometimes the Java platform locks up and you have to kill the process, if there were some kind of errors in the code. One time the output was corrupted, so I had to copy all code, delete the project, create a new one and paste. Yet I am hellbent on mastering the basics of it, so here goes:

I am doing an exercise where an Applet is supposed randomly generate 15 rectangles, and make sure that the largest one is blue whilst the others are yellow. Each dimension is randomly generated, x offset, y offset, height and width. A do-while loop is in place to make sure that the rectangles do not get too close or exceed the window bounds (800x600).

Now, the problem is that the Applet refuses to run. At all. A window is created within the browser, but it just stays blank, no warnings or errors. First it would at least render the background colour defined in init(), and a test string in the paint method. After a few changes it would render the background, but nothing from the method whatsoever, even if I commented out all other code logic. Now it simply just remains blank. Are there any logic errors here I should be aware of, or have NetBeans just corrupted the output again? Thanks in advance!

(Yes I have tested identic versions of the HTML and the class declaration in other Applets, it worked fine.)

import java.awt.Graphics;
import java.awt.Color;
import java.applet.Applet;

public class Exercise9_5 extends Applet{
    public void init(){
        setBackground(Color.white);
        setForeground(Color.black);
    }

    public void paint(Graphics item){
        int largestRectangle = 0;
        int largestValue = 0;
        int[][] rectValues;
        rectValues = new int[15][4];
        for (int first = 0; first <= 15; first++){
            for (int second = 0; second <= 5; second ++){
                rectValues[first][second] = 0;
            }
        }
        for (int a = 0; a <= 15; a++){
            for (int b = 0; b <= 4; b++){
                do{
                    if (b == 0 || b == 2){
                        rectValues[a][b] = (int)(1+Math.random()*780);
                    }else{
                        rectValues[a][b] = (int)(1+Math.random()*580);
                    }
                }while (rectValues[a][0] + rectValues[a][2] > 790 || rectValues[a][1] + rectValues[a][3] > 590);
            }
            if (rectValues[a][2] * rectValues[a][3] > largestValue){
                largestValue = rectValues[a][2] * rectValues[a][3];
                largestRectangle = a;
            }
        }
        for (int i = 0; i <= 15; i++){
            if (i == largestRectangle) {
            item.setColor(Color.blue);
            }else{
                item.setColor(Color.yellow);
            }
            item.fillRect(rectValues[i][0], rectValues[i][1], rectValues[i][2], rectValues[i][3]);
        }
    }
}
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.