Whats wrong with my code?
Author |
Message |
Integrate
|
Posted: Tue May 20, 2008 11:14 pm Post subject: Whats wrong with my code? |
|
|
I'm trying to put my title page for a game into its own class and have it draw out the page. I wanted my main program to just import this title page and just call its draw function. However, I'm having problems with getting it to work.
Here is my test code:
code: |
import java.applet.*;
import java.awt.*;
public class PicTest extends Applet
{
TitleMain title = new TitleMain ();
Thread animator;
public void init ()
{
}
public void paint (Graphics g)
{
title.paint(g);
}
}
|
code: |
import java.applet.*;
import java.awt.*;
public class TitleMain extends Applet
{
Image bg;
public void init ()
{
bg = getImage (getDocumentBase (), "title.jpg");
}
public void paint (Graphics g)
{
g.drawImage (bg, 0, 0, this);
}
}
|
It's giving me this error:
Exception occurred during event dispatching:
java.lang.NullPointerException
at sun.awt.windows.WGraphics.drawImage(WGraphics.java:289)
at TitleMain.paint(TitleMain.java:16)
at PicTest.paint(PicTest.java:14)
at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:106)
at java.awt.Component.dispatchEventImpl(Component.java:1865)
at java.awt.Container.dispatchEventImpl(Container.java:946)
at java.awt.Component.dispatchEvent(Component.java:1744)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
However, TitleMain can run on its own, so whats going on?
So basically, I'm trying to put one java applet into another. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Dan
|
Posted: Wed May 21, 2008 12:44 am Post subject: RE:Whats wrong with my code? |
|
|
I don't think bg is being set befor the paint method in TitleMain is run.
Since you seem to be excuting PicTest only the init method in PicTest will be ran at inintalization and then the paint method in PicTest when the screen needs to be drawn. (Witch you have calling the paint method in TitleMain).
So it will get to the paint method in TitleMain and the image bg will not have been set, giving you a NullPointerException.
I am some what at a losse as to what you are doing, having two calsses extend applet and having two init and paint methods, so it is hard to tell you the correct way to fix it, however calling title.init() from the init method in PaintTest might at least make the code run. |
Computer Science Canada
Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more! |
|
|
|
|
Integrate
|
Posted: Wed May 21, 2008 12:52 am Post subject: RE:Whats wrong with my code? |
|
|
Your right, the bg is not initializing, but I tried adding TitleMain.init() to PicTest and I still get the error.
The reason I want to do this is because I can't really think of another way to add 2 java programs together without putting the code into the same file.
For example, say I have a banner with some buttons in one applet, and I have the main program in another applet, I want to be able to use the banner applet in the main applet. So while the banner program adds the individual buttons, the main program will add the whole banner.
I'm thinking that there must be a better way to do this? (note: I'm using RTP) |
|
|
|
|
|
HellblazerX
|
Posted: Wed May 21, 2008 1:45 pm Post subject: Re: RE:Whats wrong with my code? |
|
|
Integrate @ Wed May 21, 2008 12:52 am wrote: Your right, the bg is not initializing, but I tried adding TitleMain.init() to PicTest and I still get the error.
Make sure its title.init () and not TitleMain.init (). You should be initializing the instance of TitleMain in your PicTest class, not the the TitleMain class itself. If that doesn't work, create a Constructor method for your TitleMain class and have the picture loaded up there. |
|
|
|
|
|
Integrate
|
Posted: Wed May 21, 2008 4:22 pm Post subject: RE:Whats wrong with my code? |
|
|
I'm sorry, it was title.init (), not TitleMain. It didn't work.
I have also tried putting bg = getImage (getDocumentBase (), "title.jpg"); into a constructor in TitleMain and calling it that way. I have even tried to create a url variable in TitleMain and have PicTest send a url using getDocumentBase () through TitleMain's constructor. All have failed so far with null pointers. |
|
|
|
|
|
HeavenAgain
|
Posted: Wed May 21, 2008 5:01 pm Post subject: RE:Whats wrong with my code? |
|
|
code: | g.drawImage (bg, 0, 0, this); | i dont think is this but rather null instead |
|
|
|
|
|
Integrate
|
Posted: Wed May 21, 2008 5:41 pm Post subject: RE:Whats wrong with my code? |
|
|
I have tried that too, doesn't work .
It seems the error resides in this line:
bg = getImage (getDocumentBase (), "title.jpg");
I have tried placing that into constructors and also replacing getDocumentBase with a URL variable which recieves the URL from PicTest. None of those work.
Does this code work with anybody else, or has anybody done something similar to this and have some code that I can look at? It's driving me crazy... |
|
|
|
|
|
HeavenAgain
|
Posted: Wed May 21, 2008 5:51 pm Post subject: RE:Whats wrong with my code? |
|
|
well, i dont really understand why you need to use 2 class for this, when simply your TitleMain is doing the whole thing
but anyways, start off with getImage, i've never used this........ and by the looks of the getDocumentBase method, it does not return what you are expecting, at least i dont think it does, try getCodeBase if that doesnt work.... you should look into ImageIO, which i know for sure it works |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Integrate
|
Posted: Wed May 21, 2008 10:39 pm Post subject: RE:Whats wrong with my code? |
|
|
I think I got it, I used a Toolkit instead of getDocumentBase or getCodeBase. It was able to run the program successfully.
EDIT: new problem. Now that I've gotten images to work, I want a button or other applet object to work the same way
code: |
import java.applet.*;
import java.awt.*;
public class TitleMain extends Applet
{
Button buttonTest = new Button("test");
public void init ()
{
add (buttonTest);
}
public void paint (Graphics g)
{
}
}
|
code: |
import java.applet.*;
import java.awt.*;
public class ButtonTest extends Applet
{
TitleMain title = new TitleMain ();
public void init ()
{
}
public void paint (Graphics g)
{
title.paint(g);
}
}
|
TitleMain can run by itself and all it does is display a button. I want ButtonTest to be able to draw TitleMain's button. It currently doesn't draw anything when I run it like that. Is there a way to make this work? |
|
|
|
|
|
|
|