Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Multi-Class Applets

Reply
Thread Tools

Multi-Class Applets

 
 
Thomas F. Shehan, III
Guest
Posts: n/a
 
      11-30-2003
Hello,

I am having difficulty getting an applet that I am writing to run. I
have it in several different classes within the same package. I have an
html page with an applet with a code directed towards the applet class,
and the others are imported to the applet class. However, when I try to
run, it says that it can not find the applet class (in my case, named
game).

Thanks for any help.

Trey
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      11-30-2003
<Thomas F. Shehan>; "III" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) t...
....
> I am having difficulty getting an applet that I am writing to run.


I am haviing difficulty seeing it at your URL,
probably because you did not supply one.

Also, your code is not very clear, probably
because you did not supply it either (though
linking to it from your URL would be best).

You make it rather hard to help.

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site


 
Reply With Quote
 
 
 
 
Thomas F. Shehan, III
Guest
Posts: n/a
 
      11-30-2003
In article <hRcyb.33134$(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed) says...
> <Thomas F. Shehan>; "III" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) t...
> ...
> > I am having difficulty getting an applet that I am writing to run.

>
> I am haviing difficulty seeing it at your URL,
> probably because you did not supply one.
>
> Also, your code is not very clear, probably
> because you did not supply it either (though
> linking to it from your URL would be best).
>

Here is my code:

game.java:

package gg;

import java.awt.*;
import java.lang.*;
import gg.Ball;
import gg.Gorilla;

public class game extends java.applet.Applet
{
double angle=0.0;
int power=0;
int stage=0;
int turn=0;
Gorilla gorilla1,gorilla2;

public boolean mouseDown(Event evt, int x, int y)
{
if ((x>=500) && (x<=550) && (y>=25) && (y<=50))

if (stage==-9) start();
else stop();
if ((x>=275) && (x<=325) && (y>=190) && (y<=217))
{
stage=1;
repaint();
}
return true;
}

public boolean keyDown(Event evt, int key)
{
if (key==Event.UP)
power+=5;
if (key==Event.DOWN)
power-=5;
if (key==Event.LEFT)
angle-=.1;
if (key==Event.RIGHT)
angle+=.1;

if(angle>90) angle=90;
if(angle<-45) angle=-45;
if(power<0) power=0;
stage=2;
repaint();
return true;

}
public void init()
{
gorilla1 = new Gorilla();
gorilla2 = new Gorilla();
gorilla1.config(40,285,20,30,0);
gorilla2.config(560,285,20,30,1);


}
public void start()
{
stage=0;
repaint();
}

public void stop()
{
stage=-9;
repaint();
}


public Image drawTerrain()
{
Image offscreenImage;
Graphics offscg;
offscreenImage = createImage(size().width, size().height);
offscg=offscreenImage.getGraphics();
offscg.setColor(Color.red);
//int x1s[]={0,200,300,350,446,500,600,600,0,0};
//int y1s[]={300,300,150,300,250,300,300,400,400,300};
Image img = getImage(getDocumentBase(), "terrain.gif");
offscg.drawImage(img,0,0,this);
return offscreenImage;
}

public Image drawGorillas()
{
Image offscreenIm;
offscreenIm= drawTerrain();
Graphics offscg=offscreenIm.getGraphics();
offscg.setColor(Color.blue);
offscg.fillRect(30,270,20,30);
offscg.fillRect(600-(30+20),270,20,30);
return offscreenIm;

}


public void paint(Graphics g)
{
if (stage==2)
{
Image back;
Font f = new Font("TimesRoman",Font.BOLD, 16);
FontMetrics fmetrics = getFontMetrics(f);
int fontheight=fmetrics.getHeight();
back=drawGorillas();
g.drawImage(back,0,0,this);
g.setColor(Color.black);
g.setFont(f);

//Draw Angle and Power Info
g.drawString("Angle: " + angle, 30,350);
g.drawString("Power: " + power, 30, 350+fontheight);
if (turn == 0)
{
int x1,y1,x2,y2;
Double dx1 = new Double(40); //replace with gorilla1.xC
Double dy1 = new Double(285); //replace with gorilla.yC
Double dx2,dy2;
dx2 = new Double(dx1.doubleValue() + 40.0 * ( Math.cos( Math.toRadians(angle))));
dy2 = new Double(dy1.doubleValue() - 40.0 * Math.sin( Math.toRadians(angle)));
x2=dx2.intValue();
y2=dy2.intValue();
x1=dx1.intValue();
y1=dy1.intValue();
g.setColor(Color.yellow);
g.drawLine(x1,y1,x2,y2);
}
if (turn==1)
{
int x1,y1,x2,y2;
Double dx1 = new Double(560); //replace with gorilla1.xC
Double dy1 = new Double(285); //replace with gorilla.yC
Double dx2,dy2;
dx2 = new Double(dx1.doubleValue() + 40.0 * ( Math.cos( Math.toRadians(180-angle))));
dy2 = new Double(dy1.doubleValue() - 40.0 * Math.sin( Math.toRadians(180-angle)));
x2=dx2.intValue();
y2=dy2.intValue();
x1=dx1.intValue();
y1=dy1.intValue();
g.setColor(Color.yellow);
g.drawLine(x1,y1,x2,y2);
}
g.setColor(Color.black);

//Draw Quit Box
g.drawRoundRect(500,25,50,25,10,20);
String quit="Quit";
int xstart = 500+(50 - fmetrics.stringWidth(quit)) / 2;
int ystart = 20+(25 + fmetrics.getHeight()) / 2;
g.drawString(quit,xstart,ystart);
}
if (stage==0)
{
g.setColor(Color.black);
g.fillRect(0,0,600,400);
Font f = new Font("timesRoman",Font.BOLD,16);
FontMetrics fmetrics = getFontMetrics(f);
g.setFont(f);
g.setColor(Color.red);

g.drawRoundRect(275,190,50,26,10,20);
String start="Start";
int xstart = 275+(50 - fmetrics.stringWidth(start)) / 2;
int ystart = 188+(20 + fmetrics.getHeight()) / 2;
g.drawString(start,xstart,ystart);

}
if (stage==01)
{
Image back;
Font f = new Font("TimesRoman",Font.BOLD, 16);
FontMetrics fmetrics = getFontMetrics(f);
int fontheight=fmetrics.getHeight();

back=drawGorillas();
g.drawImage(back,0,0,this);
g.setColor(Color.black);
g.setFont(f);

g.drawString("Angle: " + angle, 30,350);
g.drawString("Power: " + power, 30, 350+fontheight);

g.setColor(Color.red);
g.drawRoundRect(500,25,50,25,10,20);
String quit="Quit";
int xstart = 500+(50 - fmetrics.stringWidth(quit)) / 2;
int ystart = 20+(25 + fmetrics.getHeight()) / 2;
g.drawString(quit,xstart,ystart);
}
if (stage==-9)
{
int xstart,ystart;
Font f = new Font("TimesRoman", Font.BOLD, 16);
g.setFont(f);
FontMetrics fmetrics = getFontMetrics(f);
String thanks = "Thank You For Playing";
xstart=(600-fmetrics.stringWidth(thanks))/2;
ystart=(400-fmetrics.getHeight())/2;

g.setColor(Color.black);
g.fillRect(0,0,600,400);
g.setColor(Color.red);
g.drawString(thanks,xstart,ystart);
}
}


public void update(Graphics g)
{
paint(g);
}

}


------
Ball.java
------
package gg;

public class Ball
{
//fill in later
}

------
Gorilla.java
------
package gg;
public class Gorilla
{
int x1,y1,x2,y2,player,xC,yC;
Ball myball;
int score=0;
public void config(int mxc,int myc,int wid,int hei,int pla)
{
xC=mxc;
yC=myc;
player=pla;
x1=xC-wid/2;
x2=xC+wid/2;
y1=yC-hei/2;
y2=yC+hei/2;
myball = new Ball();
}
}

------
m.htm
------

<html>

<head>

<body>
<p>
<applet width="600" height="400" code="c:\javaproj\gg\game.class">
</applet></p>

</body>

</html>
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      11-30-2003
<Thomas F. Shehan>; "III" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) t...
> In article <hRcyb.33134$(E-Mail Removed)>,

(E-Mail Removed) says...
> > <Thomas F. Shehan>; "III" <(E-Mail Removed)> wrote in

message
> > news:(E-Mail Removed) t...
> > ...
> > > I am having difficulty getting an applet that I am writing to run.

> >
> > I am haviing difficulty seeing it at your URL,
> > probably because you did not supply one.

.....
> Here is my code:
>
> game.java:


I was about to comment on not including an URL
when I noticed this right at the bottom.

> ------
> m.htm
> ------
>
> <html>
>
> <head>
>
> <body>
> <p>
> <applet width="600" height="400" code="c:\javaproj\gg\game.class">
> </applet></p>
>
> </body>
>
> </html>


OK. This is seriously damaged HTML.
There is no close tag for the head, no doctype,
no charset.

What a browser does with this HTML would
be akin to guesswork.

I suggest you throw it at the HTML validator 1st.
http://validator.w3.org/

2nd, to your problem. I have never had to
specify absolute paths the way you do. Vis.
'c:\javaproj\gg\game.class'

I would suggest putting the class files and HTML
in the same directory and using
'code="game.class"'

HTH

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site


 
Reply With Quote
 
Jose Rubio
Guest
Posts: n/a
 
      11-30-2003
> Here is my code:
>
> game.java:
>
> package gg;
>
> import java.awt.*;
> import java.lang.*;
> import gg.Ball;
> import gg.Gorilla;
>
> public class game extends java.applet.Applet


You need to import the Applet class

> {
> double angle=0.0;
> int power=0;
> int stage=0;
> int turn=0;
> Gorilla gorilla1,gorilla2;
>
> public boolean mouseDown(Event evt, int x, int y)
> {
> if ((x>=500) && (x<=550) && (y>=25) && (y<=50))
>
> if (stage==-9) start();
> else stop();
> if ((x>=275) && (x<=325) && (y>=190) && (y<=217))
> {
> stage=1;
> repaint();
> }
> return true;
> }
>
> public boolean keyDown(Event evt, int key)
> {
> if (key==Event.UP)
> power+=5;
> if (key==Event.DOWN)
> power-=5;
> if (key==Event.LEFT)
> angle-=.1;
> if (key==Event.RIGHT)
> angle+=.1;
>
> if(angle>90) angle=90;
> if(angle<-45) angle=-45;
> if(power<0) power=0;
> stage=2;
> repaint();
> return true;
>
> }
> public void init()
> {
> gorilla1 = new Gorilla();
> gorilla2 = new Gorilla();
> gorilla1.config(40,285,20,30,0);
> gorilla2.config(560,285,20,30,1);
>
>
> }
> public void start()
> {
> stage=0;
> repaint();
> }
>
> public void stop()
> {
> stage=-9;
> repaint();
> }
>
>
> public Image drawTerrain()
> {
> Image offscreenImage;
> Graphics offscg;
> offscreenImage = createImage(size().width, size().height);
> offscg=offscreenImage.getGraphics();
> offscg.setColor(Color.red);
> //int x1s[]={0,200,300,350,446,500,600,600,0,0};
> //int y1s[]={300,300,150,300,250,300,300,400,400,300};
> Image img = getImage(getDocumentBase(), "terrain.gif");
> offscg.drawImage(img,0,0,this);
> return offscreenImage;
> }
>
> public Image drawGorillas()
> {
> Image offscreenIm;
> offscreenIm= drawTerrain();
> Graphics offscg=offscreenIm.getGraphics();
> offscg.setColor(Color.blue);
> offscg.fillRect(30,270,20,30);
> offscg.fillRect(600-(30+20),270,20,30);
> return offscreenIm;
>
> }
>
>
> public void paint(Graphics g)
> {
> if (stage==2)
> {
> Image back;
> Font f = new Font("TimesRoman",Font.BOLD, 16);
> FontMetrics fmetrics = getFontMetrics(f);
> int fontheight=fmetrics.getHeight();
> back=drawGorillas();
> g.drawImage(back,0,0,this);
> g.setColor(Color.black);
> g.setFont(f);
>
> //Draw Angle and Power Info
> g.drawString("Angle: " + angle, 30,350);
> g.drawString("Power: " + power, 30, 350+fontheight);
> if (turn == 0)
> {
> int x1,y1,x2,y2;
> Double dx1 = new Double(40); //replace

with gorilla1.xC
> Double dy1 = new Double(285); //replace

with gorilla.yC
> Double dx2,dy2;
> dx2 = new Double(dx1.doubleValue() + 40.0 * ( Math.cos(

Math.toRadians(angle))));
> dy2 = new Double(dy1.doubleValue() - 40.0 * Math.sin(

Math.toRadians(angle)));
> x2=dx2.intValue();
> y2=dy2.intValue();
> x1=dx1.intValue();
> y1=dy1.intValue();
> g.setColor(Color.yellow);
> g.drawLine(x1,y1,x2,y2);
> }
> if (turn==1)
> {
> int x1,y1,x2,y2;
> Double dx1 = new Double(560); //replace

with gorilla1.xC
> Double dy1 = new Double(285); //replace

with gorilla.yC
> Double dx2,dy2;
> dx2 = new Double(dx1.doubleValue() + 40.0 * ( Math.cos(

Math.toRadians(180-angle))));
> dy2 = new Double(dy1.doubleValue() - 40.0 * Math.sin(

Math.toRadians(180-angle)));
> x2=dx2.intValue();
> y2=dy2.intValue();
> x1=dx1.intValue();
> y1=dy1.intValue();
> g.setColor(Color.yellow);
> g.drawLine(x1,y1,x2,y2);
> }
> g.setColor(Color.black);
>
> //Draw Quit Box
> g.drawRoundRect(500,25,50,25,10,20);
> String quit="Quit";
> int xstart = 500+(50 - fmetrics.stringWidth(quit)) / 2;
> int ystart = 20+(25 + fmetrics.getHeight()) / 2;
> g.drawString(quit,xstart,ystart);
> }
> if (stage==0)
> {
> g.setColor(Color.black);
> g.fillRect(0,0,600,400);
> Font f = new Font("timesRoman",Font.BOLD,16);
> FontMetrics fmetrics = getFontMetrics(f);
> g.setFont(f);
> g.setColor(Color.red);
>
> g.drawRoundRect(275,190,50,26,10,20);
> String start="Start";
> int xstart = 275+(50 - fmetrics.stringWidth(start)) / 2;
> int ystart = 188+(20 + fmetrics.getHeight()) / 2;
> g.drawString(start,xstart,ystart);
>
> }
> if (stage==01)
> {
> Image back;
> Font f = new Font("TimesRoman",Font.BOLD, 16);
> FontMetrics fmetrics = getFontMetrics(f);
> int fontheight=fmetrics.getHeight();
>
> back=drawGorillas();
> g.drawImage(back,0,0,this);
> g.setColor(Color.black);
> g.setFont(f);
>
> g.drawString("Angle: " + angle, 30,350);
> g.drawString("Power: " + power, 30, 350+fontheight);
>
> g.setColor(Color.red);
> g.drawRoundRect(500,25,50,25,10,20);
> String quit="Quit";
> int xstart = 500+(50 - fmetrics.stringWidth(quit)) / 2;
> int ystart = 20+(25 + fmetrics.getHeight()) / 2;
> g.drawString(quit,xstart,ystart);
> }
> if (stage==-9)
> {
> int xstart,ystart;
> Font f = new Font("TimesRoman", Font.BOLD, 16);
> g.setFont(f);
> FontMetrics fmetrics = getFontMetrics(f);
> String thanks = "Thank You For Playing";
> xstart=(600-fmetrics.stringWidth(thanks))/2;
> ystart=(400-fmetrics.getHeight())/2;
>
> g.setColor(Color.black);
> g.fillRect(0,0,600,400);
> g.setColor(Color.red);
> g.drawString(thanks,xstart,ystart);
> }
> }
>
>
> public void update(Graphics g)
> {
> paint(g);
> }
>
> }
>
>
> ------
> Ball.java
> ------
> package gg;
>
> public class Ball
> {
> //fill in later
> }
>
> ------
> Gorilla.java
> ------
> package gg;
> public class Gorilla
> {
> int x1,y1,x2,y2,player,xC,yC;
> Ball myball;
> int score=0;
> public void config(int mxc,int myc,int wid,int hei,int pla)
> {
> xC=mxc;
> yC=myc;
> player=pla;
> x1=xC-wid/2;
> x2=xC+wid/2;
> y1=yC-hei/2;
> y2=yC+hei/2;
> myball = new Ball();
> }
> }
>
> ------
> m.htm
> ------
>
> <html>
>
> <head>
>
> <body>
> <p>
> <applet width="600" height="400" code="c:\javaproj\gg\game.class">
> </applet></p>
>
> </body>
>
> </html>
>



 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      11-30-2003
Thomas wrote:
> In article <hRcyb.33134$(E-Mail Removed)>, (E-Mail Removed) says...
>


Oops...looks like another person who should read the following:
<http://www.sudsy.net/technology/java-classes.html>
So many people confused about the fully qualified name of a class
versus the "path to a file".
Here's what I did (commands are in quotes):
- "cd /tmp"
- extracted your code into the appropriately named files
- "javac -d . game.java Ball.java Gorilla.java"
- modified the applet tag in m.htm to the following:
<applet width="600" height="400" code="gg.game">
- "appletviewer m.htm"

I suggest you visit the Sun site (java.sun.com) and read up on the
applet tag. It's also documented here:
<http://www.w3.org/TR/html401/struct/objects.html>

HTH

 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      11-30-2003
Jose Rubio wrote:
<snip>
>>public class game extends java.applet.Applet

>
>
> You need to import the Applet class


No, he doesn't: he's fully qualified the class. You, however, need
to stop reposting the entire text of the original post with just a
single line addition (with incorrect information to boot!).

 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      11-30-2003
"Andrew Thompson" <(E-Mail Removed)> wrote in message
news:RBdyb.33178$(E-Mail Removed)...
....
> 2nd, to your problem. I have never had to
> specify absolute paths the way you do. Vis.
> 'c:\javaproj\gg\game.class'
>


Oops! Missed the entire but about it being
a package.

What I suggested would not work.

I take it the point of this exercise is working
with packages, right? [ Otherwise I'd advise you
to take out the 'package' statement and be done
with it! ]

If you are trying to work with packages, it
is far netter to be working from the command
line or an application. In fact, I think it is
very counter-productive to attempt _anything_
the first time in an applet - they are quirky little
beasts.

HST (having said that) a Q&D way that might work..
1) drop your classes into a 'WinZip' type program
2) save the zip file
3) rename myfile.zip to myapplet.jar
4) use the archive and (possibly) codebase tag(s)..

<applet width="600" height="400"
ARCHIVE="myapplet.jar"
CODEBASE="gg"
code="game.class">
</applet>

I am not sure of this since I generally put the first
class of an applet in the 'root' of a .jar, thus my
codebase="/" and
code="myclass.class"

HTH (..this time)

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      11-30-2003
"Sudsy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thomas wrote:
> > In article <hRcyb.33134$(E-Mail Removed)>,

(E-Mail Removed) says...
> >

>
> Oops...looks like another person who should read the following:
> <http://www.sudsy.net/technology/java-classes.html>


Hey Sudsy! Get your attributions right..

You've mixed Thomas' name with my email address,
when I think you were criticising my advice.
[ ..and quite rightly so ]

I suspect the OP is specifically trying to figure
out packages. Is that right Thomas?

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site


 
Reply With Quote
 
Tim Tyler
Guest
Posts: n/a
 
      11-30-2003
Thomas F. Shehan, III <(E-Mail Removed)> wrote or quoted:

> <applet width="600" height="400" code="c:\javaproj\gg\game.class">


It is the internet you are dealing with here - not Windows.

Don't put absolute Windows paths into your applet tag.

Instead follow the advice Sudsy gave...
--
__________
|im |yler http://timtyler.org/ (E-Mail Removed) Remove lock to reply.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dotnet versus Java-Applets Eitan ASP .Net 40 08-06-2005 10:57 PM
Equivalent of applets or ActiveX controls in .NET? =?Utf-8?B?RXJpYyBEYW4=?= ASP .Net 11 01-20-2005 09:52 PM
Applets (.NET style!) Alan Seunarayan ASP .Net 3 04-20-2004 06:34 AM
Equivalent to JAVA Applets shreesh ASP .Net 1 10-18-2003 07:07 PM
Downloading files with applets Praveen M V Java 1 08-13-2003 08:56 PM



Advertisments