Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Build a game client in Java

Status
Not open for further replies.

tchatzi

Technical User
Dec 15, 2004
744
GR
Hello everyone,

I have my own web server (Apache/2.0.54 (Unix) mod_perl/1.999.22 Perl/v5.8.6)
I'm building a backgammon game server in Perl (almost done). It now plays from the command line only (with telnet).

So i thought it would be nice to build a client in Java.

The problem is I don't have any idea how can i do that (i didn't lay a hand on Java ever in my life before).

How should i start, should i use an applet, should i make it like the yahoo games that preinstalling themselves?
And how can i do all these things?

You will be seeing me in this forum a lot, i have a lot of question, so i need you to be patient with me.

Thanks for your time.


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Here is what i have right now.

[red]Game.java[/red]
Code:
import java.applet.*;
import java.awt.*;
import java.awt.image.*; 
import java.net.*;

public class Game extends Applet //implements Runnable 
{ 
	Image backImage;
	
	// init - method is called the first time you enter the HTML site with the applet
	public void init() 
	{
		// load file background.gif
		//backImage = getImage (getCodeBase (), "background.gif"); 
		backImage = getImage (getDocumentBase (), "background.gif"); 
	}

	// start - method is called every time you enter the HTML - site with the applet
	public void start() 
	{
	}
	
	
	
	// stop - method is called if you leave the site with the applet
	public void stop() {}

	
	
	// destroy method is called if you leave the page finally (e. g. closing browser)
	public void destroy() {}
	
	
	
	//Threads are implemented by the class Thread, the interface Runnable and the method run()
	public void run () 
	{
        }
	
	// paint - method allows you to paint into your applet. 
	//This method is called e.g. if you move your browser window or if you call repaint() */
	public void paint (Graphics g) 
	{
		 g.drawImage (backImage, 0, 0, this);
	}
	
	
	
	// Update - Method, implements double buffering 
	public void update (Graphics g)
	{
	}	

}

[red]game.html[/red]
Code:
<html>
  <head>
    <title>The BackGammon Applet</title>
  </head>
  <body>
    <p align="center">
      The BackGammon Applet.
    </p>
    <p align="center">  
      <applet code="Game.class" width="463" height="332">
        Your browser does not support Java, or Java is not enabled. Sorry!
      </applet>
    </p>
    
    <script language="JavaScript">
    // Now call the function we defined above.
    var d = new Date();  // today's date and time.
    document.write(d.toLocaleString());
    </script>
  </body>
</html>
This works great, but only if i try to access game.html from the linux where the apache is.
When i try to access the page from a winxp in my lan, i get
[red]Applet Game notinited[/red]
The javascript though is working (i get the Title of the page, and the time at the bottom left corner of the page). No applet though!

I know that the java class i made, might not be what it should. Is there something i should read first, to learn how can i make it work?


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Is your Game.class in the same directory as your game.html ?

I can run your applet via a web server OK, so it must be something to do with your web server setup.

--------------------------------------------------
Free Database Connection Pooling Software
 
Yes it is in the same directory.
But why the xp can run yahoo games and not this applet?

And why can i run it from the linux which has the apache, with
So the class i made till now is good written? Is this the correct way to do it?


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
>>> But why the xp can run yahoo games and not this applet?

No idea - never used "yahoo games" or whatever that is.

>>> And why can i run it from the linux which has the apache
No idea - all I can tell you is that if you can run it locally using a URL, and I can also run it from a web server, then it looks like its a permissions issue on your computer running XP.

>>> So the class i made till now is good written? Is this the correct way to do it?
Well there is not a lot to really dicuss - its just loading an image.
There is no point in defining your applet as extending Runnable, nor is there any point in defining a run() method.
There is also not a lot of point in defining methods that override the superclass, which do nothing.
Also, your applet does not draw the image when it loads, only on a graphics repaint.

As I said before, I'd go back to basics and learn Java properly if you want to write Java. Just cutting and pasting from some tutorial and hoping for the best is not really going to work in the long run. You'll find that you cannot debug even simple problems, and are constantly reliant on forums like this for debugging your code for you - which I'm sure you'll agree is not an ideal position to be in.



--------------------------------------------------
Free Database Connection Pooling Software
 
Yes i agree, i've been in this position.
Anyway you 'll be seeing me later on.
Thanks for our small chat.


``The wise man doesn't give the right answers,
he poses the right questions.''
TIMTOWTDI
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top