Jump to content
  • Advertisement
Sign in to follow this  
JCDenton

[java] Java applet frustrations..

This topic is 4373 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have turned to converting my Tetris game from a JFRame to a JApplet today but am coming across the most frustrating problems loading the required images.. In the desktop version I simply used ImageIO.read() but , alas , that is no longer possible. Now, all shapes are individual classes. (Admittedly, a poor design choice but the assignment required you to create at least 8 classes or more.. so implementing the game this way was a straight forward solution to satisfying the criteria). Each of these classes inherit from the abstract class Shape. To load their respective image classes call their inherited method loadImage(). Each has a string attribute specifying the path of the image. So it is merely a case of altering the method loadImage to use JApplet.getImage(). But, as Shape isn't derived from JApplet I added a static member JApplet jApplet that is assigned to as when the JApplet derived class is instantiated. I made sure Shape.jApplet is assigned to before attempting to load the images. So my image loading method now mainly consists of this: jApplet.getImage( new URL( "http://mywebserver/" + imgPath)); The applet is still run locally but is intended to retrieve the images off the webserver.. When the applet is run the console output looks like this: java.lang.NullPointerException at java.applet.Applet.getAppletContext(Applet.java:181) at java.applet.Applet.getImage(Applet.java:236) at Shape.loadImage(Shape.java:60) at ShapePyramid.<init>(ShapePyramid.java:13) at Tetris.initTetris(Tetris.java:87) at Tetris.<init>(Tetris.java:79) at MainGame.initGame(MainGame.java:36) at MainGame.<init>(MainGame.java:25) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstruct orAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingC onstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:494) at java.lang.Class.newInstance0(Class.java:350) at java.lang.Class.newInstance(Class.java:303) at sun.applet.AppletPanel.createApplet(AppletPanel.java:721) at sun.applet.AppletPanel.runLoader(AppletPanel.java:650) at sun.applet.AppletPanel.run(AppletPanel.java:324) at java.lang.Thread.run(Thread.java:595) It would seem jApplet is not set , hence the NPE but there is a check in place which notifies the user of this , had it been the case. Many thanx..

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by JCDenton
It would seem jApplet is not set , hence the NPE but there is a check in place which notifies the user of this , had it been the case.

Well obviously your check is wrong the. Post code so we can see exactly what you're doing, and where it's going wrong.

Share this post


Link to post
Share on other sites

if(this.jApplet == null)System.out.println("isnull japplet");


there's the check. The text is not displayed.

Here's the offending code:


try
{
jApplet.getImage( new URL( "http://mywebserver/" + imgPath.));

} catch (MalformedURLException ex)
{
ex.printStackTrace();
}


looking at the stacktrace that is not where the error but in Sun's
java.applet.Applet.getAppletContext(). I guess a missing context..

Share this post


Link to post
Share on other sites
I wouldn't do the JApplet static member route... anything like that will almost always turn into a bigger mess than you bargained for.

Instead, try the java.awt.Toolkit way! :) Code would go something like this:

this.getToolkit().getImage(your_url)

getToolkit() Documentation (note that getToolkit() is available for both JFrames and JApplets)
getImage() Documentation

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!