Results 1 to 11 of 11
Thread: JCreator image
- 07-14-2009, 10:03 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 21
- Rep Power
- 0
JCreator image
Hello,
I have started using Java very recently, and I want to display an image in an applet. I'm pretty sure theres nothing wrong with the coding, but when I run the applet the applet window appears blank. I am using JCreator, and I think that the image isn't showing because it can't find the image. (The image is a 32 by 32 GIF) I have heard that you need to place the needed images in a certain folder or you have to include the image path, or something. And I am also wondering can you display an image in an applet before JARing it?
Any code snippets, full codes or instructions on where to place the image to use it in an applet are appreciated.
-
have you tried converting the GIF to a JPG?
- 07-14-2009, 11:39 PM #3
I don't know about JCreator but I might be able to help you show an image in an applet.
Since the applet security sandbox doesn't allow us to make a new File or URL it is usually easier to use the Class method getResource. This method delegates to the class loader which will look for the resource, your image, on the classpath. That's the first thing to be aware of: the resource must be on your classpath to be found. Also, if there are multiple resources only the first one found will be returned. This method will look for resources in jar files.
To help with debugging along the way you can write out the returned URL to see where java thinks the file is.
Another possibility is to load the image in another class (ie, outside the applet).
Generally you can put your images in a folder named "images" and put this folder in the folder you do your developing in. So your current directory, the one you are working in will have a folder named "images" and it will contain your image(s).
Then you fetch the image with:
The extension, "ext" above, depends on what methods and/or java version you are using.Java Code:String path = "images/myImage.ext"; URL url = getClass().getResource(path); System.out.println("url = " + url); You can hand this url to any of: 1 — the newest way to load images: ImageIO.[i]read[/i], 2 — the older ImageIcon constructor or 3 — any of the antique Applet/Toolkit.[i]getImage[/i] ot Toolkit.[i]createImage[/i] methods that have a URL parameter.
ImageIcon and the antique methods(3) will load gif, jpg and png image formats.
ImageIO will load all of these plus, in j2se 1.5+, bmp and wbmp images.
For feedback about loading errors:
1 — ImageIO throws (IllegalArgument) exceptions
2 — ImageIcon has a getImageLoadStatus method you can use
3 — the antique methods require a MediaTracker to actually load the image data and this class has methods you can use to track/query the loading success/status. Without this extra work you get no feedback about what went wrong: couldn't find the file, corrupted/unreadable image data, unrecognizable/unsupported format, etc.
can you display an image in an applet before JARing it
Yes, as above.
- 07-15-2009, 03:02 AM #4
Member
- Join Date
- Jul 2009
- Posts
- 21
- Rep Power
- 0
Would this code be able to fetch the image if I did as you suggested with the "Images" Folder?
Java Code:import java.applet.Applet; import java.awt.*; import java.awt.image.*; import java.net.*; public class Test extends Applet { Image img = null; Image rot = null; int buffer[] = new int[32 * 32]; int rotate[] = new int[32 * 32]; public void init() { try { MediaTracker tracker = new MediaTracker (this); img = getImage(new URL(getDocumentBase(), "gumby.gif")); tracker.addImage (img, 0); tracker.waitForAll(); PixelGrabber grabber = new PixelGrabber(img, 0, 0, 32, 32, buffer, 0, 32); try { grabber.grabPixels(); } catch(InterruptedException e) { e.printStackTrace(); } for(int y = 0; y < 32; y++) { for(int x = 0; x < 32; x++) { rotate[((32-x-1)*32)+y] = buffer[(y*32)+x]; } } rot = createImage(new MemoryImageSource(32, 32, rotate, 0, 32)); } catch (Exception e) { e.printStackTrace(); } } public void update( Graphics g) { paint(g); } public void paint(Graphics g) { g.drawImage(img, 0, 0,this); g.drawImage(rot,0, 40, this); } }Last edited by Tb0h; 07-15-2009 at 03:08 AM.
- 07-15-2009, 03:23 AM #5
Member
- Join Date
- Jul 2009
- Posts
- 21
- Rep Power
- 0
With most IDEs, when you make a new applet you have two main folders; "src" and "classes". In my IDE, (JCreator) "classes" has the HTML file and "src" has all of the coding. I created an "Images" Folder in src and added the picture. I used the code I displayed above and the applet appeared blank. What am I doing wrong?
- 07-15-2009, 03:49 AM #6
Would this code be able to fetch the image if I did as you suggested with the "Images" Folder?
No. This
will have java look for the image "gumby.gif" in the same folder as your document/html file is in and not inside another folder.Java Code:URL url = new URL(getDocumentBase(), "gumby.gif");
If the "Images" folder is in the same folder as your document/html file then you would look for it with:
I loaded a gif contained in an "images" folder with this:Java Code:URL url = new URL(getDocumentBase(), "Images/gumby.gif");
and got this in the console:Java Code:MediaTracker tracker = new MediaTracker (this); // your original url construction URL url = new URL(getDocumentBase(), "gumby.gif"); // where will it point? System.out.println("url = " + url); // is there such a file? System.out.println("exists: " + new java.io.File(url.getPath()).exists()); // try duke in the "images" folder url = getClass().getResource("images/dukeWaveRed.gif"); // where... System.out.println("url = " + url); // exists? System.out.println("exists: " + new java.io.File(url.getPath()).exists()); // this loaded the image okay img = getImage(new URL(getDocumentBase(), "images/dukeWaveRed.gif"));
The img loading was successful and both images showed up.Java Code:C:\jexp>javac testapplet.java C:\jexp>appletviewer TestApplet.java url = file:/C:/jexp/gumby.gif exists: false url = file:/C:/jexp/images/dukeWaveRed.gif exists: true
- 07-15-2009, 04:02 AM #7
"classes" has the HTML file and "src" has all of the coding.
I would have thought it would be the other way around: the source (.java) files in the "src" folder and the compiled byte code (.class files) in the "classes" folder. But that's not important.
The documentBase should point to the folder where your html file is located and the codeBase should be the folder that contains the compiled (.class) file(s).
You can write these out with System.out.println to see where it takes you.
I added an html comment at the top of your source file and compile/run the applet with appletviewer (html files are too chaotic for development because of caching problems):
Then adding these statements in initJava Code:// <applet code="TestApplet" width="200" height="200"></applet>
gives this in the console:Java Code:System.out.println("documentBase: " + getDocumentBase()); System.out.println("codeBase: " + getCodeBase());
Java Code:C:\jexp>appletviewer TestApplet.java documentBase: file:/C:/jexp/TestApplet.java codeBase: file:/C:/jexp/
- 07-15-2009, 08:09 PM #8
Member
- Join Date
- Jul 2009
- Posts
- 21
- Rep Power
- 0
How should I incorporate your code with my Test applet code, displaying only the gumby.gif (rotating)
- 07-15-2009, 11:03 PM #9
Member
- Join Date
- Jul 2009
- Posts
- 21
- Rep Power
- 0
This is the revised version of my code. The applet still appeared blank. I had the image folder in and everything, could you revise my code?
Java Code:import java.applet.Applet; import java.awt.*; import java.awt.image.*; import java.net.*; public class TowerGame extends Applet { Image img = null; Image rot = null; int buffer[] = new int[32 * 32]; int rotate[] = new int[32 * 32]; public void init() { try { MediaTracker tracker = new MediaTracker (this); img = getImage(new URL(getDocumentBase(), "gumby.gif")); tracker.addImage (img, 0); tracker.waitForAll(); System.out.println("documentBase: " + getDocumentBase()); System.out.println("codeBase: " + getCodeBase()); PixelGrabber grabber = new PixelGrabber(img, 0, 0, 32, 32, buffer, 0, 32); try { grabber.grabPixels(); } catch(InterruptedException e) { e.printStackTrace(); } for(int y = 0; y < 32; y++) { for(int x = 0; x < 32; x++) { rotate[((32-x-1)*32)+y] = buffer[(y*32)+x]; } } rot = createImage(new MemoryImageSource(32, 32, rotate, 0, 32)); } catch (Exception e) { e.printStackTrace(); } } public void update( Graphics g) { paint(g); } public void paint(Graphics g) { g.drawImage(img, 0, 0,this); g.drawImage(rot,0, 40, this); } }Last edited by Tb0h; 07-15-2009 at 11:04 PM. Reason: code brackets
- 07-16-2009, 12:22 AM #10
You're still looking for the image in the same folder as your html file.
You can copy the image to that folder if you want to load it from there. If you want to load it from an "images" type folder you should include the folder in your relative path. So if your folder name is "images" your revised img assignment statement would look like this:
This tells java: start in the folder in which the html file is located, ie, the documentBase, and find the "images" folder. Go inside this folder and find the "gumby.gif" file.Java Code:img = getImage(new URL(getDocumentBase(), "images/gumby.gif"));
Since you are focusing on image loading let's try this:
I'm not having any trouble with sandbox security issues while running this in the appletviewer.Java Code:// <applet code="TestApplet" width="400" height="200"></applet> import java.applet.Applet; import java.awt.*; import java.net.*; public class TestApplet extends Applet { Image[] images = new Image[4]; public void init() { try { System.out.println("documentBase: " + getDocumentBase()); System.out.println("codeBase: " + getCodeBase()); String path = "images/dukeWaveRed.gif"; URL docBaseUrl = new URL(getDocumentBase(), path); System.out.println("docBaseUrl = " + docBaseUrl); images[0] = getImage(docBaseUrl); URL codeBaseUrl = new URL(getCodeBase(), path); System.out.println("codeBaseUrl = " + codeBaseUrl); images[1] = getImage(codeBaseUrl); URL resourceUrl = getClass().getResource(path); System.out.println("resourceUrl = " + resourceUrl); images[2] = getImage(resourceUrl); URL directUrl = new URL("file:/C:/jexp/images/dukeWaveRed.gif"); System.out.println("directUrl = " + directUrl); images[3] = getImage(directUrl); MediaTracker tracker = new MediaTracker(this); for(int i = 0; i < images.length; i++) { tracker.addImage (images[i], 0); } tracker.waitForAll(); } catch(Exception e) { System.out.println(e.getClass().getName() + ": " + e.getMessage()); } } public void update( Graphics g) { paint(g); } public void paint(Graphics g) { int w = getWidth(); int h = getHeight(); int iw = images[0].getWidth(this); int ih = images[0].getHeight(this); int pad = 5; int x0 = (w - images.length*iw - (images.length-1)*pad)/2; int y = (h - ih)/2; int x = x0; for(int i = 0; i < images.length; i++) { g.drawImage(images[i], x, y, this); x += iw + pad; } } }
Here's the console output:
Make sure you change the path statement in the code above to suit your folder/imagefile:Java Code:C:\jexp>javac testapplet.java C:\jexp>appletviewer TestApplet.java documentBase: file:/C:/jexp/TestApplet.java codeBase: file:/C:/jexp/ docBaseUrl = file:/C:/jexp/images/dukeWaveRed.gif codeBaseUrl = file:/C:/jexp/images/dukeWaveRed.gif resourceUrl = file:/C:/jexp/images/dukeWaveRed.gif directUrl = file:/C:/jexp/images/dukeWaveRed.gif
String path = "images/dukeWaveRed.gif";
- 07-16-2009, 03:00 AM #11
Member
- Join Date
- Jul 2009
- Posts
- 21
- Rep Power
- 0
Similar Threads
-
JCreator error:
By guru_boy in forum New To JavaReplies: 4Last Post: 05-06-2009, 05:53 AM -
Applet in JCreator
By mgm2010 in forum JCreatorReplies: 0Last Post: 03-02-2009, 02:30 PM -
why my JCreator can't compile even if I have the JDK???
By aiacos88 in forum JCreatorReplies: 8Last Post: 02-26-2009, 03:21 PM -
About using jcreator
By yuchuang in forum JCreatorReplies: 4Last Post: 11-29-2008, 08:29 PM -
jcreator
By nikhil_solanki015 in forum JCreatorReplies: 2Last Post: 10-28-2008, 07:04 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks