0

Hey guys I was trying to run a Applet from netbeans 6.9.1
My code has no error when i compile, but when i run the file I received an error which says "start applet not initialized".

what does that mean? thanks

There are 4 classes in my code, which is "Ball", "BallWorld", "Direction" and "Position". Class "Direction" and "Position" has no other implementation except integer variables only, the "Ball" class contains the method and attribute for the Ball object in the "BallWorld" class. The Applet i run is on "BallWorld" class.Below is my code:

//Ball class

import java.awt.*;

public class Ball{
    private Color c;
    private int s;    //size
   // private int x, y; //Position
   // private int dx, dy; //Direction
    Direction d;
    BallWorld bw;
    Position p;
    public Ball(Color col, int posx, int posy, int size, int movx, int movy)
    {
        c = col;
        p.x = posx;
        p.y = posy;
        s = size;
        d.dx = movx;
        d.dy = movy;
    }

    public void move()
    {
       // x+= dx;  //Update x, y
       // y+= dy;

        bw.Bounce(p, s, d);
        // if(x < BallWorld.left)//Hit the left boundary
        // {
            // x = BallWorld.left;
            // dx = -dx;
        // }
        //complete the code including checks for other boundaries
        // if(x > BallWorld.right - s)
        // {
            // x = BallWorld.right - s;
            // dx = -dx;
        // }

        // if(y > BallWorld.bottom - s)
        // {
            // y = BallWorld.bottom - s;
            // dy = -dy;
        // }

        // if(y < BallWorld.top)
        // {
            // y = BallWorld.top;
            // dy = -dy;
        // }


    }

    public void draw(Graphics g)
    {
        move();
        g.setColor(c);
        g.fillOval(p.x, p.y, s, s);
    }
}


//BallWorld class
import java.awt.*;
import java.applet.*;

public class BallWorld extends Applet {
    public static int left = 50, right = 450, top = 50, bottom = 550;

    Ball b1 = new Ball(Color.BLUE, 50, 70, 10, 10, 10);
    Ball b = new Ball(Color.RED, 50, 80, 10, 11, 11);
    Ball b2 = new Ball(Color.BLUE, 55, 105, 15, 13, 13);
    public void paint(Graphics g)
    {
        g.setColor(Color.GREEN);
        g.drawRect(left, top, right - left, bottom - top);
        b.draw(g); //draw ball
        b1.draw(g);
        b2.draw(g);
        slow(100); //slow the display
        repaint();
    }

    public void Bounce(Position p, int s, Direction d){

        p.x+= d.dx;  //Update x, y
        p.y+= d.dy;
    if(p.x < BallWorld.left)//Hit the left boundary
        {
            p.x = BallWorld.left;
            d.dx = -d.dx;
        }
        // complete the code including checks for other boundaries
        if(p.x > BallWorld.right - s)
        {
            p.x = BallWorld.right - s;
            d.dx = -d.dx;
        }

        if(p.y > BallWorld.bottom - s)
        {
            p.y = BallWorld.bottom - s;
            d.dy = -d.dy;
        }

        if(p.y < top)
        {
            p.y = BallWorld.top;
            d.dy = -d.dy;
        }
    }
    public void slow(int t)
    {
        try{
            Thread.sleep(t);
        }catch(Exception e){}
    }
}


//Position class
public class Position {
    int x;
    int y;
}


//Direction class
public class Direction {
    int dx;
    int dy;
}
2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by SasseMan
1

Hi!
First off, use of JApplet is prefferable over Applet since it supports Swing component architecture. So let your BallWorld class extend JApplet instead, it is essentially the same thing.

Then you should maybe override the init() method in your BallWorld class to set up you applet. You should instansiate your variables in init() instead of where you declare them.

I noticed that you call Thread.sleep in your paint method and then call repaint. This is not how you should set up animations. Have a look at the class javax.swing.Timer which lets you define a timer that will call a method a given number of times per second or something. You can then directly call paint or repaint each time the timer calls your callback method.

I don't know exactly why you get the error. Do you have a stack trace?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.