getGraphics () Help
Author |
Message |
Johnathon
|
Posted: Thu Jan 08, 2009 9:16 pm Post subject: getGraphics () Help |
|
|
Hey again. I've been (slowly) working on my Networked LAN game, and I've hit a bit of a road bump. I'm trying to add a (very basic) GUI. Only problem is that I am unable to call my Graphics. It seems that It's forcing me to use a static method (Protected, ect lead to an error with my main being unable to access a non-static method). However, it seems that my getGraphics will not work in a static method. Any help would be wonderful... Im likely just overlooking something VERY simple.
Here's my code, since I'm not afraid of anyone stealing it, quite yet.
code: | // The "ClientTest" class.
import java.io.*;
import java.net.*;
import java.awt.*;
import java.applet.*;
public class ClientTest extends Canvas
{
static Socket socket;
static PrintWriter out;
static BufferedReader in;
static BufferedReader kbIn = new BufferedReader
(new InputStreamReader (System.in));
static String ipAddress;
public static void main (String[] args) throws IOException
{
mainMenu ();
} // main method
public static void mainMenu () throws IOException
{
//Very unstructured code; Will require reformatting when finished.
//To be included:
// - Options for selection of different games
// - Option to open chat
// - Option to open settings to set play
static Graphics layout = getGraphics ();
System.out.print ("Please enter an IP Address: ");
ipAddress = kbIn.readLine ();
ipCheck (ipAddress);
}
public static void ipCheck (String ip) throws IOException
{
try
{
socket = new Socket (ipAddress, 4444);
out = new PrintWriter (socket.getOutputStream (), true);
in = new BufferedReader (new InputStreamReader
(socket.getInputStream ()));
}
catch (UnknownHostException e)
{
System.err.println ("Could not connect to " + ipAddress + ".");
}
catch (IOException e)
{
System.err.println ("Could not connect to " + ipAddress + ".");
}
}
} // ClientTest class |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
OneOffDriveByPoster
|
Posted: Thu Jan 08, 2009 9:53 pm Post subject: Re: getGraphics () Help |
|
|
You need an instance of your class. You can create one in main(). You can then call a non-static mainMenu() on that instance. |
|
|
|
|
|
Johnathon
|
Posted: Thu Jan 08, 2009 10:03 pm Post subject: Re: getGraphics () Help |
|
|
OneOffDriveByPoster @ Thu Jan 08, 2009 9:53 pm wrote: You need an instance of your class. You can create one in main(). You can then call a non-static mainMenu() on that instance.
Im not sure if it's just your wording, but mind giving me an example...? Ive been trying to comprehend what you mean, but it's not going so well...
All the came to mind was:
code: | public static void main (String[] args) throws IOException
{
ClientTest.mainMenu ();
} // main method |
|
|
|
|
|
|
OneOffDriveByPoster
|
Posted: Thu Jan 08, 2009 10:24 pm Post subject: Re: getGraphics () Help |
|
|
Java: | (new ClientTest()).mainMenu (); |
|
|
|
|
|
|
Johnathon
|
Posted: Thu Jan 08, 2009 10:44 pm Post subject: Re: getGraphics () Help |
|
|
OneOffDriveByPoster @ Thu Jan 08, 2009 10:24 pm wrote: Java: | (new ClientTest()).mainMenu (); |
Which now leads to a nice
java.lang.NullPointerException
at ClientTest.mainMenu(ClientTest.java:37)
Java: | g.drawOval (5, 5, 10, 10);//Test Oval :3
|
at ClientTest.main(ClientTest.java:23)
Java: | new ClientTest ().mainMenu (); |
|
|
|
|
|
|
[Gandalf]
|
Posted: Fri Jan 09, 2009 2:06 am Post subject: RE:getGraphics () Help |
|
|
You'll have to show us your complete updated code, but it sounds like your method doesn't take the required Graphics or Graphics2D (or isn't calling that parameter 'g').
Edit: glancing a bit closer at your code... your Graphics object is called 'layout', not 'g'. |
|
|
|
|
|
Johnathon
|
Posted: Fri Jan 09, 2009 9:01 am Post subject: Re: RE:getGraphics () Help |
|
|
Quote: You'll have to show us your complete updated code, but it sounds like your method doesn't take the required Graphics or Graphics2D (or isn't calling that parameter 'g').
Edit: glancing a bit closer at your code... your Graphics object is called 'layout', not 'g'.
Since this is only a ... test code, I felt that some things should be changed for simplicity, and better understandings, so I did change my Graphics to g from layout.
Since you requested it, here is my new code. Ignore the drawOval, it's simply a method for me to ... debug, and figure out my barings.
Java: | // The "ClientTest" class.
import java.io.*;
import java.net.*;
import java.awt.*;
import java.applet.*;
public class ClientTest extends Canvas
{
Socket socket;
PrintWriter out;
BufferedReader in;
BufferedReader kbIn = new BufferedReader
(new InputStreamReader (System. in));
String ipAddress;
public static void main (String[] args ) throws IOException
{
new ClientTest (). mainMenu ();
} // main method
public void mainMenu () throws IOException
{
//Very unstructured code; Will require reformatting when finished.
//To be included:
// - Options for selection of different games
// - Option to open chat
// - Option to open settings to set play
Graphics g = getGraphics ();
g. fillOval (5, 5, 25, 25);
System. out. print ("Please enter an IP Address: ");
ipAddress = kbIn. readLine ();
if (ipAddress. equals (""))
ipAddress = "[No Address Specified]";
ipCheck (ipAddress );
}
public void ipCheck (String ip ) throws IOException
{
try
{
socket = new Socket (ipAddress, 4444);
out = new PrintWriter (socket. getOutputStream (), true);
in = new BufferedReader (new InputStreamReader
(socket. getInputStream ()));
}
catch (UnknownHostException e )
{
System. err. println ("Could not connect to " + ipAddress + ".");
}
catch (IOException e )
{
System. err. println ("Could not connect to " + ipAddress + ".");
}
}
} // ClientTest class
|
|
|
|
|
|
|
[Gandalf]
|
Posted: Sat Jan 10, 2009 3:00 am Post subject: RE:getGraphics () Help |
|
|
There's a problem with your getGraphics() call, it's returning null. Let's see what the Java Documentation has to say about that...
Quote: Creates a graphics context for this component. This method will return null if this component is currently not displayable.
May I suggest redesigning your game to use Swing, and model it around a JFrame? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|