Static function woes

Hi,

I’m in the process of converting my games from applets to applications, and I’ve become stuck. I feel like a newbie all over again! :o)

OK, so I have an app, with of course a main() entry point. main has to be declared static to be seen, but that causes me all sorts of problems with what I can do inside main()…

Here’s a code snippet:

public static void main(String args[])    
{
		gInnards = new Innards( this );

            . . . . 
}

The problem is, I’m not allowed to pass ‘this’ - or anything runtime generated - within main().

So, what’s the cleanest way of letting the Innards class find out the reference to the calling class…?

Thanks :-[

When you are in a static context, there is no “this” reference. What you want to do might look something like:


public class Outards
{
   public static void main( String[] args )
  {
    new Outards();
  }

  public Outards()
  {
    // we are now in a non-static context
    // presumably you have a gInnards field already declared in this class
    gInnards = new Innards( this );
  }
}

While there may be a calling class, there isn’t any instances of it yet. If you want one you’ll have to create one:


public static void main(String[] args)
{
  MainClass m = new MainClass();
  glInnards = new Innards(m);
}

Often the easiest thing to do in main() is simply create an instance of it’s enclosing class, then call a run() or similar method which does all the work. Or even run the whole app in the constructor if you feel like it.

Edit: Heh, too slow. :slight_smile:

Great, many thanks!

I’ve been coding in Java since 1996, but always with applets. I feel like I’m starting all over again! :o

Now I’m struggling with loading images:

    img = Toolkit.getDefaultToolkit().getImage( getClass().getResource( "/fred.jpg" ) );

…there doesn’t seem to be a media tracker to wait for images to load…? img returns with width and height as -1.

Also, what’s the minimumm framework I need to extend from - I just have “… extends Canvas”. Do I need a JFrame or JPanel?

In the meantime I’ll continue Googling :smiley:

For image loading: take a look at imageio.

As to what to extend/draw on, I always seem to end up using a JPanel and overriding the draw() method to do whatever you want, and then sticking it in a JFrame. Mixing heavyweight (AWT) with lightweight (Swing) components is discouraged.

However, I am a bit pished as I write this, so don’t take it as gospel.

edit: wrong link -> Again, pished.

It’s so frustrating when you can’t find a simple answer to something, without wading through examples wondering if this particular way is the best way to code things…

Anyway, during my googling I came across the 4K competition. Of course! There’s bound to be a tight, efficient way of doing things in an application in there somewhere I thought - and I found the answers I needed in the Billiard4K game. So thanks to Luis Javier López Arredondo for saving my sanity!

Blobbit Push is up and running now - just need to integrate jorbis and then I’ll see if this JET compiler doobry is any good :smiley:

Thanks again :slight_smile:

[quote=“CheekyRipley,post:6,topic:29347”]
Thanks to you. I didn’t know my code could help you, :stuck_out_tongue: