• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

JApplets Can't Initialize

Hi!

Why can't this code initialize...

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

//      Described in Chapter 5 of
//      Understanding Object-Oriented Programming with Java
//      by Timothy A Budd
//      Published by Addison-Wesley
//
//      see ftp://ftp.cs.orst.edu/pub/budd/java/ReadMe.html

import java.awt.*;
import java.util.Vector;
import javax.swing.JFrame;

public class MultiBallApplet extends JApplet {
   //public static void main (String [ ] args) {
   public void init(){
  // MultiBallApplet world = new MultiBallApplet(Color.red);


 }

  private static final int FrameWidth = 600;
  private static final int FrameHeight = 400;
  private int counter = 0;
  private static final int BallArraySize = 10;
  private Ball [] ballArray = new Ball [BallArraySize];

  private MultiBallApplet (Color ballColor) {
      // general application initialization
    //setSize (FrameWidth, FrameHeight);
    //setTitle ("MultiBall World");
        // application specific initialization
    for (int i = 0; i < BallArraySize; i++) {
        ballArray[i] = new Ball(new Point(10, 15), 5);
      ballArray[i].setColor (ballColor);
      ballArray[i].setMotion (3.0+i, 6.0-i);
    }
  }

public void start(){
  while(true)run();
  }
  public void paint (Graphics g) {
    super.paint(g);
    for (int i = 0; i < BallArraySize; i++) {
      ballArray[i].paint(g);
    }
  }

  public void run(){
        // then move it slightly
    for (int i = 0; i < BallArraySize; i++) {
      ballArray[i].move();
      Point pos = ballArray[i].location();
      if ((pos.x < ballArray[i].radius()) || (pos.x > FrameWidth - ballArray[i].radius()))
              ballArray[i].reflectHorz();
      if ((pos.y < ballArray[i].radius()) || (pos.y > FrameHeight - ballArray[i].radius()))
                ballArray[i].reflectVert();
    }
    repaint();
    try{
      Thread.sleep(10);
    } catch(Exception e) {System.exit(0);}
  }
}
0
ZURINET
Asked:
ZURINET
  • 13
  • 10
  • 9
  • +1
2 Solutions
 
zzynxSoftware engineerCommented:
Does it help if you uncomment

  // MultiBallApplet world = new MultiBallApplet(Color.red);

?
0
 
ZURINETAuthor Commented:
You are too far...... :-)
0
 
mkandreCommented:
at first glance it seems as if the init method is being define inside the main method

take a look

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
zzynxSoftware engineerCommented:
>> at first glance it seems as if the init method is being define inside the main method
No. main() is commented out.

>> You are too far...... :-)
???

0
 
ZURINETAuthor Commented:
This will be close

public static void main (String [ ] args) {
    MultiBallApplet world = new MultiBallApplet(Color.red);    
     world.init();            // run world's init() method
      System.exit(0);
   }
0
 
zzynxSoftware engineerCommented:
A **applet** needs an init() method. That's the entry point.
no main() method is needed. That's the entry point for an **application**
0
 
zzynxSoftware engineerCommented:
0
 
ZURINETAuthor Commented:
I am trying to use the Same Code for Applications and Applets... ;-)
0
 
mkandreCommented:
I don't think putting System.exit(0) in the main like that is wise.

the main thread of execution will continue after the init method is complete and will close ur application even if it is not completed all its functions

0
 
ZURINETAuthor Commented:
I its removed....


But ??
0
 
zzynxSoftware engineerCommented:
>> I am trying to use the Same Code for Applications and Applets
Then I think you'll have to change this:
>> public class MultiBallApplet extends ***JApplet*** {
0
 
mkandreCommented:
when you say applet can't initialize what do u mean?

it won't load in browser?

ensure that your html page has the <applet></applet> tags

and that you specify the source in the open <applet>tag

eg <applet code = "package.MyClass"> </applet>

0
 
ZURINETAuthor Commented:
/*  when you say applet can't initialize what do u mean? */

The Applet shows Start: applet not initialized..

on the poped op new window..
0
 
mkandreCommented:
additionally, applets automatically call the init(), start() then paint(Graphics) methods in that order;

You have called the run method from the start() method; instead modify the class declaration to public class Class MultiBallApplet extends JApplet implements Runnable{

then in the start method do the following

Thread t = new Thread(this);
t.start();

that allows the gui to udate itself independently of the main thread;

also from main you should call the methods init and start, to simulate the browser effect;

0
 
mkandreCommented:
usually when applets don't initialize its because its init method is not called (or completed) or the browser can't find the compiled .class file;

ensure the code attribute of the applet tag has the full name of the class i.e. its package path plus class name without the .class

e.g <applet code = "package1.subpackage.MyClass"></applet>

0
 
ZURINETAuthor Commented:
Try did not work...
here is my code below

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.Vector;

//      Described in Chapter 5 of
//      Understanding Object-Oriented Programming with Java
//      by Timothy A Budd
//      Published by Addison-Wesley
//



public class MultiBallApplet extends JApplet implements Runnable{
  public static void main (String [ ] args) {
    MultiBallApplet world = new MultiBallApplet(Color.red);
     world.init();
            // run world's init() method

   }
  public void start(){
  Thread t = new Thread(this);
  t.start();
  while (true)run();
  }

  public static final int FrameWidth = 600;
  public static final int FrameHeight = 400;
  public int counter = 0;
  private static final int BallArraySize = 10;
  private Ball [] ballArray = new Ball [BallArraySize];



  private MultiBallApplet (Color ballColor) {
      // general application initialization
    //setSize (FrameWidth, FrameHeight);
    //setTitle ("MultiBall World");
        // application specific initialization
    for (int i = 0; i < BallArraySize; i++) {
        ballArray[i] = new Ball(new Point(10, 15), 5);
      ballArray[i].setColor (ballColor);
      ballArray[i].setMotion (3.0+i, 6.0-i);
    }
  }


  public void paint (Graphics g) {
    super.paint(g);
    for (int i = 0; i < BallArraySize; i++) {
      ballArray[i].paint(g);
    }
  }
  public void run() {
        // then move it slightly
    for (int i = 0; i < BallArraySize; i++) {
      ballArray[i].move();
      Point pos = ballArray[i].location();
      if ((pos.x < ballArray[i].radius()) || (pos.x > FrameWidth - ballArray[i].radius()))
              ballArray[i].reflectHorz();
      if ((pos.y < ballArray[i].radius()) || (pos.y > FrameHeight - ballArray[i].radius()))
                ballArray[i].reflectVert();
    }
    repaint();
    try{
      Thread.sleep(10);
    } catch(Exception e) {System.exit(0);}

  }

}


Note I ran the Test code below it responded... ;-)


public class MultiBallApplet extends JApplet {
  public static void main (String args[]) {
    init();   // (does not work!)
      System.exit(0);
   }
   public void init() {
      // here is where the action is
      JOptionPane.showMessageDialog(null, "TEST message");
   }
}

0
 
zzynxSoftware engineerCommented:
It's all explained at the link "How to make applets" I posted
0
 
zzynxSoftware engineerCommented:
That last one was directed @mkandre
0
 
zzynxSoftware engineerCommented:
>> Try did not work...
Where's the init() method?
0
 
zzynxSoftware engineerCommented:
>> I ran the Test code below it responded.
because it has an init() method
0
 
zzynxSoftware engineerCommented:
What do you mean with "running"?
As applet or as application?
0
 
mkandreCommented:
yea, i noticed you didn't define an init method either

so you really dont need to call it;

also remove the >> while(true) run(); statement from main

if you need the run method to loop use the while statement inside the the run method itself, otherwise just let it run once;

0
 
ZURINETAuthor Commented:
Thanks zzynx for the link...
0
 
mkandreCommented:
here's something else you can try

create a class that extends JPanel

have the class do its painting on the panel using its paintCcomponent(Graphics) method. add the panel to the applet and run it.
0
 
mkandreCommented:
I agree with zzynx, check out the link, it explains the JApplet very well.

0
 
ZURINETAuthor Commented:
Did not work out .. I am just understress....

will take time out and fix the code...
0
 
mkandreCommented:
ok, keep us informed

0
 
objectsCommented:
code looks fine except for the few things mentioned by mkandre.
Check the java console to see what error is stopping it from starting.
0
 
ZURINETAuthor Commented:
Hi! Objects

Which code are you refering to the first one or the second?
0
 
objectsCommented:
the 2nd one

you need to get rid of:
   while (true)run();
0
 
objectsCommented:
I see the problem, you need to add an empty constructor
something like:

public MultiBallApplet()
{
   this(Color.red);
}

0
 
ZURINETAuthor Commented:
Thanks you been helpful...

Were then will I call the run() method.. cause the the ball is static .. just one ball standing...:-)
0
 
objectsCommented:
The thread you start with the following line will call the run() method

t.start();

0
 
objectsCommented:
your run method only moves the balls once, you need to add a loop to repeatedly move them

 public void run() {
   while (true) {
       // then move it slightly
    for (int i = 0; i < BallArraySize; i++) {
      ballArray[i].move();
      Point pos = ballArray[i].location();
      if ((pos.x < ballArray[i].radius()) || (pos.x > FrameWidth - ballArray[i].radius()))
             ballArray[i].reflectHorz();
      if ((pos.y < ballArray[i].radius()) || (pos.y > FrameHeight - ballArray[i].radius()))
                ballArray[i].reflectVert();
    }
    repaint();
    try{
      Thread.sleep(10);
    } catch(Exception e) {System.exit(0);}
   }
  }
0
 
ZURINETAuthor Commented:
Just was wondering if I include show() at the end of world.init(); would enhance the programme ...


0
 
objectsCommented:
no it won't, did you want to run it as an application?

0
 
ZURINETAuthor Commented:
Any way thanks a lot..... I have to wake up around 4:00 am to think about this problem..
Its now 5:27 am ...

You have been so helpful.....
I guess you are in the States.....

Thanks a lot...........

For other.. Guys ... that have contributed... below is the final code....

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.Vector;





public class MultiBallApplet extends JApplet implements Runnable{
  public static void main (String [ ] args) {
    MultiBallApplet world = new MultiBallApplet(Color.red);
     world.init();

            // run world's init() method

   }
  public void start(){
   Thread t = new Thread(this);
  t.start();


  }

  public static final int FrameWidth = 600;
  public static final int FrameHeight = 400;
  public int counter = 0;
  private static final int BallArraySize = 10;
  private Ball [] ballArray = new Ball [BallArraySize];
  public MultiBallApplet()
{
   this(Color.red);
}


  private MultiBallApplet (Color ballColor) {
     // general application initialization
    //setSize (FrameWidth, FrameHeight);
    //setTitle ("MultiBall World");
        // application specific initialization
    for (int i = 0; i < BallArraySize; i++) {
        ballArray[i] = new Ball(new Point(10, 15), 5);
     ballArray[i].setColor (ballColor);
     ballArray[i].setMotion (3.0+i, 6.0-i);
    }

  }


  public void paint (Graphics g) {
    super.paint(g);
    for (int i = 0; i < BallArraySize; i++) {
      ballArray[i].paint(g);
    }
  }


    public void run() {
   while (true) {
       // then move it slightly
    for (int i = 0; i < BallArraySize; i++) {
      ballArray[i].move();
      Point pos = ballArray[i].location();
      if ((pos.x < ballArray[i].radius()) || (pos.x > FrameWidth - ballArray[i].radius()))
             ballArray[i].reflectHorz();
      if ((pos.y < ballArray[i].radius()) || (pos.y > FrameHeight - ballArray[i].radius()))
                ballArray[i].reflectVert();
    }
    repaint();
    try{
      Thread.sleep(10);
    } catch(Exception e) {System.exit(0);}
   }
   }
   }



0
 
objectsCommented:
> I guess you are in the States.....

Australia actually :)

0
 
ZURINETAuthor Commented:
Which city?.....
Will be they around june for a holiday.. in Melbour...:-)
0
 
objectsCommented:
I'm in Sydney
When you're in Melbourne if you get a chance head down to the Great Ocean Road for a drive.

0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 13
  • 10
  • 9
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now