Board index » jbuilder » Converting standalone apps to Applets

Converting standalone apps to Applets


2005-03-06 04:53:48 AM
jbuilder19
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.
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 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 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);
}
show();
}
public void paint (Graphics g) {
super.paint(g);
for (int i = 0; i < BallArraySize; i++) {
ballArray[i].paint(g);
}
}
}
 
 

Re:Converting standalone apps to Applets

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

Re:Converting standalone apps to Applets

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

{smallsort}

Re:Converting standalone apps to Applets

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" < XXXX@XXXXX.COM >wrote in message
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.

 

Re:Converting standalone apps to Applets

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

Re:Converting standalone apps to Applets

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" < XXXX@XXXXX.COM >wrote in message
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?

 

Re:Converting standalone apps to Applets

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.
www.datadevelopment.com/
NEW WHITEPAPERS
Team Development with JBuilder and Borland Enterprise Server
Securing Borland Enterprise Server
www.datadevelopment.com/papers/index.html
Please see Borland's newsgroup guidelines at
info.borland.com/newsgroups/guide.html
 

Re:Converting standalone apps to Applets

Luren,
Please post the original app from which you want help
converting to an applet.
Thanks
Bob H
"Luren" < XXXX@XXXXX.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.

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

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


}

 

Re:Converting standalone apps to Applets

OK Bob, this is the original:
// Described in Chapter 5 of
// Understanding Object-Oriented Programming with Java
// by Timothy A Budd
// Published by Addison-Wesley
//
// see ftp.cs.orst.edu/pub/budd/java/ReadMe.html
import java.awt.*;
import java.util.Vector;
import javax.swing.JFrame;
public class MultiBallWorld extends JFrame {
public static void main (String [ ] args) {
MultiBallWorld world = new MultiBallWorld(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];
private MultiBallWorld (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);}
}
}
"Bob H" < XXXX@XXXXX.COM >wrote:
Quote

Luren,

Please post the original app from which you want help
converting to an applet.

Thanks

Bob H
 

Re:Converting standalone apps to Applets

Hi Lauren,
Did you get this example to run as typed?
I could not get it to run. And to transition to Applet next
without a fix would be a mistake.
I am using JBuilderX Foundation to test it.
For me seems the problem is here:
for (int i = 0; i < BallArraySize; i++) {
Quote
ballArray[i] = new Ball(new Point(10, 15), 5);
ballArray[i].setColor (ballColor);
ballArray[i].setMotion (3.0+i, 6.0-i);
}
Bob H
XXXX@XXXXX.COM
"Lauren" < XXXX@XXXXX.COM >wrote:
Quote

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

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

public class MultiBallWorld extends JFrame {
public static void main (String [ ] args) {
MultiBallWorld world = new MultiBallWorld(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];

private MultiBallWorld (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);}
}
}



"Bob H" < XXXX@XXXXX.COM >wrote:
>
>Luren,
>
>Please post the original app from which you want help
>converting to an applet.
>
>Thanks
>
>Bob H

 

Re:Converting standalone apps to Applets

Sorry, I am using another class in my project called Ball. Here is the other file added to my project, I also have the html using JBuilder 4 :
import java.awt.*;
public class Ball{
public Ball (Point lc, int r) { //constructor for new ball
//ball centre at point loc, radius rad
loc = lc;
rad = r;
}
protected Point loc; //position in window
protected int rad; //radius of ball
protected double changeInX = 0.0; //horizontal change in ball position in one cycle
protected double changeInY = 0.0; //vertical change in ball position in one cycle
protected Color color = Color.blue; //colour of ball
//methods that set attributes of ball
public void setColor(Color newColor) {color = newColor;}
public void setMotion(double dx,double dy)
{changeInX = dx; changeInY = dy;}
//methods that access attributes of ball
public int radius() {return rad;}
public Point location() {return loc;}
//methods to reverse motion of the ball
public void reflectVert(){ changeInY = -changeInY; }
public void reflectHorz(){ changeInX = -changeInX; }
//methods to move the ball
public void moveTo(int x, int y) {loc.move(x,y);}
public void move(){loc.translate((int)changeInX, (int)changeInY);}
//method to display ball
public void paint (Graphics g) {
g.setColor(color);
g.fillRect(loc.x-rad, loc.y-rad, 2*rad, 2*rad);
}
}
"Bob H" < XXXX@XXXXX.COM >wrote:
Quote

Hi Lauren,
Did you get this example to run as typed?
I could not get it to run. And to transition to Applet next

without a fix would be a mistake.

I am using JBuilderX Foundation to test it.

For me seems the problem is here:

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


Bob H

XXXX@XXXXX.COM
 

Re:Converting standalone apps to Applets

Hi Lauren,
Thanks for the addition here.
Well, it really does run successfully with this addition.
As for the .html that JB4 made, that will come into use
with modifications later.
Now I'll study it so as to make it runnable as possible
sample applets, in "adjusting the code". This does not just
fall out, so please be patient.
Bob H
"Lauren" < XXXX@XXXXX.COM >wrote:
Quote

Sorry, I am using another class in my project called Ball. Here is the other file added to my project, I also have the html using JBuilder 4 :

import java.awt.*;

public class Ball{

public Ball (Point lc, int r) { //constructor for new ball
//ball centre at point loc, radius rad
loc = lc;
rad = r;
}

protected Point loc; //position in window
protected int rad; //radius of ball
protected double changeInX = 0.0; //horizontal change in ball position in one cycle
protected double changeInY = 0.0; //vertical change in ball position in one cycle
protected Color color = Color.blue; //colour of ball

//methods that set attributes of ball

public void setColor(Color newColor) {color = newColor;}

public void setMotion(double dx,double dy)
{changeInX = dx; changeInY = dy;}

//methods that access attributes of ball

public int radius() {return rad;}

public Point location() {return loc;}

//methods to reverse motion of the ball

public void reflectVert(){ changeInY = -changeInY; }

public void reflectHorz(){ changeInX = -changeInX; }

//methods to move the ball

public void moveTo(int x, int y) {loc.move(x,y);}

public void move(){loc.translate((int)changeInX, (int)changeInY);}

//method to display ball

public void paint (Graphics g) {
g.setColor(color);
g.fillRect(loc.x-rad, loc.y-rad, 2*rad, 2*rad);
}
}


"Bob H" < XXXX@XXXXX.COM >wrote:
>
>Hi Lauren,
>Did you get this example to run as typed?
>I could not get it to run. And to transition to Applet next
>
>without a fix would be a mistake.
>
>I am using JBuilderX Foundation to test it.
>
>For me seems the problem is here:
>
>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);
>>}
>
>
>Bob H
>
> XXXX@XXXXX.COM
 

Re:Converting standalone apps to Applets

Thanks Kev, its all dancing now.
"Kevin Dean [TeamB]" < XXXX@XXXXX.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.
www.datadevelopment.com/

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

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

Re:Converting standalone apps to Applets

Hi Lauren,
Here is the first page with the code revisions. The public
class Ball is not changed at all.
This is only one possibility for your app to be an applet.
I first studied a simple revision of an app to an applet and
that was easy. Then I advanced to one app where the data
was
such that data involve movement that had to be set before the
running the applet.
To do your app required that I morf some of my last
attempts into your program. It was a learning situation for me
too.
My advice is to approach learning Java from the simple,
then a bit less simple, over and over again.
You should build a library of good examples to reference to.
Maybe you
do not have time to do it this way. But in the end it makes
things easier while on the learning curve.
This problem was not a simple one to start with.
Bob H
//package multiballworldappletdemo;
import java.awt.*;
import javax.swing.JApplet; //Changed to JApplet
/* a .html page model. It is NOT referenced to JBuilder package
<HTML>
<HEAD>
<TITLE>MultiBallWorld Applet</TITLE>
</HEAD>
<BODY>
<body bgcolor="#0000ff">
<H1>Testing MultiBallWorld Applet</H1>
<APPLET CODE="MultiBallWorldApplet.class" WIDTH=600 HEIGHT=400>
</APPLET>
</BODY>
</HTML>
*/
//Removed the JFrame reference and added JApplet and Runnable
public class MultiBallWorldApplet extends JApplet
implements Runnable {
//Took off the Static main
/*
public static void main (String [ ] args) {
MultiBallWorld world = new MultiBallWorld(Color.red);
world.show ();
while (true) world.run();
}
*/
//Added this runner
Thread runner;
//took off the privates, why I have no idea
static final int FrameWidth = 600;
static final int FrameHeight = 400;
static final int BallArraySize = 10;
Ball [] ballArray = new Ball [BallArraySize];
public void init() // Changed this to an init ()
{ // Color Removed this reference
// general application initialization
setSize (FrameWidth, FrameHeight);
//setTitle ("MultiBall World"); // Not needed
// application specific initialization
for (int i = 0; i < BallArraySize; i++) {
ballArray[i] = new Ball(new Point(10, 15), 5);
ballArray[i].setColor (Color.red); //Direct Referenced this
ballArray[i].setMotion (3.0+i, 6.0-i);
}
start();
setVisible(true);
}
public void paint (Graphics g) {
super.paint(g);
for (int i = 0; i < BallArraySize; i++) {
ballArray[i].paint(g);
}
}
// added this to start runner
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
// added this to stop runner
public void stop() {
if (runner != null) {
runner = null;
}
}
public void run(){
// then move it slightly
while (true) {// changed this RH
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);}
}
}
}
"Bob H" < XXXX@XXXXX.COM >wrote:
Quote

Hi Lauren,
Thanks for the addition here.
Well, it really does run successfully with this addition.
As for the .html that JB4 made, that will come into use
with modifications later.

Now I'll study it so as to make it runnable as possible
sample applets, in "adjusting the code". This does not just
fall out, so please be patient.

Bob H

"Lauren" < XXXX@XXXXX.COM >wrote:
>
>Sorry, I am using another class in my project called Ball. Here is the other file added to my project, I also have the html using JBuilder 4 :
>
>import java.awt.*;
>
>public class Ball{
>
>public Ball (Point lc, int r) { //constructor for new ball
>//ball centre at point loc, radius rad
>loc = lc;
>rad = r;
>}
>
>protected Point loc; //position in window
>protected int rad; //radius of ball
>protected double changeInX = 0.0; //horizontal change in ball position in one cycle
>protected double changeInY = 0.0; //vertical change in ball position in one cycle
>protected Color color = Color.blue; //colour of ball
>
>//methods that set attributes of ball
>
>public void setColor(Color newColor) {color = newColor;}
>
>public void setMotion(double dx,double dy)
>{changeInX = dx; changeInY = dy;}
>
>//methods that access attributes of ball
>
>public int radius() {return rad;}
>
>public Point location() {return loc;}
>
>//methods to reverse motion of the ball
>
>public void reflectVert(){ changeInY = -changeInY; }
>
>public void reflectHorz(){ changeInX = -changeInX; }
>
>//methods to move the ball
>
>public void moveTo(int x, int y) {loc.move(x,y);}
>
>public void move(){loc.translate((int)changeInX, (int)changeInY);}
>
>//method to display ball
>
>public void paint (Graphics g) {
>g.setColor(color);
>g.fillRect(loc.x-rad, loc.y-rad, 2*rad, 2*rad);
>}
>}
>
>
>"Bob H" < XXXX@XXXXX.COM >wrote:
>>
>>Hi Lauren,
>>Did you get this example to run as typed?
>>I could not get it to run. And to transition to Applet next
>>
>>without a fix would be a mistake.
>>
>>I am using JBuilderX Foundation to test it.
>>
>>For me seems the problem is here:
>>
>>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);
>>>}
>>
>>
>>Bob H
>>
>> XXXX@XXXXX.COM