# Embedding Java Games



## jrom727 (Jan 22, 2005)

hello,

how can I insert a Java game on a website such as 'My Space' or 'Friendster'... this sites allow it's users to have an embedded video/music... but I want mine as a Java Game..

I already have the code but don't know how to embed/insert it...

any idea???


----------



## Shadow2531 (Apr 30, 2001)

You can load java applets quite a few different ways. Here are some examples.

This one is the only one of the 3 that will work in IE. For alternate content, you need to specify it both via the alt tag and as altenrnate html. IE uses the alternate html for the applet tag. Other browsers use the alt attribute.

```
<applet codebase="http://home.tbbs.net/shadow/html/embedding/" code="JavaAppletTest.class" width="200px" height="30px" alt="Error: Loading http://home.tbbs.net/shadow/html/embedding/JavaAppletTest.class failed.">
    <strong>Error:</strong> Loading <a href="http://home.tbbs.net/shadow/html/embedding/JavaAppletTest.class">JavaAppletTest.class</a> failed.
</applet>
```
This one is the W3C recommended way to load an applet. IE doesn't handle it though, but modern browsers do.

```
<object classid="java:JavaAppletTest.class" codebase="http://home.tbbs.net/shadow/html/embedding/" width="200px" height="30px">
    <strong>Error:</strong> Loading <a href="http://home.tbbs.net/shadow/html/embedding/JavaAppletTest.class">JavaAppletTest.class</a> failed.
</object>
```
Here's another method that just happens to work, but this method shouldn't really be used as it breaks the "Main data source shouldn't be a param" rule.

```
<object type="application/x-java-applet" codebase="http://home.tbbs.net/shadow/html/embedding/" width="200px" height="30px">
    <param name="code" value="JavaAppletTest.class" valuetype="ref" type="application/x-java-applet" />
    <strong>Error:</strong> Loading <a href="http://home.tbbs.net/shadow/html/embedding/JavaAppletTest.class">JavaAppletTest.class</a> failed.
</object>
```
There are a few other ways, but if you want the most compatibility, use the applet tag.

As to whether myspace will allow the code, I do not know, but I assume you would paste your code in the music textarea in your profile or in one of the other profile sections.


----------



## jrom727 (Jan 22, 2005)

Thanks a lot for replying Shadow2531,

guess what? the applet tag didn't worked but then I tried the second object tag.
i was suprised it works with IE coz you said IE doesn't handle it...

now the only thing I need to do is substitute the applet game.

I might still have some problems, coz I'm still a beginner in Web Development.
I still didn't have a chance to study javascript that much... so im still confused with a some of the things...

I will post my prob if I experience one and hope you can help me with it..

Thanks again!


----------



## jrom727 (Jan 22, 2005)

well, here it is... can you just do it for me if it is possible, Im still going nuts in terms of javascripts, im a real beginner.

it's the Knight Game, i hope it's possible... (i got it from javascriptsdotcom)

Score: 0


----------



## Shadow2531 (Apr 30, 2001)

I have no idea what you want me to do with that or what it's supposed to do, but I cleaned up the code a little bit and the grid works now.


```
<script type="text/javascript">
    document.write('<table style="cursor: pointer; cursor: hand;" border="2px" cellpadding="0" cellspacing="0">')
    var squarenum = 0;
    var curnum = 0;
    curcol = "#996633";
    for (var count1 = 0; count1 < 8; count1++ ){
        document.write('<tr>');
        if ( curcol == "#996633" ) {
            curcol = "#663300";
        } else {
            curcol = "#996633";
        }
        for (var count = 0; count < 8; count++) {
            if (curcol == "#996633") {
                curcol = "#663300";
            } else {
                curcol="#996633";
            }
            document.write('<td bgcolor="' + curcol + '" id="' + curnum +  '" name="' + squarenum + '" width="50px" height="50px" onclick="moveto(this)"></td>');
            curnum++;
            squarenum++;
        }
        document.write('</tr>')
        curnum += 20;
    }
    document.write('</table>')
    document.getElementById(0).innerHTML = '<img src="horse.gif">';
    document.getElementById(0).bgColor = "#FF99FF";
    var squares = new Array(true);
    var score = 0;
    var where;
    var oldwhere = 0;
    function moveto(square){
        where = square.id;
        if ( ( oldwhere == (where - 26 ) || oldwhere == (where - 30 ) || oldwhere == (where - 55 ) || oldwhere == (where - 57 ) || (oldwhere - 26 ) == where || (oldwhere - 30 ) == where || (oldwhere - 55 ) == where || (oldwhere - 57 ) == where) ) {
            score++;
            document.getElementById("scorey").innerHTML = score;
            document.getElementById(oldwhere).innerHTML = " ";
            document.getElementById(where).innerHTML = '<img src="horse.gif" width="48" height="48">';
            document.getElementById(where).bgColor = "#FF99FF";
            oldwhere = where;
            squares[square.name] = true;
        }
        for (var i = 0; i < 63; i++) {
            if (squares[i] != true) {
                var stuffed = true;
            }
        }
        if ( stuffed != true) {
            alert("You win in only " + score + " moves!");
            window.location.href = window.location.href;
        }
    }
</script>
<p>Score: <span id="scorey">0</span><br>
```


----------



## jrom727 (Jan 22, 2005)

Im wondering IF I can put this inside the Object tag, is it possible???


----------



## Shadow2531 (Apr 30, 2001)

Why?

Do you want the javascript to be a fallback for a java applet that doesn't load?

If so, put it between the opening and close object tags.

The question here is are you referring to Java or Javascript or both?

Javascript is what I just cleaned up and posted.

Java (as in a java applet) looks like this:


```
import java.applet.Applet;
import java.awt.*;

public class JavaAppletTest extends Applet {
    public void paint(Graphics g) {
        String code = this.getParameter("code");
        setBackground(Color.black);
        g.setColor(Color.white);
        g.drawString(code, 10, 20);
    }
}
```
You compile that into a class file using the java software development kit or the gcj compiler and embed it in a browser. Java is totally different than Javascript.


----------

