3

Ok so i was wondering if anyone could tell me where i am going wrong.

I have a JApplet called Game which runs fine if i run it from within eclipse using the AppletViewer and a JFrame called GUI that holds the JApplet. Here is the Code for both:

PS. i got rid of most of the code from Game to make it smaller it is now just basic.

Game.java:

package com.ion.brickdodge;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JApplet;


public class Game extends JApplet implements Runnable{

private static final long serialVersionUID = 1L;
Image man1;
Image man2;
Image cman; 
int WIDTH = 250;
int HEIGHT = 350;
int lives = 3;
int spx = WIDTH / 2;
int score = 0;

Image dbImage;
Graphics dbg;

public void init () {
    setSize  (WIDTH, HEIGHT);
    setFocusable(true);
    setBackground(Color.LIGHT_GRAY);

    man1 = getImage(getCodeBase(), "res/man1.png");
    man2 = getImage(getCodeBase(), "res/man2.png");

    cman = man1;

}

public void start() {
    Thread th = new Thread(this);
    th.start();
}

public void run() {
    Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
    while(true){
        repaint();

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    }   
}

public void paint(Graphics g) {

    g.drawImage(cman, spx, 320, this);
    g.setColor(Color.BLACK);
    g.drawString("Score: " + score, 5, 10);
    g.drawString("Lives: " + lives, 5, 25);

}

public void update(Graphics g){
    if (dbImage == null)
    {
        dbImage = createImage (this.getSize().width, this.getSize().height);
        dbg = dbImage.getGraphics ();
    }

    dbg.setColor (getBackground ());
    dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

    dbg.setColor (getForeground());
    paint (dbg);

    g.drawImage (dbImage, 0, 0, this);
}
    }

And here is GUI.java:

package com.ion.brickdodge;

import java.awt.BorderLayout;

import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class GUI {

public static void main(String args[]) {
      JFrame frame = new JFrame("test");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(1000, 1000);

      JPanel panel = new JPanel(new BorderLayout());
      frame.add(panel);

      JApplet applet = new Game();
      panel.add(applet, BorderLayout.CENTER);
      applet.init();
      frame.pack();

      frame.setVisible(true);
    }
}

The Error GUI.java throws when i run it is...

Exception in thread "main" java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Unknown Source)
at com.ion.brickdodge.Game.init(Game.java:30)
at com.ion.brickdodge.GUI.main(GUI.java:22)

Any help would be much appreciated

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Lucas_F98
  • 105
  • 1
  • 1
  • 5
  • Why are you trying to do this? why not just have your class create a JPanel, and then use it as an applet or a desktop as you desire simply by putting it into a JApplet or a JFrame as needed? – Hovercraft Full Of Eels Jun 24 '12 at 03:41
  • Your NPE is probably generated by `man1 = getImage(getCodeBase(), "res/man1.png");` – Kazekage Gaara Jun 24 '12 at 03:43
  • Are the image files in the same directory as the applet itself? – fireshadow52 Jun 24 '12 at 03:43
  • Note. An applet can be launched free-floating using [JWS](http://stackoverflow.com/tags/java-web-start/info). But you would be better off coding the game in a panel and putting it in a frame to start with, then launch it using JWS. – Andrew Thompson Jun 24 '12 at 06:42

2 Answers2

3

I'll make this an answer: I think the better solution is to gear your GUI's towards creating JPanels. Then if you wish to run the GUI as an applet you can simply create your JPanel and place it in the JApplet's contentPane. Likewise if you want to create a JFrame, then create your JPanel and place it in the JFrame's contentPane.

Other issues:

  • Consider getting your images as resources as they'll likely be held in the jar file.
  • Don't draw directly in a top-level window such as a JApplet, and don't use the paint method.
  • Instead draw in the JPanel's overridden paintComponent(...) method. This will allow your code to take advantage of Swing's double buffering.
  • Don't override update(...) in a Swing GUI. This is done for AWT but should be avoided for Swing.
  • Don't read in any images or do any other cpu-intensive actions within a painting method.
  • Check out the Swing painting tutorials to see how one should be doing this kind of coding. You can find the introductory tutorial here, and a more advanced article here
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • OK im still a bit confused The game is a Applet because it needs to run in webpages but i needed to make a standalone application for this particular game. I thought i could change it to a JApplet and just include it in a JFrame but is this not possible? – Lucas_F98 Jun 24 '12 at 03:51
  • It is possible, but awkward. You still will need to create a JPanel or JComponent with a `paintComponent(...)` override to do your drawing in. Have you gone through the Swing graphics tutorials (I would assume not given your code). Consider doing this before moving too far ahead in this project. – Hovercraft Full Of Eels Jun 24 '12 at 03:55
  • @Lucas: great, but still look at the links I've provided. You won't regret reading them, especially the second. – Hovercraft Full Of Eels Jun 24 '12 at 03:59
  • yeah i have they are good tutorials thats how i learnt to make games in java – Lucas_F98 Jun 24 '12 at 04:03
3

Note: If the question were "Why is this happening?" the answer would be that the JVM sets up an applet context and stub for the applet automatically. But that does not happen unless the applet is loaded in a web page. If you are loading it yourself, you need to implement, instantiate & attach those facilities.

It is not very hard to do so, once you have the details normally supplied by the applet element.

(OTOH the best strategy is to convert the game to a panel, put it in a frame, and launch it using JWS.)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433