Multi-Class Applets

  • Thread starter Thomas F. Shehan, III
  • Start date
T

Thomas F. Shehan, III

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
 
A

Andrew Thompson

....
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.
 
T

Thomas F. Shehan, III

...

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>
 
A

Andrew Thompson

.....
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
 
J

Jose Rubio

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>
 
S

Sudsy

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
 
S

Sudsy

Jose Rubio wrote:
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!).
 
A

Andrew Thompson

....
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)
 
T

Tim Tyler

Thomas F. Shehan said:
<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...
 
J

Jose Rubio

Had a bad morning?

Sorry for wasting 7kb of your bandwidth with a bad answer. All of us arent't
perfect :)

Jose
 
S

Sudsy

Jose said:
Had a bad morning?

Sorry for wasting 7kb of your bandwidth with a bad answer. All of us arent't
perfect :)

Not at all! I was just raising a couple of points of netiquette.
Some people don't realize that their newsreaders are set to include
the original post in its entirety. A bit of judicious cutting is
all that's required.
As to the correctness of the answer, I'm not trying to dissuade
anyone from posting. It just helps if you take a couple of minutes
and actually test the posted code.
And you can learn something to boot, in this case that it's not
necessary to import the class if you fully qualify the name.
No problem, right?

Cheers!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top