Computer Science Canada Java Import Image Help! |
Author: | Airjuggernaut [ Mon Dec 29, 2008 1:11 am ] |
Post subject: | Java Import Image Help! |
Hey everyone, Me and my friend are trying to figure out how to import images into an Applet in Java. We've tried the following. Quote: // The "Final_Game" class.
import java.applet.*; import java.awt.*; import java.awt.Image; public class Final_Game extends Applet { // Place instance variables here public void init () { Image myImage = getImage(getCodeBase(), "C:/dp.jpg"); // Place the body of the initialization method here } // init method public void paint (Graphics g) { // Place the body of the drawing method here g.drawImage(myImage, 300, 200, 60, 120, this); } // paint method } // Final_Game class The code keeps getting the following Null pointer exceptions. Quote: public void paint (Graphics g) { Image myImage = getImage(getCodeBase(), "C:/dp.jpg"); g.drawImage(myImage, 300, 200, 60, 120, this); } // paint method What are we doing wrong? Any pointers, tuts or tips? Any help would be appreciated! Thanks! |
Author: | TheHobbit [ Mon Dec 29, 2008 1:18 am ] |
Post subject: | Re: Java Import Image Help! |
Im working with juggernaut on this, and er, we've tried quite a bit of things to get imports working, but we seem to be running problems all over, this seemed to be one of the more simple and effective methods ( from the many complicated ones we found ) and we're running null pointer exceptions everywhere. We think it has something to do with the getCodeBase bit, since we're not entirely sure what it does/mean. If you guys wana point us in a direction, it'd be much appreciated If there's any other simpler methods, a leg up would be helpful ![]() |
Author: | wtd [ Mon Dec 29, 2008 2:00 am ] |
Post subject: | RE:Java Import Image Help! |
Well, to start... do you know what cause a NullPointerException? |
Author: | TheHobbit [ Mon Dec 29, 2008 3:19 am ] |
Post subject: | Re: Java Import Image Help! |
alot of things among which ( the only one that made sense ) was g.drawImage(myImage, 300, 200, 60, 120, this); that line ^ i c/p 'ed the rest in case it makes any sense whatsoever to you java.lang.NullPointerException at sun.java2d.SunGraphics2D.drawImage(Unknown Source) at sun.java2d.SunGraphics2D.drawImage(Unknown Source) at Final_Game_Ammar_Moeed.paint(Final_Game.java:22) at sun.awt.RepaintArea.paint(Unknown Source) at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) |
Author: | chili5 [ Mon Dec 29, 2008 7:44 am ] | ||
Post subject: | RE:Java Import Image Help! | ||
For one, your image variable is out of scope in your paint method. You declared it in the init method, so it doesn't exist to paint. Also my guess is that Java cannot find your image. When you specify getCodeBase() it means that java should search in the directory with the .class file in a folder called C:\dp.jpg So java is probably looking for something like: file:/C:/Documents%20and%20Settings/James/workspace/GUI/bin/C:/dp.jpg Put your dp.jpg file in the folder with your .class and have:
Yeah that should work ![]() |
Author: | TheHobbit [ Mon Dec 29, 2008 2:47 pm ] | ||||
Post subject: | Re: Java Import Image Help! | ||||
thanks for replying guys so, i changed the destination of dp.jpg to where the .class is , just like you said, and moved the myImage bit into the paint method ( i actually did that before too ) andd it compiled fine, and i got an error " Out of Memory " so i figured the pic was too big or something ( 4.2 mb hheh ) i then made a new picture, which is about 11 kb and did what you siad it compiled fine, when it ran, the applet was just blank for a few seconds and then, chaos : all i see is a never ending list of errors, going so fast i couldnt even read ![]() ( i took a screenie and it looks like the same nullpointer as before, screenie attahced ) ANY idea whats going on ? just in case :
Mod Edit: Remember to use syntax tags! Thanks ![]()
|
Author: | chili5 [ Mon Dec 29, 2008 2:57 pm ] | ||
Post subject: | RE:Java Import Image Help! | ||
Not sure what's causing the null pointer. It might have to do with you using Ready To Program (it's not quite Java imo). This is the code I used:
I had to resize the applet to see the picture though. ![]() |
Author: | TheHobbit [ Mon Dec 29, 2008 5:14 pm ] |
Post subject: | Re: Java Import Image Help! |
oh ssweet, i see what you did you declared the myImage up above i tried to do that earlier, hoping to set it as a static but that didnt work out but this definitely does thanks so much bud ! |
Author: | TheHobbit [ Mon Dec 29, 2008 5:16 pm ] |
Post subject: | Re: Java Import Image Help! |
p.s for future reference, how do you insert ur code into the post ? NOT c/ping it . |
Author: | chili5 [ Mon Dec 29, 2008 5:16 pm ] |
Post subject: | RE:Java Import Image Help! |
No problem ![]() ![]() ![]() I just copy it and put it in [ code ] [ /code ] tags ![]() |
Author: | sgware [ Fri Jan 15, 2010 3:24 pm ] |
Post subject: | Re: Java Import Image Help! |
There is an alternative method to accomplish image importing that I use in my applets. Instead of importing the images, convert them to .java files and then simply use them in your code ![]() I made a tool for doing this, which makes the process a lot easier: http://stephengware.com/projects/imagetoclass Hope that helps! -- Stephen |