This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Game Development and the fly likes Battlefield game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Game Development
Bookmark "Battlefield game" Watch "Battlefield game" New topic
Author

Battlefield game

Bhavik Gandhi
Greenhorn

Joined: Oct 18, 2007
Posts: 1
I have made this program for Battlefield game heres the code for client implementation

import edu.rit.util.Timer;
import edu.rit.util.TimerTask;
import edu.rit.util.TimerThread;

import java.awt.Toolkit;


import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import java.io.IOException;
import java.io.PrintStream;

import java.util.Scanner;

import java.util.regex.Pattern;

import javax.swing.WindowConstants;


public class ClientImpl
implements ClientRef
{

// Hidden data members.

private String myPlayer;
private int myplayerNo;
private double myangle,X,Y;
private ServerRef myServer;
private Timer myExitTimer =
TimerThread.getDefault().createTimer
(new TimerTask()
{
public void action
(Timer theTimer)
{
System.exit (0);
}
});


public ClientImpl
(String player,
ServerRef server)

{

myPlayer = player;
myServer = server;
myplayerNo = myServer.join (this, player);
if(myplayerNo == 1)
{
BattlefieldUI ui = new BattlefieldUI(true);
}
else
{
BattlefieldUI ui = new BattlefieldUI(false);
}

}




public synchronized void aim
(double angle)
{

myServer.Aimchanged(this,myplayerNo,angle);

}

public synchronized void fire(double x, double angle)
{

myServer.fire(this,myplayerNo);

}

public synchronized void quit()
{

myServer.quit(this,myplayerNo);
System.exit(0);

}


// Exported operations.



/*
* Set the position of the given player.
*
* @param playerNo player's number.
* @param player Player's name.
* @param X X-Co-ordinate.
*/
public synchronized void setPosition
(int playerNo,
String player,
double X)
{
if (playerNo ==1)
{
BattlefieldUI.setLeftName(player);
BattlefieldUI.setLeftCannonX(X);
BattlefieldUI.setLeftCannonAngle(0);
}
else
{
BattlefieldUI.setRightName(player);
BattlefieldUI.setRightCannonX(X);
BattlefieldUI.setRightCannonAngle(180);
}
}

public synchronized void changecannonangle
(int playerNo,
double angle)
{

if (playerNo ==1)
BattlefieldUI.setLeftCannonAngle(angle);
else
BattlefieldUI.setRightCannonAngle(angle);

}




public synchronized void setcannonball
(int playerNo,
double X,
double Y)
{
if (playerNo == 1)
BattlefieldUI.setLeftCannonball(X,Y,true);
else
BattlefieldUI.setRightCannonball(X,Y,true);
}


public synchronized void hit(int playerNo)
{
if (playerNo == 1)
{

BattlefieldUI.setLeftCannonX(-1);
BattlefieldUI.setLeftGravestone(true);
BattlefieldUI.setRightCannonball(0,0,false);

}
else
{

BattlefieldUI.setRightCannonX(-1);
BattlefieldUI.setRightGravestone(true);
BattlefieldUI.setLeftCannonball(0,0,false);

}

}

public synchronized void quit
(int playerNo)
{

myExitTimer.start (2000L);

}
public synchronized void userClosedWindow()
{

myServer.quit(this,myplayerNo);
myExitTimer.start (2000L);

}

/**
* Close this client. This method is called when the server will no longer
* communicate with this client.
*/
public synchronized void close()
{
myExitTimer.start (2000L);
}

public static void main(String[] args)
{

if (args.length != 1) usage();
String name = args[0];
ClientImpl clientimpl =
new ClientImpl
(name,
new ServerRef()
{
public int join
(ClientRef client,
String player){}
public void Aimchanged
(ClientRef client,
int playerNo,
double angle){}
public void fire
(ClientRef client,
int playerNo){}
public void quit
(ClientRef client,
int playerNo){}
public void close
(ClientRef client){}
});

}

private static void usage()
{
System.err.println ("Usage:ClientImpl <player> ");
System.err.println ("<player> = your name");
System.exit (1);
}
}

////////////////////////////////////////////////////////


But when I try to run it gives me following error

ClientImpl.java:121: non-static method setLeftName(java.lang.String) cannot be referenced from a static context
BattlefieldUI.setLeftName(player);
^
ClientImpl.java:122: non-static method setLeftCannonX(double) cannot be referenced from a static context
BattlefieldUI.setLeftCannonX(X);
^
ClientImpl.java:123: non-static method setLeftCannonAngle(double) cannot be referenced from a static context
BattlefieldUI.setLeftCannonAngle(0);
^
ClientImpl.java:127: non-static method setRightName(java.lang.String) cannot be referenced from a static context
BattlefieldUI.setRightName(player);
......................

I have not included list of all the error, but there are 16 errors in all which are similar for each battlefieldUI method call.

In BattlefieldUI all methods are non-static, but I cannot figure out why I am facing above errors though all methods in client implementation are non static.

Thanks in Advance
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Well, there is a lot of code there.
But the error itself sounds like you are trying to call an non-static method from a static method.

So you probably have a static method some place in you your code (probably main) that is trying to call none static methods from its own, or another class.

Gavin
 
Don't get me started about those stupid light bulbs.
 
subject: Battlefield game