Multi-Class Applets

Discussion in 'Java' started by Thomas F. Shehan, III, Nov 30, 2003.

  1. 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
     
    Thomas F. Shehan, III, Nov 30, 2003
    #1
    1. Advertising

  2. <Thomas F. Shehan>; "III" <> wrote in message
    news:...
    ....
    > 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
     
    Andrew Thompson, Nov 30, 2003
    #2
    1. Advertising

  3. In article <hRcyb.33134$>, says...
    > <Thomas F. Shehan>; "III" <> wrote in message
    > news:...
    > ...
    > > 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>
     
    Thomas F. Shehan, III, Nov 30, 2003
    #3
  4. <Thomas F. Shehan>; "III" <> wrote in message
    news:...
    > In article <hRcyb.33134$>,

    says...
    > > <Thomas F. Shehan>; "III" <> wrote in

    message
    > > news:...
    > > ...
    > > > 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
     
    Andrew Thompson, Nov 30, 2003
    #4
  5. Thomas F. Shehan, III

    Jose Rubio Guest

    > 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>
    >
     
    Jose Rubio, Nov 30, 2003
    #5
  6. Thomas F. Shehan, III

    Sudsy Guest

    Thomas wrote:
    > In article <hRcyb.33134$>, 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
     
    Sudsy, Nov 30, 2003
    #6
  7. Thomas F. Shehan, III

    Sudsy Guest

    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!).
     
    Sudsy, Nov 30, 2003
    #7
  8. "Andrew Thompson" <> wrote in message
    news:RBdyb.33178$...
    ....
    > 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
     
    Andrew Thompson, Nov 30, 2003
    #8
  9. "Sudsy" <> wrote in message
    news:...
    > Thomas wrote:
    > > In article <hRcyb.33134$>,

    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
     
    Andrew Thompson, Nov 30, 2003
    #9
  10. Thomas F. Shehan, III

    Tim Tyler Guest

    Thomas F. Shehan, III <> 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/ Remove lock to reply.
     
    Tim Tyler, Nov 30, 2003
    #10
  11. Thomas F. Shehan, III

    Jose Rubio Guest

    Had a bad morning?

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

    Jose

    "Sudsy" <> wrote in message
    news:...
    > 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!).
    >
     
    Jose Rubio, Dec 1, 2003
    #11
  12. Thomas F. Shehan, III

    Sudsy Guest

    Jose Rubio wrote:
    > 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!
     
    Sudsy, Dec 1, 2003
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Grandy
    Replies:
    0
    Views:
    628
    John Grandy
    Sep 13, 2005
  2. E11
    Replies:
    1
    Views:
    4,844
    Thomas Weidenfeller
    Oct 12, 2005
  3. Dave
    Replies:
    3
    Views:
    400
    Kevin Goodsell
    Apr 19, 2004
  4. Replies:
    38
    Views:
    1,297
    Dennis Lee Bieber
    Feb 15, 2005
  5. Sanny
    Replies:
    5
    Views:
    315
    Knute Johnson
    Feb 26, 2008
Loading...

Share This Page