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
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:
paintComponent(...) method. This will allow your code to take advantage of Swing's double buffering.update(...) in a Swing GUI. This is done for AWT but should be avoided for Swing.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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With