BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Converting standalone apps to Applets

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> JBuilder Applet Issues
View previous topic :: View next topic  
Author Message
Lauren
Guest





PostPosted: Sat Mar 05, 2005 10:16 pm    Post subject: Converting standalone apps to Applets Reply with quote




Hello,

Its my first into OO with Java, and this problem had made me
nearly pull my hair out 4da whole day. I have read up the book
I am studying and the internet, cun anyone help me convert the
original main() to suit the Applets init(), start() etc.

I mean, would my constructor become the new init()? I have done it so, and it seemed to come up with a window with just the buttons(start,stop etc).

I have the other things i.e the html sorted.

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

public class MultiBallApplet extends JApplet {

public static void main (String [ ] args) {

MultiBallApplet world = new MultiBallApplet(Color.red);

world.show ();
while (true) world.run();
}



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



public void MultiBallApplet(){
// 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 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);}
}


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


}

Back to top
Arthur Ore
Guest





PostPosted: Sun Mar 06, 2005 10:40 am    Post subject: Re: Converting standalone apps to Applets Reply with quote



Hi Lauren,

You are trying to instaniate (create) a new applet passing in the colour
red.You need to create a construcor that accepts a colour

public MultiBallApplet(Color myColour)
{
// your code here
}

Next, are you aware that you code is utilising the default Java constructor
which has no code other than a call to super() in it? Constructors don't
have a return type, in your case "void". See mine above; public void
MultiBallApplet() is a valid method, but it is not a constructor.

Next main(String [] args) is the entry point into an application, whereas
init() is for applets. Hence its main rather than the constructor which
could be replaced.

However, you can keep the main(String [] args) method. Simply add the
following method to your applet.

public void init()
{
main(null);
}

This will call your existing main method

Hope this helps

Arth


"Lauren" <lauril (AT) 37 (DOT) com> wrote

Quote:

Hello,

Its my first into OO with Java, and this problem had made me
nearly pull my hair out 4da whole day. I have read up the book
I am studying and the internet, cun anyone help me convert the
original main() to suit the Applets init(), start() etc.

I mean, would my constructor become the new init()? I have done it so, and
it seemed to come up with a window with just the buttons(start,stop etc).

I have the other things i.e the html sorted.




Back to top
Lauren
Guest





PostPosted: Sun Mar 06, 2005 12:56 pm    Post subject: Re: Converting standalone apps to Applets Reply with quote




Thanks for thereply Arth, I was making changes to the file at
the time. But the init() method you explained was interesting.

Its just that I have been receiving this error every time
I compile:
"The applet MultiBallApplet does not have a public constructor MultiBallApplet()"

But I do not have MultiBallApplet() now?

Here is the code after I made th changes:
import java.applet.*;
import java.awt.*;
import java.util.Vector;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics;

public class MultiBallApplet extends JApplet {
public static void main (String [ ] args) {

MultiBallApplet world = new MultiBallApplet(Color.red);
world.show ();
while (true) world.run();
}



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];

public MultiBallApplet(Color ballColor){
// 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);
}
show();
}



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);}
}

public void init(){
main(null);
}

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

}

Back to top
Arthur Ore
Guest





PostPosted: Sun Mar 06, 2005 4:06 pm    Post subject: Re: Converting standalone apps to Applets Reply with quote

Hi Lauren,

I'm not sure what you are saying. I think you are saying it does compile
now, but you don't understand why?

You are correct, you don't have a constructor MultiBallApplet() , but from
the code you have posted you shouldn't need one.

I suspect the message said it couldn't find MultiBallApplet(java.awt.Color)
, which was true at the time.

Every Java class must have at least one constructor. If you don't specify
one, the compiler provides one for you which is effectively :-
MultiBallApplet()
{
super();
}

Once you specify a constructor (as you have now) the compiler will not add
one for you. Before you had a method with the same name as the constructor,
but with a return type of "void" making it a standard method instead of a
constructor. Hence before, you did have a constructor MultiBallApplet()
(provided by the compiler) but effectively it had no code in it. However,
your statement new MultiBallApplet(Color.red) means your code expects to
find a constructor that takes a Color as a parameter. You now have this.

If I've misunderstood and you still have an error please post the contents
and someone may be able to help.

Arth

"Lauren" <Lauriel (AT) 37 (DOT) com> wrote

Quote:

Thanks for thereply Arth, I was making changes to the file at
the time. But the init() method you explained was interesting.

Its just that I have been receiving this error every time
I compile:
"The applet MultiBallApplet does not have a public constructor
MultiBallApplet()"

But I do not have MultiBallApplet() now?




Back to top
Kevin Dean [TeamB]
Guest





PostPosted: Mon Mar 07, 2005 1:19 pm    Post subject: Re: Converting standalone apps to Applets Reply with quote

Lauren wrote:

Quote:

Thanks for thereply Arth, I was making changes to the file at
the time. But the init() method you explained was interesting.

Its just that I have been receiving this error every time
I compile:
"The applet MultiBallApplet does not have a public constructor
MultiBallApplet()"

But I do not have MultiBallApplet() now?


No. The code you have posted has a constructor
"MultiBallApplet(Color)"; you need one that has no parameters at all.
In the absence of any constructor at all, Java creates a default
constructor with no parameters but the moment you create one
constructor Java won't create one for you.

Here's how you should rewrite it...

Replace:

public MultiBallApplet(Color ballColor){
...
}

with:

public void init(){
Color ballColor = /* Construct ball color from applet parameter */

// 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);
}
}

Replace:

public static void main (String [ ] args) {

MultiBallApplet world = new MultiBallApplet(Color.red);
world.show ();
while (true) world.run();
}

with:

public void start() {
show();
while (true) {
run();
}
}

--
Kevin Dean [TeamB]
Dolphin Data Development Ltd.
http://www.datadevelopment.com/

NEW WHITEPAPERS
Team Development with JBuilder and Borland Enterprise Server
Securing Borland Enterprise Server
http://www.datadevelopment.com/papers/index.html

Please see Borland's newsgroup guidelines at
http://info.borland.com/newsgroups/guide.html

Back to top
Lauren
Guest





PostPosted: Tue Mar 08, 2005 4:17 pm    Post subject: Re: Converting standalone apps to Applets Reply with quote


Thanks Kev, its all dancing now.

"Kevin Dean [TeamB]" <NkOdSePaAnM (AT) datadevelopment (DOT) com> wrote:
Quote:
Lauren wrote:


Thanks for thereply Arth, I was making changes to the file at
the time. But the init() method you explained was interesting.

Its just that I have been receiving this error every time
I compile:
"The applet MultiBallApplet does not have a public constructor
MultiBallApplet()"

But I do not have MultiBallApplet() now?


No. The code you have posted has a constructor
"MultiBallApplet(Color)"; you need one that has no parameters at all.
In the absence of any constructor at all, Java creates a default
constructor with no parameters but the moment you create one
constructor Java won't create one for you.

Here's how you should rewrite it...

Replace:

public MultiBallApplet(Color ballColor){
...
}

with:

public void init(){
Color ballColor = /* Construct ball color from applet parameter */

// 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);
}
}

Replace:

public static void main (String [ ] args) {

MultiBallApplet world = new MultiBallApplet(Color.red);
world.show ();
while (true) world.run();
}

with:

public void start() {
show();
while (true) {
run();
}
}

--
Kevin Dean [TeamB]
Dolphin Data Development Ltd.
http://www.datadevelopment.com/

NEW WHITEPAPERS
Team Development with JBuilder and Borland Enterprise Server
Securing Borland Enterprise Server
http://www.datadevelopment.com/papers/index.html

Please see Borland's newsgroup guidelines at
http://info.borland.com/newsgroups/guide.html


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> JBuilder Applet Issues All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.