Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (524)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Static function woes  (Read 1668 times)
0 Members and 1 Guest are viewing this topic.
Offline CheekyRipley

Junior Devvie


Projects: 1



« Posted 2007-02-16 13:16:13 »

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! Shocked)

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  Embarrassed

Mark Ripley<br />Cheeky
Offline ryanm

Senior Devvie


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #1 - Posted 2007-02-16 13:50:44 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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 );
  }
}
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #2 - Posted 2007-02-16 13:51:42 »

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

Thanks  Embarrassed

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:
1  
2  
3  
4  
5  
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.  Smiley

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CheekyRipley

Junior Devvie


Projects: 1



« Reply #3 - Posted 2007-02-16 20:00:50 »

Great, many thanks!

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

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  Cheesy

Mark Ripley<br />Cheeky
Offline ryanm

Senior Devvie


Projects: 1
Exp: 15 years


Used to be bleb


« Reply #4 - Posted 2007-02-16 21:24:31 »

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.
Offline CheekyRipley

Junior Devvie


Projects: 1



« Reply #5 - Posted 2007-02-17 09:37:22 »

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  Cheesy

Thanks again  Smiley

Mark Ripley<br />Cheeky
Offline hano

Senior Newbie


Projects: 1


http://es.geocities.com/luisja80


« Reply #6 - Posted 2007-02-17 13:10:10 »

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!

Thanks to you. I didn't know my code could help you, Tongue

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (57 views)
2014-11-26 15:22:04

toopeicgaming1999 (50 views)
2014-11-26 15:20:36

toopeicgaming1999 (10 views)
2014-11-26 15:20:08

SHC (24 views)
2014-11-25 12:00:59

SHC (24 views)
2014-11-25 11:53:45

Norakomi (27 views)
2014-11-25 11:26:43

Gibbo3771 (24 views)
2014-11-24 19:59:16

trollwarrior1 (37 views)
2014-11-22 12:13:56

xFryIx (75 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!