-
Method problem
i have a issue in my code for a adventure game with networking to allow 2 computers to communicate the positions of the players to each other. The code works if i want to transfer only a set string but when i try to transfer a variable thru the code i get a error "non static variable X cannot be referenced from static context." here is my code
Code:
import java.lang.Math;
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.lang.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.awt.image.*;
public class Server extends Applet
implements MouseMotionListener, KeyListener
{
Rectangle [] wall=new Rectangle[100];
int width, height;
int mx, my; // the mouse coordinates
Point[] points;
int N = 300;
Image img;
Image backbuffer;
Image Player1;
Graphics backg;
int X=515;
int Y=500;
String Player1image;
int rightflag=0;
int leftflag=0;
public void init()
{
wall[1]=new Rectangle (413,414,185,90);
setSize(948,750);
width = getSize().width;
height = getSize().height;
mx = width/2;
my = height/2;
points = new Point[ N ];
for ( int i = 0; i < N; ++i )
{
int x = (int)(( Math.random() - 0.5 ) * width / 1.5);
int y = (int)(( Math.random() - 0.5 ) * height / 1.5);
points[i] = new Point( x, y );
}
img = getImage(getDocumentBase(), "Pallet.gif");
backbuffer = createImage( width, height );
backg = backbuffer.getGraphics();
backg.setColor( Color.white );
addKeyListener( this );
addMouseMotionListener( this );
repaint();
}
public void mouseMoved( MouseEvent e )
{
mx = e.getX();
my = e.getY();
showStatus( "Mouse at (" + mx + "," + my + ")" );
repaint();
e.consume();
}
public void mouseDragged( MouseEvent e )
{
}
//networking code
public static void main(String args)
{
int serverPort = 3456; // server port number
double number;
java.net.ServerSocket sock = null; // original server socket
java.net.Socket clientSocket = null; // socket created by accept
java.io.PrintWriter pw = null; // socket output stream
java.io.BufferedReader br = null; // socket input stream
int loop=1;
try
{
sock = new java.net.ServerSocket(serverPort); // create socket and bind to port
System.out.println("waiting for client to connect");
clientSocket = sock.accept(); // wait for client to connect
System.out.println("client has connected");
pw = new java.io.PrintWriter(clientSocket.getOutputStream(),true);
br = new java.io.BufferedReader(
new java.io.InputStreamReader(clientSocket.getInputStream()));
//gets the string
String msg = br.readLine(); // read msg from client
do{
System.out.println("Message from the client >" + msg);
pw.println("X:"+X ); // send msg to client
}while(loop==1);
//pw.close(); // close everything
//br.close();
//clientSocket.close();
//sock.close();
}
catch (Throwable e)
{
System.out.println("Error " + e.getMessage());
e.printStackTrace();
loop=2;
}
}
public void keyPressed( KeyEvent ke ) {
int k =(ke.getKeyCode ());
//System.out.println("key number " + k );
//System.out.println("direction " + direction);
//walk left
int direction;
if (k==37)
{
Player1image="walk_left";
Player1=getImage(getDocumentBase(), Player1image+".gif");
X=X-5;
repaint();
}
//walk up
if (k==38)
{
Player1image="walk_back";
Player1=getImage(getDocumentBase(), Player1image+".gif");
Y=Y-5;
repaint();
}
if (k==39)
{
Player1image="walk_right";
Player1=getImage(getDocumentBase(), Player1image+".gif");
X=X+5;
repaint();
}
if (k==40)
{
Player1image="walk_front";
Player1=getImage(getDocumentBase(), Player1image+".gif");
Y=Y+5;
repaint();
}
backg.drawImage( img, 0, 0, this );
boundry();
}
public void keyReleased( KeyEvent e )
{
int k;
k=0;
}
public void keyTyped( KeyEvent e )
{
}
//draw the grpahics
public void update( Graphics g )
{
g.drawImage( backbuffer, 0, 0, this );
getToolkit().sync();
}
public void paint( Graphics g )
{
update( g );
}
public void boundry()
{
/*
for(int i=0;i<=1;i++)
{
if((X>wall[i].getX()) && (X< (wall[i].getX()+wall[i].getWidth())) && ( Y>wall[i].getY() )&& (Y< (wall[i].getY()+wall[i].getHeight())))
{
rightflag=1;
}
else if ( (X> (wall[i].getX()+wall[i].getWidth())) && (X< (wall[i].getX()+wall[i].getWidth()+20)) && (Y>wall[i].getY())&&(Y<(wall[i].getY()+wall[i].getHeight())) )// left boundry working
{
leftflag=1;
}
}
*/
if (rightflag==1)
{
X=X-5;
}
if (leftflag==1)
{
X=X+5;
}
backg.drawImage( Player1,X,Y,this);
}
}
the issue is on line 100 the X is to be the X coord on of the player. Do i need to add a main method, change that method that is the main right now to like a method called "networking" and have the main call that method if so how would i do that?
Thanks in advance
-
You've defined a member variable 'int X=515;' and then are trying to access it from with your main method, which is static. A static method can not access a member variable of a class. What you need to do, is pull all the code out of your 'main', and use your main to do nothing more than construct your class:
Code:
public static void main(String[] args) {
Server server = new Server();
}