?
Solved

JApplets Can't Initialize

Posted on 2005-03-17
40
Medium Priority
?
375 Views
Last Modified: 2013-11-23
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
Comment
Question by:ZURINET
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 13
  • 10
  • 9
  • +1
40 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 13565456
Does it help if you uncomment

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

?
0
 

Author Comment

by:ZURINET
ID: 13565501
You are too far...... :-)
0
 
LVL 1

Expert Comment

by:mkandre
ID: 13565549
at first glance it seems as if the init method is being define inside the main method

take a look

0
Industry Leaders: 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!

 
LVL 37

Expert Comment

by:zzynx
ID: 13565578
>> 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
 

Author Comment

by:ZURINET
ID: 13565618
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13565668
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13565682
0
 

Author Comment

by:ZURINET
ID: 13565745
I am trying to use the Same Code for Applications and Applets... ;-)
0
 
LVL 1

Expert Comment

by:mkandre
ID: 13565835
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
 

Author Comment

by:ZURINET
ID: 13565865
I its removed....


But ??
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13565921
>> 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
 
LVL 1

Expert Comment

by:mkandre
ID: 13565927
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
 

Author Comment

by:ZURINET
ID: 13566012
/*  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
 
LVL 1

Expert Comment

by:mkandre
ID: 13566043
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
 
LVL 1

Expert Comment

by:mkandre
ID: 13566102
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
 

Author Comment

by:ZURINET
ID: 13566146
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
 
LVL 37

Expert Comment

by:zzynx
ID: 13566148
It's all explained at the link "How to make applets" I posted
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13566193
That last one was directed @mkandre
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13566206
>> Try did not work...
Where's the init() method?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13566221
>> I ran the Test code below it responded.
because it has an init() method
0
 
LVL 37

Expert Comment

by:zzynx
ID: 13566230
What do you mean with "running"?
As applet or as application?
0
 
LVL 1

Assisted Solution

by:mkandre
mkandre earned 400 total points
ID: 13566244
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
 

Author Comment

by:ZURINET
ID: 13566261
Thanks zzynx for the link...
0
 
LVL 1

Expert Comment

by:mkandre
ID: 13566292
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
 
LVL 1

Expert Comment

by:mkandre
ID: 13566349
I agree with zzynx, check out the link, it explains the JApplet very well.

0
 

Author Comment

by:ZURINET
ID: 13566356
Did not work out .. I am just understress....

will take time out and fix the code...
0
 
LVL 1

Expert Comment

by:mkandre
ID: 13566435
ok, keep us informed

0
 
LVL 92

Expert Comment

by:objects
ID: 13569203
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
 

Author Comment

by:ZURINET
ID: 13571591
Hi! Objects

Which code are you refering to the first one or the second?
0
 
LVL 92

Expert Comment

by:objects
ID: 13571597
the 2nd one

you need to get rid of:
   while (true)run();
0
 
LVL 92

Accepted Solution

by:
objects earned 1600 total points
ID: 13571602
I see the problem, you need to add an empty constructor
something like:

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

0
 

Author Comment

by:ZURINET
ID: 13571671
Thanks you been helpful...

Were then will I call the run() method.. cause the the ball is static .. just one ball standing...:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 13571686
The thread you start with the following line will call the run() method

t.start();

0
 
LVL 92

Expert Comment

by:objects
ID: 13571697
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
 

Author Comment

by:ZURINET
ID: 13571728
Just was wondering if I include show() at the end of world.init(); would enhance the programme ...


0
 
LVL 92

Expert Comment

by:objects
ID: 13571737
no it won't, did you want to run it as an application?

0
 

Author Comment

by:ZURINET
ID: 13571738
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
 
LVL 92

Expert Comment

by:objects
ID: 13571741
> I guess you are in the States.....

Australia actually :)

0
 

Author Comment

by:ZURINET
ID: 13571747
Which city?.....
Will be they around june for a holiday.. in Melbour...:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 13571769
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

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question