making an applet into a applet/application hybrid

i need to make my applet be runnable as application.
therefore i did nothing more that adding a main method like


public static void main(String args[])
      {
            final Memory memory = new Memory();
            JFrame frame = new JFrame();
            frame.add(memory);
            frame.setSize(550, 550);
            frame.setResizable(false);
            
            memory.init();
            memory.start();
            
            // minimize !!
            frame.addWindowListener(new WindowAdapter()
            {
                  public void windowClosing(WindowEvent we)
                  {
                              memory.destroy();
                              System.out.println("Applikation wird geschlossen");
                              System.exit(0);
                  }
            });
            
            frame.setVisible(true);
      }

but by launching it as application i get a NullPointerException in my init() methode where i create my offscreen Buffer:


public void init()
      {
            //Definition des des OffScreen Bereiches
            offscreenImg  = createImage(getSize().width, getSize().height);
            offscreen         = (Graphics2D)offscreenImg.getGraphics();
            
            offscreen.setColor(new Color(247, 22, 20));
            offscreen.setFont(new Font("Monospaced", Font.BOLD, 18));
            
            showStartMenu();

      }

right at offscreenImg = createImage(getSize().width, getSize().height);

first i thought it would be cause an application doesnt seek for the dimension of the applet in the html so i have to set it with setSize(…). but this doesnt fix the problem.

any ideas ?

IIRC you have to call setVisible( true ) first.

Until you do, Java doesn’t create screen memory etc for your app.

Until you have screen memory, AWT will bomb if you try to create an Image (which is why headless systems needed a special set of features in 1.4.x to allow them to create and process images, IIRC).

Assuming I’m not spouting BS :wink: here, I sympathise with you that this is one of those really crappy parts of AWT desgin/docs that I recall having lots of pain in early days discovering :(.

ahhh, very nice, thx. this is one of those mistakes i wouldnt have figured out in years. now i have several other errors, but i first a little try&error. perhaps i need to post them later…

ok, all problems solved, the game is running as an application. but in my quick&dirty way a new aspect comes to my mind.
ive never done a game using a real frame. my applets uses an offscreen image (like you’ve seen) and paints it active in a game loop. but now i only add the applet-canvas to the jframe. but a swing comonent already uses double-buffering. is the way of adding the applet to the frame a good (normal) one or should i do it in a diffrent way. my sorrow is that there are any problems (perhaps performance) to do active rendering in a swing component…

The problem with mixing applets and frames is the fact they are both top level containers (heavy weights). I got around that by using a JPanel to implement my own light weight applet (I should probably change that to a Canvas acutally…). You can still hijack the rendering but its not recommended.